KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > AnalysisLocal


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2004-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 package edu.umd.cs.findbugs;
20 import java.util.Map JavaDoc;
21
22 import edu.umd.cs.findbugs.ba.AnalysisContext;
23
24 public class AnalysisLocal<T> {
25     protected T initialValue() {
26         return null;
27     }
28     
29     protected Map JavaDoc <AnalysisLocal<T>, T> getMap() {
30         Map JavaDoc<?,?> m = AnalysisContext.currentAnalysisContext().getAnalysisLocals();
31         return (Map JavaDoc<AnalysisLocal<T>, T>) m;
32     }
33     
34     public T get() {
35         Map JavaDoc<AnalysisLocal<T>, T> m = getMap();
36
37         if (m.containsKey(this)) {
38             return m.get(this);
39         }
40         
41         synchronized(m) {
42             if (m.containsKey(this)) {
43                 return m.get(this);
44             }
45             T result = initialValue();
46             m.put(this,result);
47             return result;
48         }
49     }
50     public void set(T value) {
51         Map JavaDoc<AnalysisLocal<T>, T> m = getMap();
52         m.put(this, value);
53     }
54     public void remove() {
55         Map JavaDoc<AnalysisLocal<T>, T> m = getMap();
56         m.remove(this);
57     }
58 }
59
Popular Tags