com.groupbyinc.common.apache.commons.collections4.bag.AbstractMapBag Maven / Gradle / Ivy
/*
* 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.collections4.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.Map.Entry;
import java.util.Set;
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.set.UnmodifiableSet;
/**
* Abstract implementation of the {@link Bag} interface to simplify the creation
* of subclass implementations.
*
* 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 3.0 (previously DefaultMapBag v2.0)
* @version $Id: AbstractMapBag.java 1684859 2015-06-11 11:57:24Z tn $
*/
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;
/** Unique view of the elements */
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(final 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
*/
@Override
public int size() {
return size;
}
/**
* Returns true if the underlying map is empty.
*
* @return true if bag is empty
*/
@Override
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
*/
@Override
public int getCount(final Object object) {
final MutableInteger count = 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
*/
@Override
public boolean contains(final Object object) {
return map.containsKey(object);
}
/**
* Determines if the bag contains the given elements.
*
* @param coll the collection to check against
* @return true
if the Bag contains all the collection
*/
@Override
public boolean containsAll(final Collection> coll) {
if (coll instanceof Bag) {
return containsAll((Bag>) coll);
}
return containsAll(new HashBag