1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2009 Eric Lafortune (eric@graphics.cornell.edu)
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21package proguard.evaluation.value;
22
23/**
24 * This FloatValue represents a particular float value.
25 *
26 * @author Eric Lafortune
27 */
28final class ParticularFloatValue extends SpecificFloatValue
29{
30    private final float value;
31
32
33    /**
34     * Creates a new particular float value.
35     */
36    public ParticularFloatValue(float value)
37    {
38        this.value = value;
39    }
40
41
42    // Implementations for FloatValue.
43
44    public float value()
45    {
46        return value;
47    }
48
49
50    // Implementations of unary methods of FloatValue.
51
52    public FloatValue negate()
53    {
54        return new ParticularFloatValue(-value);
55    }
56
57    public IntegerValue convertToInteger()
58    {
59        return new ParticularIntegerValue((int)value);
60    }
61
62    public LongValue convertToLong()
63    {
64        return new ParticularLongValue((long)value);
65    }
66
67    public DoubleValue convertToDouble()
68    {
69        return new ParticularDoubleValue((float)value);
70    }
71
72
73    // Implementations of binary methods of FloatValue.
74
75    public FloatValue generalize(FloatValue other)
76    {
77        return other.generalize(this);
78    }
79
80    public FloatValue add(FloatValue other)
81    {
82        return value == 0.0 ? other : other.add(this);
83    }
84
85    public FloatValue subtract(FloatValue other)
86    {
87        return value == 0.0 ? other.negate() : other.subtractFrom(this);
88    }
89
90    public FloatValue subtractFrom(FloatValue other)
91    {
92        return value == 0.0 ? other : other.subtract(this);
93    }
94
95    public FloatValue multiply(FloatValue other)
96    {
97        return other.multiply(this);
98    }
99
100    public FloatValue divide(FloatValue other)
101    {
102        return other.divideOf(this);
103    }
104
105    public FloatValue divideOf(FloatValue other)
106    {
107        return other.divide(this);
108    }
109
110    public FloatValue remainder(FloatValue other)
111    {
112        return other.remainderOf(this);
113    }
114
115    public FloatValue remainderOf(FloatValue other)
116    {
117        return other.remainder(this);
118    }
119
120    public IntegerValue compare(FloatValue other)
121    {
122        return other.compareReverse(this);
123    }
124
125
126    // Implementations of binary FloatValue methods with ParticularFloatValue
127    // arguments.
128
129    public FloatValue generalize(ParticularFloatValue other)
130    {
131        return this.value == other.value ? this : ValueFactory.FLOAT_VALUE;
132    }
133
134    public FloatValue add(ParticularFloatValue other)
135    {
136        return new ParticularFloatValue(this.value + other.value);
137    }
138
139    public FloatValue subtract(ParticularFloatValue other)
140    {
141        return new ParticularFloatValue(this.value - other.value);
142    }
143
144    public FloatValue subtractFrom(ParticularFloatValue other)
145    {
146        return new ParticularFloatValue(other.value - this.value);
147    }
148
149    public FloatValue multiply(ParticularFloatValue other)
150    {
151        return new ParticularFloatValue(this.value * other.value);
152    }
153
154    public FloatValue divide(ParticularFloatValue other)
155    {
156        return new ParticularFloatValue(this.value / other.value);
157    }
158
159    public FloatValue divideOf(ParticularFloatValue other)
160    {
161        return new ParticularFloatValue(other.value / this.value);
162    }
163
164    public FloatValue remainder(ParticularFloatValue other)
165    {
166        return new ParticularFloatValue(this.value % other.value);
167    }
168
169    public FloatValue remainderOf(ParticularFloatValue other)
170    {
171        return new ParticularFloatValue(other.value % this.value);
172    }
173
174    public IntegerValue compare(ParticularFloatValue other)
175    {
176        return this.value <  other.value ? SpecificValueFactory.INTEGER_VALUE_M1 :
177               this.value == other.value ? SpecificValueFactory.INTEGER_VALUE_0  :
178                                           SpecificValueFactory.INTEGER_VALUE_1;
179    }
180
181
182    // Implementations for Value.
183
184    public boolean isParticular()
185    {
186        return true;
187    }
188
189
190    // Implementations for Object.
191
192    public boolean equals(Object object)
193    {
194        return super.equals(object) &&
195               this.value == ((ParticularFloatValue)object).value;
196    }
197
198
199    public int hashCode()
200    {
201        return super.hashCode() ^
202               Float.floatToIntBits(value);
203    }
204
205
206    public String toString()
207    {
208        return value+"f";
209    }
210}