1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
19 package org.apache.catalina.util;
20
21
22 import java.util.HashMap;
23 import java.util.Map;
24
25
26 /**
27 * Extended implementation of <strong>HashMap</strong> that includes a
28 * <code>locked</code> property. This class can be used to safely expose
29 * Catalina internal parameter map objects to user classes without having
30 * to clone them in order to avoid modifications. When first created, a
31 * <code>ParmaeterMap</code> instance is not locked.
32 *
33 * @author Craig R. McClanahan
34 * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
35 */
36
37 public final class ParameterMap extends HashMap {
38
39
40 // ----------------------------------------------------------- Constructors
41
42
43 /**
44 * Construct a new, empty map with the default initial capacity and
45 * load factor.
46 */
47 public ParameterMap() {
48
49 super();
50
51 }
52
53
54 /**
55 * Construct a new, empty map with the specified initial capacity and
56 * default load factor.
57 *
58 * @param initialCapacity The initial capacity of this map
59 */
60 public ParameterMap(int initialCapacity) {
61
62 super(initialCapacity);
63
64 }
65
66
67 /**
68 * Construct a new, empty map with the specified initial capacity and
69 * load factor.
70 *
71 * @param initialCapacity The initial capacity of this map
72 * @param loadFactor The load factor of this map
73 */
74 public ParameterMap(int initialCapacity, float loadFactor) {
75
76 super(initialCapacity, loadFactor);
77
78 }
79
80
81 /**
82 * Construct a new map with the same mappings as the given map.
83 *
84 * @param map Map whose contents are dupliated in the new map
85 */
86 public ParameterMap(Map map) {
87
88 super(map);
89
90 }
91
92
93 // ------------------------------------------------------------- Properties
94
95
96 /**
97 * The current lock state of this parameter map.
98 */
99 private boolean locked = false;
100
101
102 /**
103 * Return the locked state of this parameter map.
104 */
105 public boolean isLocked() {
106
107 return (this.locked);
108
109 }
110
111
112 /**
113 * Set the locked state of this parameter map.
114 *
115 * @param locked The new locked state
116 */
117 public void setLocked(boolean locked) {
118
119 this.locked = locked;
120
121 }
122
123
124 /**
125 * The string manager for this package.
126 */
127 private static final StringManager sm =
128 StringManager.getManager("org.apache.catalina.util");
129
130
131 // --------------------------------------------------------- Public Methods
132
133
134
135 /**
136 * Remove all mappings from this map.
137 *
138 * @exception IllegalStateException if this map is currently locked
139 */
140 public void clear() {
141
142 if (locked)
143 throw new IllegalStateException
144 (sm.getString("parameterMap.locked"));
145 super.clear();
146
147 }
148
149
150 /**
151 * Associate the specified value with the specified key in this map. If
152 * the map previously contained a mapping for this key, the old value is
153 * replaced.
154 *
155 * @param key Key with which the specified value is to be associated
156 * @param value Value to be associated with the specified key
157 *
158 * @return The previous value associated with the specified key, or
159 * <code>null</code> if there was no mapping for key
160 *
161 * @exception IllegalStateException if this map is currently locked
162 */
163 public Object put(Object key, Object value) {
164
165 if (locked)
166 throw new IllegalStateException
167 (sm.getString("parameterMap.locked"));
168 return (super.put(key, value));
169
170 }
171
172
173 /**
174 * Copy all of the mappings from the specified map to this one. These
175 * mappings replace any mappings that this map had for any of the keys
176 * currently in the specified Map.
177 *
178 * @param map Mappings to be stored into this map
179 *
180 * @exception IllegalStateException if this map is currently locked
181 */
182 public void putAll(Map map) {
183
184 if (locked)
185 throw new IllegalStateException
186 (sm.getString("parameterMap.locked"));
187 super.putAll(map);
188
189 }
190
191
192 /**
193 * Remove the mapping for this key from the map if present.
194 *
195 * @param key Key whose mapping is to be removed from the map
196 *
197 * @return The previous value associated with the specified key, or
198 * <code>null</code> if there was no mapping for that key
199 *
200 * @exception IllegalStateException if this map is currently locked
201 */
202 public Object remove(Object key) {
203
204 if (locked)
205 throw new IllegalStateException
206 (sm.getString("parameterMap.locked"));
207 return (super.remove(key));
208
209 }
210
211
212 }