KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdi > internal > PrimitiveValueImpl


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdi.internal;
12
13
14 import java.io.DataInputStream JavaDoc;
15 import java.io.DataOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17
18 import com.sun.jdi.InternalException;
19 import com.sun.jdi.PrimitiveType;
20 import com.sun.jdi.PrimitiveValue;
21
22 /**
23  * this class implements the corresponding interfaces
24  * declared by the JDI specification. See the com.sun.jdi package
25  * for more information.
26  *
27  */

28 public abstract class PrimitiveValueImpl extends ValueImpl implements PrimitiveValue, Comparable JavaDoc {
29     /** Primitive value in wrapper. */
30     Object JavaDoc fValue;
31     
32     /**
33      * Creates new ValueImpl.
34      */

35     public PrimitiveValueImpl(String JavaDoc description, VirtualMachineImpl vmImpl, Object JavaDoc value) {
36         super(description, vmImpl);
37         fValue = value;
38     }
39     
40     /**
41      * @return Returns Primitive Value converted to required type.
42      */

43     public boolean booleanValue() {
44         if (fValue instanceof Boolean JavaDoc)
45             return ((Boolean JavaDoc)fValue).booleanValue();
46         else if (fValue instanceof Character JavaDoc)
47             return ((Character JavaDoc)fValue).charValue() != 0;
48         else return ((Number JavaDoc)fValue).doubleValue() != 0;
49     }
50     
51     /**
52      * @return Returns Primitive Value converted to required type.
53      */

54     public char charValue() {
55         if (fValue instanceof Boolean JavaDoc)
56             return ((Boolean JavaDoc)fValue).booleanValue() ? (char)1 : (char)0;
57         else if (fValue instanceof Character JavaDoc)
58             return ((Character JavaDoc)fValue).charValue();
59         else return (char)((Number JavaDoc)fValue).intValue();
60     }
61
62     /**
63      * @return Returns Primitive Value converted to required type.
64      */

65     public byte byteValue() {
66         if (fValue instanceof Boolean JavaDoc)
67             return ((Boolean JavaDoc)fValue).booleanValue() ? (byte)1 : (byte)0;
68         else if (fValue instanceof Character JavaDoc)
69             return (byte)((Character JavaDoc)fValue).charValue();
70         else return ((Number JavaDoc)fValue).byteValue();
71     }
72
73     /**
74      * @return Returns Primitive Value converted to required type.
75      */

76     public double doubleValue() {
77         if (fValue instanceof Boolean JavaDoc)
78             return ((Boolean JavaDoc)fValue).booleanValue() ? (double)1 : (double)0;
79         else if (fValue instanceof Character JavaDoc)
80             return ((Character JavaDoc)fValue).charValue();
81         else return ((Number JavaDoc)fValue).doubleValue();
82     }
83
84     /**
85      * @return Returns Primitive Value converted to required type.
86      */

87     public float floatValue() {
88         if (fValue instanceof Boolean JavaDoc)
89             return ((Boolean JavaDoc)fValue).booleanValue() ? (float)1 : (float)0;
90         else if (fValue instanceof Character JavaDoc)
91             return ((Character JavaDoc)fValue).charValue();
92         else return ((Number JavaDoc)fValue).floatValue();
93     }
94
95     /**
96      * @return Returns Primitive Value converted to required type.
97      */

98     public int intValue() {
99         if (fValue instanceof Boolean JavaDoc)
100             return ((Boolean JavaDoc)fValue).booleanValue() ? (int)1 : (int)0;
101         else if (fValue instanceof Character JavaDoc)
102             return ((Character JavaDoc)fValue).charValue();
103         else return ((Number JavaDoc)fValue).intValue();
104     }
105
106     /**
107      * @return Returns Primitive Value converted to required type.
108      */

109     public long longValue() {
110         if (fValue instanceof Boolean JavaDoc)
111             return ((Boolean JavaDoc)fValue).booleanValue() ? (long)1 : (long)0;
112         else if (fValue instanceof Character JavaDoc)
113             return ((Character JavaDoc)fValue).charValue();
114         else return ((Number JavaDoc)fValue).longValue();
115     }
116
117     /**
118      * @return Returns Primitive Value converted to required type.
119      */

120     public short shortValue() {
121         if (fValue instanceof Boolean JavaDoc)
122             return ((Boolean JavaDoc)fValue).booleanValue() ? (short)1 : (short)0;
123         else if (fValue instanceof Character JavaDoc)
124             return (short)((Character JavaDoc)fValue).charValue();
125         else return ((Number JavaDoc)fValue).shortValue();
126     }
127     
128     /**
129      * @return Returns true if two values are equal.
130      * @see java.lang.Object#equals(Object)
131      */

132     public boolean equals(Object JavaDoc object) {
133         return object != null && object.getClass().equals(this.getClass()) && fValue.equals(((PrimitiveValueImpl)object).fValue);
134     }
135     
136     /**
137      * @return Returns a has code for this object.
138      * @see java.lang.Object#hashCode
139      */

140     public int hashCode() {
141         return fValue.hashCode();
142     }
143     
144     /**
145      * Compares this object with the specified object for order.
146      * Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
147      *
148      * May throw a ClassCastException if obj is not comparable. This is in accordance
149      * with Java 1.4 compareTo(Object) methods.
150      */

151     public int compareTo(Object JavaDoc obj) {
152         if (fValue instanceof Character JavaDoc)
153             return ((Character JavaDoc)fValue).compareTo((Character JavaDoc) obj);
154         else if (fValue instanceof Byte JavaDoc)
155             return ((Byte JavaDoc)fValue).compareTo((Byte JavaDoc) obj);
156         else if (fValue instanceof Double JavaDoc)
157             return ((Double JavaDoc)fValue).compareTo((Double JavaDoc) obj);
158         else if (fValue instanceof Float JavaDoc)
159             return ((Float JavaDoc)fValue).compareTo((Float JavaDoc) obj);
160         else if (fValue instanceof Integer JavaDoc)
161             return ((Integer JavaDoc)fValue).compareTo((Integer JavaDoc) obj);
162         else if (fValue instanceof Long JavaDoc)
163             return ((Long JavaDoc)fValue).compareTo((Long JavaDoc) obj);
164         else if (fValue instanceof Short JavaDoc)
165             return ((Short JavaDoc)fValue).compareTo((Short JavaDoc) obj);
166             
167         throw new InternalException(JDIMessages.PrimitiveValueImpl_Invalid_Primitive_Value_encountered_1);
168         
169     }
170     
171     /**
172      * @return Returns description of Mirror object.
173      */

174     public String JavaDoc toString() {
175         return fValue.toString();
176     }
177
178     /**
179      * Writes value without value tag.
180      */

181     public abstract void write(MirrorImpl target, DataOutputStream JavaDoc out) throws IOException JavaDoc;
182     
183     /**
184      * @return Reads JDWP representation and returns new instance.
185      */

186     public static PrimitiveValueImpl readWithoutTag(MirrorImpl target, PrimitiveType type, DataInputStream JavaDoc in) throws IOException JavaDoc {
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