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