• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18 
19 /**
20  * @author Aleksey V. Yantsen
21  */
22 
23 /**
24  * Created on 10.25.2004
25  */
26 package org.apache.harmony.jpda.tests.framework.jdwp;
27 
28 import org.apache.harmony.jpda.tests.framework.TestErrorException;
29 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
30 
31 /**
32  * This class represents generic value used in JDWP packets.
33  */
34 public class Value {
35 
36     /**
37      * Creates new boolean value.
38      */
createBoolean(boolean value)39     public static Value createBoolean(boolean value) {
40         return new Value(value);
41     }
42 
43     /**
44      * Creates new byte value.
45      */
createByte(byte value)46     public static Value createByte(byte value) {
47         return new Value(JDWPConstants.Tag.BYTE_TAG, Byte.valueOf(value));
48     }
49 
50     /**
51      * Creates new char value.
52      */
createChar(char value)53     public static Value createChar(char value) {
54         return new Value(value);
55     }
56 
57     /**
58      * Creates new short value.
59      */
createShort(short value)60     public static Value createShort(short value) {
61         return new Value(JDWPConstants.Tag.SHORT_TAG, Short.valueOf(value));
62     }
63 
64     /**
65      * Creates new int value.
66      */
createInt(int value)67     public static Value createInt(int value) {
68         return new Value(JDWPConstants.Tag.INT_TAG, Integer.valueOf(value));
69     }
70 
71     /**
72      * Creates new long value.
73      */
createLong(long value)74     public static Value createLong(long value) {
75         return new Value(JDWPConstants.Tag.LONG_TAG, Long.valueOf(value));
76     }
77 
78     /**
79      * Creates new float value.
80      */
createFloat(float value)81     public static Value createFloat(float value) {
82         return new Value(JDWPConstants.Tag.FLOAT_TAG, Float.valueOf(value));
83     }
84 
85     /**
86      * Creates new double value.
87      */
createDouble(double value)88     public static Value createDouble(double value) {
89         return new Value(JDWPConstants.Tag.DOUBLE_TAG, Double.valueOf(value));
90     }
91 
92     /**
93      * Creates void value.
94      */
createVoidValue()95     public static Value createVoidValue() {
96         return new Value(JDWPConstants.Tag.VOID_TAG, Long.valueOf(0));
97     }
98 
99     /**
100      * Creates object value.
101      */
createObjectValue(byte tag, long value)102     public static Value createObjectValue(byte tag, long value) {
103         if (isPrimitiveTag(tag)) {
104             throw new AssertionError(JDWPConstants.Tag.getName(tag) + " is primitive");
105         }
106         return new Value(tag, Long.valueOf(value));
107     }
108 
109     private final byte tag;
110 
111     private final Number numberValue;
112 
113     private final boolean booleanValue;
114 
115     private final char charValue;
116 
117     /**
118      * Creates new value.
119      */
Value(byte tag, Number numberValue)120     private Value(byte tag, Number numberValue) {
121         this.tag = tag;
122         this.numberValue = numberValue;
123         this.booleanValue = false;
124         this.charValue = 0;
125     }
126 
127     /**
128      * Creates new boolean value.
129      */
Value(boolean value)130     private Value(boolean value) {
131         this.tag = JDWPConstants.Tag.BOOLEAN_TAG;
132         this.booleanValue = value;
133         this.numberValue = null;
134         this.charValue = 0;
135     }
136 
137     /**
138      * Creates new char value.
139      */
Value(char value)140     private Value(char value) {
141         this.tag = JDWPConstants.Tag.CHAR_TAG;
142         this.charValue = value;
143         this.numberValue = null;
144         this.booleanValue = false;
145     }
146 
147     /**
148      * Returns tag of this value.
149      *
150      * @return Returns the tag.
151      */
getTag()152     public byte getTag() {
153         return tag;
154     }
155 
156     /**
157      * Returns byte representation of this value.
158      *
159      * @return byte value
160      */
getByteValue()161     public byte getByteValue() {
162         return numberValue.byteValue();
163     }
164 
165     /**
166      * Returns short representation of this value.
167      *
168      * @return short value
169      */
getShortValue()170     public short getShortValue() {
171         return numberValue.shortValue();
172     }
173 
174     /**
175      * Returns int representation of this value.
176      *
177      * @return int value
178      */
getIntValue()179     public int getIntValue() {
180         return numberValue.intValue();
181     }
182 
183     /**
184      * Returns long representation of this value.
185      *
186      * @return long value
187      */
getLongValue()188     public long getLongValue() {
189         return numberValue.longValue();
190     }
191 
192     /**
193      * Returns float representation of this value.
194      *
195      * @return float value
196      */
getFloatValue()197     public float getFloatValue() {
198         return numberValue.floatValue();
199     }
200 
201     /**
202      * Returns double representation of this value.
203      *
204      * @return double value
205      */
getDoubleValue()206     public double getDoubleValue() {
207         return numberValue.doubleValue();
208     }
209 
210     /**
211      * Returns boolean representation of this value.
212      *
213      * @return boolean value
214      */
getBooleanValue()215     public boolean getBooleanValue() {
216         return booleanValue;
217     }
218 
219     /**
220      * Returns char representation of this value.
221      *
222      * @return char value
223      */
getCharValue()224     public char getCharValue() {
225         return charValue;
226     }
227 
isPrimitiveTag(byte tag)228     private static boolean isPrimitiveTag(byte tag) {
229         switch (tag) {
230             case JDWPConstants.Tag.BOOLEAN_TAG:
231             case JDWPConstants.Tag.BYTE_TAG:
232             case JDWPConstants.Tag.CHAR_TAG:
233             case JDWPConstants.Tag.SHORT_TAG:
234             case JDWPConstants.Tag.INT_TAG:
235             case JDWPConstants.Tag.LONG_TAG:
236             case JDWPConstants.Tag.FLOAT_TAG:
237             case JDWPConstants.Tag.DOUBLE_TAG:
238             case JDWPConstants.Tag.VOID_TAG:
239                 return true;
240             case JDWPConstants.Tag.NO_TAG:
241             case JDWPConstants.Tag.ARRAY_TAG:
242             case JDWPConstants.Tag.CLASS_LOADER_TAG:
243             case JDWPConstants.Tag.CLASS_OBJECT_TAG:
244             case JDWPConstants.Tag.OBJECT_TAG:
245             case JDWPConstants.Tag.STRING_TAG:
246             case JDWPConstants.Tag.THREAD_TAG:
247             case JDWPConstants.Tag.THREAD_GROUP_TAG:
248                 return false;
249             default:
250                 throw new TestErrorException("Illegal tag value: " + tag);
251         }
252     }
253 
254     /**
255      * Compares with other value.
256      */
257     @Override
equals(Object arg0)258     public boolean equals(Object arg0) {
259         if (!(arg0 instanceof Value))
260             return false;
261 
262         Value value0 = (Value) arg0;
263         if (tag != value0.tag)
264             return false;
265 
266         switch (tag) {
267         case JDWPConstants.Tag.BOOLEAN_TAG:
268             return getBooleanValue() == value0.getBooleanValue();
269         case JDWPConstants.Tag.BYTE_TAG:
270             return getByteValue() == value0.getByteValue();
271         case JDWPConstants.Tag.CHAR_TAG:
272             return getCharValue() == value0.getCharValue();
273         case JDWPConstants.Tag.DOUBLE_TAG:
274             if (Double.isNaN(getDoubleValue())
275                     && (Double.isNaN(value0.getDoubleValue())))
276                 return true;
277             return getDoubleValue() == value0.getDoubleValue();
278         case JDWPConstants.Tag.FLOAT_TAG:
279             if (Float.isNaN(getFloatValue())
280                     && (Float.isNaN(value0.getFloatValue())))
281                 return true;
282             return getFloatValue() == value0.getFloatValue();
283         case JDWPConstants.Tag.INT_TAG:
284             return getIntValue() == value0.getIntValue();
285         case JDWPConstants.Tag.LONG_TAG:
286             return getLongValue() == value0.getLongValue();
287         case JDWPConstants.Tag.SHORT_TAG:
288             return getShortValue() == value0.getShortValue();
289         case JDWPConstants.Tag.STRING_TAG:
290         case JDWPConstants.Tag.ARRAY_TAG:
291         case JDWPConstants.Tag.CLASS_LOADER_TAG:
292         case JDWPConstants.Tag.CLASS_OBJECT_TAG:
293         case JDWPConstants.Tag.OBJECT_TAG:
294         case JDWPConstants.Tag.THREAD_GROUP_TAG:
295         case JDWPConstants.Tag.THREAD_TAG:
296             return getLongValue() == value0.getLongValue();
297         }
298 
299         throw new TestErrorException("Illegal tag value");
300     }
301 
302     /**
303      * Converts this value to string representation for printing.
304      */
305     @Override
toString()306     public String toString() {
307 
308         switch (tag) {
309         case JDWPConstants.Tag.BOOLEAN_TAG:
310             return "boolean: " + getBooleanValue();
311         case JDWPConstants.Tag.BYTE_TAG:
312             return "byte: " + getByteValue();
313         case JDWPConstants.Tag.CHAR_TAG:
314             return "char: " + getCharValue();
315         case JDWPConstants.Tag.DOUBLE_TAG:
316             return "double: " + getDoubleValue();
317         case JDWPConstants.Tag.FLOAT_TAG:
318             return "float: " + getFloatValue();
319         case JDWPConstants.Tag.INT_TAG:
320             return "int: " + getIntValue();
321         case JDWPConstants.Tag.LONG_TAG:
322             return "long: " + getLongValue();
323         case JDWPConstants.Tag.SHORT_TAG:
324             return "short: " + getShortValue();
325         case JDWPConstants.Tag.STRING_TAG:
326             return "StringID: " + getLongValue();
327         case JDWPConstants.Tag.ARRAY_TAG:
328             return "ArrayID: " + getLongValue();
329         case JDWPConstants.Tag.CLASS_LOADER_TAG:
330             return "ClassLoaderID: " + getLongValue();
331         case JDWPConstants.Tag.CLASS_OBJECT_TAG:
332             return "ClassObjectID: " + getLongValue();
333         case JDWPConstants.Tag.OBJECT_TAG:
334             return "ObjectID: " + getLongValue();
335         case JDWPConstants.Tag.THREAD_GROUP_TAG:
336             return "ThreadGroupID: " + getLongValue();
337         case JDWPConstants.Tag.THREAD_TAG:
338             return "ThreadID: " + getLongValue();
339         }
340 
341         throw new TestErrorException("Illegal tag value: " + tag);
342     }
343 }
344