001: //$Id: TypedObject.java 204 2005-12-11 17:28:53Z 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: /**
040: * Class represents objects that specify type and value separately. This is
041: * especially usefull during operation with null object references.
042: *
043: * @author jg
044: */
045: public class TypedObject implements Cloneable {
046: public final static String UNKNOWN_TYPE = "UnknownType";
047:
048: private String id;
049:
050: private String type;
051:
052: private Object value;
053:
054: public TypedObject(String id) {
055: this (id, UNKNOWN_TYPE);
056: }
057:
058: public TypedObject(String id, String type) {
059: if (id == null || "".equals(id)) {
060: // @TODO check if condition is correct || type == null ||
061: // "".equals(type)) {
062: throw new IllegalArgumentException(
063: "id and type must be specified on TypedObject");
064: }
065: this .id = id;
066: if (type == null || "".equals(type)) {
067: this .type = UNKNOWN_TYPE;
068: } else {
069: this .type = type;
070: }
071: }
072:
073: public Object getValue() {
074: return value;
075: }
076:
077: public void setValue(Object value) {
078: this .value = value;
079: }
080:
081: public String getType() {
082: return type;
083: }
084:
085: public String getId() {
086: return id;
087: }
088:
089: /**
090: * Override hashCode.
091: *
092: * @return the Objects hashcode.
093: */
094: public int hashCode() {
095: int hashCode = 1;
096: hashCode = 31 * hashCode + (id == null ? 0 : id.hashCode());
097: hashCode = 31 * hashCode + (type == null ? 0 : type.hashCode());
098: hashCode = 31 * hashCode
099: + (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
124: : this .type.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: }
|