1 6 7 package com.jofti.btree; 8 9 import java.io.Externalizable ; 10 import java.io.IOException ; 11 import java.io.ObjectInput ; 12 import java.io.ObjectOutput ; 13 14 15 16 24 public class ValueObject implements Comparable , Externalizable { 25 26 29 public static final Comparable MIN_COMAPARBLE_VALUE = new MinComparableValue(); 30 public static final Comparable MAX_COMAPARBLE_VALUE = new MaxComparableValue(); 31 32 int dimension =0; 33 34 35 Comparable realValue = null; 36 volatile int hashCode =0; 37 38 39 public ValueObject() { 40 } 41 42 public ValueObject(int dimension, Comparable value) { 43 this.dimension = dimension; 44 this.realValue = value; 45 } 46 47 public ValueObject(Comparable value) { 48 this.realValue = value; 49 } 50 51 public int compareTo(Object o) { 52 53 55 56 ValueObject obj = (ValueObject)o; 57 58 59 60 if (dimension == obj.dimension) { 61 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 } 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 }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 }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 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 107 public int getDimension() { 108 return dimension; 109 } 110 111 112 116 public java.lang.Object getRealValue() { 117 return realValue; 118 } 119 120 public String toString(){ 121 StringBuffer buf = new StringBuffer (); 122 buf.append("dimension:" + dimension); 123 buf.append(", real Value:" + realValue); 124 return buf.toString(); 125 } 126 127 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException { 128 dimension = in.readInt(); 129 realValue = (Comparable )in.readObject(); 130 131 } 132 133 public void writeExternal(ObjectOutput out) throws IOException { 134 out.writeInt(dimension); 135 out.writeObject(realValue); 136 137 } 138 139 140 141 } 142 | Popular Tags |