/** * Alias for {@link TreeMap#of(Comparable, Object)} * * @param <K> The key type. * @param <V> The value type. * @param k1 The key * @param v1 The value * @return A new {@link TreeMap} instance containing the given entries */ public static <K extends Comparable<? super K>, V> SortedMap<K, V> SortedMap(K k1, V v1) { return TreeMap.of(k1, v1); }
/** * Converts this to a {@link Map}. * * @param comparator A comparator that induces an order of the Map keys. * @param f A function that maps an element to a key/value pair represented by Tuple2 * @param <K> The key type * @param <V> The value type * @return A new {@link TreeMap}. */ default <K, V> SortedMap<K, V> toSortedMap(Comparator<? super K> comparator, Function<? super T, ? extends Tuple2<? extends K, ? extends V>> f) { Objects.requireNonNull(comparator, "comparator is null"); Objects.requireNonNull(f, "f is null"); final Function<Tuple2<? extends K, ? extends V>, SortedMap<K, V>> ofElement = t -> TreeMap.of(comparator, t); final Function<Iterable<Tuple2<? extends K, ? extends V>>, SortedMap<K, V>> ofAll = t -> TreeMap.ofEntries(comparator, t); return ValueModule.toMap(this, TreeMap.empty(comparator), ofElement, ofAll, f); }
/** * Alias for {@link TreeMap#of(Comparator, Object, Object)} * * @param <K> The key type. * @param <V> The value type. * @param keyComparator The comparator used to sort the entries by their key * @param key A singleton map key. * @param value A singleton map value. * @return A new {@link TreeMap} instance containing the given entry */ public static <K, V> SortedMap<K, V> SortedMap(Comparator<? super K> keyComparator, K key, V value) { return TreeMap.of(keyComparator, key, value); }
/** * Converts this to a {@link Map}. * * @param comparator A comparator that induces an order of the Map keys. * @param f A function that maps an element to a key/value pair represented by Tuple2 * @param <K> The key type * @param <V> The value type * @return A new {@link TreeMap}. */ default <K, V> SortedMap<K, V> toSortedMap(Comparator<? super K> comparator, Function<? super T, ? extends Tuple2<? extends K, ? extends V>> f) { Objects.requireNonNull(comparator, "comparator is null"); Objects.requireNonNull(f, "f is null"); final Function<Tuple2<? extends K, ? extends V>, SortedMap<K, V>> ofElement = t -> TreeMap.of(comparator, t); final Function<Iterable<Tuple2<? extends K, ? extends V>>, SortedMap<K, V>> ofAll = t -> TreeMap.ofEntries(comparator, t); return ValueModule.toMap(this, TreeMap.empty(comparator), ofElement, ofAll, f); }
/** * Alias for {@link TreeMap#of(Comparable, Object, Comparable, Object)} * * @param <K> The key type. * @param <V> The value type. * @param k1 The key of the 1st pair * @param v1 The value of the 1st pair * @param k2 The key of the 2nd pair * @param v2 The value of the 2nd pair * @return A new {@link TreeMap} instance containing the given entries */ public static <K extends Comparable<? super K>, V> SortedMap<K, V> SortedMap(K k1, V v1, K k2, V v2) { return TreeMap.of(k1, v1, k2, v2); }
/** * Alias for {@link TreeMap#of(Comparable, Object, Comparable, Object, Comparable, Object)} * * @param <K> The key type. * @param <V> The value type. * @param k1 The key of the 1st pair * @param v1 The value of the 1st pair * @param k2 The key of the 2nd pair * @param v2 The value of the 2nd pair * @param k3 The key of the 3rd pair * @param v3 The value of the 3rd pair * @return A new {@link TreeMap} instance containing the given entries */ public static <K extends Comparable<? super K>, V> SortedMap<K, V> SortedMap(K k1, V v1, K k2, V v2, K k3, V v3) { return TreeMap.of(k1, v1, k2, v2, k3, v3); }
/** * Alias for {@link TreeMap#of(Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object)} * * @param <K> The key type. * @param <V> The value type. * @param k1 The key of the 1st pair * @param v1 The value of the 1st pair * @param k2 The key of the 2nd pair * @param v2 The value of the 2nd pair * @param k3 The key of the 3rd pair * @param v3 The value of the 3rd pair * @param k4 The key of the 4th pair * @param v4 The value of the 4th pair * @return A new {@link TreeMap} instance containing the given entries */ public static <K extends Comparable<? super K>, V> SortedMap<K, V> SortedMap(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { return TreeMap.of(k1, v1, k2, v2, k3, v3, k4, v4); }
/** * Alias for {@link TreeMap#of(Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object)} * * @param <K> The key type. * @param <V> The value type. * @param k1 The key of the 1st pair * @param v1 The value of the 1st pair * @param k2 The key of the 2nd pair * @param v2 The value of the 2nd pair * @param k3 The key of the 3rd pair * @param v3 The value of the 3rd pair * @param k4 The key of the 4th pair * @param v4 The value of the 4th pair * @param k5 The key of the 5th pair * @param v5 The value of the 5th pair * @return A new {@link TreeMap} instance containing the given entries */ public static <K extends Comparable<? super K>, V> SortedMap<K, V> SortedMap(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) { return TreeMap.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5); }
/** * Alias for {@link TreeMap#of(Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object)} * * @param <K> The key type. * @param <V> The value type. * @param k1 The key of the 1st pair * @param v1 The value of the 1st pair * @param k2 The key of the 2nd pair * @param v2 The value of the 2nd pair * @param k3 The key of the 3rd pair * @param v3 The value of the 3rd pair * @param k4 The key of the 4th pair * @param v4 The value of the 4th pair * @param k5 The key of the 5th pair * @param v5 The value of the 5th pair * @param k6 The key of the 6th pair * @param v6 The value of the 6th pair * @return A new {@link TreeMap} instance containing the given entries */ public static <K extends Comparable<? super K>, V> SortedMap<K, V> SortedMap(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) { return TreeMap.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6); }
/** * Alias for {@link TreeMap#of(Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object)} * * @param <K> The key type. * @param <V> The value type. * @param k1 The key of the 1st pair * @param v1 The value of the 1st pair * @param k2 The key of the 2nd pair * @param v2 The value of the 2nd pair * @param k3 The key of the 3rd pair * @param v3 The value of the 3rd pair * @param k4 The key of the 4th pair * @param v4 The value of the 4th pair * @param k5 The key of the 5th pair * @param v5 The value of the 5th pair * @param k6 The key of the 6th pair * @param v6 The value of the 6th pair * @param k7 The key of the 7th pair * @param v7 The value of the 7th pair * @return A new {@link TreeMap} instance containing the given entries */ public static <K extends Comparable<? super K>, V> SortedMap<K, V> SortedMap(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) { return TreeMap.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7); }
/** * Alias for {@link TreeMap#of(Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object)} * * @param <K> The key type. * @param <V> The value type. * @param k1 The key of the 1st pair * @param v1 The value of the 1st pair * @param k2 The key of the 2nd pair * @param v2 The value of the 2nd pair * @param k3 The key of the 3rd pair * @param v3 The value of the 3rd pair * @param k4 The key of the 4th pair * @param v4 The value of the 4th pair * @param k5 The key of the 5th pair * @param v5 The value of the 5th pair * @param k6 The key of the 6th pair * @param v6 The value of the 6th pair * @param k7 The key of the 7th pair * @param v7 The value of the 7th pair * @param k8 The key of the 8th pair * @param v8 The value of the 8th pair * @return A new {@link TreeMap} instance containing the given entries */ public static <K extends Comparable<? super K>, V> SortedMap<K, V> SortedMap(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8) { return TreeMap.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8); }
/** * Alias for {@link TreeMap#of(Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object)} * * @param <K> The key type. * @param <V> The value type. * @param k1 The key of the 1st pair * @param v1 The value of the 1st pair * @param k2 The key of the 2nd pair * @param v2 The value of the 2nd pair * @param k3 The key of the 3rd pair * @param v3 The value of the 3rd pair * @param k4 The key of the 4th pair * @param v4 The value of the 4th pair * @param k5 The key of the 5th pair * @param v5 The value of the 5th pair * @param k6 The key of the 6th pair * @param v6 The value of the 6th pair * @param k7 The key of the 7th pair * @param v7 The value of the 7th pair * @param k8 The key of the 8th pair * @param v8 The value of the 8th pair * @param k9 The key of the 9th pair * @param v9 The value of the 9th pair * @return A new {@link TreeMap} instance containing the given entries */ public static <K extends Comparable<? super K>, V> SortedMap<K, V> SortedMap(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) { return TreeMap.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9); }
/** * Alias for {@link TreeMap#of(Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object, Comparable, Object)} * * @param <K> The key type. * @param <V> The value type. * @param k1 The key of the 1st pair * @param v1 The value of the 1st pair * @param k2 The key of the 2nd pair * @param v2 The value of the 2nd pair * @param k3 The key of the 3rd pair * @param v3 The value of the 3rd pair * @param k4 The key of the 4th pair * @param v4 The value of the 4th pair * @param k5 The key of the 5th pair * @param v5 The value of the 5th pair * @param k6 The key of the 6th pair * @param v6 The value of the 6th pair * @param k7 The key of the 7th pair * @param v7 The value of the 7th pair * @param k8 The key of the 8th pair * @param v8 The value of the 8th pair * @param k9 The key of the 9th pair * @param v9 The value of the 9th pair * @param k10 The key of the 10th pair * @param v10 The value of the 10th pair * @return A new {@link TreeMap} instance containing the given entries */ public static <K extends Comparable<? super K>, V> SortedMap<K, V> SortedMap(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) { return TreeMap.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9, k10, v10); }
/** * Alias for {@link TreeMap#of(Comparable, Object)} * * @param <K> The key type. * @param <V> The value type. * @param k1 The key * @param v1 The value * @return A new {@link TreeMap} instance containing the given entries */ public static <K extends Comparable<? super K>, V> SortedMap<K, V> SortedMap(K k1, V v1) { return TreeMap.of(k1, v1); }
/** * Alias for {@link TreeMap#of(Comparator, Object, Object)} * * @param <K> The key type. * @param <V> The value type. * @param keyComparator The comparator used to sort the entries by their key * @param key A singleton map key. * @param value A singleton map value. * @return A new {@link TreeMap} instance containing the given entry */ public static <K, V> SortedMap<K, V> SortedMap(Comparator<? super K> keyComparator, K key, V value) { return TreeMap.of(keyComparator, key, value); }
/** * Alias for {@link TreeMap#of(Comparable, Object, Comparable, Object)} * * @param <K> The key type. * @param <V> The value type. * @param k1 The key of the 1st pair * @param v1 The value of the 1st pair * @param k2 The key of the 2nd pair * @param v2 The value of the 2nd pair * @return A new {@link TreeMap} instance containing the given entries */ public static <K extends Comparable<? super K>, V> SortedMap<K, V> SortedMap(K k1, V v1, K k2, V v2) { return TreeMap.of(k1, v1, k2, v2); }
/** * Alias for {@link TreeMap#of(Comparable, Object, Comparable, Object, Comparable, Object)} * * @param <K> The key type. * @param <V> The value type. * @param k1 The key of the 1st pair * @param v1 The value of the 1st pair * @param k2 The key of the 2nd pair * @param v2 The value of the 2nd pair * @param k3 The key of the 3rd pair * @param v3 The value of the 3rd pair * @return A new {@link TreeMap} instance containing the given entries */ public static <K extends Comparable<? super K>, V> SortedMap<K, V> SortedMap(K k1, V v1, K k2, V v2, K k3, V v3) { return TreeMap.of(k1, v1, k2, v2, k3, v3); }
@Test public void testDeserializeToSortedMap() throws IOException { Clazz c = new Clazz(); c.setSet(TreeMap.of(1, 3, 5, 7)); Clazz dc = mapper().readValue(mapper().writeValueAsString(c), Clazz.class); assertEquals(c, dc); } }
@Test public void testTreeMap() throws Exception { TreeMap<String, I> src = TreeMap.of("a", new A(), "b", new B()); String json = MAPPER.writeValueAsString(new TreeMapPojo().setValue(src)); Assert.assertEquals(json, "{\"value\":{\"a\":{\"type\":\"a\"},\"b\":{\"type\":\"b\"}}}"); TreeMapPojo pojo = MAPPER.readValue(json, TreeMapPojo.class); TreeMap<String, I> restored = pojo.getValue(); Assert.assertTrue(restored.get("a").get() instanceof A); Assert.assertTrue(restored.get("b").get() instanceof B); }
@Test public void testTreeMap() throws Exception { TreeMap<String, A> src = TreeMap.of("a", new B("a", "b")); String json = MAPPER.writeValueAsString(new TreeMapPojo().setValue(src)); Assert.assertEquals(json, "{\"value\":{\"a\":{\"ExtFieldsPojoTest$B\":{\"a\":\"a\",\"b\":\"b\"}}}}"); TreeMapPojo pojo = MAPPER.readValue(json, TreeMapPojo.class); TreeMap<String, A> restored = pojo.getValue(); Assert.assertTrue(restored.get("a").get() instanceof B); Assert.assertEquals(restored.get("a").get().a, "a"); Assert.assertEquals(((B) restored.get("a").get()).b, "b"); }