@Override public Set<E> uniqueSet() { return decorated().uniqueSet(); }
/** * Remove any members of the bag that are not in the given bag, respecting * cardinality. * @see #retainAll(Collection) * * @param other the bag to retain * @return <code>true</code> if this call changed the collection */ boolean retainAll(final Bag<?> other) { boolean result = false; final Bag<E> excess = new HashBag<>(); final Iterator<E> i = uniqueSet().iterator(); while (i.hasNext()) { final E current = i.next(); final int myCount = getCount(current); final int otherCount = other.getCount(current); if (1 <= otherCount && otherCount <= myCount) { excess.add(current, myCount - otherCount); } else { excess.add(current, myCount); } } if (!excess.isEmpty()) { result = removeAll(excess); } return result; }
/** * 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; }
/** * Returns <code>true</code> if the bag contains all elements in the given * collection, respecting cardinality. * * @param other the bag to check against * @return <code>true</code> if the Bag contains all the collection */ boolean containsAll(final Bag<?> other) { final Iterator<?> it = other.uniqueSet().iterator(); while (it.hasNext()) { final Object current = it.next(); if (getCount(current) < other.getCount(current)) { return false; } } return true; }
@Override public int getCount(final Object object) { synchronized (lock) { return getBag().getCount(object); } }
/** * 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<>(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; }
/** * @param specFile to parse * @return all tags used in spec */ public static Collection<String> getTags(File specFile) { try { Bag<String> tags = new HashBag<>(); List<String> lines = FileUtils.readLines(specFile); for (String line : lines) { String trimmedLine = line.trim(); if (StringUtils.startsWith(trimmedLine, PATTERN_LINE_WITH_TAGS_IN_SPEC)) { String tagsAsString = StringUtils.removeStart(trimmedLine, PATTERN_LINE_WITH_TAGS_IN_SPEC); String[] tagsAsArray = tagsAsString.split(","); for (String tag : tagsAsArray) { if (StringUtils.isNotBlank(tag)) { String cleanedTag = tag.trim().replaceAll("[^A-Za-z0-9]*", ""); if (StringUtils.isNotBlank(cleanedTag)) { tags.add(cleanedTag); } } } } } return tags.uniqueSet(); } catch (IOException ex) { throw new GaleniumException("when parsing '" + specFile + "'"); } }
@Override public boolean add(final E object, final int count) { synchronized (lock) { return getBag().add(object, count); } }
@Benchmark public int countsBySuitApacheUnmodifiable(Deck deck) { return deck.apacheCommonsDeckOfCardsAsList.countsBySuit() .size(); }
@Override public boolean remove(final Object object, final int count) { synchronized (lock) { return getBag().remove(object, count); } }
@Override public Iterator<E> iterator() { return UnmodifiableIterator.<E> unmodifiableIterator(decorated().iterator()); }
/** * @param filtersToCombine list of SectionFilters * @return section filter containing all included and excluded tags of the passed filters */ public static SectionFilter getCombinedSectionFilter(SectionFilter... filtersToCombine) { Bag<String> excludedTagBag = new HashBag<String>(); Bag<String> includedTagBag = new HashBag<String>(); for (SectionFilter sectionFilter : filtersToCombine) { excludedTagBag.addAll(sectionFilter.getExcludedTags()); includedTagBag.addAll(sectionFilter.getIncludedTags()); } List<String> excludedTagList = getBagAsUniqueList(excludedTagBag); List<String> includedTagList = getBagAsUniqueList(includedTagBag); List<String> intersection = ListUtils.intersection(includedTagList, excludedTagList); if (intersection.isEmpty()) { return new SectionFilter(includedTagList, excludedTagList); } String intersectionAsString = StringUtils.join(intersection, ", "); throw new GaleniumException("tags in included and excluded collide: [" + intersectionAsString + "]"); }
@Override public int getCount(final Object object) { return getBag().getCount(object); }
@Override public boolean add(final E object, final int count) { return decorated().add(object, count); }
@Benchmark public int countsBySuitApache(Deck deck) { return deck.apacheCommonsDeckOfCards.countsBySuit() .size(); }
@Override public boolean remove(final Object object, final int nCopies) { return getBag().remove(object, nCopies); }
@Override public Set<E> uniqueSet() { return decorated().uniqueSet(); }
@Override public int getCount(final Object object) { return decorated().getCount(object); }
/** * <i>(Change)</i> * Adds <code>count</code> copies of the specified object to the Bag. * <p> * Since this method always increases the size of the bag, it * will always return <code>true</code>. * * @param object the object to add * @param count the number of copies to add * @return <code>true</code>, always */ @Override public boolean add(final E object, final int count) { decorated().add(object, count); return true; }
@Override public boolean remove(final Object object, final int count) { return decorated().remove(object, count); }