1 2 6 21 22 package com.jofti.util; 23 24 25 26 46 47 public abstract class AbstractMap { 48 49 50 51 52 57 58 protected int distinct; 59 60 61 62 75 76 protected int lowWaterMark; 77 78 protected int highWaterMark; 79 80 81 82 87 88 protected double minLoadFactor; 89 90 91 92 97 98 protected double maxLoadFactor; 99 100 101 102 protected static final int defaultCapacity = 277; 103 104 protected static final double defaultMinLoadFactor = 0.2; 105 106 protected static final double defaultMaxLoadFactor = 0.5; 107 108 113 114 protected AbstractMap() {} 115 116 125 126 protected int chooseGrowCapacity(int size, double minLoad, double maxLoad) { 127 128 return nextPrime(Math.max(size+1, (int) ((4*size / (3*minLoad+maxLoad))))); 129 130 } 131 132 139 140 protected int chooseHighWaterMark(int capacity, double maxLoad) { 141 142 return Math.min(capacity-2, (int) (capacity * maxLoad)); 144 } 145 146 153 154 protected int chooseLowWaterMark(int capacity, double minLoad) { 155 156 return (int) (capacity * minLoad); 157 158 } 159 160 171 172 protected int chooseMeanCapacity(int size, double minLoad, double maxLoad) { 173 174 return nextPrime(Math.max(size+1, (int) ((2*size / (minLoad+maxLoad))))); 175 176 } 177 178 187 188 protected int chooseShrinkCapacity(int size, double minLoad, double maxLoad) { 189 190 return nextPrime(Math.max(size+1, (int) ((4*size / (minLoad+3*maxLoad))))); 191 192 } 193 194 199 200 public abstract void clear(); 201 202 225 226 public abstract void ensureCapacity(int minCapacity) ; 227 228 237 238 public boolean isEmpty() { 239 240 return distinct == 0; 241 242 } 243 244 253 254 protected int nextPrime(int desiredCapacity) { 255 256 return PrimeFinder.nextPrime(desiredCapacity); 257 258 } 259 260 277 278 protected void setUp(int initialCapacity, double minLoadFactor, double maxLoadFactor) { 279 280 if (initialCapacity < 0) 281 282 throw new IllegalArgumentException ("Initial Capacity must not be less than zero: "+ initialCapacity); 283 284 if (minLoadFactor < 0.0 || minLoadFactor >= 1.0) 285 286 throw new IllegalArgumentException ("Illegal minLoadFactor: "+ minLoadFactor); 287 288 if (maxLoadFactor <= 0.0 || maxLoadFactor >= 1.0) 289 290 throw new IllegalArgumentException ("Illegal maxLoadFactor: "+ maxLoadFactor); 291 292 if (minLoadFactor >= maxLoadFactor) 293 294 throw new IllegalArgumentException ("Illegal minLoadFactor: "+ minLoadFactor+" and maxLoadFactor: "+ maxLoadFactor); 295 296 } 297 298 307 308 public int size() { 309 310 return distinct; 311 312 } 313 314 327 328 public abstract void trimToSize(); 329 330 } 331 332 | Popular Tags |