KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > npe2 > NullnessValue


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, 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
20 package edu.umd.cs.findbugs.ba.npe2;
21
22 /**
23  * Symbolic values representing the nullness of a runtime value.
24  *
25  * @author David Hovemeyer
26  */

27 public class NullnessValue {
28     static final int DEFINITELY_NULL = 0;
29     static final int DEFINITELY_NOT_NULL = 1;
30     static final int CHECKED = 2;
31     static final int NO_KABOOM = 3;
32     
33     static final int FLAGS_MAX = 4;
34     
35     private static final NullnessValue[] instanceList = new NullnessValue[1 << FLAGS_MAX];
36     static {
37         for (int i = 0; i < instanceList.length; i++) {
38             instanceList[i] = new NullnessValue(i);
39         }
40     }
41     
42     private final int flags;
43     
44     private NullnessValue(int flags) {
45         this.flags= flags;
46     }
47     
48     int getFlags() {
49         return flags;
50     }
51
52     public boolean isDefinitelyNull() {
53         return isFlagSet(DEFINITELY_NULL);
54     }
55     
56     public boolean isDefinitelyNotNull() {
57         return isFlagSet(DEFINITELY_NOT_NULL);
58     }
59     
60     public boolean isChecked() {
61         return isFlagSet(CHECKED);
62     }
63     
64     public boolean isNoKaboom() {
65         return isFlagSet(NO_KABOOM);
66     }
67     
68     public NullnessValue toCheckedValue() {
69         return instanceList[flags | (1 << CHECKED)];
70     }
71     
72     public NullnessValue toNoKaboomValue() {
73         return instanceList[flags | (1 << NO_KABOOM)];
74     }
75     
76 // public NullnessValue toCheckedNullValue() {
77
// if (isDefinitelyNull() || isDefinitelyNotNull()) {
78
// throw new IllegalStateException();
79
// }
80
//
81
// return fromFlags(flags | DEFINITELY_NULL | CHECKED);
82
// }
83
//
84
// public NullnessValue toCheckedNotNullValue() {
85
// if (isDefinitelyNull() || isDefinitelyNotNull()) {
86
// throw new IllegalStateException();
87
// }
88
//
89
// return fromFlags(flags | DEFINITELY_NOT_NULL | CHECKED);
90
// }
91

92     private boolean isFlagSet(int flag) {
93         return (flags & (1 << flag)) != 0;
94     }
95     
96     static NullnessValue fromFlags(int flags) {
97         return instanceList[flags];
98     }
99     
100     public static NullnessValue definitelyNullValue() {
101         return fromFlags(1 << DEFINITELY_NULL);
102     }
103     
104     public static NullnessValue definitelyNotNullValue() {
105         return fromFlags(1 << DEFINITELY_NOT_NULL);
106     }
107     
108     public static NullnessValue unknownValue() {
109         return fromFlags(0);
110     }
111     
112     /* (non-Javadoc)
113      * @see java.lang.Object#toString()
114      */

115     @Override JavaDoc
116     public String JavaDoc toString() {
117         String JavaDoc pfx = "";
118         
119         if (isChecked()) {
120             pfx += "c";
121         }
122         
123         if (isNoKaboom()) {
124             pfx += "k";
125         }
126
127         String JavaDoc val;
128         
129         if (isDefinitelyNull()) {
130             val = "n";
131         } else if (isDefinitelyNotNull()) {
132             val = "N";
133         } else {
134             val = "-";
135         }
136         
137         return pfx + val;
138     }
139 }
140
Popular Tags