001: /*
002: * ValueObject.java
003: *
004: * Created on 02 June 2003, 20:59
005: */
006:
007: package com.jofti.btree;
008:
009: import java.io.Externalizable;
010: import java.io.IOException;
011: import java.io.ObjectInput;
012: import java.io.ObjectOutput;
013:
014: /**
015: *
016: * This is used to wrap all values put into the Tree. It is is responsible for the comparison ordering for dimensions
017: * and for checking the values against the min and max values int he tree and the dimension.
018: *
019: * @author Steve Woodcock
020: * @version 1.0<br>
021: */
022: public class ValueObject implements Comparable, Externalizable {
023:
024: /**
025: *
026: */
027: public static final Comparable MIN_COMAPARBLE_VALUE = new MinComparableValue();
028: public static final Comparable MAX_COMAPARBLE_VALUE = new MaxComparableValue();
029:
030: int dimension = 0;
031:
032: Comparable realValue = null;
033: volatile int hashCode = 0;
034:
035: public ValueObject() {
036: }
037:
038: /** Creates a new instance of ValueObject */
039: public ValueObject(int dimension, Comparable value) {
040: this .dimension = dimension;
041: this .realValue = value;
042: }
043:
044: public ValueObject(Comparable value) {
045: this .realValue = value;
046: }
047:
048: public int compareTo(Object o) {
049:
050: //comparison by identity
051:
052: ValueObject obj = (ValueObject) o;
053:
054: if (dimension == obj.dimension) {
055: // none are special values
056: if (realValue != MAX_COMAPARBLE_VALUE
057: && obj.realValue != MAX_COMAPARBLE_VALUE
058: && realValue != MIN_COMAPARBLE_VALUE
059: && obj.realValue != MIN_COMAPARBLE_VALUE) {
060: return realValue.compareTo(obj.realValue);
061: // if either is special value to result in this obj being bigger
062: } else if ((realValue == MAX_COMAPARBLE_VALUE && obj.realValue != MAX_COMAPARBLE_VALUE)
063: || (realValue != MIN_COMAPARBLE_VALUE)
064: && obj.realValue == MIN_COMAPARBLE_VALUE) {
065: return 1;
066: // if either is special value to result in this obj being smaller
067: } else if ((realValue != MAX_COMAPARBLE_VALUE && obj.realValue == MAX_COMAPARBLE_VALUE)
068: || (realValue == MIN_COMAPARBLE_VALUE)
069: && obj.realValue != MIN_COMAPARBLE_VALUE) {
070: return -1;
071: // otherwise equla for special values
072: } else {
073: return 0;
074:
075: }
076:
077: } else if (dimension > obj.dimension) {
078: return 1;
079: } else {
080: return -1;
081: }
082:
083: }
084:
085: public boolean equals(Object o) {
086:
087: return (compareTo(o) == 0);
088:
089: }
090:
091: public int hashCode() {
092: if (hashCode == 0) {
093: int temp = 17;
094: temp = temp * 37 + dimension;
095: temp = temp * 37 + realValue.hashCode();
096: hashCode = temp;
097: }
098: return hashCode;
099: }
100:
101: /** Getter for property dimension.
102: * @return Value of property dimension.
103: *
104: */
105: public int getDimension() {
106: return dimension;
107: }
108:
109: /** Getter for property realValue.
110: * @return Value of property realValue.
111: *
112: */
113: public java.lang.Object getRealValue() {
114: return realValue;
115: }
116:
117: public String toString() {
118: StringBuffer buf = new StringBuffer();
119: buf.append("dimension:" + dimension);
120: buf.append(", real Value:" + realValue);
121: return buf.toString();
122: }
123:
124: public void readExternal(ObjectInput in) throws IOException,
125: ClassNotFoundException {
126: dimension = in.readInt();
127: realValue = (Comparable) in.readObject();
128:
129: }
130:
131: public void writeExternal(ObjectOutput out) throws IOException {
132: out.writeInt(dimension);
133: out.writeObject(realValue);
134:
135: }
136:
137: }
|