|
16 | 16 | */ |
17 | 17 | package org.apache.solr.common.util; |
18 | 18 |
|
| 19 | +import java.util.AbstractMap; |
| 20 | +import java.util.AbstractSet; |
19 | 21 | import java.util.ArrayList; |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.Iterator; |
20 | 24 | import java.util.List; |
21 | 25 | import java.util.Map; |
| 26 | +import java.util.Set; |
22 | 27 |
|
23 | 28 | /** |
24 | 29 | * <code>SimpleOrderedMap</code> is a {@link NamedList} where access by key is more important than |
|
30 | 35 | * {"foo":10,"bar":20} and may choose to write a NamedList as ["foo",10,"bar",20]. An XML response |
31 | 36 | * writer may choose to render both the same way. |
32 | 37 | * |
33 | | - * <p>This class does not provide efficient lookup by key, its main purpose is to hold data to be |
34 | | - * serialized. It aims to minimize overhead and to be efficient at adding new elements. |
| 38 | + * <p>This class does not provide efficient lookup by key. The lookup performance is only O(N), and |
| 39 | + * not O(1) or O(Log N) as it is for the most common Map-implementations. Its main purpose is to |
| 40 | + * hold data to be serialized. It aims to minimize overhead and to be efficient at adding new |
| 41 | + * elements. |
35 | 42 | */ |
36 | | -public class SimpleOrderedMap<T> extends NamedList<T> { |
| 43 | +public class SimpleOrderedMap<T> extends NamedList<T> implements Map<String, T> { |
37 | 44 |
|
38 | 45 | private static final SimpleOrderedMap<Object> EMPTY = new SimpleOrderedMap<>(List.of()); |
39 | 46 |
|
@@ -64,28 +71,124 @@ public SimpleOrderedMap(Map.Entry<String, T>[] nameValuePairs) { |
64 | 71 | super(nameValuePairs); |
65 | 72 | } |
66 | 73 |
|
| 74 | + // TODO override asShallowMap in Solr 10 |
| 75 | + |
67 | 76 | @Override |
68 | 77 | public SimpleOrderedMap<T> clone() { |
69 | 78 | ArrayList<Object> newList = new ArrayList<>(nvPairs.size()); |
70 | 79 | newList.addAll(nvPairs); |
71 | 80 | return new SimpleOrderedMap<>(newList); |
72 | 81 | } |
73 | 82 |
|
| 83 | + @Override |
| 84 | + public boolean isEmpty() { |
| 85 | + return nvPairs.isEmpty(); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public boolean containsKey(final Object key) { |
| 90 | + return this.indexOf((String) key) >= 0; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public boolean containsValue(final Object value) { |
| 95 | + return values().contains(value); |
| 96 | + } |
| 97 | + |
74 | 98 | /** |
75 | | - * Returns a shared, empty, and immutable instance of SimpleOrderedMap. |
| 99 | + * {@inheritDoc} |
| 100 | + * |
| 101 | + * <p>Has linear lookup time O(N) |
76 | 102 | * |
77 | | - * @return Empty SimpleOrderedMap (immutable) |
| 103 | + * @see NamedList#get(String) |
78 | 104 | */ |
79 | | - public static SimpleOrderedMap<Object> of() { |
80 | | - return EMPTY; |
| 105 | + @Override |
| 106 | + public T get(final Object key) { |
| 107 | + return super.get((String) key); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public T put(String key, T value) { |
| 112 | + int idx = indexOf(key); |
| 113 | + if (idx == -1) { |
| 114 | + add(key, value); |
| 115 | + return null; |
| 116 | + } else { |
| 117 | + T t = get(key); |
| 118 | + setVal(idx, value); |
| 119 | + return t; |
| 120 | + } |
81 | 121 | } |
82 | 122 |
|
83 | 123 | /** |
84 | | - * Returns an immutable instance of SimpleOrderedMap with a single key-value pair. |
| 124 | + * @see NamedList#remove(String) |
| 125 | + */ |
| 126 | + @Override |
| 127 | + public T remove(final Object key) { |
| 128 | + return super.remove((String) key); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void putAll(final Map<? extends String, ? extends T> m) { |
| 133 | + if (isEmpty()) { |
| 134 | + m.forEach(this::add); |
| 135 | + } else { |
| 136 | + m.forEach(this::put); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public Set<String> keySet() { |
| 142 | + return new InnerMap().keySet(); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public Collection<T> values() { |
| 147 | + return new InnerMap().values(); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public Set<Entry<String, T>> entrySet() { |
| 152 | + |
| 153 | + return new AbstractSet<>() { |
| 154 | + @Override |
| 155 | + public Iterator<Entry<String, T>> iterator() { |
| 156 | + return SimpleOrderedMap.this.iterator(); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public int size() { |
| 161 | + return SimpleOrderedMap.this.size(); |
| 162 | + } |
| 163 | + }; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Returns an immutable instance of {@link SimpleOrderedMap} with a single key-value pair. |
85 | 168 | * |
86 | | - * @return SimpleOrderedMap containing one key-value pair |
| 169 | + * @return {@link SimpleOrderedMap} containing one key-value pair |
87 | 170 | */ |
88 | 171 | public static <T> SimpleOrderedMap<T> of(String name, T val) { |
89 | 172 | return new SimpleOrderedMap<>(List.of(name, val)); |
90 | 173 | } |
| 174 | + |
| 175 | + /** |
| 176 | + * Returns a shared, empty, and immutable instance of {@link SimpleOrderedMap}. |
| 177 | + * |
| 178 | + * @return Empty {@link SimpleOrderedMap} (immutable) |
| 179 | + */ |
| 180 | + public static SimpleOrderedMap<Object> of() { |
| 181 | + return EMPTY; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * {@link SimpleOrderedMap} extending {@link NamedList}, we are not able to extend {@link |
| 186 | + * AbstractMap}. With the help of InnerMap we can still use {@link AbstractMap} methods. |
| 187 | + */ |
| 188 | + private class InnerMap extends AbstractMap<String, T> { |
| 189 | + @Override |
| 190 | + public Set<Entry<String, T>> entrySet() { |
| 191 | + return SimpleOrderedMap.this.entrySet(); |
| 192 | + } |
| 193 | + } |
91 | 194 | } |
0 commit comments