/** * Compares this Bag to another. This Bag equals another Bag if it contains * the same number of occurrences of the same elements. * * @param object the Bag to compare to * @return true if equal */ @Override public boolean equals(final Object object) { if (object == this) { return true; } if (object instanceof Bag == false) { return false; } final Bag<?> other = (Bag<?>) object; if (other.size() != size()) { return false; } for (final E element : map.keySet()) { if (other.getCount(element) != getCount(element)) { return false; } } return true; }
/** * Factory method to create a transforming bag that will transform * existing contents of the specified bag. * <p> * If there are any elements already in the bag being decorated, they * will be transformed by this method. * Contrast this with {@link #transformingBag(Bag, Transformer)}. * * @param <E> the type of the elements in the bag * @param bag the bag to decorate, must not be null * @param transformer the transformer to use for conversion, must not be null * @return a new transformed Bag * @throws NullPointerException if bag or transformer is null * @since 4.0 */ public static <E> Bag<E> transformedBag(final Bag<E> bag, final Transformer<? super E, ? extends E> transformer) { final TransformedBag<E> decorated = new TransformedBag<E>(bag, transformer); if (bag.size() > 0) { @SuppressWarnings("unchecked") // Bag is of type E final E[] values = (E[]) bag.toArray(); // NOPMD - false positive for generics bag.clear(); for (final E value : values) { decorated.decorated().add(transformer.transform(value)); } } return decorated; }