CstFloat.java revision 579d7739c53a2707ad711a2d2cae46d7d782f061
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (C) 2007 The Android Open Source Project
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Licensed under the Apache License, Version 2.0 (the "License");
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * you may not use this file except in compliance with the License.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * You may obtain a copy of the License at
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *      http://www.apache.org/licenses/LICENSE-2.0
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Unless required by applicable law or agreed to in writing, software
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * distributed under the License is distributed on an "AS IS" BASIS,
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * See the License for the specific language governing permissions and
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * limitations under the License.
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)package com.android.dx.rop.cst;
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import com.android.dx.rop.type.Type;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import com.android.dx.util.Hex;
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Constants of type {@code CONSTANT_Float_info}.
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)public final class CstFloat
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        extends CstLiteral32 {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    /** {@code non-null;} instance representing {@code 0} */
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    public static final CstFloat VALUE_0 = make(Float.floatToIntBits(0.0f));
29
30    /** {@code non-null;} instance representing {@code 1} */
31    public static final CstFloat VALUE_1 = make(Float.floatToIntBits(1.0f));
32
33    /** {@code non-null;} instance representing {@code 2} */
34    public static final CstFloat VALUE_2 = make(Float.floatToIntBits(2.0f));
35
36    /**
37     * Makes an instance for the given value. This may (but does not
38     * necessarily) return an already-allocated instance.
39     *
40     * @param bits the {@code float} value as {@code int} bits
41     */
42    public static CstFloat make(int bits) {
43        /*
44         * Note: Javadoc notwithstanding, this implementation always
45         * allocates.
46         */
47        return new CstFloat(bits);
48    }
49
50    /**
51     * Constructs an instance. This constructor is private; use {@link #make}.
52     *
53     * @param bits the {@code float} value as {@code int} bits
54     */
55    private CstFloat(int bits) {
56        super(bits);
57    }
58
59    /** {@inheritDoc} */
60    @Override
61    public String toString() {
62        int bits = getIntBits();
63        return "float{0x" + Hex.u4(bits) + " / " +
64            Float.intBitsToFloat(bits) + '}';
65    }
66
67    /** {@inheritDoc} */
68    public Type getType() {
69        return Type.FLOAT;
70    }
71
72    /** {@inheritDoc} */
73    @Override
74    public String typeName() {
75        return "float";
76    }
77
78    /** {@inheritDoc} */
79    public String toHuman() {
80        return Float.toString(Float.intBitsToFloat(getIntBits()));
81    }
82
83    /**
84     * Gets the {@code float} value.
85     *
86     * @return the value
87     */
88    public float getValue() {
89        return Float.intBitsToFloat(getIntBits());
90    }
91}
92