Skip to content

Commit e8f1888

Browse files
committed
More HMap#hMap overloads
1 parent 6c91e64 commit e8f1888

File tree

3 files changed

+253
-8
lines changed

3 files changed

+253
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
66
## [Unreleased]
77
### Added
88
- `Lens#toIso`, for converting a lens to an iso
9+
- `HMap#hMap` overloads up to 8 bindings deep
910

1011
### Fixed
1112
- Deforested iterables execute in intended nesting order, where essential

src/main/java/com/jnape/palatable/lambda/adt/hmap/HMap.java

Lines changed: 189 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static com.jnape.palatable.lambda.functions.builtin.fn2.Map.map;
2020
import static com.jnape.palatable.lambda.lens.functions.View.view;
2121
import static java.util.Collections.emptyMap;
22-
import static java.util.Collections.singletonMap;
2322

2423
/**
2524
* An immutable heterogeneous mapping from a parametrized type-safe key to any value, supporting a minimal mapping
@@ -232,6 +231,194 @@ public static <V1, V2> HMap hMap(TypeSafeKey<?, V1> key1, V1 value1,
232231
public static <V1, V2, V3> HMap hMap(TypeSafeKey<?, V1> key1, V1 value1,
233232
TypeSafeKey<?, V2> key2, V2 value2,
234233
TypeSafeKey<?, V3> key3, V3 value3) {
235-
return hMap(key1, value1, key2, value2).put(key3, value3);
234+
return hMap(key1, value1,
235+
key2, value2)
236+
.put(key3, value3);
236237
}
238+
239+
/**
240+
* Static factory method for creating an HMap from four given associations.
241+
*
242+
* @param key1 the first mapped key
243+
* @param value1 the value mapped at key1
244+
* @param key2 the second mapped key
245+
* @param value2 the value mapped at key2
246+
* @param key3 the third mapped key
247+
* @param value3 the value mapped at key3
248+
* @param key4 the fourth mapped key
249+
* @param value4 the value mapped at key4
250+
* @param <V1> value1's type
251+
* @param <V2> value2's type
252+
* @param <V3> value3's type
253+
* @param <V4> value4's type
254+
* @return an HMap with the given associations
255+
*/
256+
public static <V1, V2, V3, V4> HMap hMap(TypeSafeKey<?, V1> key1, V1 value1,
257+
TypeSafeKey<?, V2> key2, V2 value2,
258+
TypeSafeKey<?, V3> key3, V3 value3,
259+
TypeSafeKey<?, V4> key4, V4 value4) {
260+
return hMap(key1, value1,
261+
key2, value2,
262+
key3, value3)
263+
.put(key4, value4);
264+
}
265+
266+
/**
267+
* Static factory method for creating an HMap from five given associations.
268+
*
269+
* @param key1 the first mapped key
270+
* @param value1 the value mapped at key1
271+
* @param key2 the second mapped key
272+
* @param value2 the value mapped at key2
273+
* @param key3 the third mapped key
274+
* @param value3 the value mapped at key3
275+
* @param key4 the fourth mapped key
276+
* @param value4 the value mapped at key4
277+
* @param key5 the fifth mapped key
278+
* @param value5 the value mapped at key5
279+
* @param <V1> value1's type
280+
* @param <V2> value2's type
281+
* @param <V3> value3's type
282+
* @param <V4> value4's type
283+
* @param <V5> value5's type
284+
* @return an HMap with the given associations
285+
*/
286+
public static <V1, V2, V3, V4, V5> HMap hMap(TypeSafeKey<?, V1> key1, V1 value1,
287+
TypeSafeKey<?, V2> key2, V2 value2,
288+
TypeSafeKey<?, V3> key3, V3 value3,
289+
TypeSafeKey<?, V4> key4, V4 value4,
290+
TypeSafeKey<?, V5> key5, V5 value5) {
291+
return hMap(key1, value1,
292+
key2, value2,
293+
key3, value3,
294+
key4, value4)
295+
.put(key5, value5);
296+
}
297+
298+
/**
299+
* Static factory method for creating an HMap from six given associations.
300+
*
301+
* @param key1 the first mapped key
302+
* @param value1 the value mapped at key1
303+
* @param key2 the second mapped key
304+
* @param value2 the value mapped at key2
305+
* @param key3 the third mapped key
306+
* @param value3 the value mapped at key3
307+
* @param key4 the fourth mapped key
308+
* @param value4 the value mapped at key4
309+
* @param key5 the fifth mapped key
310+
* @param value5 the value mapped at key5
311+
* @param key6 the sixth mapped key
312+
* @param value6 the value mapped at key6
313+
* @param <V1> value1's type
314+
* @param <V2> value2's type
315+
* @param <V3> value3's type
316+
* @param <V4> value4's type
317+
* @param <V5> value5's type
318+
* @param <V6> value6's type
319+
* @return an HMap with the given associations
320+
*/
321+
public static <V1, V2, V3, V4, V5, V6> HMap hMap(TypeSafeKey<?, V1> key1, V1 value1,
322+
TypeSafeKey<?, V2> key2, V2 value2,
323+
TypeSafeKey<?, V3> key3, V3 value3,
324+
TypeSafeKey<?, V4> key4, V4 value4,
325+
TypeSafeKey<?, V5> key5, V5 value5,
326+
TypeSafeKey<?, V6> key6, V6 value6) {
327+
return hMap(key1, value1,
328+
key2, value2,
329+
key3, value3,
330+
key4, value4,
331+
key5, value5)
332+
.put(key6, value6);
333+
}
334+
335+
/**
336+
* Static factory method for creating an HMap from seven given associations.
337+
*
338+
* @param key1 the first mapped key
339+
* @param value1 the value mapped at key1
340+
* @param key2 the second mapped key
341+
* @param value2 the value mapped at key2
342+
* @param key3 the third mapped key
343+
* @param value3 the value mapped at key3
344+
* @param key4 the fourth mapped key
345+
* @param value4 the value mapped at key4
346+
* @param key5 the fifth mapped key
347+
* @param value5 the value mapped at key5
348+
* @param key6 the sixth mapped key
349+
* @param value6 the value mapped at key6
350+
* @param key7 the seventh mapped key
351+
* @param value7 the value mapped at key7
352+
* @param <V1> value1's type
353+
* @param <V2> value2's type
354+
* @param <V3> value3's type
355+
* @param <V4> value4's type
356+
* @param <V5> value5's type
357+
* @param <V6> value6's type
358+
* @param <V7> value7's type
359+
* @return an HMap with the given associations
360+
*/
361+
public static <V1, V2, V3, V4, V5, V6, V7> HMap hMap(TypeSafeKey<?, V1> key1, V1 value1,
362+
TypeSafeKey<?, V2> key2, V2 value2,
363+
TypeSafeKey<?, V3> key3, V3 value3,
364+
TypeSafeKey<?, V4> key4, V4 value4,
365+
TypeSafeKey<?, V5> key5, V5 value5,
366+
TypeSafeKey<?, V6> key6, V6 value6,
367+
TypeSafeKey<?, V7> key7, V7 value7) {
368+
return hMap(key1, value1,
369+
key2, value2,
370+
key3, value3,
371+
key4, value4,
372+
key5, value5,
373+
key6, value6)
374+
.put(key7, value7);
375+
}
376+
377+
/**
378+
* Static factory method for creating an HMap from eight given associations.
379+
*
380+
* @param key1 the first mapped key
381+
* @param value1 the value mapped at key1
382+
* @param key2 the second mapped key
383+
* @param value2 the value mapped at key2
384+
* @param key3 the third mapped key
385+
* @param value3 the value mapped at key3
386+
* @param key4 the fourth mapped key
387+
* @param value4 the value mapped at key4
388+
* @param key5 the fifth mapped key
389+
* @param value5 the value mapped at key5
390+
* @param key6 the sixth mapped key
391+
* @param value6 the value mapped at key6
392+
* @param key7 the seventh mapped key
393+
* @param value7 the value mapped at key7
394+
* @param key8 the eighth mapped key
395+
* @param value8 the value mapped at key8
396+
* @param <V1> value1's type
397+
* @param <V2> value2's type
398+
* @param <V3> value3's type
399+
* @param <V4> value4's type
400+
* @param <V5> value5's type
401+
* @param <V6> value6's type
402+
* @param <V7> value7's type
403+
* @param <V8> value8's type
404+
* @return an HMap with the given associations
405+
*/
406+
public static <V1, V2, V3, V4, V5, V6, V7, V8> HMap hMap(TypeSafeKey<?, V1> key1, V1 value1,
407+
TypeSafeKey<?, V2> key2, V2 value2,
408+
TypeSafeKey<?, V3> key3, V3 value3,
409+
TypeSafeKey<?, V4> key4, V4 value4,
410+
TypeSafeKey<?, V5> key5, V5 value5,
411+
TypeSafeKey<?, V6> key6, V6 value6,
412+
TypeSafeKey<?, V7> key7, V7 value7,
413+
TypeSafeKey<?, V8> key8, V8 value8) {
414+
return hMap(key1, value1,
415+
key2, value2,
416+
key3, value3,
417+
key4, value4,
418+
key5, value5,
419+
key6, value6,
420+
key7, value7)
421+
.put(key8, value8);
422+
}
423+
237424
}

