1 22 package org.jboss.metadata.spi.scope; 23 24 import java.io.Serializable ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.Iterator ; 28 import java.util.SortedMap ; 29 import java.util.TreeMap ; 30 31 37 public class ScopeKey implements Serializable , Cloneable 38 { 39 40 private static final long serialVersionUID = -496238095349593371L; 41 42 43 public static ScopeKey DEFAULT_SCOPE = new ScopeKey(new Scope(CommonLevels.JVM, "THIS")); 44 45 46 private SortedMap <ScopeLevel, Scope> scopes = Collections.synchronizedSortedMap(new TreeMap <ScopeLevel, Scope>()); 47 48 49 private ScopeLevel maxScopeLevel; 50 51 52 private volatile boolean frozen; 53 54 static 55 { 56 DEFAULT_SCOPE.freeze(); 57 } 58 59 62 public ScopeKey() 63 { 64 } 65 66 71 public ScopeKey(Scope scope) 72 { 73 addScope(scope); 74 } 75 76 82 public ScopeKey(ScopeLevel level, String qualifier) 83 { 84 addScope(level, qualifier); 85 } 86 87 92 public ScopeKey(Collection <Scope> scopes) 93 { 94 if (scopes == null) 95 throw new IllegalArgumentException ("Null scopes"); 96 for (Scope scope : scopes) 97 addScope(scope); 98 } 99 100 105 public ScopeKey(Scope[] scopes) 106 { 107 if (scopes == null) 108 throw new IllegalArgumentException ("Null scopes"); 109 for (Scope scope : scopes) 110 addScope(scope); 111 } 112 113 118 public boolean isFrozen() 119 { 120 return frozen; 121 } 122 123 126 public void freeze() 127 { 128 if (scopes.isEmpty()) 129 throw new IllegalStateException ("Attempt to freeze an empty key"); 130 this.frozen = true; 131 } 132 133 138 public Collection <Scope> getScopes() 139 { 140 return Collections.unmodifiableCollection(scopes.values()); 141 } 142 143 148 public ScopeLevel getMaxScopeLevel() 149 { 150 return maxScopeLevel; 151 } 152 153 158 public ScopeKey getParent() 159 { 160 if (scopes.size() < 2) 161 return null; 162 163 ScopeKey result = new ScopeKey(); 164 for (Iterator <Scope> i = scopes.values().iterator(); i.hasNext();) 165 { 166 Scope scope = i.next(); 167 if (i.hasNext()) 168 result.addScope(scope); 169 } 170 return result; 171 } 172 173 public boolean isParent(ScopeKey key) 174 { 175 if (key.scopes.size() < 2) 177 return false; 178 179 if (scopes.size() != key.scopes.size() - 1) 181 return false; 182 183 Iterator <Scope> thisScopes = scopes.values().iterator(); 184 Iterator <Scope> keyScopes = key.scopes.values().iterator(); 185 186 while (thisScopes.hasNext()) 187 { 188 Scope thisScope = thisScopes.next(); 189 Scope keyScope = keyScopes.next(); 190 if (thisScope.equals(keyScope) == false) 191 return false; 192 } 193 194 return true; 195 } 196 197 203 public Scope addScope(Scope scope) 204 { 205 if (scope == null) 206 throw new IllegalArgumentException ("Null scope"); 207 if (frozen) 208 throw new IllegalStateException ("The scope key is frozen"); 209 210 ScopeLevel level = scope.getScopeLevel(); 211 Scope result = scopes.put(level, scope); 212 if (maxScopeLevel == null || level.compareTo(maxScopeLevel) >= 0) 213 maxScopeLevel = level; 214 return result; 215 } 216 217 224 public Scope addScope(ScopeLevel level, String qualifier) 225 { 226 Scope scope = new Scope(level, qualifier); 227 return addScope(scope); 228 } 229 230 236 public Scope removeScope(Scope scope) 237 { 238 if (scope == null) 239 throw new IllegalArgumentException ("Null scope"); 240 241 return removeScopeLevel(scope.getScopeLevel()); 242 } 243 244 250 public Scope getScopeLevel(ScopeLevel scopeLevel) 251 { 252 if (scopeLevel == null) 253 throw new IllegalArgumentException ("Null scope level"); 254 255 return scopes.get(scopeLevel); 256 } 257 258 264 public Scope removeScopeLevel(ScopeLevel scopeLevel) 265 { 266 if (scopeLevel == null) 267 throw new IllegalArgumentException ("Null scope level"); 268 if (frozen) 269 throw new IllegalStateException ("The scope key is frozen"); 270 271 Scope result = scopes.remove(scopeLevel); 272 if (scopeLevel.equals(maxScopeLevel)) 273 { 274 maxScopeLevel = null; 275 for (ScopeLevel level : scopes.keySet()) 276 maxScopeLevel = level; 277 } 278 return result; 279 } 280 281 public String toString() 282 { 283 return scopes.values().toString(); 284 } 285 286 public boolean equals(Object object) 287 { 288 if (object == this) 289 return true; 290 if (object == null || object instanceof ScopeKey == false) 291 return false; 292 293 ScopeKey other = (ScopeKey) object; 294 return scopes.equals(other.scopes); 295 } 296 297 public int hashCode() 298 { 299 return scopes.hashCode(); 300 } 301 302 protected ScopeKey clone() 303 { 304 try 305 { 306 return (ScopeKey) super.clone(); 307 } 308 catch (CloneNotSupportedException e) 309 { 310 throw new Error (e); 311 } 312 } 313 } 314 | Popular Tags |