KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > collection > util > ArrayFactory


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

21 package org.jacorb.collection.util;
22
23 public class ArrayFactory {
24     private static final int MAX = 16;
25     private static final int MIN_OFFSET = 4;
26     private static final int THREASHOLD = 16;
27     private static DynArray [] pool = new DynArray[ MAX ];
28
29     static {
30     for( int i = MAX; i > 0;){
31             pool[--i]=new DynArray();
32         }
33     }
34
35     private final static int log2(int n){
36     int l =0;
37     int nn = n-1;
38     while( (nn >>l) != 0 )
39         l++;
40
41     return l;
42     }
43
44     public static void release()
45     {
46     for( int i= MAX; i > 0; )
47     {
48         i--;
49         pool[i].removeAllElements();
50     }
51     }
52
53     public static synchronized Object JavaDoc [] get_array( int capacity ){
54         int log = 0;
55         if( capacity != 0 ){
56             log = log2(capacity);
57         }
58     DynArray v= pool[log > MIN_OFFSET ? log-MIN_OFFSET : 0 ];
59
60     if( ! v.isEmpty() ){
61         Object JavaDoc o = v.lastElement();
62         v.removeElementAt(v.size()-1);
63         return (Object JavaDoc [])o;
64     } else {
65         return new Object JavaDoc[log > MIN_OFFSET ? 1<<log : 1 << MIN_OFFSET ];
66     }
67     }
68     public static synchronized void free_array( Object JavaDoc [] current ){
69     int log_curr = log2(current.length);
70         if( log_curr-MIN_OFFSET < pool.length ){
71             clear_array( current );
72             DynArray v = pool[ log_curr-MIN_OFFSET ];
73             if( v.size() < THREASHOLD ) {
74                 v.addElement( current );
75             }
76         }
77     }
78     private static void clear_array( Object JavaDoc [] current ){
79         for( int i=current.length;i>0;){
80             current[--i] = null;
81         }
82     }
83
84
85
86
87
88
89 }
90
91
92
93
Popular Tags