KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > parser > MethodScope


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.javacore.parser;
21
22 import java.util.*;
23
24 /**
25  *
26  * @author Tomas Hurka
27  */

28 public class MethodScope implements ScopeMember {
29
30     private MethodScope parentScope;
31     private Map positiveCache;
32     private Set negativeCache;
33     private ArrayList members;
34
35     /** Creates a new instance of Scope */
36     MethodScope(MethodScope parent) {
37         parentScope=parent;
38         positiveCache=new HashMap();
39         negativeCache=new HashSet();
40         members=new ArrayList();
41     }
42
43     public Object JavaDoc lookup(final Object JavaDoc key) {
44         Object JavaDoc val=positiveCache.get(key);
45         Object JavaDoc valParent=null;
46         
47         if (val!=null)
48             return val;
49         if (negativeCache.contains(key))
50             return null;
51         val=lookupMembers(key);
52         if (parentScope!=null)
53             valParent=parentScope.lookup(key);
54         val=combine(val,valParent);
55         if (val!=null)
56             positiveCache.put(key,val);
57         else
58             negativeCache.add(key);
59         return val;
60     }
61     
62     void addMember(final ScopeMember member) {
63         positiveCache.clear();
64         negativeCache.clear();
65         members.add(member);
66     }
67     
68     public boolean equals(Object JavaDoc obj) {
69         if (obj!=null && obj instanceof MethodScope) {
70             MethodScope sc=(MethodScope)obj;
71             if (!members.equals(sc.members))
72                 return false;
73             if (parentScope==null)
74                 return parentScope==sc.parentScope;
75             return parentScope.equals(sc.parentScope);
76         }
77         return false;
78     }
79     
80     protected Object JavaDoc clone() {
81         MethodScope cloned=new MethodScope(parentScope);
82         
83         cloned.members=(ArrayList)members.clone();
84         return cloned;
85     }
86     
87     private Object JavaDoc combine(Object JavaDoc val,Object JavaDoc val1) {
88         List valList;
89         
90         if (val==null)
91             return val1;
92         if (val1==null)
93             return val;
94         if (!(val instanceof List)) {
95             valList=new ArrayList();
96             valList.add(val);
97         } else {
98             valList=(List)val;
99         }
100         if (!(val1 instanceof List)) {
101             valList.add(val1);
102         } else {
103             valList.addAll((Collection)val1);
104         }
105         return valList;
106     }
107     
108     private Object JavaDoc lookupMembers(final Object JavaDoc key) {
109         Iterator it=members.iterator();
110         Object JavaDoc value=null;
111         
112         while(it.hasNext()) {
113             ScopeMember m=(ScopeMember)it.next();
114             Object JavaDoc val=m.lookup(key);
115             
116             if (val==null)
117                 continue;
118             if (value==null) {
119                 value=val;
120                 continue;
121             }
122             value=combine(value,val);
123         }
124         return value;
125     }
126 }
Popular Tags