1 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 [] 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 o = v.lastElement(); 62 v.removeElementAt(v.size()-1); 63 return (Object [])o; 64 } else { 65 return new Object [log > MIN_OFFSET ? 1<<log : 1 << MIN_OFFSET ]; 66 } 67 } 68 public static synchronized void free_array( Object [] 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 [] 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 |