1 package org.enhydra.shark.utilities; 2 3 import java.util.HashMap ; 4 import java.util.Iterator ; 5 import java.util.Map ; 6 7 public class SizeLimitedToolAgentMap extends HashMap { 8 9 private int maximumSize = Integer.MAX_VALUE-1; 10 11 public SizeLimitedToolAgentMap() { 12 super(); 13 System.out.println("Tool agent map created, maximum number of mappings is "+maximumSize); 14 } 15 16 public SizeLimitedToolAgentMap (int maxSize) { 17 this(); 18 if (maxSize<0) { 19 maximumSize=Integer.MAX_VALUE-1; 20 } else { 21 maximumSize = maxSize; 22 } 23 System.out.println("Tool agent map created, maximum number of mappings is "+maximumSize); 24 } 25 26 public SizeLimitedToolAgentMap (int initialCapacity, float loadFactor) { 27 super(initialCapacity, loadFactor); 28 System.out.println("Tool agent map created, maximum number of mappings is "+maximumSize); 29 } 30 31 public SizeLimitedToolAgentMap (int initialCapacity, float loadFactor, int maxSize) { 32 this(initialCapacity, loadFactor); 33 maximumSize = maxSize; 34 System.out.println("Tool agent map created, maximum number of mappings is "+maximumSize); 35 } 36 37 public SizeLimitedToolAgentMap (Map m) { 38 maximumSize = m.size()+1; 39 putAll(m); 40 System.out.println("Tool agent map created, maximum number of mappings is "+maximumSize); 41 } 42 43 public synchronized Object put( Object key, Object value ) { 44 int mapSize = size(); 45 46 if (mapSize >= maximumSize) { 47 System.err.println("Tool agent map can't accept more mappings - the maximum no. of mappings is "+maximumSize+" !"); 48 return null; 49 } else { 50 System.out.println("New mapping added to tool agent map, tool agent map currently has "+(mapSize+1)+" mappings !"); 51 return super.put(key,value); 52 } 53 } 54 55 public synchronized void putAll(Map t) { 56 Iterator iter = t.entrySet().iterator(); 57 while(iter.hasNext()) { 58 Map.Entry entry = (Map.Entry )iter.next(); 59 this.put(entry.getKey(), entry.getValue()); 60 } 61 } 62 63 public synchronized Object remove (Object key) { 64 return super.remove(key); 65 } 66 67 public int getMaximumSize() { 68 return maximumSize; 69 } 70 71 } 72 | Popular Tags |