KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > constant > Constant


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

19 package edu.umd.cs.findbugs.ba.constant;
20
21 import edu.umd.cs.findbugs.annotations.Nullable;
22
23 /**
24  * Abstract dataflow value representing a value which
25  * may or may not be a constant.
26  *
27  * @see edu.umd.cs.findbugs.ba.constant.ConstantAnalysis
28  * @author David Hovemeyer
29  */

30 public class Constant {
31     private final Object JavaDoc value;
32     
33     /**
34      * Single instance representing all non-constant values.
35      */

36     public static final Constant NOT_CONSTANT = new Constant(null);
37     
38     /**
39      * Constructor for a constant value.
40      *
41      * @param value the constant value; must be a String, Integer, etc.
42      */

43     public Constant(@Nullable Object JavaDoc value) {
44         this.value = value;
45     }
46     
47     /**
48      * Return whether or not this value is a constant.
49      *
50      * @return true if the value is a constant, false if not
51      */

52     public boolean isConstant() {
53         return value != null;
54     }
55     
56     /**
57      * Return whether or not this value is a constant String.
58      *
59      * @return true if the value is a constant String, false if not
60      */

61     public boolean isConstantString() {
62         return isConstant() && (value instanceof String JavaDoc);
63     }
64     
65     /**
66      * Get the constant String value of this value.
67      *
68      * @return the constant String value
69      */

70     public String JavaDoc getConstantString() {
71         return (String JavaDoc) value;
72     }
73
74     /**
75      * Return whether or not this value is a constant int/Integer.
76      *
77      * @return true if the value is a constant int/Integer, false if not
78      */

79     public boolean isConstantInteger() {
80         return isConstant() && (value instanceof Integer JavaDoc);
81     }
82     
83     
84     /**
85      * Get the constant int value of this value.
86      *
87      * @return the constant int value
88      */

89     public int getConstantInt() {
90         return ((Integer JavaDoc) value).intValue();
91     }
92
93     
94     /**
95      * Merge two Constants.
96      *
97      * @param a a Constant
98      * @param b another Constant
99      * @return the merge (dataflow meet) of the two Constants
100      */

101     public static Constant merge(Constant a, Constant b) {
102         if (!a.isConstant() || !b.isConstant())
103             return NOT_CONSTANT;
104         if (a.value.getClass() != b.value.getClass() || !a.value.equals(b.value))
105             return NOT_CONSTANT;
106         return a;
107     }
108     
109     //@Override
110
@Override JavaDoc
111          public boolean equals(Object JavaDoc obj) {
112         if (obj == null || obj.getClass() != this.getClass())
113             return false;
114         Constant other = (Constant) obj;
115         if (other.value == this.value)
116             return true;
117         else if (other.value == null || this.value == null)
118             return false;
119         else
120             return this.value.equals(other.value);
121     }
122     
123     //@Override
124
@Override JavaDoc
125          public int hashCode() {
126         return (value == null) ? 123 : value.hashCode();
127     }
128     
129     @Override JavaDoc
130          public String JavaDoc toString() {
131         if (!isConstant()) {
132             return "-";
133         } else {
134             return "<" + value.toString() + ">";
135         }
136     }
137 }
138
Popular Tags