1 19 20 package org.netbeans.modules.javacore.parser; 21 22 import java.util.*; 23 24 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 36 MethodScope(MethodScope parent) { 37 parentScope=parent; 38 positiveCache=new HashMap(); 39 negativeCache=new HashSet(); 40 members=new ArrayList(); 41 } 42 43 public Object lookup(final Object key) { 44 Object val=positiveCache.get(key); 45 Object 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 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 clone() { 81 MethodScope cloned=new MethodScope(parentScope); 82 83 cloned.members=(ArrayList)members.clone(); 84 return cloned; 85 } 86 87 private Object combine(Object val,Object 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 lookupMembers(final Object key) { 109 Iterator it=members.iterator(); 110 Object value=null; 111 112 while(it.hasNext()) { 113 ScopeMember m=(ScopeMember)it.next(); 114 Object 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 |