001: //$Id: TypedObjectMap.java 256 2006-10-23 21:56:10Z jg_hamburg $
002: /********************************************************************************
003: * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
004: * Copyright (c) 2004, Joerg and Kai Gellien
005: * All rights reserved.
006: *
007: * The Software is provided under the terms of the Common Public License 1.0
008: * as provided with the distribution of DDTUnit in the file cpl-v10.html.
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * + Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * + Redistributions in binary form must reproduce the above
017: * copyright notice, this list of conditions and the following
018: * disclaimer in the documentation and/or other materials provided
019: * with the distribution.
020: *
021: * + Neither the name of the authors or DDTUnit, nor the
022: * names of its contributors may be used to endorse or promote
023: * products derived from this software without specific prior
024: * written permission.
025: *
026: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
027: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
028: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
029: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
030: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
031: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
032: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
033: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
036: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: ********************************************************************************/package junitx.ddtunit.data;
038:
039: import java.util.HashMap;
040: import java.util.Iterator;
041: import java.util.Map;
042: import java.util.Set;
043: import java.util.Map.Entry;
044:
045: import junitx.ddtunit.DDTException;
046:
047: /**
048: * @author jg
049: *
050: * TODO To change the template for this generated type comment go to Window -
051: * Preferences - Java - Code Style - Code Templates
052: */
053: public class TypedObjectMap implements Cloneable {
054: private Map objectMap;
055:
056: public TypedObjectMap() {
057: this .objectMap = new HashMap();
058: }
059:
060: public void put(String id, TypedObject entry) {
061: if (objectMap.containsKey(id)) {
062: Object value = objectMap.get(id);
063: if (MultiTypeMap.class.isInstance(value)) {
064: ((MultiTypeMap) value).put(entry.getType(), entry);
065: } else if (TypedObject.class.isInstance(value)) {
066: TypedObject valueEntry = (TypedObject) value;
067: MultiTypeMap typeMap = new MultiTypeMap();
068: typeMap.put(valueEntry.getType(), valueEntry);
069: typeMap.put(entry.getType(), entry);
070: objectMap.put(id, typeMap);
071: } else {
072: throw new DDTException(
073: "Wrong data in internal Structure");
074: }
075: } else {
076: objectMap.put(id, entry);
077: }
078: }
079:
080: public TypedObject get(String id, String type) {
081: TypedObject obj = null;
082: if (this .objectMap.containsKey(id)) {
083: Object value = objectMap.get(id);
084: if (MultiTypeMap.class.isInstance(value)) {
085: obj = ((MultiTypeMap) value).get(type);
086: } else if (TypedObject.class.isInstance(value)) {
087: obj = (TypedObject) value;
088: }
089: }
090: return obj;
091: }
092:
093: public TypedObject get(String id) {
094: TypedObject obj = null;
095: if (this .objectMap.containsKey(id)) {
096: Object value = objectMap.get(id);
097: if (MultiTypeMap.class.isInstance(value)) {
098: MultiTypeMap multiMap = (MultiTypeMap) value;
099: 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()
105: .iterator().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
150: .getValue()).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: }
|