src/test/java/com/jnape/palatable/lambda/adt/hmap/HMapTest.java

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,75 @@ public void values() {
191191

192192
@Test
193193
public void convenienceStaticFactoryMethods() {
194-
TypeSafeKey<String, String> stringKey = typeSafeKey();
195-
TypeSafeKey<Integer, Integer> intKey = typeSafeKey();
196-
TypeSafeKey<Float, Float> floatKey = typeSafeKey();
197-
assertEquals(emptyHMap().put(stringKey, "string value"),
194+
TypeSafeKey.Simple<String> stringKey = typeSafeKey();
195+
TypeSafeKey.Simple<Integer> intKey = typeSafeKey();
196+
TypeSafeKey.Simple<Float> floatKey = typeSafeKey();
197+
TypeSafeKey.Simple<Byte> byteKey = typeSafeKey();
198+
TypeSafeKey.Simple<Short> shortKey = typeSafeKey();
199+
TypeSafeKey.Simple<Long> longKey = typeSafeKey();
200+
TypeSafeKey.Simple<Double> doubleKey = typeSafeKey();
201+
TypeSafeKey.Simple<Character> charKey = typeSafeKey();
202+
203+
HMap m1 = emptyHMap().put(stringKey, "string value");
204+
HMap m2 = m1.put(intKey, 1);
205+
HMap m3 = m2.put(floatKey, 1f);
206+
HMap m4 = m3.put(byteKey, (byte) 1);
207+
HMap m5 = m4.put(shortKey, (short) 1);
208+
HMap m6 = m5.put(longKey, 1L);
209+
HMap m7 = m6.put(doubleKey, 1D);
210+
HMap m8 = m7.put(charKey, '1');
211+
212+
assertEquals(m1,
198213
singletonHMap(stringKey, "string value"));
199-
assertEquals(emptyHMap().put(stringKey, "string value").put(intKey, 1),
214+
215+
assertEquals(m2,
200216
hMap(stringKey, "string value",
201217
intKey, 1));
202-
assertEquals(emptyHMap().put(stringKey, "string value").put(intKey, 1).put(floatKey, 1f),
218+
219+
assertEquals(m3,
203220
hMap(stringKey, "string value",
204221
intKey, 1,
205222
floatKey, 1f));
223+
224+
assertEquals(m4,
225+
hMap(stringKey, "string value",
226+
intKey, 1,
227+
floatKey, 1f,
228+
byteKey, (byte) 1));
229+
230+
assertEquals(m5,
231+
hMap(stringKey, "string value",
232+
intKey, 1,
233+
floatKey, 1f,
234+
byteKey, (byte) 1,
235+
shortKey, (short) 1));
236+
237+
assertEquals(m6,
238+
hMap(stringKey, "string value",
239+
intKey, 1,
240+
floatKey, 1f,
241+
byteKey, (byte) 1,
242+
shortKey, (short) 1,
243+
longKey, 1L));
244+
245+
assertEquals(m7,
246+
hMap(stringKey, "string value",
247+
intKey, 1,
248+
floatKey, 1f,
249+
byteKey, (byte) 1,
250+
shortKey, (short) 1,
251+
longKey, 1L,
252+
doubleKey, 1D));
253+
254+
assertEquals(m8,
255+
hMap(stringKey, "string value",
256+
intKey, 1,
257+
floatKey, 1f,
258+
byteKey, (byte) 1,
259+
shortKey, (short) 1,
260+
longKey, 1L,
261+
doubleKey, 1D,
262+
charKey, '1'));
206263
}
207264

208265
@Test

0 commit comments

Comments
 (0)