KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > DoubleConstant


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997-1999 Raja Vallee-Rai
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27
28
29
30 package soot.jimple;
31
32 import soot.*;
33 import soot.util.*;
34 import java.util.*;
35
36 public class DoubleConstant extends RealConstant
37 {
38     public final double value;
39
40     private DoubleConstant(double value)
41     {
42         this.value = value;
43     }
44
45     public static DoubleConstant v(double value)
46     {
47         return new DoubleConstant(value);
48     }
49
50     public boolean equals(Object JavaDoc c)
51     {
52         return (c instanceof DoubleConstant && ((DoubleConstant) c).value == this.value);
53     }
54
55     /** Returns a hash code for this DoubleConstant object. */
56     public int hashCode()
57     {
58         long v = Double.doubleToLongBits(value);
59         return (int)(v^(v>>>32));
60     }
61
62     // PTC 1999/06/28
63
public NumericConstant add(NumericConstant c)
64     {
65         if (!(c instanceof DoubleConstant))
66             throw new IllegalArgumentException JavaDoc("DoubleConstant expected");
67         return DoubleConstant.v(this.value + ((DoubleConstant)c).value);
68     }
69
70     public NumericConstant subtract(NumericConstant c)
71     {
72         if (!(c instanceof DoubleConstant))
73             throw new IllegalArgumentException JavaDoc("DoubleConstant expected");
74         return DoubleConstant.v(this.value - ((DoubleConstant)c).value);
75     }
76
77     public NumericConstant multiply(NumericConstant c)
78     {
79         if (!(c instanceof DoubleConstant))
80             throw new IllegalArgumentException JavaDoc("DoubleConstant expected");
81         return DoubleConstant.v(this.value * ((DoubleConstant)c).value);
82     }
83
84     public NumericConstant divide(NumericConstant c)
85     {
86         if (!(c instanceof DoubleConstant))
87             throw new IllegalArgumentException JavaDoc("DoubleConstant expected");
88         return DoubleConstant.v(this.value / ((DoubleConstant)c).value);
89     }
90
91     public NumericConstant remainder(NumericConstant c)
92     {
93         if (!(c instanceof DoubleConstant))
94             throw new IllegalArgumentException JavaDoc("DoubleConstant expected");
95         return DoubleConstant.v(this.value % ((DoubleConstant)c).value);
96     }
97
98     public NumericConstant equalEqual(NumericConstant c)
99     {
100         if (!(c instanceof DoubleConstant))
101             throw new IllegalArgumentException JavaDoc("DoubleConstant expected");
102         return IntConstant.v((this.value == ((DoubleConstant)c).value) ? 1 : 0);
103     }
104
105     public NumericConstant notEqual(NumericConstant c)
106     {
107         if (!(c instanceof DoubleConstant))
108             throw new IllegalArgumentException JavaDoc("DoubleConstant expected");
109         return IntConstant.v((this.value != ((DoubleConstant)c).value) ? 1 : 0);
110     }
111
112     public NumericConstant lessThan(NumericConstant c)
113     {
114         if (!(c instanceof DoubleConstant))
115             throw new IllegalArgumentException JavaDoc("DoubleConstant expected");
116         return IntConstant.v((this.value < ((DoubleConstant)c).value) ? 1 : 0);
117     }
118
119     public NumericConstant lessThanOrEqual(NumericConstant c)
120     {
121         if (!(c instanceof DoubleConstant))
122             throw new IllegalArgumentException JavaDoc("DoubleConstant expected");
123         return IntConstant.v((this.value <= ((DoubleConstant)c).value) ? 1 : 0);
124     }
125
126     public NumericConstant greaterThan(NumericConstant c)
127     {
128         if (!(c instanceof DoubleConstant))
129             throw new IllegalArgumentException JavaDoc("DoubleConstant expected");
130         return IntConstant.v((this.value > ((DoubleConstant)c).value) ? 1 : 0);
131     }
132
133     public NumericConstant greaterThanOrEqual(NumericConstant c)
134     {
135         if (!(c instanceof DoubleConstant))
136             throw new IllegalArgumentException JavaDoc("DoubleConstant expected");
137         return IntConstant.v((this.value >= ((DoubleConstant)c).value) ? 1 : 0);
138     }
139
140     public IntConstant cmpg(RealConstant c) {
141         if (!(c instanceof DoubleConstant))
142             throw new IllegalArgumentException JavaDoc("DoubleConstant expected");
143         double cValue = ((DoubleConstant) c).value;
144         if (this.value < cValue)
145             return IntConstant.v(-1);
146         else if (this.value == cValue)
147             return IntConstant.v(0);
148         else /* this or c could be NaN */
149             return IntConstant.v(1);
150     }
151     
152     public IntConstant cmpl(RealConstant c) {
153         if (!(c instanceof DoubleConstant))
154             throw new IllegalArgumentException JavaDoc("DoubleConstant expected");
155         double cValue = ((DoubleConstant) c).value;
156         if (this.value > cValue)
157             return IntConstant.v(1);
158         else if (this.value == cValue)
159             return IntConstant.v(0);
160         else /* this or c could be NaN */
161             return IntConstant.v(-1);
162     }
163     
164     public NumericConstant negate()
165     {
166         return DoubleConstant.v(-(this.value));
167     }
168
169     public String JavaDoc toString()
170     {
171         String JavaDoc doubleString = new Double JavaDoc(value).toString();
172         
173         if(doubleString.equals("NaN") ||
174             doubleString.equals("Infinity") ||
175             doubleString.equals("-Infinity"))
176             return "#" + doubleString;
177         else
178             return doubleString;
179     }
180     
181     public Type getType()
182     {
183         return DoubleType.v();
184     }
185
186     public void apply(Switch sw)
187     {
188         ((ConstantSwitch) sw).caseDoubleConstant(this);
189     }
190 }
191
Popular Tags