KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > service > namespace > 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.service.namespace;
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.alfresco.service.ServiceRegistry;
25 import org.alfresco.util.ApplicationContextHelper;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 /**
30  * A Map that holds as it's key a QName stored in it's internal String representation.
31  * Calls to get and put automatically map the key to and from the QName representation.
32  *
33  * @author gavinc
34  */

35 public class QNameMap<K,V> implements Map JavaDoc, Cloneable JavaDoc
36 {
37     protected static Log logger = LogFactory.getLog(QNameMap.class);
38     protected Map JavaDoc<String JavaDoc, Object JavaDoc> contents = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>(11, 1.0f);
39     protected NamespacePrefixResolver resolver = null;
40     
41     /**
42      * Constructor
43      *
44      * @param resolver Mandatory NamespacePrefixResolver helper
45      */

46     public QNameMap(NamespacePrefixResolver resolver)
47     {
48         if (resolver == null)
49         {
50             throw new IllegalArgumentException JavaDoc("NamespacePrefixResolver is mandatory.");
51         }
52         this.resolver = resolver;
53     }
54     
55     /**
56      * @see java.util.Map#size()
57      */

58     public final int size()
59     {
60         return this.contents.size();
61     }
62     
63     /**
64      * @see java.util.Map#isEmpty()
65      */

66     public boolean isEmpty()
67     {
68         return this.contents.isEmpty();
69     }
70     
71     /**
72      * @see java.util.Map#containsKey(java.lang.Object)
73      */

74     public boolean containsKey(Object JavaDoc key)
75     {
76         return (this.contents.containsKey(QName.resolveToQNameString(resolver, key.toString())));
77     }
78     
79     /**
80      * @see java.util.Map#containsValue(java.lang.Object)
81      */

82     public boolean containsValue(Object JavaDoc value)
83     {
84         return this.contents.containsValue(value);
85     }
86     
87     /**
88      * @see java.util.Map#get(java.lang.Object)
89      */

90     public Object JavaDoc get(Object JavaDoc key)
91     {
92         String JavaDoc qnameKey = QName.resolveToQNameString(resolver, key.toString());
93         Object JavaDoc obj = this.contents.get(qnameKey);
94         
95         return obj;
96     }
97     
98     /**
99      * @see java.util.Map#put(K, V)
100      */

101     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
102     {
103         return this.contents.put(QName.resolveToQNameString(resolver, key.toString()), value);
104     }
105     
106     /**
107      * @see java.util.Map#remove(java.lang.Object)
108      */

109     public Object JavaDoc remove(Object JavaDoc key)
110     {
111         return this.contents.remove(QName.resolveToQNameString(resolver, key.toString()));
112     }
113     
114     /**
115      * @see java.util.Map#putAll(java.util.Map)
116      */

117     public void putAll(Map JavaDoc t)
118     {
119         for (Object JavaDoc key : t.keySet())
120         {
121             this.put(key, t.get(key));
122         }
123     }
124     
125     /**
126      * @see java.util.Map#clear()
127      */

128     public void clear()
129     {
130         this.contents.clear();
131     }
132     
133     /**
134      * @see java.util.Map#keySet()
135      */

136     public Set JavaDoc keySet()
137     {
138         return this.contents.keySet();
139     }
140     
141     /**
142      * @see java.util.Map#values()
143      */

144     public Collection JavaDoc values()
145     {
146         return this.contents.values();
147     }
148     
149     /**
150      * @see java.util.Map#entrySet()
151      */

152     public Set JavaDoc entrySet()
153     {
154         return this.contents.entrySet();
155     }
156     
157     /**
158      * Override Object.toString() to provide useful debug output
159      */

160     public String JavaDoc toString()
161     {
162         return this.contents.toString();
163     }
164     
165     /**
166      * Shallow copy the map by copying keys and values into a new QNameMap
167      */

168     public Object JavaDoc clone()
169     {
170         QNameMap map = new QNameMap(resolver);
171         map.putAll(this);
172         
173         return map;
174     }
175 }
176
Popular Tags