Skip to content

Commit a9f6ea7

Browse files
committed
Add CIKey and associated classes
1 parent 7deb784 commit a9f6ea7

File tree

13 files changed

+2471
-0
lines changed

13 files changed

+2471
-0
lines changed

Diff for: stroom-util-shared/build.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ dependencies {
99
implementation libs.swagger_annotations
1010

1111
testImplementation project(':stroom-test-common')
12+
testImplementation project(':stroom-util')
1213

1314
testImplementation libs.assertj_core
1415
testImplementation libs.commons_lang
1516
testImplementation libs.guice
1617
testImplementation libs.jackson_core
1718
testImplementation libs.jackson_databind
1819
testImplementation libs.jackson_datatype_jdk8
20+
testImplementation libs.jmh_core
21+
testImplementation libs.jmh_generator_annprocess
1922
testImplementation libs.junit_jupiter_api
2023
testImplementation libs.junit_jupiter_params
2124
testImplementation libs.slf4j_api

Diff for: stroom-util-shared/src/main/java/stroom/util/shared/CompareUtil.java

+14
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,18 @@ public static <T1, T2, T3, T4 extends Comparable<T4>> Comparator<T1> getNullSafe
262262
public static <T> Comparator<T> name(final String name, final Comparator<T> comparator) {
263263
return new NamedComparator<>(name, comparator);
264264
}
265+
266+
/**
267+
* Normalises an {@link Comparable#compareTo(Object)} result into -1, 0, or 1.
268+
* Useful for doing equality assertions on comparators.
269+
*/
270+
public static int normalise(final int compareResult) {
271+
if (compareResult < 0) {
272+
return -1;
273+
} else if (compareResult > 0) {
274+
return 1;
275+
} else {
276+
return compareResult;
277+
}
278+
}
265279
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
1+
/*
2+
* Copyright 2024 Crown Copyright
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package stroom.util.shared.string;
18+
19+
import stroom.util.shared.GwtNullSafe;
20+
21+
import java.util.Collections;
22+
import java.util.HashMap;
23+
import java.util.Map;
24+
import java.util.Set;
25+
import java.util.stream.Collectors;
26+
27+
/**
28+
* A standard {@link HashMap} keyed with case-insensitive {@link CIKey} keys.
29+
* <p>
30+
* Various methods of {@link HashMap} are overridden so you can interact with the
31+
* map using {@link String} or {@link CIKey} keys, with {@link String} keys getting
32+
* converted to {@link CIKey} on the fly.
33+
* </p>
34+
*/
35+
public class CIHashMap<V> extends HashMap<CIKey, V> {
36+
37+
@SuppressWarnings("rawtypes")
38+
private static final Map<CIKey, ?> EMPTY = Collections.emptyMap();
39+
40+
public static <V> CIHashMap<V> empty() {
41+
//noinspection unchecked
42+
return (CIHashMap<V>) EMPTY;
43+
}
44+
45+
public CIHashMap(final int initialCapacity, final float loadFactor) {
46+
super(initialCapacity, loadFactor);
47+
}
48+
49+
public CIHashMap(final int initialCapacity) {
50+
super(initialCapacity);
51+
}
52+
53+
public CIHashMap() {
54+
super();
55+
}
56+
57+
public CIHashMap(final Map<? extends CIKey, ? extends V> m) {
58+
super(m);
59+
}
60+
61+
public V put(final String key, final V value) {
62+
// CIString used to trim all keys
63+
return super.put(CIKey.of(key), value);
64+
}
65+
66+
/**
67+
* Converts key into a {@link CIKey} before calling {@link CIHashMap#get(Object)}
68+
*/
69+
// Overload all the super methods that take key as an Object as the compiler
70+
// won't spot people using a String key.
71+
public V get(final String key) {
72+
return super.get(CIKey.of(key));
73+
}
74+
75+
public V get(final CIKey key) {
76+
return super.get(key);
77+
}
78+
79+
public boolean replace(final String key, final V oldValue, final V newValue) {
80+
return super.replace(CIKey.of(key), oldValue, newValue);
81+
}
82+
83+
public boolean replace(final CIKey key, final V oldValue, final V newValue) {
84+
return super.replace(key, oldValue, newValue);
85+
}
86+
87+
public V replace(final String key, final V value) {
88+
return super.replace(CIKey.of(key), value);
89+
}
90+
91+
public V replace(final CIKey key, final V value) {
92+
return super.replace(key, value);
93+
}
94+
95+
/**
96+
* Converts key into a {@link CIKey} before calling {@link CIHashMap#getOrDefault(Object, Object)}
97+
*/
98+
public V getOrDefault(final String key, final V defaultValue) {
99+
return super.getOrDefault(CIKey.of(key), defaultValue);
100+
}
101+
102+
/**
103+
* Converts key into a {@link CIKey} before calling {@link CIHashMap#remove(String, Object)}
104+
*/
105+
public boolean remove(final String key, final Object value) {
106+
return super.remove(CIKey.of(key), value);
107+
}
108+
109+
public boolean remove(final CIKey key, final Object value) {
110+
return super.remove(key, value);
111+
}
112+
113+
/**
114+
* Converts key into a {@link CIKey} before calling {@link CIHashMap#remove(Object)}
115+
*/
116+
public V remove(final String key) {
117+
return super.remove(CIKey.of(key));
118+
}
119+
120+
public V remove(final CIKey key) {
121+
return super.remove(key);
122+
}
123+
124+
public boolean containsKey(final String key) {
125+
return super.containsKey(CIKey.of(key));
126+
}
127+
128+
public boolean containsKey(final CIKey key) {
129+
return super.containsKey(key);
130+
}
131+
132+
@Override
133+
public void putAll(final Map<? extends CIKey, ? extends V> m) {
134+
super.putAll(m);
135+
}
136+
137+
/**
138+
* Equivalent to {@link Map#putAll(Map)} but for a map keyed by strings.
139+
*/
140+
public void putAllWithStringKeys(final Map<String, V> map) {
141+
GwtNullSafe.map(map)
142+
.forEach(this::put);
143+
}
144+
145+
public Set<String> keySetAsStrings() {
146+
return keySet().stream()
147+
.map(CIKey::get)
148+
.collect(Collectors.toSet());
149+
}
150+
151+
/**
152+
* Create a {@link CIKey} keyed map
153+
*/
154+
public static <V> CIHashMap<V> of(String k1, V v1) {
155+
return new CIHashMap<>(CIKey.mapOf(k1, v1));
156+
}
157+
158+
/**
159+
* Create a {@link CIKey} keyed map
160+
*/
161+
public static <V> CIHashMap<V> of(String k1, V v1, String k2, V v2) {
162+
return new CIHashMap<>(CIKey.mapOf(
163+
k1, v1,
164+
k2, v2));
165+
}
166+
167+
/**
168+
* Create a {@link CIKey} keyed map
169+
*/
170+
public static <V> CIHashMap<V> of(String k1, V v1,
171+
String k2, V v2,
172+
String k3, V v3) {
173+
return new CIHashMap<>(CIKey.mapOf(
174+
k1, v1,
175+
k2, v2,
176+
k3, v3));
177+
}
178+
179+
/**
180+
* Create a {@link CIKey} keyed map
181+
*/
182+
public static <V> CIHashMap<V> of(String k1, V v1,
183+
String k2, V v2,
184+
String k3, V v3,
185+
String k4, V v4) {
186+
return new CIHashMap<>(CIKey.mapOf(
187+
k1, v1,
188+
k2, v2,
189+
k3, v3,
190+
k4, v4));
191+
}
192+
193+
/**
194+
* Create a {@link CIKey} keyed map
195+
*/
196+
public static <V> CIHashMap<V> of(String k1, V v1,
197+
String k2, V v2,
198+
String k3, V v3,
199+
String k4, V v4,
200+
String k5, V v5) {
201+
return new CIHashMap<>(CIKey.mapOf(
202+
k1, v1,
203+
k2, v2,
204+
k3, v3,
205+
k4, v4,
206+
k5, v5));
207+
}
208+
209+
/**
210+
* Create a {@link CIKey} keyed map
211+
*/
212+
public static <V> CIHashMap<V> of(String k1, V v1,
213+
String k2, V v2,
214+
String k3, V v3,
215+
String k4, V v4,
216+
String k5, V v5,
217+
String k6, V v6) {
218+
return new CIHashMap<>(CIKey.mapOf(
219+
k1, v1,
220+
k2, v2,
221+
k3, v3,
222+
k4, v4,
223+
k5, v5,
224+
k6, v6));
225+
}
226+
227+
/**
228+
* Create a {@link CIKey} keyed map
229+
*/
230+
public static <V> CIHashMap<V> of(String k1, V v1,
231+
String k2, V v2,
232+
String k3, V v3,
233+
String k4, V v4,
234+
String k5, V v5,
235+
String k6, V v6,
236+
String k7, V v7) {
237+
return new CIHashMap<>(CIKey.mapOf(
238+
k1, v1,
239+
k2, v2,
240+
k3, v3,
241+
k4, v4,
242+
k5, v5,
243+
k6, v6,
244+
k7, v7));
245+
}
246+
247+
/**
248+
* Create a {@link CIKey} keyed map
249+
*/
250+
public static <V> CIHashMap<V> of(String k1, V v1,
251+
String k2, V v2,
252+
String k3, V v3,
253+
String k4, V v4,
254+
String k5, V v5,
255+
String k6, V v6,
256+
String k7, V v7,
257+
String k8, V v8) {
258+
return new CIHashMap<>(CIKey.mapOf(
259+
k1, v1,
260+
k2, v2,
261+
k3, v3,
262+
k4, v4,
263+
k5, v5,
264+
k6, v6,
265+
k7, v7,
266+
k8, v8));
267+
}
268+
269+
/**
270+
* Convert an array of {@link String} keyed entries into a {@link CIKey} keyed map.
271+
*/
272+
@SafeVarargs
273+
public static <V> CIHashMap<V> ofEntries(final Entry<String, ? extends V>... entries) {
274+
return GwtNullSafe.stream(entries)
275+
.collect(Collectors.toMap(
276+
entry ->
277+
CIKey.of(entry.getKey()),
278+
Entry::getValue,
279+
(object, object2) -> object,
280+
CIHashMap::new));
281+
}
282+
283+
/**
284+
* Convert a {@link String} keyed map into a {@link CIKey} keyed map.
285+
* Accepts nulls and never returns a null.
286+
*/
287+
public static <V> CIHashMap<V> of(final Map<String, ? extends V> map) {
288+
return GwtNullSafe.map(map)
289+
.entrySet()
290+
.stream()
291+
.collect(Collectors.toMap(
292+
entry ->
293+
CIKey.of(entry.getKey()),
294+
Entry::getValue,
295+
(object, object2) -> object,
296+
CIHashMap::new));
297+
}
298+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2024 Crown Copyright
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package stroom.util.shared.string;
18+
19+
import stroom.util.shared.GwtNullSafe;
20+
21+
import java.util.Collection;
22+
import java.util.HashSet;
23+
import java.util.stream.Collectors;
24+
25+
/**
26+
* A {@link HashSet} containing case-insensitive {@link CIKey} string values.
27+
*/
28+
public class CIHashSet extends HashSet<CIKey> {
29+
30+
public CIHashSet(final Collection<String> collection) {
31+
super(GwtNullSafe.stream(collection)
32+
.map(CIKey::of)
33+
.collect(Collectors.toList()));
34+
}
35+
36+
public boolean add(final String value) {
37+
return super.add(CIKey.of(value));
38+
}
39+
40+
/**
41+
* Remove an item matching value (case-insensitive).
42+
*/
43+
public boolean removeString(final String value) {
44+
return super.remove(CIKey.of(value));
45+
}
46+
47+
/**
48+
* True if there is an item matching value (case-insensitive).
49+
*/
50+
public boolean containsString(final String value) {
51+
return super.contains(CIKey.of(value));
52+
}
53+
}

0 commit comments

Comments
 (0)