Example usage for org.apache.commons.collections15 Bag add

List of usage examples for org.apache.commons.collections15 Bag add

Introduction

In this page you can find the example usage for org.apache.commons.collections15 Bag add.

Prototype

boolean add(E object, int nCopies);

Source Link

Document

Adds nCopies copies of the specified object to the Bag.

Usage

From source file:de.dhke.projects.cutil.collections.BagUtil.java

public static <T> Bag<T> unionBags(final Bag<T> b0, final Bag<T> b1, final Bag<T> targetBag) {
    targetBag.clear();/* www .j a  v a  2s . c o m*/
    for (T item : b0) {
        final int count0 = b0.getCount(item);
        final int count1 = b1.getCount(item);
        targetBag.add(item, Math.max(count0, count1));
    }
    for (T item : b1) {
        if (!b0.contains(item)) {
            final int count1 = b1.getCount(item);
            targetBag.add(item, count1);
        }
    }
    return targetBag;
}

From source file:de.dhke.projects.cutil.collections.BagUtil.java

public static <T> Bag<T> intersectBags(final Bag<T> b0, final Bag<T> b1, final Bag<T> targetBag) {
    targetBag.clear();/*from w  ww .jav  a  2s  . c  om*/
    for (T item : b0) {
        final int count0 = b0.getCount(item);
        final int count1 = b1.getCount(item);
        if ((count0 > 0) && (count1 > 0)) {
            targetBag.add(item, Math.min(count0, count1));
        }
    }
    return targetBag;
}

From source file:mulavito.utils.BagUtil.java

/**
 * Set the counter of the given entry in the given set.
 * /*from w w  w .  ja  va  2  s .  co m*/
 * @param bag
 *            The bag to modify
 * @param entry
 *            The entry whose counter is set
 * @param count
 *            The new non-negative counter value
 */
public static <T> void setCount(Bag<T> bag, T entry, int count) {
    if (bag == null)
        throw new IllegalArgumentException();
    else if (count < 0)
        throw new IllegalArgumentException();

    int oldCount = bag.getCount(entry);

    if (count > oldCount)
        bag.add(entry, count - oldCount);
    else if (count < oldCount)
        bag.remove(entry, oldCount - count);
}