View Javadoc

1   //$Id: TypedObjectMap.java 256 2006-10-23 21:56:10Z jg_hamburg $
2   /********************************************************************************
3    * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
4    * Copyright (c) 2004, Joerg and Kai Gellien
5    * All rights reserved.
6    *
7    * The Software is provided under the terms of the Common Public License 1.0
8    * as provided with the distribution of DDTUnit in the file cpl-v10.html.
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   *     + Redistributions of source code must retain the above copyright
14   *       notice, this list of conditions and the following disclaimer.
15   *
16   *     + Redistributions in binary form must reproduce the above
17   *       copyright notice, this list of conditions and the following
18   *       disclaimer in the documentation and/or other materials provided
19   *       with the distribution.
20   *
21   *     + Neither the name of the authors or DDTUnit, nor the
22   *       names of its contributors may be used to endorse or promote
23   *       products derived from this software without specific prior
24   *       written permission.
25   *
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
30   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * @author jg
50   * 
51   * TODO To change the template for this generated type comment go to Window -
52   * Preferences - Java - Code Style - Code Templates
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 }