KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > smartValueObject > container > SmartMap


1 package org.bsf.smartValueObject.container;
2
3 import org.bsf.smartValueObject.container.SmartCollection;
4 import org.bsf.smartValueObject.container.AbstractSmartContainer;
5 import org.bsf.smartValueObject.Versionable;
6
7 import java.util.*;
8
9 /**
10  * A smart wrapper class around <tt>java.util.Map</tt>.
11  *
12  * @see org.bsf.smartValueObject.container.SmartContainer
13  * @see java.util.Map
14  */

15 public class SmartMap extends AbstractSmartContainer implements Map {
16     private Map map;
17
18     public SmartMap(Map m, Versionable v) {
19         super(v);
20         this.map = m;
21     }
22
23     protected boolean addToContainer(Object JavaDoc o) {
24         return map.put(o, o) == o;
25     }
26
27     protected Object JavaDoc addToContainer(Object JavaDoc key, Object JavaDoc o) {
28         return map.put(key, o);
29     }
30
31     protected Object JavaDoc getFromContainer(Object JavaDoc key) {
32         return map.get(key);
33     }
34
35     protected boolean removeFromContainer(Object JavaDoc o) {
36         throw new UnsupportedOperationException JavaDoc();
37     }
38
39     protected Object JavaDoc removeKeyFromContainer(Object JavaDoc key) {
40         return map.remove(key);
41     }
42
43     protected boolean containerContains(Object JavaDoc o) {
44         return map.containsValue(o);
45     }
46
47     protected boolean containerContainsKey(Object JavaDoc key) {
48         return map.containsKey(key);
49     }
50
51     protected int containerSize() {
52         return map.size();
53     }
54
55     protected Iterator containerIterator() {
56         return map.values().iterator();
57     }
58
59     protected void containerClear() {
60         map.clear();
61     }
62
63     protected Object JavaDoc[] toObjectArray() {
64         throw new UnsupportedOperationException JavaDoc();
65     }
66
67     public Object JavaDoc getContainer() {
68         return map;
69     }
70
71     public Object JavaDoc remove(Object JavaDoc key) {
72         return removeObjectByKey(key);
73     }
74
75     public void putAll(Map t) {
76         throw new UnsupportedOperationException JavaDoc();
77     }
78
79     public Set keySet() {
80         // new SmartSet() ??
81
Set set = map.keySet();
82         Set newset = new HashSet();
83         Iterator it = set.iterator();
84         while (it.hasNext()) {
85             Object JavaDoc key = it.next();
86             Object JavaDoc o = map.get(key);
87             if (o instanceof Versionable) {
88                 if (((Versionable) o).isDeleted()) {
89                     continue;
90                 }
91             }
92             newset.add(key);
93         }
94
95         return newset;
96     }
97
98     public Collection values() {
99         // TODO this = ok ?
100
return new SmartCollection(map.values(), this);
101     }
102
103     public Set entrySet() {
104         return new SmartSet(map.entrySet(), this);
105     }
106
107     /**
108      * Overrides baseclass with a specialized method.
109      * @see org.bsf.smartValueObject.container.AbstractSmartContainer#clear
110      */

111     public void clear() {
112         Iterator it = map.keySet().iterator();
113         while (it.hasNext()) {
114            removeObjectByKey(it.next());
115         }
116     }
117 }
118
Popular Tags