|
Commons Collections example source code file (AbstractMapBag.java)
The Commons Collections AbstractMapBag.java source code
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections.bag;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.commons.collections.Bag;
import org.apache.commons.collections.set.UnmodifiableSet;
/**
* Abstract implementation of the {@link Bag} interface to simplify the creation
* of subclass implementations.
* <p>
* Subclasses specify a Map implementation to use as the internal storage.
* The map will be used to map bag elements to a number; the number represents
* the number of occurrences of that element in the bag.
*
* @since Commons Collections 3.0 (previously DefaultMapBag v2.0)
* @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
*
* @author Chuck Burdick
* @author Michael A. Smith
* @author Stephen Colebourne
* @author Janek Bogucki
* @author Steve Clark
*/
public abstract class AbstractMapBag implements Bag {
/** The map to use to store the data */
private transient Map map;
/** The current total size of the bag */
private int size;
/** The modification count for fail fast iterators */
private transient int modCount;
/** The modification count for fail fast iterators */
private transient Set uniqueSet;
/**
* Constructor needed for subclass serialisation.
*
*/
protected AbstractMapBag() {
super();
}
/**
* Constructor that assigns the specified Map as the backing store.
* The map must be empty and non-null.
*
* @param map the map to assign
*/
protected AbstractMapBag(Map map) {
super();
this.map = map;
}
/**
* Utility method for implementations to access the map that backs
* this bag. Not intended for interactive use outside of subclasses.
*
* @return the map being used by the Bag
*/
protected Map getMap() {
return map;
}
//-----------------------------------------------------------------------
/**
* Returns the number of elements in this bag.
*
* @return current size of the bag
*/
public int size() {
return size;
}
/**
* Returns true if the underlying map is empty.
*
* @return true if bag is empty
*/
public boolean isEmpty() {
return map.isEmpty();
}
/**
* Returns the number of occurrence of the given element in this bag
* by looking up its count in the underlying map.
*
* @param object the object to search for
* @return the number of occurrences of the object, zero if not found
*/
public int getCount(Object object) {
MutableInteger count = (MutableInteger) map.get(object);
if (count != null) {
return count.value;
}
return 0;
}
//-----------------------------------------------------------------------
/**
* Determines if the bag contains the given element by checking if the
* underlying map contains the element as a key.
*
* @param object the object to search for
* @return true if the bag contains the given element
*/
public boolean contains(Object object) {
return map.containsKey(object);
}
/**
* Determines if the bag contains the given elements.
*
* @param coll the collection to check against
* @return <code>true 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));
}
/**
* Returns <code>true if the bag contains all elements in
* the given collection, respecting cardinality.
*
* @param other the bag to check against
* @return <code>true 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;
}
//-----------------------------------------------------------------------
/**
* Gets an iterator over the bag elements.
* Elements present in the Bag more than once will be returned repeatedly.
*
* @return the iterator
*/
public Iterator iterator() {
return new BagIterator(this);
}
/**
* Inner class iterator for the Bag.
*/
static class BagIterator implements Iterator {
private AbstractMapBag parent;
private Iterator entryIterator;
private Map.Entry current;
private int itemCount;
private final int mods;
private boolean canRemove;
/**
* Constructor.
*
* @param parent the parent bag
*/
public BagIterator(AbstractMapBag parent) {
this.parent = parent;
this.entryIterator = parent.map.entrySet().iterator();
this.current = null;
this.mods = parent.modCount;
this.canRemove = false;
}
public boolean hasNext() {
return (itemCount > 0 || entryIterator.hasNext());
}
public Object next() {
if (parent.modCount != mods) {
throw new ConcurrentModificationException();
}
if (itemCount == 0) {
current = (Map.Entry) entryIterator.next();
itemCount = ((MutableInteger) current.getValue()).value;
}
canRemove = true;
itemCount--;
return current.getKey();
}
public void remove() {
if (parent.modCount != mods) {
throw new ConcurrentModificationException();
}
if (canRemove == false) {
throw new IllegalStateException();
}
MutableInteger mut = (MutableInteger) current.getValue();
if (mut.value > 1) {
mut.value--;
} else {
entryIterator.remove();
}
parent.size--;
canRemove = false;
}
}
//-----------------------------------------------------------------------
/**
* Adds a new element to the bag, incrementing its count in the underlying map.
*
* @param object the object to add
* @return <code>true if the object was not already in the
Other Commons Collections examples (source code examples)Here is a short list of links related to this Commons Collections AbstractMapBag.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.