All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.netty.core.util.CaseInsensitiveKeyMap Maven / Gradle / Ivy

package com.github.netty.core.util;

import java.util.*;

/**
 * 不敏感大小写的map
 * 

* A Map implementation that uses case-insensitive (using {@link * Locale#ENGLISH}) strings as keys. *

* Keys must be instances of {@link String}. Note that this means that * null keys are not permitted. *

* This implementation is not thread-safe. * * @param Type of values placed in this Map. */ public class CaseInsensitiveKeyMap extends AbstractMap { private final Map map; public CaseInsensitiveKeyMap() { this.map = new LinkedHashMap<>(); } public CaseInsensitiveKeyMap(int initialCapacity) { map = new LinkedHashMap<>(initialCapacity); } @Override public V get(Object key) { return map.get(Key.getInstance(key)); } @Override public V put(String key, V value) { Key caseInsensitiveKey = Key.getInstance(key); if (caseInsensitiveKey == null) { throw new NullPointerException(); } return map.put(caseInsensitiveKey, value); } /** * {@inheritDoc} *

* Use this method with caution. If the input Map contains duplicate * keys when the keys are compared in a case insensitive manner then some * values will be lost when inserting via this method. */ @Override public void putAll(Map m) { super.putAll(m); } @Override public boolean containsKey(Object key) { return map.containsKey(Key.getInstance(key)); } @Override public V remove(Object key) { return map.remove(Key.getInstance(key)); } @Override public Set> entrySet() { return new EntrySet<>(map.entrySet()); } private static class EntrySet extends AbstractSet> { private final Set> entrySet; public EntrySet(Set> entrySet) { this.entrySet = entrySet; } @Override public Iterator> iterator() { return new EntryIterator<>(entrySet.iterator()); } @Override public int size() { return entrySet.size(); } } private static class EntryIterator implements Iterator> { private final Iterator> iterator; public EntryIterator(Iterator> iterator) { this.iterator = iterator; } @Override public boolean hasNext() { return iterator.hasNext(); } @Override public Entry next() { Entry entry = iterator.next(); return new EntryImpl<>(entry.getKey().getKey(), entry.getValue()); } @Override public void remove() { iterator.remove(); } } private static class EntryImpl implements Entry { private final String key; private final V value; public EntryImpl(String key, V value) { this.key = key; this.value = value; } @Override public String getKey() { return key; } @Override public V getValue() { return value; } @Override public V setValue(V value) { throw new UnsupportedOperationException(); } } private static class Key { private final String key; private final String lcKey; private Key(String key) { this.key = key; this.lcKey = key.toLowerCase(Locale.ENGLISH); } public static Key getInstance(Object o) { if (o instanceof String) { return new Key((String) o); } return null; } public String getKey() { return key; } @Override public int hashCode() { return lcKey.hashCode(); } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } Key other = (Key) obj; return lcKey.equals(other.lcKey); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy