001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.common.tagging;
009:
010: //base classes
011: import java.math.BigDecimal;
012: import java.sql.Timestamp;
013:
014: //project specific classes
015: import org.jfolder.common.StandardDataTypes;
016: import org.jfolder.common.UnexpectedSystemException;
017: import org.jfolder.common.utils.misc.MiscHelper;
018:
019: //other classes
020:
021: public class ValueAndClassForConceptTag {
022:
023: //
024: private Object value = null;
025: private Class valueClass = null;
026: private boolean prim = false;
027: //
028: private boolean stateInconsistent = false;
029:
030: //
031:
032: public String toString() {
033:
034: StringBuffer outValue = new StringBuffer();
035:
036: //
037: outValue.append("{(VAC),(Class = ");
038: outValue.append(this .valueClass.getName());
039: outValue.append("),(Value = ");
040: outValue.append(this .value);
041: outValue.append(")}");
042:
043: return outValue.toString();
044: }
045:
046: public int hashCode() {
047:
048: int outValue = 0;
049:
050: //
051: //
052: outValue += MiscHelper.hashCodeOrZero(this .value);
053: outValue += MiscHelper.hashCodeOrZero(this .valueClass);
054: if (this .prim) {
055: outValue += 1;
056: }
057:
058: return outValue;
059: }
060:
061: public boolean equals(Object inObj) {
062:
063: boolean outValue = true;
064:
065: if (inObj instanceof ValueAndClassForConceptTag) {
066: //
067: ValueAndClassForConceptTag vacfct2 = ((ValueAndClassForConceptTag) inObj);
068: //
069: outValue &= MiscHelper.equalsOrNull(this .value,
070: vacfct2.value);
071: outValue &= MiscHelper.equalsOrNull(this .valueClass,
072: vacfct2.valueClass);
073: outValue &= (this .prim == vacfct2.prim);
074: } else {
075: outValue &= false;
076: }
077:
078: return outValue;
079: }
080:
081: //TO DO: should this take serializable only?
082: private ValueAndClassForConceptTag(Object inValue, Class inClass) {
083:
084: if (inClass == null) {
085: throw new UnexpectedSystemException(
086: "SimpleValueAndClass class type cannot be null");
087: }
088:
089: this .value = inValue;
090: this .valueClass = getNonPrimitiveClass(inClass);
091: this .prim = inClass.isPrimitive();
092:
093: String className = getActualClass(this .valueClass, this .prim)
094: .getName();
095:
096: if (this .prim && this .value == null
097: && !className.equals(Void.TYPE.getName())) {
098:
099: throw new UnexpectedSystemException(inClass
100: + ", a primitive class, cannot have a null value");
101: }
102: }
103:
104: public final static ValueAndClassForConceptTag newInstance(
105: Object inValue, Class inClass) {
106:
107: return new ValueAndClassForConceptTag(inValue, inClass);
108: }
109:
110: //
111: public final static ValueAndClassForConceptTag newHtml(
112: String inValue) {
113: //
114: return newInstance(StandardDataTypes.getHtmlInstance(inValue),
115: StandardDataTypes.getHtmlClass());
116: }
117:
118: public final static ValueAndClassForConceptTag newTimestamp(
119: Timestamp inValue) {
120: //
121: return newInstance(StandardDataTypes
122: .getTimestampInstance(inValue), StandardDataTypes
123: .getTimestampClass());
124: }
125:
126: public final static ValueAndClassForConceptTag newBinary(
127: byte inValue[]) {
128: return newInstance(
129: StandardDataTypes.getBinaryInstance(inValue),
130: StandardDataTypes.getBinaryClass());
131: }
132:
133: public final static ValueAndClassForConceptTag newBoolean(
134: Boolean inValue) {
135: return newInstance(StandardDataTypes
136: .getBooleanInstance(inValue), StandardDataTypes
137: .getBooleanClass());
138: }
139:
140: public final static ValueAndClassForConceptTag newString(
141: String inValue) {
142: return newInstance(
143: StandardDataTypes.getStringInstance(inValue),
144: StandardDataTypes.getStringClass());
145: }
146:
147: public final static ValueAndClassForConceptTag newDecimal(
148: BigDecimal inValue) {
149: //
150: return newInstance(StandardDataTypes
151: .getDecimalInstance(inValue), StandardDataTypes
152: .getDecimalClass());
153: }
154:
155: //
156: public final static ValueAndClassForConceptTag newEmptyList() {
157: //
158: return newInstance(StandardDataTypes.getEmptyListInstance(),
159: StandardDataTypes.getListClass());
160: }
161:
162: //
163: public final static ValueAndClassForConceptTag newEmptyMap() {
164: //
165: return newInstance(StandardDataTypes.getEmptyMapInstance(),
166: StandardDataTypes.getMapClass());
167: }
168:
169: //
170: public final static ValueAndClassForConceptTag newNothing() {
171: //
172: return newInstance(StandardDataTypes.getNothingInstance(),
173: StandardDataTypes.getNothingClass());
174: }
175:
176: boolean isStateInconsistent() {
177: return this .stateInconsistent;
178: }
179:
180: public Object getValue() {
181: return this .value;
182: }
183:
184: public Class getValueClass() {
185: return getActualClass(this .valueClass, this .prim);
186: }
187:
188: private final static Class getNonPrimitiveClass(Class inClass) {
189:
190: Class outValue = null;
191:
192: if (inClass.isPrimitive()) {
193: if (inClass.getName().equals(Boolean.TYPE.getName())) {
194: outValue = Boolean.class;
195: } else if (inClass.getName().equals(Byte.TYPE.getName())) {
196: outValue = Byte.class;
197: } else if (inClass.getName().equals(Short.TYPE.getName())) {
198: outValue = Short.class;
199: } else if (inClass.getName().equals(
200: Character.TYPE.getName())) {
201: outValue = Character.class;
202: } else if (inClass.getName().equals(Integer.TYPE.getName())) {
203: outValue = Integer.class;
204: } else if (inClass.getName().equals(Long.TYPE.getName())) {
205: outValue = Long.class;
206: } else if (inClass.getName().equals(Float.TYPE.getName())) {
207: outValue = Float.class;
208: } else if (inClass.getName().equals(Double.TYPE.getName())) {
209: outValue = Double.class;
210: } else if (inClass.getName().equals(Void.TYPE.getName())) {
211: outValue = Void.class;
212: } else {
213: throw new UnexpectedSystemException(
214: "No non-primitive equivalent for "
215: + inClass.getName());
216: }
217: } else {
218: outValue = inClass;
219: }
220:
221: return outValue;
222: }
223:
224: private final static Class getActualClass(Class inClass,
225: boolean inPrim) {
226:
227: Class outValue = null;
228:
229: if (inPrim) {
230: if (inClass.getName().equals(Boolean.class.getName())) {
231: outValue = Boolean.TYPE;
232: } else if (inClass.getName().equals(Byte.class.getName())) {
233: outValue = Byte.TYPE;
234: } else if (inClass.getName().equals(Short.class.getName())) {
235: outValue = Short.TYPE;
236: } else if (inClass.getName().equals(
237: Character.class.getName())) {
238: outValue = Character.TYPE;
239: } else if (inClass.getName()
240: .equals(Integer.class.getName())) {
241: outValue = Integer.TYPE;
242: } else if (inClass.getName().equals(Long.class.getName())) {
243: outValue = Long.TYPE;
244: } else if (inClass.getName().equals(Float.class.getName())) {
245: outValue = Float.TYPE;
246: } else if (inClass.getName().equals(Double.class.getName())) {
247: outValue = Double.TYPE;
248: } else if (inClass.getName().equals(Void.class.getName())) {
249: outValue = Void.TYPE;
250: } else {
251: throw new UnexpectedSystemException(
252: "No primitive equivalent for "
253: + inClass.getName());
254: }
255: } else {
256: outValue = inClass;
257: }
258:
259: return outValue;
260: }
261: }
|