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 */
26package org.apache.harmony.jpda.tests.framework.jdwp;
27
28import org.apache.harmony.jpda.tests.framework.TestErrorException;
29import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
30
31/**
32 * This class represents generic value used in JDWP packets.
33 */
34public class Value {
35
36    private byte tag;
37
38    private Number numberValue;
39
40    private boolean booleanValue;
41
42    private char charValue;
43
44    /**
45     * Creates new value with no tag.
46     */
47    public Value() {
48        tag = JDWPConstants.Tag.NO_TAG;
49    }
50
51    /**
52     * Creates new ID value with specified tag.
53     */
54    public Value(byte tag, long value) {
55        this.tag = tag;
56        this.numberValue = new Long(value);
57    }
58
59    /**
60     * Creates new byte value.
61     */
62    public Value(byte value) {
63        this.tag = JDWPConstants.Tag.BYTE_TAG;
64        this.numberValue = new Byte(value);
65    }
66
67    /**
68     * Creates new short value.
69     */
70    public Value(short value) {
71        this.tag = JDWPConstants.Tag.SHORT_TAG;
72        this.numberValue = new Short(value);
73    }
74
75    /**
76     * Creates new int value.
77     */
78    public Value(int value) {
79        this.tag = JDWPConstants.Tag.INT_TAG;
80        this.numberValue = new Integer(value);
81    }
82
83    /**
84     * Creates new long value.
85     */
86    public Value(long value) {
87        this.tag = JDWPConstants.Tag.LONG_TAG;
88        this.numberValue = new Long(value);
89    }
90
91    /**
92     * Creates new short value.
93     */
94    public Value(float value) {
95        this.tag = JDWPConstants.Tag.FLOAT_TAG;
96        this.numberValue = new Float(value);
97    }
98
99    /**
100     * Creates new double value.
101     */
102    public Value(double value) {
103        this.tag = JDWPConstants.Tag.DOUBLE_TAG;
104        this.numberValue = new Double(value);
105    }
106
107    /**
108     * Creates new boolean value.
109     */
110    public Value(boolean value) {
111        this.tag = JDWPConstants.Tag.BOOLEAN_TAG;
112        this.booleanValue = value;
113    }
114
115    /**
116     * Creates new char value.
117     */
118    public Value(char value) {
119        this.tag = JDWPConstants.Tag.CHAR_TAG;
120        this.charValue = value;
121    }
122
123    /**
124     * Returns tag of this value.
125     *
126     * @return Returns the tag.
127     */
128    public byte getTag() {
129        return tag;
130    }
131
132    /**
133     * Returns byte representation of this value.
134     *
135     * @return byte value
136     */
137    public byte getByteValue() {
138        return numberValue.byteValue();
139    }
140
141    /**
142     * Returns short representation of this value.
143     *
144     * @return short value
145     */
146    public short getShortValue() {
147        return numberValue.shortValue();
148    }
149
150    /**
151     * Returns int representation of this value.
152     *
153     * @return int value
154     */
155    public int getIntValue() {
156        return numberValue.intValue();
157    }
158
159    /**
160     * Returns long representation of this value.
161     *
162     * @return long value
163     */
164    public long getLongValue() {
165        return numberValue.longValue();
166    }
167
168    /**
169     * Returns float representation of this value.
170     *
171     * @return float value
172     */
173    public float getFloatValue() {
174        return numberValue.floatValue();
175    }
176
177    /**
178     * Returns double representation of this value.
179     *
180     * @return double value
181     */
182    public double getDoubleValue() {
183        return numberValue.doubleValue();
184    }
185
186    /**
187     * Returns boolean representation of this value.
188     *
189     * @return boolean value
190     */
191    public boolean getBooleanValue() {
192        return booleanValue;
193    }
194
195    /**
196     * Returns char representation of this value.
197     *
198     * @return char value
199     */
200    public char getCharValue() {
201        return charValue;
202    }
203
204    /**
205     * Compares with other value.
206     */
207    public boolean equals(Object arg0) {
208        if (!(arg0 instanceof Value))
209            return false;
210
211        Value value0 = (Value) arg0;
212        if (value0.tag != value0.tag)
213            return false;
214
215        switch (tag) {
216        case JDWPConstants.Tag.BOOLEAN_TAG:
217            return getBooleanValue() == value0.getBooleanValue();
218        case JDWPConstants.Tag.BYTE_TAG:
219            return getByteValue() == value0.getByteValue();
220        case JDWPConstants.Tag.CHAR_TAG:
221            return getCharValue() == value0.getCharValue();
222        case JDWPConstants.Tag.DOUBLE_TAG:
223            if (Double.isNaN(getDoubleValue())
224                    && (Double.isNaN(value0.getDoubleValue())))
225                return true;
226            return getDoubleValue() == value0.getDoubleValue();
227        case JDWPConstants.Tag.FLOAT_TAG:
228            if (Float.isNaN(getFloatValue())
229                    && (Float.isNaN(value0.getFloatValue())))
230                return true;
231            return getFloatValue() == value0.getFloatValue();
232        case JDWPConstants.Tag.INT_TAG:
233            return getIntValue() == value0.getIntValue();
234        case JDWPConstants.Tag.LONG_TAG:
235            return getLongValue() == value0.getLongValue();
236        case JDWPConstants.Tag.SHORT_TAG:
237            return getShortValue() == value0.getShortValue();
238        case JDWPConstants.Tag.STRING_TAG:
239        case JDWPConstants.Tag.ARRAY_TAG:
240        case JDWPConstants.Tag.CLASS_LOADER_TAG:
241        case JDWPConstants.Tag.CLASS_OBJECT_TAG:
242        case JDWPConstants.Tag.OBJECT_TAG:
243        case JDWPConstants.Tag.THREAD_GROUP_TAG:
244        case JDWPConstants.Tag.THREAD_TAG:
245            return getLongValue() == value0.getLongValue();
246        }
247
248        throw new TestErrorException("Illegal tag value");
249    }
250
251    /**
252     * Converts this value to string representation for printing.
253     */
254    public String toString() {
255
256        switch (tag) {
257        case JDWPConstants.Tag.BOOLEAN_TAG:
258            return "boolean: " + getBooleanValue();
259        case JDWPConstants.Tag.BYTE_TAG:
260            return "byte: " + getByteValue();
261        case JDWPConstants.Tag.CHAR_TAG:
262            return "char: " + getCharValue();
263        case JDWPConstants.Tag.DOUBLE_TAG:
264            return "double: " + getDoubleValue();
265        case JDWPConstants.Tag.FLOAT_TAG:
266            return "float: " + getFloatValue();
267        case JDWPConstants.Tag.INT_TAG:
268            return "int: " + getIntValue();
269        case JDWPConstants.Tag.LONG_TAG:
270            return "long: " + getLongValue();
271        case JDWPConstants.Tag.SHORT_TAG:
272            return "short: " + getShortValue();
273        case JDWPConstants.Tag.STRING_TAG:
274            return "StringID: " + getLongValue();
275        case JDWPConstants.Tag.ARRAY_TAG:
276            return "ObjectID: " + getLongValue();
277
278        case JDWPConstants.Tag.CLASS_LOADER_TAG:
279            return "ClassLoaderID: " + getLongValue();
280        case JDWPConstants.Tag.CLASS_OBJECT_TAG:
281            return "ClassObjectID: " + getLongValue();
282        case JDWPConstants.Tag.OBJECT_TAG:
283            return "ObjectID: " + getLongValue();
284        case JDWPConstants.Tag.THREAD_GROUP_TAG:
285            return "ThreadGroupID: " + getLongValue();
286        case JDWPConstants.Tag.THREAD_TAG:
287            return "ThreadID: " + getLongValue();
288        }
289
290        throw new TestErrorException("Illegal tag value: " + tag);
291    }
292}
293