KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jofti > btree > ValueObject


1 /*
2  * ValueObject.java
3  *
4  * Created on 02 June 2003, 20:59
5  */

6
7 package com.jofti.btree;
8
9 import java.io.Externalizable JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.ObjectInput JavaDoc;
12 import java.io.ObjectOutput JavaDoc;
13
14
15
16 /**
17  *
18  * This is used to wrap all values put into the Tree. It is is responsible for the comparison ordering for dimensions
19  * and for checking the values against the min and max values int he tree and the dimension.
20  *
21  * @author Steve Woodcock
22  * @version 1.0<br>
23  */

24 public class ValueObject implements Comparable JavaDoc, Externalizable JavaDoc {
25     
26     /**
27      *
28      */

29     public static final Comparable JavaDoc MIN_COMAPARBLE_VALUE = new MinComparableValue();
30     public static final Comparable JavaDoc MAX_COMAPARBLE_VALUE = new MaxComparableValue();
31
32     int dimension =0;
33  
34     
35     Comparable JavaDoc realValue = null;
36     volatile int hashCode =0;
37     
38     
39     public ValueObject() {
40     }
41     /** Creates a new instance of ValueObject */
42     public ValueObject(int dimension, Comparable JavaDoc value) {
43         this.dimension = dimension;
44         this.realValue = value;
45     }
46     
47     public ValueObject(Comparable JavaDoc value) {
48         this.realValue = value;
49     }
50     
51     public int compareTo(Object JavaDoc o) {
52         
53         //comparison by identity
54

55         
56         ValueObject obj = (ValueObject)o;
57         
58         
59         
60          if (dimension == obj.dimension) {
61                 // none are special values
62
if ( realValue != MAX_COMAPARBLE_VALUE && obj.realValue != MAX_COMAPARBLE_VALUE
63                         && realValue != MIN_COMAPARBLE_VALUE && obj.realValue != MIN_COMAPARBLE_VALUE){
64                     return realValue.compareTo(obj.realValue);
65                     // if either is special value to result in this obj being bigger
66
} else if ( (realValue == MAX_COMAPARBLE_VALUE && obj.realValue != MAX_COMAPARBLE_VALUE)
67                         || (realValue != MIN_COMAPARBLE_VALUE ) && obj.realValue== MIN_COMAPARBLE_VALUE ){
68                     return 1;
69                     // if either is special value to result in this obj being smaller
70
}else if ( (realValue != MAX_COMAPARBLE_VALUE && obj.realValue == MAX_COMAPARBLE_VALUE)
71                         || (realValue == MIN_COMAPARBLE_VALUE ) && obj.realValue!= MIN_COMAPARBLE_VALUE ){
72                         return -1;
73                         // otherwise equla for special values
74
}else{
75                     return 0;
76                     
77                 }
78
79             } else if (dimension > obj.dimension) {
80                 return 1;
81             } else {
82                 return -1;
83             }
84         
85     }
86     
87     
88     public boolean equals(Object JavaDoc o){
89         
90             return (compareTo(o)==0);
91         
92     }
93     
94     public int hashCode (){
95         if (hashCode == 0){
96             int temp =17;
97             temp = temp*37 +dimension;
98             temp= temp*37 + realValue.hashCode();
99             hashCode = temp;
100         }
101         return hashCode;
102     }
103     /** Getter for property dimension.
104      * @return Value of property dimension.
105      *
106      */

107     public int getDimension() {
108         return dimension;
109     }
110     
111     
112     /** Getter for property realValue.
113      * @return Value of property realValue.
114      *
115      */

116     public java.lang.Object JavaDoc getRealValue() {
117         return realValue;
118     }
119     
120     public String JavaDoc toString(){
121         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
122         buf.append("dimension:" + dimension);
123         buf.append(", real Value:" + realValue);
124         return buf.toString();
125     }
126
127     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
128         dimension = in.readInt();
129         realValue = (Comparable JavaDoc)in.readObject();
130         
131     }
132
133     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
134         out.writeInt(dimension);
135         out.writeObject(realValue);
136         
137     }
138    
139
140  
141 }
142
Popular Tags