KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > metadata > spi > scope > ScopeKey


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.metadata.spi.scope;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.SortedMap JavaDoc;
29 import java.util.TreeMap JavaDoc;
30
31 /**
32  * ScopeKey.
33  *
34  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
35  * @version $Revision: 57321 $
36  */

37 public class ScopeKey implements Serializable JavaDoc, Cloneable JavaDoc
38 {
39    /** The serialVersionUID */
40    private static final long serialVersionUID = -496238095349593371L;
41
42    /** The default scope */
43    public static ScopeKey DEFAULT_SCOPE = new ScopeKey(new Scope(CommonLevels.JVM, "THIS"));
44    
45    /** The scopes */
46    private SortedMap JavaDoc<ScopeLevel, Scope> scopes = Collections.synchronizedSortedMap(new TreeMap JavaDoc<ScopeLevel, Scope>());
47    
48    /** The scope level for this key */
49    private ScopeLevel maxScopeLevel;
50    
51    /** Whether the key is frozen */
52    private volatile boolean frozen;
53
54    static
55    {
56       DEFAULT_SCOPE.freeze();
57    }
58    
59    /**
60     * Create a new ScopeKey.
61     */

62    public ScopeKey()
63    {
64    }
65
66    /**
67     * Create a new ScopeKey.
68     *
69     * @param scope the scope
70     */

71    public ScopeKey(Scope scope)
72    {
73       addScope(scope);
74    }
75
76    /**
77     * Create a new ScopeKey.
78     *
79     * @param level the scope level
80     * @param qualifier the scope qualifier
81     */

82    public ScopeKey(ScopeLevel level, String JavaDoc qualifier)
83    {
84       addScope(level, qualifier);
85    }
86
87    /**
88     * Create a new ScopeKey.
89     *
90     * @param scopes the scopes
91     */

92    public ScopeKey(Collection JavaDoc<Scope> scopes)
93    {
94       if (scopes == null)
95          throw new IllegalArgumentException JavaDoc("Null scopes");
96       for (Scope scope : scopes)
97          addScope(scope);
98    }
99
100    /**
101     * Create a new ScopeKey.
102     *
103     * @param scopes the scopes
104     */

105    public ScopeKey(Scope[] scopes)
106    {
107       if (scopes == null)
108          throw new IllegalArgumentException JavaDoc("Null scopes");
109       for (Scope scope : scopes)
110          addScope(scope);
111    }
112    
113    /**
114     * Get the frozen.
115     *
116     * @return the frozen.
117     */

118    public boolean isFrozen()
119    {
120       return frozen;
121    }
122
123    /**
124     * Set to frozen.
125     */

126    public void freeze()
127    {
128       if (scopes.isEmpty())
129          throw new IllegalStateException JavaDoc("Attempt to freeze an empty key");
130       this.frozen = true;
131    }
132    
133    /**
134     * Get the scopes
135     *
136     * @return the scopes
137     */

138    public Collection JavaDoc<Scope> getScopes()
139    {
140       return Collections.unmodifiableCollection(scopes.values());
141    }
142    
143    /**
144     * Get the maximum scope level
145     *
146     * @return the largest scope level
147     */

148    public ScopeLevel getMaxScopeLevel()
149    {
150       return maxScopeLevel;
151    }
152    
153    /**
154     * Get the parent scope key
155     *
156     * @return the parent or null if there is no parent
157     */

158    public ScopeKey getParent()
159    {
160       if (scopes.size() < 2)
161          return null;
162       
163       ScopeKey result = new ScopeKey();
164       for (Iterator JavaDoc<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       // The passed key doesn't have a parent
176
if (key.scopes.size() < 2)
177          return false;
178       
179       // If it is a child, it will have one more scope
180
if (scopes.size() != key.scopes.size() - 1)
181          return false;
182
183       Iterator JavaDoc<Scope> thisScopes = scopes.values().iterator();
184       Iterator JavaDoc<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    /**
198     * Add a scope
199     *
200     * @param scope the scope
201     * @return the previous value or null if there wasn't one
202     */

203    public Scope addScope(Scope scope)
204    {
205       if (scope == null)
206          throw new IllegalArgumentException JavaDoc("Null scope");
207       if (frozen)
208          throw new IllegalStateException JavaDoc("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    /**
218     * Add a scope
219     *
220     * @param level the scope level
221     * @param qualifier the scope qualifier
222     * @return the previous value or null if there wasn't one
223     */

224    public Scope addScope(ScopeLevel level, String JavaDoc qualifier)
225    {
226       Scope scope = new Scope(level, qualifier);
227       return addScope(scope);
228    }
229    
230    /**
231     * Remove a scope
232     *
233     * @param scope the scope
234     * @return the previous value or null if there wasn't one
235     */

236    public Scope removeScope(Scope scope)
237    {
238       if (scope == null)
239          throw new IllegalArgumentException JavaDoc("Null scope");
240
241       return removeScopeLevel(scope.getScopeLevel());
242    }
243    
244    /**
245     * Get a scope level
246     *
247     * @param scopeLevel the scope level
248     * @return the scope or null if there is no such level
249     */

250    public Scope getScopeLevel(ScopeLevel scopeLevel)
251    {
252       if (scopeLevel == null)
253          throw new IllegalArgumentException JavaDoc("Null scope level");
254
255       return scopes.get(scopeLevel);
256    }
257    
258    /**
259     * Remove a scope level
260     *
261     * @param scopeLevel the scope level
262     * @return the scope or null if there is no such level
263     */

264    public Scope removeScopeLevel(ScopeLevel scopeLevel)
265    {
266       if (scopeLevel == null)
267          throw new IllegalArgumentException JavaDoc("Null scope level");
268       if (frozen)
269          throw new IllegalStateException JavaDoc("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 JavaDoc toString()
282    {
283       return scopes.values().toString();
284    }
285    
286    public boolean equals(Object JavaDoc 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 JavaDoc e)
309       {
310          throw new Error JavaDoc(e);
311       }
312    }
313 }
314
Popular Tags