@Override public V getOrElse(K key, V defaultValue) { return get(key).getOrElse(defaultValue); }
/** * Returns this {@code TreeMap} if it is nonempty, * otherwise {@code TreeMap} created from iterable, using existing comparator. * * @param other An alternative {@code Traversable} * @return this {@code TreeMap} if it is nonempty, * otherwise {@code TreeMap} created from iterable, using existing comparator. */ @Override public TreeMap<K, V> orElse(Iterable<? extends Tuple2<K, V>> other) { return isEmpty() ? ofEntries(comparator(), other) : this; }
/** * 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); }
@Override public java.util.TreeMap<K, V> toJavaMap() { return toJavaMap(() -> new java.util.TreeMap<>(comparator()), t -> t); }
/** * 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#ofEntries(Tuple2...)} * * @param <K> The key type. * @param <V> The value type. * @param entries Map entries. * @return A new {@link TreeMap} instance containing the given entries * @deprecated Will be removed in a future version. */ @Deprecated @SuppressWarnings("varargs") @SafeVarargs public static <K extends Comparable<? super K>, V> SortedMap<K, V> SortedMap(Tuple2<? extends K, ? extends V>... entries) { return TreeMap.ofEntries(entries); }
/** * Alias for {@link TreeMap#empty(Comparator)} * * @param <K> The key type. * @param <V> The value type. * @param keyComparator The comparator used to sort the entries by their key * @return A new empty {@link TreeMap} instance */ public static <K, V> SortedMap<K, V> SortedMap(Comparator<? super K> keyComparator) { return TreeMap.empty(keyComparator); }
@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"); }
@SuppressWarnings("unchecked") private static <K, V> TreeMap<K, V> createFromTuple(EntryComparator<K, V> entryComparator, Tuple2<? extends K, ? extends V> entry) { Objects.requireNonNull(entry, "entry is null"); return new TreeMap<>(RedBlackTree.of(entryComparator, (Tuple2<K, V>) entry)); }
/** * Define rel semantics for this representation. * * @param rel A defined relationship type */ public ResourceRepresentation<V> withRel(Rel rel) { if (rels.containsKey(rel.rel())) { throw new IllegalStateException(String.format("Rel %s is already declared.", rel.rel())); } final TreeMap<String, Rel> updatedRels = rels.put(rel.rel(), rel); return new ResourceRepresentation<>( content, links, updatedRels, namespaceManager, value, resources); }
@Override public TreeMap<K, V> retainAll(Iterable<? extends Tuple2<K, V>> elements) { Objects.requireNonNull(elements, "elements is null"); RedBlackTree<Tuple2<K, V>> tree = RedBlackTree.empty(entries.comparator()); for (Tuple2<K, V> entry : elements) { if (contains(entry)) { tree = tree.insert(entry); } } return new TreeMap<>(tree); }
@Override public <W> TreeMap<K, W> mapValues(Function<? super V, ? extends W> valueMapper) { Objects.requireNonNull(valueMapper, "valueMapper is null"); return map(comparator(), (k, v) -> Tuple.of(k, valueMapper.apply(v))); }
@Override public SortedSet<K> keySet() { return TreeSet.ofAll(comparator(), iterator().map(Tuple2::_1)); }
@Override public <K2, V2> TreeMap<K2, V2> bimap(Comparator<? super K2> keyComparator, Function<? super K, ? extends K2> keyMapper, Function<? super V, ? extends V2> valueMapper) { return bimap(this, EntryComparator.of(keyComparator), keyMapper, valueMapper); }
public ResourceRepresentation<V> withRepresentation( String rel, ResourceRepresentation<?> resource) { if (resources.containsValue(resource)) { throw new IllegalStateException("Resource is already embedded."); } Support.checkRelType(rel); validateSingletonRel(rel); Multimap<String, ResourceRepresentation<?>> updatedResources = resources.put(rel, resource); ResourceRepresentation<V> updatedRepresentation = new ResourceRepresentation<>( content, links, rels, namespaceManager, value, updatedResources); // Propagate null property flag to parent. if (resource.hasNullProperties()) { updatedRepresentation.hasNullProperties = true; } if (!rels.containsKey(rel)) { updatedRepresentation = updatedRepresentation.withRel(Rels.natural(rel)); } return updatedRepresentation; }
/** * 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#ofEntries(Comparator, Tuple2...)} * * @param <K> The key type. * @param <V> The value type. * @param keyComparator The comparator used to sort the entries by their key * @param entries Map entries. * @return A new {@link TreeMap} instance containing the given entry * @deprecated Will be removed in a future version. */ @Deprecated @SuppressWarnings("varargs") @SafeVarargs public static <K, V> SortedMap<K, V> SortedMap(Comparator<? super K> keyComparator, Tuple2<? extends K, ? extends V>... entries) { return TreeMap.ofEntries(keyComparator, entries); }
/** * Alias for {@link TreeMap#empty()} * * @param <K> The key type. * @param <V> The value type. * @return A new empty {@link TreeMap} instance */ public static <K extends Comparable<? super K>, V> SortedMap<K, V> SortedMap() { return TreeMap.empty(); }
@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); }