/** * Factory method to create a transforming set. * <p> * If there are any elements already in the set being decorated, they * are NOT transformed. * Contrast this with {@link #transformedSet(Set, Transformer)}. * * @param <E> the element type * @param set the set to decorate, must not be null * @param transformer the transformer to use for conversion, must not be null * @return a new transformed set * @throws NullPointerException if set or transformer is null * @since 4.0 */ public static <E> TransformedSet<E> transformingSet(final Set<E> set, final Transformer<? super E, ? extends E> transformer) { return new TransformedSet<>(set, transformer); }
@Override public int hashCode() { return decorated().hashCode(); }
/** * Returns a transformed set backed by the given set. * <p> * Each object is passed through the transformer as it is added to the * Set. It is important not to use the original set after invoking this * method, as it is a backdoor for adding untransformed objects. * <p> * Existing entries in the specified set will not be transformed. * If you want that behaviour, see {@link TransformedSet#transformedSet}. * * @param <E> the element type * @param set the set to transform, must not be null * @param transformer the transformer for the set, must not be null * @return a transformed set backed by the given set * @throws NullPointerException if the set or transformer is null */ public static <E> Set<E> transformedSet(final Set<E> set, final Transformer<? super E, ? extends E> transformer) { return TransformedSet.transformingSet(set, transformer); }
/** * Factory method to create a transforming set that will transform * existing contents of the specified set. * <p> * If there are any elements already in the set being decorated, they * will be transformed by this method. * Contrast this with {@link #transformingSet(Set, Transformer)}. * * @param <E> the element type * @param set the set to decorate, must not be null * @param transformer the transformer to use for conversion, must not be null * @return a new transformed set * @throws NullPointerException if set or transformer is null * @since 4.0 */ public static <E> Set<E> transformedSet(final Set<E> set, final Transformer<? super E, ? extends E> transformer) { final TransformedSet<E> decorated = new TransformedSet<>(set, transformer); if (set.size() > 0) { @SuppressWarnings("unchecked") // set is type E final E[] values = (E[]) set.toArray(); // NOPMD - false positive for generics set.clear(); for (final E value : values) { decorated.decorated().add(transformer.transform(value)); } } return decorated; }
@Override public Set<E> uniqueSet() { final Set<E> set = getBag().uniqueSet(); return TransformedSet.<E>transformingSet(set, transformer); }
@Override public boolean equals(final Object object) { return object == this || decorated().equals(object); }