1 11 package org.eclipse.jdi.internal; 12 13 14 import java.io.DataInputStream ; 15 import java.io.DataOutputStream ; 16 import java.io.IOException ; 17 18 import com.sun.jdi.InternalException; 19 import com.sun.jdi.PrimitiveType; 20 import com.sun.jdi.PrimitiveValue; 21 22 28 public abstract class PrimitiveValueImpl extends ValueImpl implements PrimitiveValue, Comparable { 29 30 Object fValue; 31 32 35 public PrimitiveValueImpl(String description, VirtualMachineImpl vmImpl, Object value) { 36 super(description, vmImpl); 37 fValue = value; 38 } 39 40 43 public boolean booleanValue() { 44 if (fValue instanceof Boolean ) 45 return ((Boolean )fValue).booleanValue(); 46 else if (fValue instanceof Character ) 47 return ((Character )fValue).charValue() != 0; 48 else return ((Number )fValue).doubleValue() != 0; 49 } 50 51 54 public char charValue() { 55 if (fValue instanceof Boolean ) 56 return ((Boolean )fValue).booleanValue() ? (char)1 : (char)0; 57 else if (fValue instanceof Character ) 58 return ((Character )fValue).charValue(); 59 else return (char)((Number )fValue).intValue(); 60 } 61 62 65 public byte byteValue() { 66 if (fValue instanceof Boolean ) 67 return ((Boolean )fValue).booleanValue() ? (byte)1 : (byte)0; 68 else if (fValue instanceof Character ) 69 return (byte)((Character )fValue).charValue(); 70 else return ((Number )fValue).byteValue(); 71 } 72 73 76 public double doubleValue() { 77 if (fValue instanceof Boolean ) 78 return ((Boolean )fValue).booleanValue() ? (double)1 : (double)0; 79 else if (fValue instanceof Character ) 80 return ((Character )fValue).charValue(); 81 else return ((Number )fValue).doubleValue(); 82 } 83 84 87 public float floatValue() { 88 if (fValue instanceof Boolean ) 89 return ((Boolean )fValue).booleanValue() ? (float)1 : (float)0; 90 else if (fValue instanceof Character ) 91 return ((Character )fValue).charValue(); 92 else return ((Number )fValue).floatValue(); 93 } 94 95 98 public int intValue() { 99 if (fValue instanceof Boolean ) 100 return ((Boolean )fValue).booleanValue() ? (int)1 : (int)0; 101 else if (fValue instanceof Character ) 102 return ((Character )fValue).charValue(); 103 else return ((Number )fValue).intValue(); 104 } 105 106 109 public long longValue() { 110 if (fValue instanceof Boolean ) 111 return ((Boolean )fValue).booleanValue() ? (long)1 : (long)0; 112 else if (fValue instanceof Character ) 113 return ((Character )fValue).charValue(); 114 else return ((Number )fValue).longValue(); 115 } 116 117 120 public short shortValue() { 121 if (fValue instanceof Boolean ) 122 return ((Boolean )fValue).booleanValue() ? (short)1 : (short)0; 123 else if (fValue instanceof Character ) 124 return (short)((Character )fValue).charValue(); 125 else return ((Number )fValue).shortValue(); 126 } 127 128 132 public boolean equals(Object object) { 133 return object != null && object.getClass().equals(this.getClass()) && fValue.equals(((PrimitiveValueImpl)object).fValue); 134 } 135 136 140 public int hashCode() { 141 return fValue.hashCode(); 142 } 143 144 151 public int compareTo(Object obj) { 152 if (fValue instanceof Character ) 153 return ((Character )fValue).compareTo((Character ) obj); 154 else if (fValue instanceof Byte ) 155 return ((Byte )fValue).compareTo((Byte ) obj); 156 else if (fValue instanceof Double ) 157 return ((Double )fValue).compareTo((Double ) obj); 158 else if (fValue instanceof Float ) 159 return ((Float )fValue).compareTo((Float ) obj); 160 else if (fValue instanceof Integer ) 161 return ((Integer )fValue).compareTo((Integer ) obj); 162 else if (fValue instanceof Long ) 163 return ((Long )fValue).compareTo((Long ) obj); 164 else if (fValue instanceof Short ) 165 return ((Short )fValue).compareTo((Short ) obj); 166 167 throw new InternalException(JDIMessages.PrimitiveValueImpl_Invalid_Primitive_Value_encountered_1); 168 169 } 170 171 174 public String toString() { 175 return fValue.toString(); 176 } 177 178 181 public abstract void write(MirrorImpl target, DataOutputStream out) throws IOException ; 182 183 186 public static PrimitiveValueImpl readWithoutTag(MirrorImpl target, PrimitiveType type, DataInputStream in) throws IOException { 187 switch (((PrimitiveTypeImpl)type).tag()) { 188 case 0: 189 return null; 190 case BooleanValueImpl.tag: 191 return BooleanValueImpl.read(target, in); 192 case ByteValueImpl.tag: 193 return ByteValueImpl.read(target, in); 194 case CharValueImpl.tag: 195 return CharValueImpl.read(target, in); 196 case DoubleValueImpl.tag: 197 return DoubleValueImpl.read(target, in); 198 case FloatValueImpl.tag: 199 return FloatValueImpl.read(target, in); 200 case IntegerValueImpl.tag: 201 return IntegerValueImpl.read(target, in); 202 case LongValueImpl.tag: 203 return LongValueImpl.read(target, in); 204 case ShortValueImpl.tag: 205 return ShortValueImpl.read(target, in); 206 } 207 throw new InternalException(JDIMessages.PrimitiveValueImpl_Invalid_Primitive_Value_tag_encountered___2 + type); 208 } 209 } 210 | Popular Tags |