/** * Sets the 2nd element of this tuple to the given {@code value}. * * @param value the new value * @return a copy of this tuple with a new value for the 2nd element of this Tuple. */ public Tuple2<T1, T2> update2(T2 value) { return new Tuple2<>(_1, value); }
/** * Creates a tuple of two elements. * * @param <T1> type of the 1st element * @param <T2> type of the 2nd element * @param t1 the 1st element * @param t2 the 2nd element * @return a tuple of two elements. */ static <T1, T2> Tuple2<T1, T2> of(T1 t1, T2 t2) { return new Tuple2<>(t1, t2); }
/** * Sets the 1st element of this tuple to the given {@code value}. * * @param value the new value * @return a copy of this tuple with a new value for the 1st element of this Tuple. */ public Tuple2<T1, T2> update1(T1 value) { return new Tuple2<>(value, _2); }
/** * Creates a {@code Tuple2} from a {@link Map.Entry}. * * @param <T1> Type of first component (entry key) * @param <T2> Type of second component (entry value) * @param entry A {@link java.util.Map.Entry} * @return a new {@code Tuple2} containing key and value of the given {@code entry} */ static <T1, T2> Tuple2<T1, T2> fromEntry(Map.Entry<? extends T1, ? extends T2> entry) { Objects.requireNonNull(entry, "entry is null"); return new Tuple2<>(entry.getKey(), entry.getValue()); }
@Override public TreeMap<K, V> put(K key, V value) { return new TreeMap<>(entries.insert(new Tuple2<>(key, value))); }
@Override public boolean containsKey(K key) { return entries.contains(new Tuple2<>(key, /*ignored*/null)); }
@Override public Option<V> get(K key) { final V ignored = null; return entries.find(new Tuple2<>(key, ignored)).map(Tuple2::_2); }
/** * Turns a sequence of {@code Tuple2} into a Tuple2 of {@code Seq}s. * * @param <T1> 1st component type * @param <T2> 2nd component type * @param tuples an {@code Iterable} of tuples * @return a tuple of two {@link Seq}s. */ static <T1, T2> Tuple2<Seq<T1>, Seq<T2>> sequence2(Iterable<? extends Tuple2<? extends T1, ? extends T2>> tuples) { Objects.requireNonNull(tuples, "tuples is null"); final Stream<Tuple2<? extends T1, ? extends T2>> s = Stream.ofAll(tuples); return new Tuple2<>(s.map(Tuple2::_1), s.map(Tuple2::_2)); }
@Override public TreeMap<K, V> remove(K key) { final V ignored = null; final Tuple2<K, V> entry = new Tuple2<>(key, ignored); if (entries.contains(entry)) { return new TreeMap<>(entries.delete(entry)); } else { return this; } }
@Override public TreeMap<K, V> removeAll(Iterable<? extends K> keys) { final V ignored = null; RedBlackTree<Tuple2<K, V>> removed = entries; for (K key : keys) { final Tuple2<K, V> entry = new Tuple2<>(key, ignored); if (removed.contains(entry)) { removed = removed.delete(entry); } } if (removed.size() == entries.size()) { return this; } else { return new TreeMap<>(removed); } }
@Test public void testMapToTuple2ListShouldSucceed() { List<Tuple2<Integer, String>> expectedTuples = expected.map(i -> new Tuple2<>(i, "t2" + i)); java.util.List<Tuple2<Integer, String>> tupleProjection = dbRule.getSharedHandle() .createQuery("select t1, t2 from tuples") .mapTo(new GenericType<Tuple2<Integer, String>>() {}).list(); assertThat(tupleProjection).containsOnlyElementsOf(expectedTuples); }
/** * Sets the 1st element of this tuple to the given {@code value}. * * @param value the new value * @return a copy of this tuple with a new value for the 1st element of this Tuple. */ public Tuple2<T1, T2> update1(T1 value) { return new Tuple2<>(value, _2); }
/** * Creates a tuple of two elements. * * @param <T1> type of the 1st element * @param <T2> type of the 2nd element * @param t1 the 1st element * @param t2 the 2nd element * @return a tuple of two elements. */ static <T1, T2> Tuple2<T1, T2> of(T1 t1, T2 t2) { return new Tuple2<>(t1, t2); }
/** * Sets the 2nd element of this tuple to the given {@code value}. * * @param value the new value * @return a copy of this tuple with a new value for the 2nd element of this Tuple. */ public Tuple2<T1, T2> update2(T2 value) { return new Tuple2<>(_1, value); }
/** * Creates a {@code Tuple2} from a {@link Map.Entry}. * * @param <T1> Type of first component (entry key) * @param <T2> Type of second component (entry value) * @param entry A {@link java.util.Map.Entry} * @return a new {@code Tuple2} containing key and value of the given {@code entry} */ static <T1, T2> Tuple2<T1, T2> fromEntry(Map.Entry<? extends T1, ? extends T2> entry) { Objects.requireNonNull(entry, "entry is null"); return new Tuple2<>(entry.getKey(), entry.getValue()); }
@Override public boolean containsKey(K key) { return entries.contains(new Tuple2<>(key, /*ignored*/null)); }
@Override public Option<V> get(K key) { final V ignored = null; return entries.find(new Tuple2<>(key, ignored)).map(Tuple2::_2); }
/** * Turns a sequence of {@code Tuple2} into a Tuple2 of {@code Seq}s. * * @param <T1> 1st component type * @param <T2> 2nd component type * @param tuples an {@code Iterable} of tuples * @return a tuple of two {@link Seq}s. */ static <T1, T2> Tuple2<Seq<T1>, Seq<T2>> sequence2(Iterable<? extends Tuple2<? extends T1, ? extends T2>> tuples) { Objects.requireNonNull(tuples, "tuples is null"); final Stream<Tuple2<? extends T1, ? extends T2>> s = Stream.ofAll(tuples); return new Tuple2<>(s.map(Tuple2::_1), s.map(Tuple2::_2)); }
@Override public TreeMap<K, V> remove(K key) { final V ignored = null; final Tuple2<K, V> entry = new Tuple2<>(key, ignored); if (entries.contains(entry)) { return new TreeMap<>(entries.delete(entry)); } else { return this; } }
@Test public void testTuple2Class() throws Exception { Tuple2Class src = new Tuple2Class(new Tuple2<>(42, new ImplementedClass())); String json = MAPPER.writeValueAsString(src); Tuple2Class restored = MAPPER.readValue(json, Tuple2Class.class); Assert.assertEquals(restored.value._2.getClass(), ImplementedClass.class); }