001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Aleksey V. Yantsen
021: * @version $Revision: 1.4 $
022: */
023:
024: /**
025: * Created on 10.25.2004
026: */package org.apache.harmony.jpda.tests.framework.jdwp;
027:
028: import org.apache.harmony.jpda.tests.framework.TestErrorException;
029: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
030:
031: /**
032: * This class represents generic value used in JDWP packets.
033: */
034: public class Value {
035:
036: private byte tag;
037:
038: private Number numberValue;
039:
040: private boolean booleanValue;
041:
042: private char charValue;
043:
044: /**
045: * Creates new value with no tag.
046: */
047: public Value() {
048: tag = JDWPConstants.Tag.NO_TAG;
049: }
050:
051: /**
052: * Creates new ID value with specified tag.
053: */
054: public Value(byte tag, long value) {
055: this .tag = tag;
056: this .numberValue = new Long(value);
057: }
058:
059: /**
060: * Creates new byte value.
061: */
062: public Value(byte value) {
063: this .tag = JDWPConstants.Tag.BYTE_TAG;
064: this .numberValue = new Byte(value);
065: }
066:
067: /**
068: * Creates new short value.
069: */
070: public Value(short value) {
071: this .tag = JDWPConstants.Tag.SHORT_TAG;
072: this .numberValue = new Short(value);
073: }
074:
075: /**
076: * Creates new int value.
077: */
078: public Value(int value) {
079: this .tag = JDWPConstants.Tag.INT_TAG;
080: this .numberValue = new Integer(value);
081: }
082:
083: /**
084: * Creates new long value.
085: */
086: public Value(long value) {
087: this .tag = JDWPConstants.Tag.LONG_TAG;
088: this .numberValue = new Long(value);
089: }
090:
091: /**
092: * Creates new short value.
093: */
094: public Value(float value) {
095: this .tag = JDWPConstants.Tag.FLOAT_TAG;
096: this .numberValue = new Float(value);
097: }
098:
099: /**
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: }
|