1 //$Id: TypedObject.java 204 2005-12-11 17:28:53Z 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 /**
41 * Class represents objects that specify type and value separately. This is
42 * especially usefull during operation with null object references.
43 *
44 * @author jg
45 */
46 public class TypedObject implements Cloneable {
47 public final static String UNKNOWN_TYPE = "UnknownType";
48
49 private String id;
50
51 private String type;
52
53 private Object value;
54
55 public TypedObject(String id) {
56 this(id, UNKNOWN_TYPE);
57 }
58
59 public TypedObject(String id, String type) {
60 if (id == null || "".equals(id)) {
61 // @TODO check if condition is correct || type == null ||
62 // "".equals(type)) {
63 throw new IllegalArgumentException(
64 "id and type must be specified on TypedObject");
65 }
66 this.id = id;
67 if (type == null || "".equals(type)) {
68 this.type = UNKNOWN_TYPE;
69 } else {
70 this.type = type;
71 }
72 }
73
74 public Object getValue() {
75 return value;
76 }
77
78 public void setValue(Object value) {
79 this.value = value;
80 }
81
82 public String getType() {
83 return type;
84 }
85
86 public String getId() {
87 return id;
88 }
89
90 /**
91 * Override hashCode.
92 *
93 * @return the Objects hashcode.
94 */
95 public int hashCode() {
96 int hashCode = 1;
97 hashCode = 31 * hashCode + (id == null ? 0 : id.hashCode());
98 hashCode = 31 * hashCode + (type == null ? 0 : type.hashCode());
99 hashCode = 31 * hashCode + (value == null ? 0 : value.hashCode());
100 return hashCode;
101 }
102
103 /**
104 * Returns <code>true</code> if this <code>TypedObject</code> is the
105 * same as the o argument.
106 *
107 * @return <code>true</code> if this <code>TypedObject</code> is the
108 * same as the o argument.
109 */
110 public boolean equals(Object obj) {
111 if (this == obj) {
112 return true;
113 }
114 if (obj == null) {
115 return false;
116 }
117 if (obj.getClass() != getClass()) {
118 return false;
119 }
120 TypedObject castedObj = (TypedObject) obj;
121 return ((this.id == null ? castedObj.id == null : this.id
122 .equals(castedObj.id))
123 && (this.type == null ? castedObj.type == null : this.type
124 .equals(castedObj.type)) && (this.value == null ? castedObj.value == null
125 : this.value.equals(castedObj.value)));
126 }
127
128 public void setId(String id) {
129 this.id = id;
130 }
131
132 public void setType(String type) {
133 if (type == null) {
134 throw new NullPointerException(
135 "TypedObject.type is not allowed to be null!");
136 }
137 this.type = type;
138 }
139
140 public String toString() {
141 StringBuffer buffer = new StringBuffer();
142 buffer.append("[TypedObject:");
143 buffer.append(" id: ");
144 buffer.append(id);
145 buffer.append(" type: ");
146 buffer.append(type);
147 buffer.append("]");
148 return buffer.toString();
149 }
150
151 public Object clone() {
152 TypedObject newObj = new TypedObject(this.id, this.type);
153 newObj.setValue(this.value);
154 return newObj;
155 }
156 }