org.apache.commons.collections.Bag Java Examples

The following examples show how to use org.apache.commons.collections.Bag. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractMapBag.java    From Penetration_Testing_POC with Apache License 2.0 6 votes vote down vote up
/**
 * 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 it = map.keySet().iterator(); it.hasNext();) {
        Object element = it.next();
        if (other.getCount(element) != getCount(element)) {
            return false;
        }
    }
    return true;
}
 
Example #2
Source File: AbstractMapBag.java    From Penetration_Testing_POC with Apache License 2.0 6 votes vote down vote up
/**
 * 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(Bag other) {
    boolean result = false;
    Bag excess = new HashBag();
    Iterator i = uniqueSet().iterator();
    while (i.hasNext()) {
        Object 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;
}
 
Example #3
Source File: CommonsTest.java    From java-study with Apache License 2.0 5 votes vote down vote up
/**
 * Bag 测试
 * 主要测试重复元素的统计
 */
@SuppressWarnings("deprecation")
private static void bagTest(){
	//定义4种球
	Bag box=new HashBag(Arrays.asList("red","blue","black","green"));
	System.out.println("box.getCount():"+box.getCount("red"));//box.getCount():1
	box.add("red", 5);//红色的球增加五个
	System.out.println("box.size():"+box.size());	//box.size():9
	System.out.println("box.getCount():"+box.getCount("red"));//box.getCount():6
}
 
Example #4
Source File: AbstractMapBag.java    From Penetration_Testing_POC with Apache License 2.0 5 votes vote down vote up
/**
 * 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 other) {
    boolean result = true;
    Iterator it = other.uniqueSet().iterator();
    while (it.hasNext()) {
        Object current = it.next();
        boolean contains = getCount(current) >= other.getCount(current);
        result = result && contains;
    }
    return result;
}
 
Example #5
Source File: AbstractMapBag.java    From Penetration_Testing_POC with Apache License 2.0 5 votes vote down vote up
/**
 * 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) coll);
    }
    return containsAll(new HashBag(coll));
}
 
Example #6
Source File: AbstractMapBag.java    From Penetration_Testing_POC with Apache License 2.0 3 votes vote down vote up
/**
 * Remove any members of the bag that are not in the given
 * bag, respecting cardinality.
 *
 * @param coll  the collection to retain
 * @return true if this call changed the collection
 */
public boolean retainAll(Collection coll) {
    if (coll instanceof Bag) {
        return retainAll((Bag) coll);
    }
    return retainAll(new HashBag(coll));
}
 
Example #7
Source File: UnmodifiableBag.java    From Penetration_Testing_POC with Apache License 2.0 3 votes vote down vote up
/**
 * Factory method to create an unmodifiable bag.
 * <p>
 * If the bag passed in is already unmodifiable, it is returned.
 * 
 * @param bag  the bag to decorate, must not be null
 * @return an unmodifiable Bag
 * @throws IllegalArgumentException if bag is null
 */
public static Bag decorate(Bag bag) {
    if (bag instanceof Unmodifiable) {
        return bag;
    }
    return new UnmodifiableBag(bag);
}
 
Example #8
Source File: TransformedBag.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Factory method to create a transforming bag.
 * <p>
 * If there are any elements already in the bag being decorated, they
 * are NOT transformed.
 * 
 * @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 IllegalArgumentException if bag or transformer is null
 */
public static Bag decorate(Bag bag, Transformer transformer) {
    return new TransformedBag(bag, transformer);
}
 
Example #9
Source File: PredicatedBag.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Factory method to create a predicated (validating) bag.
 * <p>
 * If there are any elements already in the bag being decorated, they
 * are validated.
 * 
 * @param bag  the bag to decorate, must not be null
 * @param predicate  the predicate to use for validation, must not be null
 * @return a new predicated Bag
 * @throws IllegalArgumentException if bag or predicate is null
 * @throws IllegalArgumentException if the bag contains invalid elements
 */
public static Bag decorate(Bag bag, Predicate predicate) {
    return new PredicatedBag(bag, predicate);
}
 
