Java 类org.apache.commons.collections15.Bag 实例源码

项目:VarJ    文件:AbstractMapBag.java   
/**
 * Remove any members of the bag that are not in the given
 * bag, respecting cardinality.
 *
 * @param other the bag to retain
 * @return <code>true</code> if this call changed the collection
 * @see #retainAll(Collection)
 */
boolean retainAll(Bag<E> other) {
    boolean result = false;
    Bag excess = new HashBag();
    Iterator<E> i = uniqueSet().iterator();
    while (i.hasNext()) {
        E current = i.next();
        int myCount = getCount(current);
        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;
}
项目:VarJ    文件:AbstractMapBag.java   
/**
 * 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
 */
public boolean equals(Object object) {
    if (object == this) {
        return true;
    }
    if (object instanceof Bag == false) {
        return false;
    }
    Bag other = (Bag) object;
    if (other.size() != size()) {
        return false;
    }
    for (Iterator<E> it = map.keySet().iterator(); it.hasNext();) {
        E element = it.next();
        if (other.getCount(element) != getCount(element)) {
            return false;
        }
    }
    return true;
}
项目:VarJ    文件:AbstractTestBag.java   
public void testRemove() {
    Bag bag = makeBag();
    bag.add("A");
    assertEquals("Should have count of 1", 1, bag.getCount("A"));
    bag.remove("A");
    assertEquals("Should have count of 0", 0, bag.getCount("A"));
    bag.add("A");
    bag.add("A");
    bag.add("A");
    bag.add("A");
    assertEquals("Should have count of 4", 4, bag.getCount("A"));
    bag.remove("A", 0);
    assertEquals("Should have count of 4", 4, bag.getCount("A"));
    bag.remove("A", 2);
    assertEquals("Should have count of 2", 2, bag.getCount("A"));
    bag.remove("A");
    assertEquals("Should have count of 0", 0, bag.getCount("A"));
}
项目:VarJ    文件:AbstractTestBag.java   
public void testRemoveAll() {
    Bag bag = makeBag();
    bag.add("A", 2);
    assertEquals("Should have count of 2", 2, bag.getCount("A"));
    bag.add("B");
    bag.add("C");
    assertEquals("Should have count of 4", 4, bag.size());
    List delete = new ArrayList();
    delete.add("A");
    delete.add("B");
    bag.removeAll(delete);
    assertEquals("Should have count of 1", 1, bag.getCount("A"));
    assertEquals("Should have count of 0", 0, bag.getCount("B"));
    assertEquals("Should have count of 1", 1, bag.getCount("C"));
    assertEquals("Should have count of 2", 2, bag.size());
}
项目:VarJ    文件:AbstractTestBag.java   
public void testContains() {
    Bag bag = makeBag();

    assertEquals("Bag does not have at least 1 'A'", false, bag.contains("A"));
    assertEquals("Bag does not have at least 1 'B'", false, bag.contains("B"));

    bag.add("A");  // bag 1A
    assertEquals("Bag has at least 1 'A'", true, bag.contains("A"));
    assertEquals("Bag does not have at least 1 'B'", false, bag.contains("B"));

    bag.add("A");  // bag 2A
    assertEquals("Bag has at least 1 'A'", true, bag.contains("A"));
    assertEquals("Bag does not have at least 1 'B'", false, bag.contains("B"));

    bag.add("B");  // bag 2A,1B
    assertEquals("Bag has at least 1 'A'", true, bag.contains("A"));
    assertEquals("Bag has at least 1 'B'", true, bag.contains("B"));
}
项目:VarJ    文件:AbstractTestBag.java   
public void testSize() {
    Bag bag = makeBag();
    assertEquals("Should have 0 total items", 0, bag.size());
    bag.add("A");
    assertEquals("Should have 1 total items", 1, bag.size());
    bag.add("A");
    assertEquals("Should have 2 total items", 2, bag.size());
    bag.add("A");
    assertEquals("Should have 3 total items", 3, bag.size());
    bag.add("B");
    assertEquals("Should have 4 total items", 4, bag.size());
    bag.add("B");
    assertEquals("Should have 5 total items", 5, bag.size());
    bag.remove("A", 2);
    assertEquals("Should have 1 'A'", 1, bag.getCount("A"));
    assertEquals("Should have 3 total items", 3, bag.size());
    bag.remove("B");
    assertEquals("Should have 1 total item", 1, bag.size());
}
项目:VarJ    文件:AbstractTestBag.java   
public void testIterator() {
    Bag bag = makeBag();
    bag.add("A");
    bag.add("A");
    bag.add("B");
    assertEquals("Bag should have 3 items", 3, bag.size());
    Iterator i = bag.iterator();

    boolean foundA = false;
    while (i.hasNext()) {
        String element = (String) i.next();
        // ignore the first A, remove the second via Iterator.remove()
        if (element.equals("A")) {
            if (foundA == false) {
                foundA = true;
            } else {
                i.remove();
            }
        }
    }

    assertTrue("Bag should still contain 'A'", bag.contains("A"));
    assertEquals("Bag should have 2 items", 2, bag.size());
    assertEquals("Bag should have 1 'A'", 1, bag.getCount("A"));
}
项目:VarJ    文件:AbstractTestBag.java   
public void testIteratorFailNoMore() {
    Bag bag = makeBag();
    bag.add("A");
    bag.add("A");
    bag.add("B");
    Iterator it = bag.iterator();
    it.next();
    it.next();
    it.next();
    try {
        it.next();
        fail("Should throw NoSuchElementException");
    } catch (NoSuchElementException ex) {
        // expected
    }
}
项目:VarJ    文件:AbstractTestBag.java   
public void testIteratorFailDoubleRemove() {
    Bag bag = makeBag();
    bag.add("A");
    bag.add("A");
    bag.add("B");
    Iterator it = bag.iterator();
    it.next();
    it.next();
    assertEquals(3, bag.size());
    it.remove();
    assertEquals(2, bag.size());
    try {
        it.remove();
        fail("Should throw IllegalStateException");
    } catch (IllegalStateException ex) {
        // expected
    }
    assertEquals(2, bag.size());
    it.next();
    it.remove();
    assertEquals(1, bag.size());
}
项目:VarJ    文件:AbstractTestBag.java   
public void testToArray() {
    Bag bag = makeBag();
    bag.add("A");
    bag.add("A");
    bag.add("B");
    bag.add("B");
    bag.add("C");
    Object[] array = bag.toArray();
    int a = 0, b = 0, c = 0;
    for (int i = 0; i < array.length; i++) {
        a += (array[i].equals("A") ? 1 : 0);
        b += (array[i].equals("B") ? 1 : 0);
        c += (array[i].equals("C") ? 1 : 0);
    }
    assertEquals(2, a);
    assertEquals(2, b);
    assertEquals(1, c);
}
项目:VarJ    文件:AbstractTestBag.java   
public void testToArrayPopulate() {
    Bag bag = makeBag();
    bag.add("A");
    bag.add("A");
    bag.add("B");
    bag.add("B");
    bag.add("C");
    String[] array = (String[]) bag.toArray(new String[0]);
    int a = 0, b = 0, c = 0;
    for (int i = 0; i < array.length; i++) {
        a += (array[i].equals("A") ? 1 : 0);
        b += (array[i].equals("B") ? 1 : 0);
        c += (array[i].equals("C") ? 1 : 0);
    }
    assertEquals(2, a);
    assertEquals(2, b);
    assertEquals(1, c);
}
项目:VarJ    文件:AbstractTestBag.java   
public void testEquals() {
    Bag bag = makeBag();
    Bag bag2 = makeBag();
    assertEquals(true, bag.equals(bag2));
    bag.add("A");
    assertEquals(false, bag.equals(bag2));
    bag2.add("A");
    assertEquals(true, bag.equals(bag2));
    bag.add("A");
    bag.add("B");
    bag.add("B");
    bag.add("C");
    bag2.add("A");
    bag2.add("B");
    bag2.add("B");
    bag2.add("C");
    assertEquals(true, bag.equals(bag2));
}
项目:VarJ    文件:AbstractTestBag.java   
public void testEqualsHashBag() {
    Bag bag = makeBag();
    Bag bag2 = new HashBag();
    assertEquals(true, bag.equals(bag2));
    bag.add("A");
    assertEquals(false, bag.equals(bag2));
    bag2.add("A");
    assertEquals(true, bag.equals(bag2));
    bag.add("A");
    bag.add("B");
    bag.add("B");
    bag.add("C");
    bag2.add("A");
    bag2.add("B");
    bag2.add("B");
    bag2.add("C");
    assertEquals(true, bag.equals(bag2));
}
项目:VarJ    文件:AbstractTestBag.java   
public void testHashCode() {
    Bag bag = makeBag();
    Bag bag2 = makeBag();
    assertEquals(0, bag.hashCode());
    assertEquals(0, bag2.hashCode());
    assertEquals(bag.hashCode(), bag2.hashCode());
    bag.add("A");
    bag.add("A");
    bag.add("B");
    bag.add("B");
    bag.add("C");
    bag2.add("A");
    bag2.add("A");
    bag2.add("B");
    bag2.add("B");
    bag2.add("C");
    assertEquals(bag.hashCode(), bag2.hashCode());

    int total = 0;
    total += ("A".hashCode() ^ 2);
    total += ("B".hashCode() ^ 2);
    total += ("C".hashCode() ^ 1);
    assertEquals(total, bag.hashCode());
    assertEquals(total, bag2.hashCode());
}
项目:VarJ    文件:AbstractTestBag.java   
public void testFullBagSerialization() throws IOException, ClassNotFoundException {
    Bag bag = makeBag();
    bag.add("A");
    bag.add("A");
    bag.add("B");
    bag.add("B");
    bag.add("C");
    int size = bag.size();
    if (!(bag instanceof Serializable && isTestSerialization())) return;

    byte[] objekt = writeExternalFormToBytes((Serializable) bag);
    Bag bag2 = (Bag) readExternalFormFromBytes(objekt);

    assertEquals("Bag should be same size", size, bag.size());
    assertEquals("Bag should be same size", size, bag2.size());
}
项目:alevin-svn2    文件:BagUtil.java   
/**
 * Set the counter of the given entry in the given set.
 * 
 * @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);
}
项目:kdxplore    文件:BmsExcelImportCallable.java   
public void report(PrintStream ps) {

    ps.println("Collected " + plots.size() + " plots");

    Bag<String> plotTypeCounts = new HashBag<>();
    for (Plot plot : plots) {
        plotTypeCounts.add(plot.getPlotType());
    }

    ps.println("Found PlotTypes:");
    for (String plotType : plotTypeCounts.uniqueSet()) {
        ps.println("\t" + plotType + ": " + plotTypeCounts.getCount(plotType));
    }

    ps.println("Created " + traitByName.size() + " Traits");
    for (Trait trait : traitByName.values()) {
        ps.println("\t" + trait + " [id=" + trait.getTraitId() + "]");

    }

    ps.println("Created " + plotAttributeByName.size() + " Plot Attributes");
    for (PlotAttribute pa : plotAttributeByName.values()) {
        ps.println("\t" + pa);
    }

    ps.println("Created " + nPlotAttributeValues + " Plot Attribute Values");
    ps.println("Created " + nSamples + " Samples");

    if (! lineNumbersByMessage.isEmpty()) {
        ps.println("Warnings:");
        for (String msg : lineNumbersByMessage.keySet()) {
            Set<Integer> range = lineNumbersByMessage.get(msg);
            ps.println(range.size()+" times: " + msg);
        }
    }
}
项目:VarJ    文件:AbstractMapBag.java   
/**
 * Determines if the bag contains the given elements.
 *
 * @param coll the collection to check against
 * @return <code>true</code> if the Bag contains all the collection
 */
public boolean containsAll(Collection<?> coll) {
    if (coll instanceof Bag) {
        return containsAll((Bag<E>) coll);
    }
    return containsAll(new HashBag(coll));
}
项目:VarJ    文件:AbstractMapBag.java   
/**
 * 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(Bag<E> other) {
    boolean result = true;
    Iterator<E> it = other.uniqueSet().iterator();
    while (it.hasNext()) {
        E current = it.next();
        boolean contains = getCount(current) >= other.getCount(current);
        result = result && contains;
    }
    return result;
}
项目:VarJ    文件:TestTransformedBag.java   
public void testTransformedBag() {
    Bag bag = TransformedBag.decorate(new HashBag(), TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
    assertEquals(0, bag.size());
    Object[] els = new Object[]{"1", "3", "5", "7", "2", "4", "6"};
    for (int i = 0; i < els.length; i++) {
        bag.add(els[i]);
        assertEquals(i + 1, bag.size());
        assertEquals(true, bag.contains(new Integer((String) els[i])));
        assertEquals(false, bag.contains(els[i]));
    }

    assertEquals(false, bag.remove(els[0]));
    assertEquals(true, bag.remove(new Integer((String) els[0])));

}
项目:VarJ    文件:TestTreeBag.java   
public void testOrdering() {
    Bag bag = setupBag();
    assertEquals("Should get elements in correct order", "A", bag.toArray()[0]);
    assertEquals("Should get elements in correct order", "B", bag.toArray()[1]);
    assertEquals("Should get elements in correct order", "C", bag.toArray()[2]);
    assertEquals("Should get first key", "A", ((SortedBag) bag).first());
    assertEquals("Should get last key", "D", ((SortedBag) bag).last());
}
项目:VarJ    文件:TestTransformedSortedBag.java   
public void testTransformedBag() {
    Bag bag = TransformedSortedBag.decorate(new TreeBag(), TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
    assertEquals(0, bag.size());
    Object[] els = new Object[]{"1", "3", "5", "7", "2", "4", "6"};
    for (int i = 0; i < els.length; i++) {
        bag.add(els[i]);
        assertEquals(i + 1, bag.size());
        assertEquals(true, bag.contains(new Integer((String) els[i])));
    }

    assertEquals(true, bag.remove(new Integer((String) els[0])));

}
项目:VarJ    文件:AbstractTestBag.java   
public void testBagAdd() {
    Bag bag = makeBag();
    bag.add("A");
    assertTrue("Should contain 'A'", bag.contains("A"));
    assertEquals("Should have count of 1", 1, bag.getCount("A"));
    bag.add("A");
    assertTrue("Should contain 'A'", bag.contains("A"));
    assertEquals("Should have count of 2", 2, bag.getCount("A"));
    bag.add("B");
    assertTrue(bag.contains("A"));
    assertTrue(bag.contains("B"));
}
项目:VarJ    文件:AbstractTestBag.java   
public void testBagEqualsSelf() {
    Bag bag = makeBag();
    assertTrue(bag.equals(bag));
    bag.add("elt");
    assertTrue(bag.equals(bag));
    bag.add("elt"); // again
    assertTrue(bag.equals(bag));
    bag.add("elt2");
    assertTrue(bag.equals(bag));
}
项目:VarJ    文件:AbstractTestBag.java   
public void testRetainAll() {
    Bag bag = makeBag();
    bag.add("A");
    bag.add("A");
    bag.add("A");
    bag.add("B");
    bag.add("B");
    bag.add("C");
    List retains = new ArrayList();
    retains.add("B");
    retains.add("C");
    bag.retainAll(retains);
    assertEquals("Should have 2 total items", 2, bag.size());
}
项目:VarJ    文件:AbstractTestBag.java   
public void testIteratorFail() {
    Bag bag = makeBag();
    bag.add("A");
    bag.add("A");
    bag.add("B");
    Iterator it = bag.iterator();
    it.next();
    bag.remove("A");
    try {
        it.next();
        fail("Should throw ConcurrentModificationException");
    } catch (ConcurrentModificationException e) {
        // expected
    }
}
项目:VarJ    文件:AbstractTestBag.java   
public void testEmptyBagSerialization() throws IOException, ClassNotFoundException {
    Bag bag = makeBag();
    if (!(bag instanceof Serializable && isTestSerialization())) return;

    byte[] objekt = writeExternalFormToBytes((Serializable) bag);
    Bag bag2 = (Bag) readExternalFormFromBytes(objekt);

    assertEquals("Bag should be empty", 0, bag.size());
    assertEquals("Bag should be empty", 0, bag2.size());
}
项目:VarJ    文件:TestTypedBag.java   
public void testlegalAddRemove() {
    Bag bag = makeTestBag();
    assertEquals(0, bag.size());
    Object[] els = new Object[]{"1", "3", "5", "7", "2", "4", "1"};
    for (int i = 0; i < els.length; i++) {
        bag.add(els[i]);
        assertEquals(i + 1, bag.size());
        assertEquals(true, bag.contains(els[i]));
    }
    Set set = ((PredicatedBag) bag).uniqueSet();
    assertTrue("Unique set contains the first element", set.contains(els[0]));
    assertEquals(true, bag.remove(els[0]));
    set = ((PredicatedBag) bag).uniqueSet();
    assertTrue("Unique set now does not contain the first element", !set.contains(els[0]));
}
项目:VarJ    文件:TestTypedBag.java   
public void testIllegalAdd() {
    Bag bag = makeTestBag();
    Integer i = new Integer(3);
    try {
        bag.add(i);
        fail("Integer should fail type check.");
    } catch (IllegalArgumentException e) {
        // expected
    }
    assertTrue("Collection shouldn't contain illegal element", !bag.contains(i));
}
项目:VarJ    文件:TestPredicatedBag.java   
public void testlegalAddRemove() {
    Bag bag = makeTestBag();
    assertEquals(0, bag.size());
    Object[] els = new Object[]{"1", "3", "5", "7", "2", "4", "1"};
    for (int i = 0; i < els.length; i++) {
        bag.add(els[i]);
        assertEquals(i + 1, bag.size());
        assertEquals(true, bag.contains(els[i]));
    }
    Set set = ((PredicatedBag) bag).uniqueSet();
    assertTrue("Unique set contains the first element", set.contains(els[0]));
    assertEquals(true, bag.remove(els[0]));
    set = ((PredicatedBag) bag).uniqueSet();
    assertTrue("Unique set now does not contain the first element", !set.contains(els[0]));
}
项目:VarJ    文件:TestPredicatedBag.java   
public void testIllegalAdd() {
    Bag bag = makeTestBag();
    Integer i = new Integer(3);
    try {
        bag.add(i);
        fail("Integer should fail string predicate.");
    } catch (IllegalArgumentException e) {
        // expected
    }
    assertTrue("Collection shouldn't contain illegal element", !bag.contains(i));
}
项目:stats    文件:Multinomial.java   
public Multinomial(Bag<T> counts) throws DistributionException
{
this();
for (T k : counts.uniqueSet())
    {
    put(k, counts.getCount(k));
    }
normalize();
}
项目:crucian    文件:BagUtil.java   
/**
 * Set the counter of the given entry in the given set.
 *
 * @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);
}
项目:Alevin    文件:BagUtil.java   
/**
 * Set the counter of the given entry in the given set.
 * 
 * @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);
}
项目:kdxplore    文件:SampleGroupViewer.java   
private String getBriefSummary(SampleGroup sampleGroup) throws IOException {
    Bag<Integer> plotIdsWithSpecimens = new HashBag<>();
    Set<Integer> plotIdsWithScores = new HashSet<>();
    int[] results = new int[3];
    java.util.function.Predicate<KdxSample> visitor = new java.util.function.Predicate<KdxSample>() {
        @Override
        public boolean test(KdxSample s) {
            plotIdsWithScores.add(s.getPlotId());
            int snum = s.getSpecimenNumber();
            if (snum <= 0) {
                ++results[0]; // Plot level sample count
            }
            else {
                ++results[1]; // Individual level sample count
                plotIdsWithSpecimens.add(s.getPlotId());
                results[2] = Math.max(results[2], snum); // maximum specimen number
            }
            return true;
        }
    };

    boolean scored = true;
    kdxdb.visitKdxSamplesForSampleGroup(
            sampleGroup, KdxploreDatabase.SampleLevel.BOTH, scored, visitor);

    int nPlotSamples = results[0];
    int nSpecimenSamples = results[1];
    int totalScoredSamples = nPlotSamples + nSpecimenSamples;

    int maxSpecimenNumber = results[2];

    int nPlotsWithSpecimens = plotIdsWithSpecimens.size();
    int nPlotsWithScores = plotIdsWithScores.size();

    int maxCount = 0;
    for (Integer plotId : plotIdsWithSpecimens.uniqueSet()) {
        int count = plotIdsWithSpecimens.getCount(plotId);
        maxCount = Math.max(maxCount, count);
    }

    StringBuilder sb = new StringBuilder("<HTML>");

    sb.append("<br><B>Scored Samples:</b> ").append(totalScoredSamples);
    sb.append("<br><B>Plot Level Samples:</b> ").append(nPlotSamples);
    sb.append("<br><B>Individual Samples:</b> ").append(nSpecimenSamples);
    sb.append("<br>");
    sb.append("<br><B>Max Individual number:</b> ").append(maxSpecimenNumber);
    sb.append("<br><B># Plots with Individuals:</b> ").append(nPlotsWithSpecimens);
    sb.append("<br>");
    sb.append("<br><B># Plots with scored samples:</b> ").append(nPlotsWithScores);
    sb.append("<br><B>Max # individual scores per plot:</b> ").append(maxCount);

    return sb.toString();
}
项目:VarJ    文件:TestTypedSortedBag.java   
public Bag makeBag() {
    return decorateBag(new TreeBag(), objectClass);
}
项目:VarJ    文件:TestTypedSortedBag.java   
protected Bag makeTestBag() {
    return decorateBag(new TreeBag(), stringClass);
}
项目:VarJ    文件:TestTransformedBag.java   
public Bag makeBag() {
    return TransformedBag.decorate(new HashBag(), TestTransformedCollection.NOOP_TRANSFORMER);
}
项目:VarJ    文件:TestTreeBag.java   
public Bag makeBag() {
    return new TreeBag();
}
项目:VarJ    文件:TestTransformedSortedBag.java   
public Bag makeBag() {
    return TransformedSortedBag.decorate(new TreeBag(), TestTransformedCollection.NOOP_TRANSFORMER);
}