KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > bean > repository > QNameMap


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.bean.repository;
18
19 import java.util.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 /**
28  * A Map that holds as it's key a QName stored in it's internal String representation.
29  * Calls to get and put automatically map the key to and from the QName representation.
30  *
31  * @author gavinc
32  */

33 public final class QNameMap<K,V> implements Map JavaDoc
34 {
35    private Log logger = LogFactory.getLog(QNameMap.class);
36    private Map JavaDoc<String JavaDoc, Object JavaDoc> contents = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>(7, 1.0f);
37    private Node parent;
38    private Map JavaDoc<String JavaDoc, NodePropertyResolver> resolvers = new HashMap JavaDoc<String JavaDoc, NodePropertyResolver>(7, 1.0f);
39    
40    /**
41     * Constructor
42     *
43     * @param parent Parent Node of the QNameMap
44     */

45    public QNameMap(Node parent)
46    {
47       if (parent == null)
48       {
49          throw new IllegalArgumentException JavaDoc("Parent Node cannot be null!");
50       }
51       this.parent = parent;
52    }
53    
54    /**
55     * Register a property resolver for the named property.
56     *
57     * @param name Name of the property this resolver is for
58     * @param resolver Property resolver to register
59     */

60    public void addPropertyResolver(String JavaDoc name, NodePropertyResolver resolver)
61    {
62       this.resolvers.put(name, resolver);
63    }
64    
65    /**
66     * @see java.util.Map#size()
67     */

68    public int size()
69    {
70       return this.contents.size();
71    }
72
73    /**
74     * @see java.util.Map#isEmpty()
75     */

76    public boolean isEmpty()
77    {
78       return this.contents.isEmpty();
79    }
80
81    /**
82     * @see java.util.Map#containsKey(java.lang.Object)
83     */

84    public boolean containsKey(Object JavaDoc key)
85    {
86       return this.contents.containsKey(Repository.resolveToQNameString((String JavaDoc)key));
87    }
88
89    /**
90     * @see java.util.Map#containsValue(java.lang.Object)
91     */

92    public boolean containsValue(Object JavaDoc value)
93    {
94       return this.contents.containsValue(value);
95    }
96
97    /**
98     * @see java.util.Map#get(java.lang.Object)
99     */

100    public Object JavaDoc get(Object JavaDoc key)
101    {
102       String JavaDoc qnameKey = Repository.resolveToQNameString((String JavaDoc)key);
103       Object JavaDoc obj = this.contents.get(qnameKey);
104       if (obj == null)
105       {
106          // if a property resolver exists for this property name then invoke it
107
NodePropertyResolver resolver = this.resolvers.get((String JavaDoc)key);
108          if (resolver != null)
109          {
110             obj = resolver.get(this.parent);
111             // cache the result
112
// obviously the cache is useless if the result is null, in most cases it shouldn't be
113
this.contents.put(qnameKey, obj);
114          }
115       }
116       
117       return obj;
118    }
119    
120    /**
121     * Perform a get without using property resolvers
122     *
123     * @param key item key
124     * @return object
125     */

126    public Object JavaDoc getRaw(Object JavaDoc key)
127    {
128       return this.contents.get(Repository.resolveToQNameString((String JavaDoc)key));
129    }
130
131    /**
132     * @see java.util.Map#put(K, V)
133     */

134    public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
135    {
136       return this.contents.put(Repository.resolveToQNameString((String JavaDoc)key), value);
137    }
138
139    /**
140     * @see java.util.Map#remove(java.lang.Object)
141     */

142    public Object JavaDoc remove(Object JavaDoc key)
143    {
144       return this.contents.remove(Repository.resolveToQNameString((String JavaDoc)key));
145    }
146
147    /**
148     * @see java.util.Map#putAll(java.util.Map)
149     */

150    public void putAll(Map JavaDoc t)
151    {
152       for (Object JavaDoc key : t.keySet())
153       {
154          this.put(key, t.get(key));
155       }
156    }
157
158    /**
159     * @see java.util.Map#clear()
160     */

161    public void clear()
162    {
163       this.contents.clear();
164    }
165
166    /**
167     * @see java.util.Map#keySet()
168     */

169    public Set JavaDoc keySet()
170    {
171       return this.contents.keySet();
172    }
173
174    /**
175     * @see java.util.Map#values()
176     */

177    public Collection JavaDoc values()
178    {
179       return this.contents.values();
180    }
181
182    /**
183     * @see java.util.Map#entrySet()
184     */

185    public Set JavaDoc entrySet()
186    {
187       return this.contents.entrySet();
188    }
189    
190    /**
191     * Override Object.toString() to provide useful debug output
192     */

193    public String JavaDoc toString()
194    {
195       return this.contents.toString();
196    }
197 }
198
Popular Tags