001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdi.internal;
011:
012: import java.io.DataInputStream;
013: import java.io.DataOutputStream;
014: import java.io.IOException;
015:
016: import com.sun.jdi.InternalException;
017: import com.sun.jdi.PrimitiveType;
018: import com.sun.jdi.PrimitiveValue;
019:
020: /**
021: * this class implements the corresponding interfaces
022: * declared by the JDI specification. See the com.sun.jdi package
023: * for more information.
024: *
025: */
026: public abstract class PrimitiveValueImpl extends ValueImpl implements
027: PrimitiveValue, Comparable {
028: /** Primitive value in wrapper. */
029: Object fValue;
030:
031: /**
032: * Creates new ValueImpl.
033: */
034: public PrimitiveValueImpl(String description,
035: VirtualMachineImpl vmImpl, Object value) {
036: super (description, vmImpl);
037: fValue = value;
038: }
039:
040: /**
041: * @return Returns Primitive Value converted to required type.
042: */
043: public boolean booleanValue() {
044: if (fValue instanceof Boolean)
045: return ((Boolean) fValue).booleanValue();
046: else if (fValue instanceof Character)
047: return ((Character) fValue).charValue() != 0;
048: else
049: return ((Number) fValue).doubleValue() != 0;
050: }
051:
052: /**
053: * @return Returns Primitive Value converted to required type.
054: */
055: public char charValue() {
056: if (fValue instanceof Boolean)
057: return ((Boolean) fValue).booleanValue() ? (char) 1
058: : (char) 0;
059: else if (fValue instanceof Character)
060: return ((Character) fValue).charValue();
061: else
062: return (char) ((Number) fValue).intValue();
063: }
064:
065: /**
066: * @return Returns Primitive Value converted to required type.
067: */
068: public byte byteValue() {
069: if (fValue instanceof Boolean)
070: return ((Boolean) fValue).booleanValue() ? (byte) 1
071: : (byte) 0;
072: else if (fValue instanceof Character)
073: return (byte) ((Character) fValue).charValue();
074: else
075: return ((Number) fValue).byteValue();
076: }
077:
078: /**
079: * @return Returns Primitive Value converted to required type.
080: */
081: public double doubleValue() {
082: if (fValue instanceof Boolean)
083: return ((Boolean) fValue).booleanValue() ? (double) 1
084: : (double) 0;
085: else if (fValue instanceof Character)
086: return ((Character) fValue).charValue();
087: else
088: return ((Number) fValue).doubleValue();
089: }
090:
091: /**
092: * @return Returns Primitive Value converted to required type.
093: */
094: public float floatValue() {
095: if (fValue instanceof Boolean)
096: return ((Boolean) fValue).booleanValue() ? (float) 1
097: : (float) 0;
098: else if (fValue instanceof Character)
099: return ((Character) fValue).charValue();
100: else
101: return ((Number) fValue).floatValue();
102: }
103:
104: /**
105: * @return Returns Primitive Value converted to required type.
106: */
107: public int intValue() {
108: if (fValue instanceof Boolean)
109: return ((Boolean) fValue).booleanValue() ? (int) 1
110: : (int) 0;
111: else if (fValue instanceof Character)
112: return ((Character) fValue).charValue();
113: else
114: return ((Number) fValue).intValue();
115: }
116:
117: /**
118: * @return Returns Primitive Value converted to required type.
119: */
120: public long longValue() {
121: if (fValue instanceof Boolean)
122: return ((Boolean) fValue).booleanValue() ? (long) 1
123: : (long) 0;
124: else if (fValue instanceof Character)
125: return ((Character) fValue).charValue();
126: else
127: return ((Number) fValue).longValue();
128: }
129:
130: /**
131: * @return Returns Primitive Value converted to required type.
132: */
133: public short shortValue() {
134: if (fValue instanceof Boolean)
135: return ((Boolean) fValue).booleanValue() ? (short) 1
136: : (short) 0;
137: else if (fValue instanceof Character)
138: return (short) ((Character) fValue).charValue();
139: else
140: return ((Number) fValue).shortValue();
141: }
142:
143: /**
144: * @return Returns true if two values are equal.
145: * @see java.lang.Object#equals(Object)
146: */
147: public boolean equals(Object object) {
148: return object != null
149: && object.getClass().equals(this .getClass())
150: && fValue.equals(((PrimitiveValueImpl) object).fValue);
151: }
152:
153: /**
154: * @return Returns a has code for this object.
155: * @see java.lang.Object#hashCode
156: */
157: public int hashCode() {
158: return fValue.hashCode();
159: }
160:
161: /**
162: * Compares this object with the specified object for order.
163: * Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
164: *
165: * May throw a ClassCastException if obj is not comparable. This is in accordance
166: * with Java 1.4 compareTo(Object) methods.
167: */
168: public int compareTo(Object obj) {
169: if (fValue instanceof Character)
170: return ((Character) fValue).compareTo((Character) obj);
171: else if (fValue instanceof Byte)
172: return ((Byte) fValue).compareTo((Byte) obj);
173: else if (fValue instanceof Double)
174: return ((Double) fValue).compareTo((Double) obj);
175: else if (fValue instanceof Float)
176: return ((Float) fValue).compareTo((Float) obj);
177: else if (fValue instanceof Integer)
178: return ((Integer) fValue).compareTo((Integer) obj);
179: else if (fValue instanceof Long)
180: return ((Long) fValue).compareTo((Long) obj);
181: else if (fValue instanceof Short)
182: return ((Short) fValue).compareTo((Short) obj);
183:
184: throw new InternalException(
185: JDIMessages.PrimitiveValueImpl_Invalid_Primitive_Value_encountered_1);
186:
187: }
188:
189: /**
190: * @return Returns description of Mirror object.
191: */
192: public String toString() {
193: return fValue.toString();
194: }
195:
196: /**
197: * Writes value without value tag.
198: */
199: public abstract void write(MirrorImpl target, DataOutputStream out)
200: throws IOException;
201:
202: /**
203: * @return Reads JDWP representation and returns new instance.
204: */
205: public static PrimitiveValueImpl readWithoutTag(MirrorImpl target,
206: PrimitiveType type, DataInputStream in) throws IOException {
207: switch (((PrimitiveTypeImpl) type).tag()) {
208: case 0:
209: return null;
210: case BooleanValueImpl.tag:
211: return BooleanValueImpl.read(target, in);
212: case ByteValueImpl.tag:
213: return ByteValueImpl.read(target, in);
214: case CharValueImpl.tag:
215: return CharValueImpl.read(target, in);
216: case DoubleValueImpl.tag:
217: return DoubleValueImpl.read(target, in);
218: case FloatValueImpl.tag:
219: return FloatValueImpl.read(target, in);
220: case IntegerValueImpl.tag:
221: return IntegerValueImpl.read(target, in);
222: case LongValueImpl.tag:
223: return LongValueImpl.read(target, in);
224: case ShortValueImpl.tag:
225: return ShortValueImpl.read(target, in);
226: }
227: throw new InternalException(
228: JDIMessages.PrimitiveValueImpl_Invalid_Primitive_Value_tag_encountered___2
229: + type);
230: }
231: }
|