annotate src/goodjava/util/CaseInsensitiveMap.java @ 1691:8d4152398825

blog scripts
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 13 Jun 2022 18:01:51 -0600
parents fa066aaa068c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1607
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.util;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.util.Map;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.util.AbstractMap;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.util.Set;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.util.AbstractSet;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import java.util.Iterator;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 public final class CaseInsensitiveMap<V> extends AbstractMap<String,V> {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 public static final class Value<V> {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 private final String s;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 private final V v;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 private Value(String s,V v) {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 this.s = s;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 this.v = v;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 private final Map<String,Value<V>> map;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 public CaseInsensitiveMap(Map<String,Value<V>> map) {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 this.map = map;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 @Override public int size() {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 return map.size();
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 @Override public boolean isEmpty() {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 return map.isEmpty();
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 @Override public boolean containsKey(Object key) {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 if( !(key instanceof String) )
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 return false;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 String s = (String)key;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 return map.containsKey(s.toLowerCase());
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 @Override public V get(Object key) {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 if( !(key instanceof String) )
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 return null;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 String s = (String)key;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 Value<V> val = map.get(s.toLowerCase());
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 return val==null ? null : val.v;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 @Override public V put(String key,V value) {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 Value<V> val = map.put( key.toLowerCase(), new Value<V>(key,value) );
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 return val==null ? null : val.v;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 @Override public V remove(Object key) {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 if( !(key instanceof String) )
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 return null;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 String s = (String)key;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 Value<V> val = map.remove(s.toLowerCase());
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 return val==null ? null : val.v;
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 @Override public void clear() {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 map.clear();
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 @Override public Set<Map.Entry<String,V>> entrySet() {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 return new MySet();
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 private class MySet extends AbstractSet<Map.Entry<String,V>> {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 @Override public int size() {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 return CaseInsensitiveMap.this.size();
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 @Override public Iterator<Map.Entry<String,V>> iterator() {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 return new MyIterator();
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 private class MyIterator implements Iterator<Map.Entry<String,V>> {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 final Iterator<Map.Entry<String,Value<V>>> iter = map.entrySet().iterator();
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 @Override public Map.Entry<String,V> next() {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 Value<V> val = iter.next().getValue();
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 return new AbstractMap.SimpleImmutableEntry<String,V>( val.s, val.v );
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 @Override public boolean hasNext() {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 return iter.hasNext();
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 @Override public void remove() {
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 iter.remove();
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 }
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99 }