Example #10
Source File: SynchronizedSortedBag.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor that wraps (not copies).
 * 
 * @param bag  the bag to decorate, must not be null
 * @param lock  the lock to use, must not be null
 * @throws IllegalArgumentException if bag is null
 */
protected SynchronizedSortedBag(Bag bag, Object lock) {
    super(bag, lock);
}
 
Example #11
Source File: TypedBag.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Factory method to create a typed bag.
 * <p>
 * If there are any elements already in the bag being decorated, they
 * are validated.
 * 
 * @param bag  the bag to decorate, must not be null
 * @param type  the type to allow into the bag, must not be null
 * @return a new typed Bag
 * @throws IllegalArgumentException if bag or type is null
 * @throws IllegalArgumentException if the bag contains invalid elements
 */
public static Bag decorate(Bag bag, Class type) {
    return new PredicatedBag(bag, InstanceofPredicate.getInstance(type));
}
 
Example #12
Source File: SynchronizedBag.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the bag being decorated.
 * 
 * @return the decorated bag
 */
protected Bag getBag() {
    return (Bag) collection;
}
 
Example #13
Source File: SynchronizedBag.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor that wraps (not copies).
 * 
 * @param bag  the bag to decorate, must not be null
 * @param lock  the lock to use, must not be null
 * @throws IllegalArgumentException if bag is null
 */
protected SynchronizedBag(Bag bag, Object lock) {
    super(bag, lock);
}
 
Example #14
Source File: SynchronizedBag.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor that wraps (not copies).
 * 
 * @param bag  the bag to decorate, must not be null
 * @throws IllegalArgumentException if bag is null
 */
protected SynchronizedBag(Bag bag) {
    super(bag);
}
 
Example #15
Source File: SynchronizedBag.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Factory method to create a synchronized bag.
 * 
 * @param bag  the bag to decorate, must not be null
 * @return a new synchronized Bag
 * @throws IllegalArgumentException if bag is null
 */
public static Bag decorate(Bag bag) {
    return new SynchronizedBag(bag);
}
 
Example #16
Source File: UnmodifiableBag.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor that wraps (not copies).
 * 
 * @param bag  the bag to decorate, must not be null
 * @throws IllegalArgumentException if bag is null
 */
private UnmodifiableBag(Bag bag) {
    super(bag);
}
 
Example #17
Source File: TransformedBag.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor that wraps (not copies).
 * <p>
 * If there are any elements already in the bag being decorated, they
 * are NOT transformed.
 * 
 * @param bag  the bag to decorate, must not be null
 * @param transformer  the transformer to use for conversion, must not be null
 * @throws IllegalArgumentException if bag or transformer is null
 */
protected TransformedBag(Bag bag, Transformer transformer) {
    super(bag, transformer);
}
 
Example #18
Source File: TransformedBag.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the decorated bag.
 * 
 * @return the decorated bag
 */
protected Bag getBag() {
    return (Bag) collection;
}
 
Example #19
Source File: AbstractBagDecorator.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor that wraps (not copies).
 * 
 * @param bag  the bag to decorate, must not be null
 * @throws IllegalArgumentException if list is null
 */
protected AbstractBagDecorator(Bag bag) {
    super(bag);
}
 
Example #20
Source File: AbstractBagDecorator.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the bag being decorated.
 * 
 * @return the decorated bag
 */
protected Bag getBag() {
    return (Bag) getCollection();
}
 
Example #21
Source File: PredicatedBag.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the decorated bag.
 * 
 * @return the decorated bag
 */
protected Bag getBag() {
    return (Bag) getCollection();
}
 
Example #22
Source File: PredicatedBag.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor that wraps (not copies).
 * <p>
 * If there are any elements already in the bag being decorated, they
 * are validated.
 * 
 * @param bag  the bag to decorate, must not be null
 * @param predicate  the predicate to use for validation, must not be null
 * @throws IllegalArgumentException if bag or predicate is null
 * @throws IllegalArgumentException if the bag contains invalid elements
 */
protected PredicatedBag(Bag bag, Predicate predicate) {
    super(bag, predicate);
}