/** * Returns a converter based on <i>existing</i> forward and backward functions. Note that it is * unnecessary to create <i>new</i> classes implementing {@code Function} just to pass them in * here. Instead, simply subclass {@code Converter} and implement its {@link #doForward} and * {@link #doBackward} methods directly. * * <p>These functions will never be passed {@code null} and must not under any circumstances * return {@code null}. If a value cannot be converted, the function should throw an unchecked * exception (typically, but not necessarily, {@link IllegalArgumentException}). * * <p>The returned converter is serializable if both provided functions are. * * @since 17.0 */ public static <A, B> Converter<A, B> from( Function<? super A, ? extends B> forwardFunction, Function<? super B, ? extends A> backwardFunction) { return from(forwardFunction, backwardFunction, "Converter.from(" + forwardFunction + ", " + backwardFunction + ")"); }
/** Uses a `Converter<T, R>` to generate a `Converter<Optional<T>, Optional<R>>`. */ public static <T, R> Converter<Optional<T>, Optional<R>> perElementConverterOpt(ConverterNullable<T, R> perElement) { Objects.requireNonNull(perElement); return Converter.from( optT -> optT.map(perElement::convert), optR -> optR.map(perElement::revert), "perElement=" + perElement); }
/** Uses a `Converter<T, R>` to generate a `Converter<ImmutableSet<T>, ImmutableSet<R>>`. */ public static <T, R> Converter<ImmutableSet<T>, ImmutableSet<R>> perElementConverterSet(ConverterNullable<T, R> perElement) { Objects.requireNonNull(perElement); return Converter.from( setOfT -> perElementMutateSet(setOfT, perElement::convert), setOfR -> perElementMutateSet(setOfR, perElement::revert), perElement.toString()); }
/** Uses a `Converter<T, R>` to generate a `Converter<ImmutableList<T>, ImmutableList<R>>`. */ public static <T, R> Converter<ImmutableList<T>, ImmutableList<R>> perElementConverterList(ConverterNullable<T, R> perElement) { Objects.requireNonNull(perElement); return Converter.from( setOfT -> perElementMutateList(setOfT, perElement::convert), setOfR -> perElementMutateList(setOfR, perElement::revert), perElement.toString()); }
/** Manipulates the selection of the given viewer with the given `RxBox<ImmutableSet>`. */ public static <T> void multiSelectionSet(StructuredViewer viewer, RxBox<ImmutableSet<T>> box) { Converter<ImmutableSet<T>, ImmutableList<T>> converter = Converter.from( ImmutableSet::asList, ImmutableSet::copyOf, "setToList"); multiSelectionList(viewer, box.map(converter)); }