Package org.apache.commons.collections

Examples of org.apache.commons.collections.Bag


        bag.remove("B");
        assertEquals("Should have 1 total item", 1, bag.size());
    }
   
    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());
    }
View Full Code Here


        bag.retainAll(retains);
        assertEquals("Should have 2 total items", 2, bag.size());
    }

    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"));
    }
View Full Code Here

        assertEquals("Bag should have 2 items", 2, bag.size());
        assertEquals("Bag should have 1 'A'", 1, bag.getCount("A"));
    }

    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
View Full Code Here

            // expected
        }
    }
   
    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();
View Full Code Here

            // expected
        }
    }
   
    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());
    }
View Full Code Here

        it.remove();
        assertEquals(1, bag.size());
    }
   
    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);
View Full Code Here

        assertEquals(2, b);
        assertEquals(1, c);
    }

    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);
View Full Code Here

        assertEquals(1, c);
    }

    //-----------------------------------------------------------------------
    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));
    }
View Full Code Here

        bag2.add("C");
        assertEquals(true, bag.equals(bag2));
    }

    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));
    }
View Full Code Here

        bag2.add("C");
        assertEquals(true, bag.equals(bag2));
    }

    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());
    }
View Full Code Here

TOP

Related Classes of org.apache.commons.collections.Bag

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.