KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > evaluation > MutableValue


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;
22
23 import proguard.evaluation.value.*;
24
25 /**
26  * This Value is a mutable wrapper for other Value instances.
27  * Its generalization method affects the contained Value as a side-effect.
28  */

29 class MutableValue extends Category1Value
30 {
31     private Value containedValue;
32
33
34     /**
35      * Generalizes the contained value with the given value.
36      * @param otherContainedValue the other value.
37      */

38     public void generalizeContainedValue(Value otherContainedValue)
39     {
40         MutableValue lastMutableValue = lastMutableValue();
41         Value lastContainedValue= lastMutableValue.containedValue;
42
43         lastMutableValue.containedValue =
44             lastContainedValue == null ? otherContainedValue :
45                                          otherContainedValue.generalize(lastContainedValue);
46     }
47
48
49     /**
50      * Sets the contained value.
51      */

52     public void setContainedValue(Value containedValue)
53     {
54         lastMutableValue().containedValue = containedValue;
55     }
56
57
58     /**
59      * Returns the contained value.
60      */

61     public Value getContainedValue()
62     {
63         return lastMutableValue().containedValue;
64     }
65
66
67     // Implementations for Value.
68

69     public Value generalize(Value other)
70     {
71         MutableValue otherMutableValue = (MutableValue)other;
72
73         MutableValue thisLastMutableValue = this.lastMutableValue();
74         MutableValue otherLastMutableValue = otherMutableValue.lastMutableValue();
75
76         Value thisLastContainedValue = thisLastMutableValue.containedValue;
77         Value otherLastContainedValue = otherLastMutableValue.containedValue;
78
79         if (thisLastMutableValue != otherLastMutableValue)
80         {
81             otherLastMutableValue.containedValue = thisLastMutableValue;
82         }
83
84         thisLastMutableValue.containedValue =
85             thisLastContainedValue == null ? otherLastContainedValue :
86             otherLastContainedValue == null ? thisLastContainedValue :
87                                               thisLastContainedValue.generalize(otherLastContainedValue);
88         return thisLastMutableValue;
89     }
90
91
92     public int computationalType()
93     {
94         return 0;
95     }
96
97     public final String JavaDoc internalType()
98     {
99         return null;
100     }
101
102
103     // Implementations for Object.
104

105     public boolean equals(Object JavaDoc object)
106     {
107         if (object == null ||
108             this.getClass() != object.getClass())
109         {
110             return false;
111         }
112
113         MutableValue other = (MutableValue)object;
114         Value thisContainedValue = this.getContainedValue();
115         Value otherContainedValue = other.getContainedValue();
116         return thisContainedValue == null ?
117             otherContainedValue == null :
118             thisContainedValue.equals(otherContainedValue);
119     }
120
121
122     public int hashCode()
123     {
124         Value containedValue = getContainedValue();
125         return this.getClass().hashCode() ^
126                (containedValue == null ? 0 : containedValue.hashCode());
127     }
128
129
130     public String JavaDoc toString()
131     {
132         return containedValue == null ? "none" : containedValue.toString();
133     }
134
135
136     // Small utility methods.
137

138     public MutableValue lastMutableValue()
139     {
140         MutableValue mutableValue = this;
141
142         while (mutableValue.containedValue instanceof MutableValue)
143         {
144             mutableValue = (MutableValue)mutableValue.containedValue;
145         }
146
147         return mutableValue;
148     }
149 }
150
Popular Tags