KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > evaluation > value > SpecificValueFactory


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 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  */

21 package proguard.evaluation.value;
22
23 /**
24  * This class provides methods to create and reuse IntegerValue objects.
25  *
26  * @author Eric Lafortune
27  */

28 public class SpecificValueFactory
29 extends ValueFactory
30 {
31     // Shared copies of Value objects, to avoid creating a lot of objects.
32
static final IntegerValue INTEGER_VALUE_M1 = new SpecificIntegerValue(-1);
33     static final IntegerValue INTEGER_VALUE_0 = new SpecificIntegerValue(0);
34     static final IntegerValue INTEGER_VALUE_1 = new SpecificIntegerValue(1);
35     static final IntegerValue INTEGER_VALUE_2 = new SpecificIntegerValue(2);
36     static final IntegerValue INTEGER_VALUE_3 = new SpecificIntegerValue(3);
37     static final IntegerValue INTEGER_VALUE_4 = new SpecificIntegerValue(4);
38     static final IntegerValue INTEGER_VALUE_5 = new SpecificIntegerValue(5);
39     static final LongValue LONG_VALUE_0 = new SpecificLongValue(0);
40     static final LongValue LONG_VALUE_1 = new SpecificLongValue(1);
41     static final FloatValue FLOAT_VALUE_0 = new SpecificFloatValue(0.0f);
42     static final FloatValue FLOAT_VALUE_1 = new SpecificFloatValue(1.0f);
43     static final FloatValue FLOAT_VALUE_2 = new SpecificFloatValue(2.0f);
44     static final DoubleValue DOUBLE_VALUE_0 = new SpecificDoubleValue(0.0);
45     static final DoubleValue DOUBLE_VALUE_1 = new SpecificDoubleValue(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 SpecificIntegerValue(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 SpecificLongValue(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 SpecificFloatValue(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 SpecificDoubleValue(value);
88     }
89 }
90
Popular Tags