diff options
author | BJ Hargrave | 2017-01-18 16:03:51 +0000 |
---|---|---|
committer | Thomas Watson | 2017-01-18 18:58:10 +0000 |
commit | f2c1c6a63763ab80aafeb8045ff8f73048870401 (patch) | |
tree | 50f760fc80a7c4b604338aa3106d9e330542523a | |
parent | 891d19b5aed96de493d85d376b9629ade4abe9b9 (diff) | |
download | rt.equinox.framework-f2c1c6a63763ab80aafeb8045ff8f73048870401.tar.gz rt.equinox.framework-f2c1c6a63763ab80aafeb8045ff8f73048870401.tar.xz rt.equinox.framework-f2c1c6a63763ab80aafeb8045ff8f73048870401.zip |
Bug 510641 - Remove CaseInsensitiveDictionaryMap.putIfAbsent method
We don’t actually need it since put can be used in the constructor
when checking for case-variant key issues. Removing putIfAbsent then
simplifies some implementation.
Change-Id: Id8ae7a2b90b0edd3662f8697d64734e314e20cf8
Signed-off-by: BJ Hargrave <hargrave@us.ibm.com>
2 files changed, 18 insertions, 64 deletions
diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/framework/util/CaseInsensitiveDictionaryMap.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/framework/util/CaseInsensitiveDictionaryMap.java index 8b9db8fb9..545cc9008 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/framework/util/CaseInsensitiveDictionaryMap.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/framework/util/CaseInsensitiveDictionaryMap.java @@ -11,6 +11,8 @@ package org.eclipse.osgi.framework.util; +import static java.util.Objects.requireNonNull; + import java.util.*; import org.eclipse.osgi.internal.messages.Msg; import org.eclipse.osgi.util.NLS; @@ -57,7 +59,7 @@ public class CaseInsensitiveDictionaryMap<K, V> extends Dictionary<K, V> impleme Enumeration<? extends K> keys = dictionary.keys(); while (keys.hasMoreElements()) { K key = keys.nextElement(); - if (putIfAbsent(key, dictionary.get(key)) != null) { + if (put(key, dictionary.get(key)) != null) { throw new IllegalArgumentException(NLS.bind(Msg.HEADER_DUPLICATE_KEY_EXCEPTION, key)); } } @@ -74,7 +76,7 @@ public class CaseInsensitiveDictionaryMap<K, V> extends Dictionary<K, V> impleme this(initialCapacity(map.size())); /* initialize the keys and values */ for (Entry<? extends K, ? extends V> e : map.entrySet()) { - if (putIfAbsent(e.getKey(), e.getValue()) != null) { + if (put(e.getKey(), e.getValue()) != null) { throw new IllegalArgumentException(NLS.bind(Msg.HEADER_DUPLICATE_KEY_EXCEPTION, e.getKey())); } } @@ -116,12 +118,7 @@ public class CaseInsensitiveDictionaryMap<K, V> extends Dictionary<K, V> impleme */ @Override public V get(Object key) { - Objects.requireNonNull(key); - return get0(keyWrap(key)); - } - - private V get0(Object key) { - return map.get(key); + return map.get(keyWrap(requireNonNull(key))); } /** @@ -163,40 +160,14 @@ public class CaseInsensitiveDictionaryMap<K, V> extends Dictionary<K, V> impleme */ @Override public V put(K key, V value) { - Objects.requireNonNull(key); - Objects.requireNonNull(value); - return put0(key, value, keyWrap(key)); - } - - private V put0(K key, V value, Object wrappedKey) { - if (key != wrappedKey) { - CaseInsensitiveKey interned = ((CaseInsensitiveKey) wrappedKey).intern(); - V previous = map.remove(interned); // remove so we put key into map - map.put(interned, value); + requireNonNull(value); + if (key instanceof String) { + Object wrappedKey = keyWrap(((String) key).intern()); + V previous = map.remove(wrappedKey); // remove so we put key into map + map.put(wrappedKey, value); return previous; } - return map.put(wrappedKey, value); - } - - /** - * If the specified key is not already associated with a value, - * associates it with the specified value and return - * {@code null}. Otherwise returns the current value of the - * specified key. - * <p> - * The key and value must be non-null. - * <p> - * If the key is a String, any case-variant will be replaced. - */ - public V putIfAbsent(K key, V value) { - Objects.requireNonNull(key); - Objects.requireNonNull(value); - Object wrappedKey = keyWrap(key); - V v = get0(wrappedKey); - if (v == null) { - v = put0(key, value, wrappedKey); - } - return v; + return map.put(requireNonNull(key), value); } /** @@ -208,8 +179,7 @@ public class CaseInsensitiveDictionaryMap<K, V> extends Dictionary<K, V> impleme */ @Override public V remove(Object key) { - Objects.requireNonNull(key); - return map.remove(keyWrap(key)); + return map.remove(keyWrap(requireNonNull(key))); } /** @@ -237,8 +207,7 @@ public class CaseInsensitiveDictionaryMap<K, V> extends Dictionary<K, V> impleme */ @Override public boolean containsKey(Object key) { - Objects.requireNonNull(key); - return map.containsKey(keyWrap(key)); + return map.containsKey(keyWrap(requireNonNull(key))); } /** @@ -248,8 +217,7 @@ public class CaseInsensitiveDictionaryMap<K, V> extends Dictionary<K, V> impleme */ @Override public boolean containsValue(Object value) { - Objects.requireNonNull(value); - return map.containsValue(value); + return map.containsValue(requireNonNull(value)); } private transient Set<Entry<K, V>> entrySet = null; @@ -351,7 +319,7 @@ public class CaseInsensitiveDictionaryMap<K, V> extends Dictionary<K, V> impleme private final Dictionary<? extends K, ? extends V> d; UnmodifiableDictionary(Dictionary<? extends K, ? extends V> d) { - this.d = Objects.requireNonNull(d); + this.d = requireNonNull(d); } @Override @@ -418,19 +386,6 @@ public class CaseInsensitiveDictionaryMap<K, V> extends Dictionary<K, V> impleme this.key = key; } - private CaseInsensitiveKey(String key, int hashCode) { - this.key = key; - this.hashCode = hashCode; - } - - CaseInsensitiveKey intern() { - String interned = key.intern(); - if (interned == key) { - return this; - } - return new CaseInsensitiveKey(interned, hashCode); - } - @Override public int hashCode() { int h = hashCode; @@ -596,8 +551,7 @@ public class CaseInsensitiveDictionaryMap<K, V> extends Dictionary<K, V> impleme @Override public V setValue(V value) { - Objects.requireNonNull(value); - return entry.setValue(value); + return entry.setValue(requireNonNull(value)); } @Override diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/serviceregistry/ServiceProperties.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/serviceregistry/ServiceProperties.java index 97d235090..b875b6cf3 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/serviceregistry/ServiceProperties.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/serviceregistry/ServiceProperties.java @@ -54,7 +54,7 @@ class ServiceProperties extends CaseInsensitiveDictionaryMap<String, Object> { Object key = keysEnum.nextElement(); if (key instanceof String) { String header = (String) key; - if (putIfAbsent(header, cloneValue(props.get(header))) != null) { + if (put(header, cloneValue(props.get(header))) != null) { throw new IllegalArgumentException(NLS.bind(Msg.HEADER_DUPLICATE_KEY_EXCEPTION, key)); } } @@ -79,7 +79,7 @@ class ServiceProperties extends CaseInsensitiveDictionaryMap<String, Object> { Object key = e.getKey(); if (key instanceof String) { String header = (String) key; - if (putIfAbsent(header, cloneValue(e.getValue())) != null) { + if (put(header, cloneValue(e.getValue())) != null) { throw new IllegalArgumentException(NLS.bind(Msg.HEADER_DUPLICATE_KEY_EXCEPTION, key)); } } |