/** * Alias for {@link TreeSet#of(Comparable)} * * @param <T> Component type of element. * @param element An element. * @return A new {@link TreeSet} instance containing the given element */ public static <T extends Comparable<? super T>> SortedSet<T> SortedSet(T element) { return TreeSet.of(element); }
/** * Converts this to a {@link SortedSet}. * * @param comparator A comparator that induces an order of the SortedSet elements. * @return A new {@link TreeSet}. */ default SortedSet<T> toSortedSet(Comparator<? super T> comparator) { Objects.requireNonNull(comparator, "comparator is null"); return ValueModule.toTraversable(this, TreeSet.empty(comparator), value -> TreeSet.of(comparator, value), values -> TreeSet.ofAll(comparator, values)); }
/** * Converts this to a {@link SortedSet}. * * @param comparator A comparator that induces an order of the SortedSet elements. * @return A new {@link TreeSet}. */ default SortedSet<T> toSortedSet(Comparator<? super T> comparator) { Objects.requireNonNull(comparator, "comparator is null"); return ValueModule.toTraversable(this, TreeSet.empty(comparator), value -> TreeSet.of(comparator, value), values -> TreeSet.ofAll(comparator, values)); }
/** * Alias for {@link TreeSet#of(Comparator, Object)} * * @param <T> Component type of element. * @param comparator The comparator used to sort the elements * @param element An element. * @return A new {@link TreeSet} instance containing the given element */ public static <T> SortedSet<T> SortedSet(Comparator<? super T> comparator, T element) { return TreeSet.of(comparator, element); }
/** * Alias for {@link TreeSet#of(Comparable...)} * * @param <T> Component type of element. * @param elements Zero or more elements. * @return A new {@link TreeSet} instance containing the given elements */ @SuppressWarnings("varargs") @SafeVarargs public static <T extends Comparable<? super T>> SortedSet<T> SortedSet(T... elements) { return TreeSet.of(elements); }
/** * Alias for {@link TreeSet#of(Comparator, Object...)} * * @param <T> Component type of element. * @param comparator The comparator used to sort the elements * @param elements Zero or more elements. * @return A new {@link TreeSet} instance containing the given elements */ @SuppressWarnings("varargs") @SafeVarargs public static <T> SortedSet<T> SortedSet(Comparator<? super T> comparator, T... elements) { return TreeSet.of(comparator, elements); }
@SuppressWarnings("varargs") @SafeVarargs public static <T extends Comparable<? super T>> TreeSet<T> of(T... values) { return TreeSet.<T> of(Comparators.naturalComparator(), values); }
public static <T extends Comparable<? super T>> TreeSet<T> of(T value) { return of(Comparators.naturalComparator(), value); }
/** * Returns a TreeSet containing tuples returned by {@code n} calls to a given Supplier {@code s}. * * @param <T> Component type of the TreeSet * @param comparator The comparator used to sort the elements * @param n The number of elements in the TreeSet * @param s The Supplier computing element values * @return A TreeSet of size {@code n}, where each element contains the result supplied by {@code s}. * @throws NullPointerException if {@code comparator} or {@code s} are null */ public static <T> TreeSet<T> fill(Comparator<? super T> comparator, int n, Supplier<? extends T> s) { Objects.requireNonNull(comparator, "comparator is null"); Objects.requireNonNull(s, "s is null"); return Collections.fill(n, s, TreeSet.empty(comparator), values -> of(comparator, values)); }
/** * Returns a TreeSet containing {@code n} values of a given Function {@code f} * over a range of integer values from 0 to {@code n - 1}. * * @param <T> Component type of the TreeSet * @param comparator The comparator used to sort the elements * @param n The number of elements in the TreeSet * @param f The Function computing element values * @return A TreeSet consisting of elements {@code f(0),f(1), ..., f(n - 1)} * @throws NullPointerException if {@code comparator} or {@code f} are null */ public static <T> TreeSet<T> tabulate(Comparator<? super T> comparator, int n, Function<? super Integer, ? extends T> f) { Objects.requireNonNull(comparator, "comparator is null"); Objects.requireNonNull(f, "f is null"); return Collections.tabulate(n, f, TreeSet.empty(comparator), values -> of(comparator, values)); }
/** * Alias for {@link TreeSet#of(Comparator, Object)} * * @param <T> Component type of element. * @param comparator The comparator used to sort the elements * @param element An element. * @return A new {@link TreeSet} instance containing the given element */ public static <T> SortedSet<T> SortedSet(Comparator<? super T> comparator, T element) { return TreeSet.of(comparator, element); }
/** * Alias for {@link TreeSet#of(Comparable)} * * @param <T> Component type of element. * @param element An element. * @return A new {@link TreeSet} instance containing the given element */ public static <T extends Comparable<? super T>> SortedSet<T> SortedSet(T element) { return TreeSet.of(element); }
/** * Alias for {@link TreeSet#of(Comparable...)} * * @param <T> Component type of element. * @param elements Zero or more elements. * @return A new {@link TreeSet} instance containing the given elements */ @SuppressWarnings("varargs") @SafeVarargs public static <T extends Comparable<? super T>> SortedSet<T> SortedSet(T... elements) { return TreeSet.of(elements); }
/** * Alias for {@link TreeSet#of(Comparator, Object...)} * * @param <T> Component type of element. * @param comparator The comparator used to sort the elements * @param elements Zero or more elements. * @return A new {@link TreeSet} instance containing the given elements */ @SuppressWarnings("varargs") @SafeVarargs public static <T> SortedSet<T> SortedSet(Comparator<? super T> comparator, T... elements) { return TreeSet.of(comparator, elements); }
@SuppressWarnings("varargs") @SafeVarargs public static <T extends Comparable<? super T>> TreeSet<T> of(T... values) { return TreeSet.<T> of(Comparators.naturalComparator(), values); }
@Test public void testTreeSetOfTuple() throws Exception { String src00 = "A"; String src01 = "B"; Tuple2<String, String> src0 = Tuple.of(src00, src01); TreeSet<Tuple2<String, String>> src = TreeSet.of(src0); String json = MAPPER.writeValueAsString(new ParameterizedTreeSetPojo<>(src)); Assert.assertEquals(json, "{\"value\":[[\"A\",\"B\"]]}"); ParameterizedTreeSetPojo<io.vavr.Tuple2<java.lang.String, java.lang.String>> restored = MAPPER.readValue(json, new TypeReference<ParameterizedTreeSetPojo<io.vavr.Tuple2<java.lang.String, java.lang.String>>>(){}); Assert.assertEquals(src, restored.getValue()); }
@Test public void testDeserializeToSortedSet() throws IOException { Clazz c = new Clazz(); c.setSet(TreeSet.of(1, 3, 5)); Clazz dc = mapper().readValue(mapper().writeValueAsString(c), Clazz.class); assertEquals(c, dc); }
@Test public void testTreeSetOfString() throws Exception { String src0 = "A"; String src1 = "B"; String src2 = "C"; TreeSet<String> src = TreeSet.of(src0, src1, src2); String json = MAPPER.writeValueAsString(new TreeSetOfString().setValue(src)); Assert.assertEquals(json, "{\"value\":[\"A\",\"B\",\"C\"]}"); TreeSetOfString restored = MAPPER.readValue(json, TreeSetOfString.class); Assert.assertEquals(src, restored.getValue()); }
@Test public void testTreeSet() throws Exception { TreeSet<I> src = TreeSet.of(new B()); String json = MAPPER.writeValueAsString(new TreeSetPojo().setValue(src)); Assert.assertEquals(json, "{\"value\":[{\"type\":\"b\"}]}"); TreeSetPojo pojo = MAPPER.readValue(json, TreeSetPojo.class); TreeSet<I> restored = pojo.getValue(); Assert.assertEquals(restored.filter(e -> e instanceof B).length(), 1); }
@Test public void testTreeSet() throws Exception { TreeSet<A> src = TreeSet.of(new B("a", "b")); String json = MAPPER.writeValueAsString(new TreeSetPojo().setValue(src)); Assert.assertEquals(json, "{\"value\":[{\"ExtFieldsPojoTest$B\":{\"a\":\"a\",\"b\":\"b\"}}]}"); TreeSetPojo pojo = MAPPER.readValue(json, TreeSetPojo.class); TreeSet<A> restored = pojo.getValue(); Assert.assertEquals(restored.filter(e -> e instanceof B).length(), 1); Assert.assertEquals(restored.head().a, "a"); Assert.assertEquals(((B) restored.head()).b, "b"); }