1 /*
2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
131 * <pre>
132 * return (String)get(name);
133 * </pre>
134 *
135 * @param name the Attributes.Name object
136 * @return the String value of the specified Attribute.Name, or null if
137 * not found.
138 */
139 public String getValue(Name name) {
140 return (String)get(name);
141 }
142
143 /**
144 * Associates the specified value with the specified attribute name
145 * (key) in this Map. If the Map previously contained a mapping for
146 * the attribute name, the old value is replaced.
147 *
148 * @param name the attribute name
149 * @param value the attribute value
150 * @return the previous value of the attribute, or null if none
151 * @exception ClassCastException if the name is not a Attributes.Name
152 * or the value is not a String
153 */
154 public Object put(Object name, Object value) {
155 return map.put((Attributes.Name)name, (String)value);
156 }
157
158 /**
159 * Associates the specified value with the specified attribute name,
160 * specified as a String. The attributes name is case-insensitive.
161 * If the Map previously contained a mapping for the attribute name,
162 * the old value is replaced.
163 * <p>
164 * This method is defined as:
165 * <pre>
166 * return (String)put(new Attributes.Name(name), value);
167 * </pre>
168 *
169 * @param name the attribute name as a string
170 * @param value the attribute value
171 * @return the previous value of the attribute, or null if none
172 * @exception IllegalArgumentException if the attribute name is invalid
173 */
174 public String putValue(String name, String value) {
175 return (String)put(Name.of(name), value);
176 }
177
178 /**
179 * Removes the attribute with the specified name (key) from this Map.
180 * Returns the previous attribute value, or null if none.
181 *
182 * @param name attribute name
183 * @return the previous value of the attribute, or null if none
184 */
185 public Object remove(Object name) {
186 return map.remove(name);
187 }
188
189 /**
190 * Returns true if this Map maps one or more attribute names (keys)
191 * to the specified value.
192 *
196 */
197 public boolean containsValue(Object value) {
198 return map.containsValue(value);
199 }
200
201 /**
202 * Returns true if this Map contains the specified attribute name (key).
203 *
204 * @param name the attribute name
205 * @return true if this Map contains the specified attribute name
206 */
207 public boolean containsKey(Object name) {
208 return map.containsKey(name);
209 }
210
211 /**
212 * Copies all of the attribute name-value mappings from the specified
213 * Attributes to this Map. Duplicate mappings will be replaced.
214 *
215 * @param attr the Attributes to be stored in this map
216 * @exception ClassCastException if attr is not an Attributes
217 */
218 public void putAll(Map<?,?> attr) {
219 // ## javac bug?
220 if (!Attributes.class.isInstance(attr))
221 throw new ClassCastException();
222 for (Map.Entry<?,?> me : (attr).entrySet())
223 put(me.getKey(), me.getValue());
224 }
225
226 /**
227 * Removes all attributes from this Map.
228 */
229 public void clear() {
230 map.clear();
231 }
232
233 /**
234 * Returns the number of attributes in this Map.
235 */
236 public int size() {
453 private final String name;
454 private final int hashCode;
455
456 /**
457 * Avoid allocation for common Names
458 */
459 private static @Stable Map<String, Name> KNOWN_NAMES;
460
461 static final Name of(String name) {
462 Name n = KNOWN_NAMES.get(name);
463 if (n != null) {
464 return n;
465 }
466 return new Name(name);
467 }
468
469 /**
470 * Constructs a new attribute name using the given string name.
471 *
472 * @param name the attribute string name
473 * @exception IllegalArgumentException if the attribute name was
474 * invalid
475 * @exception NullPointerException if the attribute name was null
476 */
477 public Name(String name) {
478 this.hashCode = hash(name);
479 this.name = name.intern();
480 }
481
482 // Checks the string is valid
483 private final int hash(String name) {
484 Objects.requireNonNull(name, "name");
485 int len = name.length();
486 if (len > 70 || len == 0) {
487 throw new IllegalArgumentException(name);
488 }
489 // Calculate hash code case insensitively
490 int h = 0;
491 for (int i = 0; i < len; i++) {
492 char c = name.charAt(i);
493 if (c >= 'a' && c <= 'z') {
494 // hashcode must be identical for upper and lower case
495 h = h * 31 + (c - 0x20);
|
1 /*
2 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
131 * <pre>
132 * return (String)get(name);
133 * </pre>
134 *
135 * @param name the Attributes.Name object
136 * @return the String value of the specified Attribute.Name, or null if
137 * not found.
138 */
139 public String getValue(Name name) {
140 return (String)get(name);
141 }
142
143 /**
144 * Associates the specified value with the specified attribute name
145 * (key) in this Map. If the Map previously contained a mapping for
146 * the attribute name, the old value is replaced.
147 *
148 * @param name the attribute name
149 * @param value the attribute value
150 * @return the previous value of the attribute, or null if none
151 * @throws ClassCastException if the name is not a Attributes.Name
152 * or the value is not a String
153 */
154 public Object put(Object name, Object value) {
155 return map.put((Attributes.Name)name, (String)value);
156 }
157
158 /**
159 * Associates the specified value with the specified attribute name,
160 * specified as a String. The attributes name is case-insensitive.
161 * If the Map previously contained a mapping for the attribute name,
162 * the old value is replaced.
163 * <p>
164 * This method is defined as:
165 * <pre>
166 * return (String)put(new Attributes.Name(name), value);
167 * </pre>
168 *
169 * @param name the attribute name as a string
170 * @param value the attribute value
171 * @return the previous value of the attribute, or null if none
172 * @throws IllegalArgumentException if the attribute name is invalid
173 */
174 public String putValue(String name, String value) {
175 return (String)put(Name.of(name), value);
176 }
177
178 /**
179 * Removes the attribute with the specified name (key) from this Map.
180 * Returns the previous attribute value, or null if none.
181 *
182 * @param name attribute name
183 * @return the previous value of the attribute, or null if none
184 */
185 public Object remove(Object name) {
186 return map.remove(name);
187 }
188
189 /**
190 * Returns true if this Map maps one or more attribute names (keys)
191 * to the specified value.
192 *
196 */
197 public boolean containsValue(Object value) {
198 return map.containsValue(value);
199 }
200
201 /**
202 * Returns true if this Map contains the specified attribute name (key).
203 *
204 * @param name the attribute name
205 * @return true if this Map contains the specified attribute name
206 */
207 public boolean containsKey(Object name) {
208 return map.containsKey(name);
209 }
210
211 /**
212 * Copies all of the attribute name-value mappings from the specified
213 * Attributes to this Map. Duplicate mappings will be replaced.
214 *
215 * @param attr the Attributes to be stored in this map
216 * @throws ClassCastException if attr is not an Attributes
217 */
218 public void putAll(Map<?,?> attr) {
219 // ## javac bug?
220 if (!Attributes.class.isInstance(attr))
221 throw new ClassCastException();
222 for (Map.Entry<?,?> me : (attr).entrySet())
223 put(me.getKey(), me.getValue());
224 }
225
226 /**
227 * Removes all attributes from this Map.
228 */
229 public void clear() {
230 map.clear();
231 }
232
233 /**
234 * Returns the number of attributes in this Map.
235 */
236 public int size() {
453 private final String name;
454 private final int hashCode;
455
456 /**
457 * Avoid allocation for common Names
458 */
459 private static @Stable Map<String, Name> KNOWN_NAMES;
460
461 static final Name of(String name) {
462 Name n = KNOWN_NAMES.get(name);
463 if (n != null) {
464 return n;
465 }
466 return new Name(name);
467 }
468
469 /**
470 * Constructs a new attribute name using the given string name.
471 *
472 * @param name the attribute string name
473 * @throws IllegalArgumentException if the attribute name was
474 * invalid
475 * @throws NullPointerException if the attribute name was null
476 */
477 public Name(String name) {
478 this.hashCode = hash(name);
479 this.name = name.intern();
480 }
481
482 // Checks the string is valid
483 private final int hash(String name) {
484 Objects.requireNonNull(name, "name");
485 int len = name.length();
486 if (len > 70 || len == 0) {
487 throw new IllegalArgumentException(name);
488 }
489 // Calculate hash code case insensitively
490 int h = 0;
491 for (int i = 0; i < len; i++) {
492 char c = name.charAt(i);
493 if (c >= 'a' && c <= 'z') {
494 // hashcode must be identical for upper and lower case
495 h = h * 31 + (c - 0x20);
|