Home | History | Annotate | Download | only in value
      1 /*
      2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
      3  *             of Java bytecode.
      4  *
      5  * Copyright (c) 2002-2009 Eric Lafortune (eric (at) 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  */
     21 package proguard.evaluation.value;
     22 
     23 /**
     24  * This DoubleValue represents a particular double value.
     25  *
     26  * @author Eric Lafortune
     27  */
     28 final class ParticularDoubleValue extends SpecificDoubleValue
     29 {
     30     private final double value;
     31 
     32 
     33     /**
     34      * Creates a new particular double value.
     35      */
     36     public ParticularDoubleValue(double value)
     37     {
     38         this.value = value;
     39     }
     40 
     41 
     42     // Implementations for DoubleValue.
     43 
     44     public double value()
     45     {
     46         return value;
     47     }
     48 
     49 
     50     // Implementations of unary methods of DoubleValue.
     51 
     52     public DoubleValue negate()
     53     {
     54         return new ParticularDoubleValue(-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 FloatValue convertToFloat()
     68     {
     69         return new ParticularFloatValue((float)value);
     70     }
     71 
     72 
     73     // Implementations of binary methods of DoubleValue.
     74 
     75     public DoubleValue generalize(DoubleValue other)
     76     {
     77         return other.generalize(this);
     78     }
     79 
     80     public DoubleValue add(DoubleValue other)
     81     {
     82         return value == 0.0 ? other : other.add(this);
     83     }
     84 
     85     public DoubleValue subtract(DoubleValue other)
     86     {
     87         return value == 0.0 ? other.negate() : other.subtractFrom(this);
     88     }
     89 
     90     public DoubleValue subtractFrom(DoubleValue other)
     91     {
     92         return value == 0.0 ? other : other.subtract(this);
     93     }
     94 
     95     public DoubleValue multiply(DoubleValue other)
     96     {
     97         return other.multiply(this);
     98     }
     99 
    100     public DoubleValue divide(DoubleValue other)
    101     {
    102         return other.divideOf(this);
    103     }
    104 
    105     public DoubleValue divideOf(DoubleValue other)
    106     {
    107         return other.divide(this);
    108     }
    109 
    110     public DoubleValue remainder(DoubleValue other)
    111     {
    112         return other.remainderOf(this);
    113     }
    114 
    115     public DoubleValue remainderOf(DoubleValue other)
    116     {
    117         return other.remainder(this);
    118     }
    119 
    120     public IntegerValue compare(DoubleValue other)
    121     {
    122         return other.compareReverse(this);
    123     }
    124 
    125 
    126     // Implementations of binary DoubleValue methods with ParticularDoubleValue
    127     // arguments.
    128 
    129     public DoubleValue generalize(ParticularDoubleValue other)
    130     {
    131         return this.value == other.value ? this : ValueFactory.DOUBLE_VALUE;
    132     }
    133 
    134     public DoubleValue add(ParticularDoubleValue other)
    135     {
    136         return new ParticularDoubleValue(this.value + other.value);
    137     }
    138 
    139     public DoubleValue subtract(ParticularDoubleValue other)
    140     {
    141         return new ParticularDoubleValue(this.value - other.value);
    142     }
    143 
    144     public DoubleValue subtractFrom(ParticularDoubleValue other)
    145     {
    146         return new ParticularDoubleValue(other.value - this.value);
    147     }
    148 
    149     public DoubleValue multiply(ParticularDoubleValue other)
    150     {
    151         return new ParticularDoubleValue(this.value * other.value);
    152     }
    153 
    154     public DoubleValue divide(ParticularDoubleValue other)
    155     {
    156         return new ParticularDoubleValue(this.value / other.value);
    157     }
    158 
    159     public DoubleValue divideOf(ParticularDoubleValue other)
    160     {
    161         return new ParticularDoubleValue(other.value / this.value);
    162     }
    163 
    164     public DoubleValue remainder(ParticularDoubleValue other)
    165     {
    166         return new ParticularDoubleValue(this.value % other.value);
    167     }
    168 
    169     public DoubleValue remainderOf(ParticularDoubleValue other)
    170     {
    171         return new ParticularDoubleValue(other.value % this.value);
    172     }
    173 
    174     public IntegerValue compare(ParticularDoubleValue 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 == ((ParticularDoubleValue)object).value;
    196     }
    197 
    198 
    199     public int hashCode()
    200     {
    201         return super.hashCode() ^
    202                (int)Double.doubleToLongBits(value);
    203     }
    204 
    205 
    206     public String toString()
    207     {
    208         return value+"d";
    209     }
    210 }