SpecificValueFactory.java revision 9f606f95f03a75961498803e24bee6799a7c0885
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 class provides methods to create and reuse IntegerValue objects.
25 *
26 * @author Eric Lafortune
27 */
28public class SpecificValueFactory
29extends      ValueFactory
30{
31    // Shared copies of Value objects, to avoid creating a lot of objects.
32    static final IntegerValue INTEGER_VALUE_M1 = new ParticularIntegerValue(-1);
33    static final IntegerValue INTEGER_VALUE_0  = new ParticularIntegerValue(0);
34    static final IntegerValue INTEGER_VALUE_1  = new ParticularIntegerValue(1);
35    static final IntegerValue INTEGER_VALUE_2  = new ParticularIntegerValue(2);
36    static final IntegerValue INTEGER_VALUE_3  = new ParticularIntegerValue(3);
37    static final IntegerValue INTEGER_VALUE_4  = new ParticularIntegerValue(4);
38    static final IntegerValue INTEGER_VALUE_5  = new ParticularIntegerValue(5);
39    static final LongValue    LONG_VALUE_0     = new ParticularLongValue(0);
40    static final LongValue    LONG_VALUE_1     = new ParticularLongValue(1);
41    static final FloatValue   FLOAT_VALUE_0    = new ParticularFloatValue(0.0f);
42    static final FloatValue   FLOAT_VALUE_1    = new ParticularFloatValue(1.0f);
43    static final FloatValue   FLOAT_VALUE_2    = new ParticularFloatValue(2.0f);
44    static final DoubleValue  DOUBLE_VALUE_0   = new ParticularDoubleValue(0.0);
45    static final DoubleValue  DOUBLE_VALUE_1   = new ParticularDoubleValue(1.0);
46
47
48    // Implementations for ValueFactory.
49
50    public IntegerValue createIntegerValue(int value)
51    {
52        switch (value)
53        {
54            case -1: return INTEGER_VALUE_M1;
55            case  0: return INTEGER_VALUE_0;
56            case  1: return INTEGER_VALUE_1;
57            case  2: return INTEGER_VALUE_2;
58            case  3: return INTEGER_VALUE_3;
59            case  4: return INTEGER_VALUE_4;
60            case  5: return INTEGER_VALUE_5;
61            default: return new ParticularIntegerValue(value);
62        }
63    }
64
65
66    public LongValue createLongValue(long value)
67    {
68        return value == 0 ? LONG_VALUE_0 :
69               value == 1 ? LONG_VALUE_1 :
70                            new ParticularLongValue(value);
71    }
72
73
74    public FloatValue createFloatValue(float value)
75    {
76        return value == 0.0f ? FLOAT_VALUE_0 :
77               value == 1.0f ? FLOAT_VALUE_1 :
78               value == 2.0f ? FLOAT_VALUE_2 :
79                               new ParticularFloatValue(value);
80    }
81
82
83    public DoubleValue createDoubleValue(double value)
84    {
85        return value == 0.0 ? DOUBLE_VALUE_0 :
86               value == 1.0 ? DOUBLE_VALUE_1 :
87                              new ParticularDoubleValue(value);
88    }
89}
90