1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package junitx.ddtunit.data;
39
40 import java.util.HashMap;
41 import java.util.Iterator;
42 import java.util.Map;
43 import java.util.Set;
44 import java.util.Map.Entry;
45
46 import junitx.ddtunit.DDTException;
47
48
49
50
51
52
53
54 public class TypedObjectMap implements Cloneable {
55 private Map objectMap;
56
57 public TypedObjectMap() {
58 this.objectMap = new HashMap();
59 }
60
61 public void put(String id, TypedObject entry) {
62 if (objectMap.containsKey(id)) {
63 Object value = objectMap.get(id);
64 if (MultiTypeMap.class.isInstance(value)) {
65 ((MultiTypeMap) value).put(entry.getType(), entry);
66 } else if (TypedObject.class.isInstance(value)) {
67 TypedObject valueEntry = (TypedObject) value;
68 MultiTypeMap typeMap = new MultiTypeMap();
69 typeMap.put(valueEntry.getType(), valueEntry);
70 typeMap.put(entry.getType(), entry);
71 objectMap.put(id, typeMap);
72 } else {
73 throw new DDTException("Wrong data in internal Structure");
74 }
75 } else {
76 objectMap.put(id, entry);
77 }
78 }
79
80 public TypedObject get(String id, String type) {
81 TypedObject obj = null;
82 if (this.objectMap.containsKey(id)) {
83 Object value = objectMap.get(id);
84 if (MultiTypeMap.class.isInstance(value)) {
85 obj = ((MultiTypeMap) value).get(type);
86 } else if (TypedObject.class.isInstance(value)) {
87 obj = (TypedObject) value;
88 }
89 }
90 return obj;
91 }
92
93 public TypedObject get(String id) {
94 TypedObject obj = null;
95 if (this.objectMap.containsKey(id)) {
96 Object value = objectMap.get(id);
97 if (MultiTypeMap.class.isInstance(value)) {
98 MultiTypeMap multiMap = (MultiTypeMap) value;
99 if (multiMap.size() != 1) {
100 throw new DDTException(
101 "Try to retrieve object out of multiples without type info."
102 + " Use signature (String id, String type) on call.");
103 }
104 obj = (TypedObject) ((Entry) multiMap.entrySet().iterator()
105 .next()).getValue();
106 } else if (TypedObject.class.isInstance(value)) {
107 obj = (TypedObject) value;
108 }
109 }
110
111 return obj;
112 }
113
114 public Set entrySet() {
115 return this.objectMap.entrySet();
116 }
117
118 public boolean isEmpty() {
119 return this.objectMap.isEmpty();
120 }
121
122 public int size() {
123 int size = 0;
124 for (Iterator iter = this.objectMap.entrySet().iterator(); iter
125 .hasNext();) {
126 Entry objEntry = (Entry) iter.next();
127 if (MultiTypeMap.class.isInstance(objEntry.getValue())) {
128 size += ((MultiTypeMap) objEntry.getValue()).size();
129 } else {
130 size += 1;
131 }
132 }
133 return size;
134 }
135
136 public Object clone() {
137 TypedObjectMap newMap = new TypedObjectMap();
138 String key;
139 for (Iterator iter = this.entrySet().iterator(); iter.hasNext();) {
140 TypedObject obj = null;
141 Entry objEntry = (Entry) iter.next();
142 key = (String) objEntry.getKey();
143 Object value = objEntry.getValue();
144 if (MultiTypeMap.class.isInstance(value)) {
145 MultiTypeMap typeMap = (MultiTypeMap) value;
146 for (Iterator typeIter = typeMap.entrySet().iterator(); typeIter
147 .hasNext();) {
148 Entry typeEntry = (Entry) typeIter.next();
149 obj = (TypedObject) ((TypedObject) typeEntry.getValue())
150 .clone();
151 newMap.put(key, obj);
152 }
153 } else if (TypedObject.class.isInstance(value)) {
154 obj = (TypedObject) ((TypedObject) value).clone();
155 newMap.put(key, obj);
156 }
157 }
158 return newMap;
159 }
160
161 class MultiTypeMap {
162 private Map multiTypeMap;
163
164 public MultiTypeMap() {
165 this.multiTypeMap = new HashMap();
166 }
167
168 public void put(String type, TypedObject obj) {
169 this.multiTypeMap.put(type, obj);
170 }
171
172 public TypedObject get(String type) {
173 return (TypedObject) this.multiTypeMap.get(type);
174 }
175
176 public int size() {
177 return this.multiTypeMap.size();
178 }
179
180 public Set entrySet() {
181 return this.multiTypeMap.entrySet();
182 }
183 }
184
185 }