1/*
2 * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.jdi;
27
28import com.sun.jdi.FloatValue;
29import com.sun.jdi.InvalidTypeException;
30import com.sun.jdi.Type;
31import com.sun.jdi.VirtualMachine;
32
33public class FloatValueImpl extends PrimitiveValueImpl
34                            implements FloatValue {
35    private float value;
36
37    FloatValueImpl(VirtualMachine aVm, float aValue) {
38        super(aVm);
39        value = aValue;
40    }
41
42    public boolean equals(Object obj) {
43        if ((obj != null) && (obj instanceof FloatValue)) {
44            return (value == ((FloatValue)obj).value()) &&
45                   super.equals(obj);
46        } else {
47            return false;
48        }
49    }
50
51    public int hashCode() {
52        /*
53         * TO DO: Better hash code
54         */
55        return intValue();
56    }
57
58    public int compareTo(FloatValue obj) {
59        float other = obj.value();
60        if (value() < other) {
61            return -1;
62        } else if (value() == other) {
63            return 0;
64        } else {
65            return 1;
66        }
67    }
68
69    public Type type() {
70        return vm.theFloatType();
71    }
72
73    public float value() {
74        return value;
75    }
76
77    public boolean booleanValue() {
78        return (value == 0.0 ? false : true);
79    }
80
81    public byte byteValue() {
82        return (byte)value;
83    }
84
85    public char charValue() {
86        return (char)value;
87    }
88
89    public short shortValue() {
90        return (short)value;
91    }
92
93    public int intValue() {
94        return (int)value;
95    }
96
97    public long longValue() {
98        return (long)value;
99    }
100
101    public float floatValue() {
102        return value;
103    }
104
105    public double doubleValue() {
106        return value;
107    }
108
109    byte checkedByteValue() throws InvalidTypeException {
110        if ((value > Byte.MAX_VALUE) || (value < Byte.MIN_VALUE)) {
111            throw new InvalidTypeException("Can't convert " + value + " to byte");
112        } else {
113            return super.checkedByteValue();
114        }
115    }
116
117    char checkedCharValue() throws InvalidTypeException {
118        if ((value > Character.MAX_VALUE) || (value < Character.MIN_VALUE)) {
119            throw new InvalidTypeException("Can't convert " + value + " to char");
120        } else {
121            return super.checkedCharValue();
122        }
123    }
124
125    short checkedShortValue() throws InvalidTypeException {
126        if ((value > Short.MAX_VALUE) || (value < Short.MIN_VALUE)) {
127            throw new InvalidTypeException("Can't convert " + value + " to short");
128        } else {
129            return super.checkedShortValue();
130        }
131    }
132
133    int checkedIntValue() throws InvalidTypeException {
134        int intValue = (int)value;
135        if (intValue != value) {
136            throw new InvalidTypeException("Can't convert " + value + " to int");
137        } else {
138            return super.checkedIntValue();
139        }
140    }
141
142    long checkedLongValue() throws InvalidTypeException {
143        long longValue = (long)value;
144        if (longValue != value) {
145            throw new InvalidTypeException("Can't convert " + value + " to long");
146        } else {
147            return super.checkedLongValue();
148        }
149    }
150
151    public String toString() {
152        return "" + value;
153    }
154
155    byte typeValueKey() {
156        return JDWP.Tag.FLOAT;
157    }
158}
159