<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
public
interface
Bag
extends
Collection
{
int getCount(Objectobject);
boolean add(Objectobject);
boolean add(Objectobject, int nCopies);
boolean remove(Objectobject);
boolean remove(Objectobject, int nCopies);
SetuniqueSet();
int size();
boolean containsAll(Collectioncoll);
boolean removeAll(Collectioncoll);
boolean retainAll(Collectioncoll);
Iteratoriterator();
}
{
int getCount(Objectobject);
boolean add(Objectobject);
boolean add(Objectobject, int nCopies);
boolean remove(Objectobject);
boolean remove(Objectobject, int nCopies);
SetuniqueSet();
int size();
boolean containsAll(Collectioncoll);
boolean removeAll(Collectioncoll);
boolean retainAll(Collectioncoll);
Iteratoriterator();
}
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
public
interface
SortedBag
extends
Bag
{
public Comparatorcomparator();
public Objectfirst();
public Objectlast();
}
{
public Comparatorcomparator();
public Objectfirst();
public Objectlast();
}
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
public
abstract
class
DefaultMapBag
implements
Bag
{
private Map_map = null ; // 底层数据存储区
private int _total = 0 ; // 元素总个数
private int _mods = 0 ; // 修改次数
public DefaultMapBag(){
}
protected DefaultMapBag(Mapmap){
setMap(map);
}
public boolean add(Objectobject){
return add(object, 1 );
}
public boolean add(Objectobject, int nCopies){
_mods ++ ;
if (nCopies > 0 ){
int count = (nCopies + getCount(object));
_map.put(object, new Integer(count));
_total += nCopies;
return (count == nCopies);
} else {
return false ;
}
}
public boolean addAll(Collectioncoll){
boolean changed = false ;
Iteratori = coll.iterator();
while (i.hasNext()){
boolean added = add(i.next());
changed = changed || added;
}
return changed;
}
public void clear(){
_mods ++ ;
_map.clear();
_total = 0 ;
}
public boolean contains(Objectobject){
return _map.containsKey(object);
}
public boolean containsAll(Collectioncoll){
return containsAll( new HashBag(coll));
}
public boolean containsAll(Bagother){
boolean result = true ;
Iteratori = other.uniqueSet().iterator();
while (i.hasNext()){
Objectcurrent = i.next();
boolean contains = getCount(current) >= other.getCount(current);
result = result && contains;
}
return result;
}
public boolean equals(Objectobject){
if (object == this ){
return true ;
}
if (object instanceof Bag == false ){
return false ;
}
Bagother = (Bag)object;
if (other.size() != size()){
return false ;
}
for (Iteratorit = _map.keySet().iterator();it.hasNext();){
Objectelement = it.next();
if (other.getCount(element) != getCount(element)){
return false ;
}
}
return true ;
}
public int hashCode(){
return _map.hashCode();
}
public boolean isEmpty(){
return _map.isEmpty();
}
public Iteratoriterator(){
return new BagIterator( this ,extractList().iterator());
}
static class BagIterator implements Iterator{
private DefaultMapBag_parent = null ;
private Iterator_support = null ; // 原始迭代器
private Object_current = null ; // 当前元素
private int _mods = 0 ;
public BagIterator(DefaultMapBagparent,Iteratorsupport){
_parent = parent;
_support = support;
_current = null ;
_mods = parent.modCount();
}
public boolean hasNext(){
return _support.hasNext();
}
public Objectnext(){
if (_parent.modCount() != _mods){
throw new ConcurrentModificationException();
}
_current = _support.next();
return _current;
}
public void remove(){
if (_parent.modCount() != _mods){
throw new ConcurrentModificationException();
}
_support.remove();
_parent.remove(_current, 1 );
_mods ++ ;
}
}
public boolean remove(Objectobject){
return remove(object,getCount(object));
}
public boolean remove(Objectobject, int nCopies){
_mods ++ ;
boolean result = false ;
int count = getCount(object);
if (nCopies <= 0 ){
result = false ;
} else if (count > nCopies){
_map.put(object, new Integer(count - nCopies));
result = true ;
_total -= nCopies;
} else { // count>0&&count<=i
// needtoremoveall
result = (_map.remove(object) != null );
_total -= count;
}
return result;
}
public boolean removeAll(Collectioncoll){
boolean result = false ;
if (coll != null ){
Iteratori = coll.iterator();
while (i.hasNext()){
boolean changed = remove(i.next(), 1 );
result = result || changed;
}
}
return result;
}
public boolean retainAll(Collectioncoll){
return retainAll( new HashBag(coll));
}
public boolean retainAll(Bagother){
boolean result = false ;
Bagexcess = new HashBag();
Iteratori = uniqueSet().iterator();
while (i.hasNext()){
Objectcurrent = 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;
}
public Object[]toArray(){
return extractList().toArray();
}
public Object[]toArray(Object[]array){
return extractList().toArray(array);
}
public int getCount(Objectobject){
int result = 0 ;
Integercount = MapUtils.getInteger(_map,object);
if (count != null ){
result = count.intValue();
}
return result;
}
public SetuniqueSet(){
return UnmodifiableSet.decorate(_map.keySet());
}
public int size(){
return _total;
}
protected int calcTotalSize(){
_total = extractList().size();
return _total;
}
protected void setMap(Mapmap){
if (map == null || map.isEmpty() == false ){
throw new IllegalArgumentException( " Themapmustbenon-nullandempty " );
}
_map = map;
}
protected MapgetMap(){
return _map;
}
private ListextractList(){
Listresult = new ArrayList();
Iteratori = uniqueSet().iterator();
while (i.hasNext()){
Objectcurrent = i.next();
for ( int index = getCount(current);index > 0 ;index -- ){
result.add(current);
}
}
return result;
}
private int modCount(){
return _mods;
}
public StringtoString(){
StringBufferbuf = new StringBuffer();
buf.append( " [ " );
Iteratori = uniqueSet().iterator();
while (i.hasNext()){
Objectcurrent = i.next();
int count = getCount(current);
buf.append(count);
buf.append( " : " );
buf.append(current);
if (i.hasNext()){
buf.append( " , " );
}
}
buf.append( " ] " );
return buf.toString();
}
}
{
private Map_map = null ; // 底层数据存储区
private int _total = 0 ; // 元素总个数
private int _mods = 0 ; // 修改次数
public DefaultMapBag(){
}
protected DefaultMapBag(Mapmap){
setMap(map);
}
public boolean add(Objectobject){
return add(object, 1 );
}
public boolean add(Objectobject, int nCopies){
_mods ++ ;
if (nCopies > 0 ){
int count = (nCopies + getCount(object));
_map.put(object, new Integer(count));
_total += nCopies;
return (count == nCopies);
} else {
return false ;
}
}
public boolean addAll(Collectioncoll){
boolean changed = false ;
Iteratori = coll.iterator();
while (i.hasNext()){
boolean added = add(i.next());
changed = changed || added;
}
return changed;
}
public void clear(){
_mods ++ ;
_map.clear();
_total = 0 ;
}
public boolean contains(Objectobject){
return _map.containsKey(object);
}
public boolean containsAll(Collectioncoll){
return containsAll( new HashBag(coll));
}
public boolean containsAll(Bagother){
boolean result = true ;
Iteratori = other.uniqueSet().iterator();
while (i.hasNext()){
Objectcurrent = i.next();
boolean contains = getCount(current) >= other.getCount(current);
result = result && contains;
}
return result;
}
public boolean equals(Objectobject){
if (object == this ){
return true ;
}
if (object instanceof Bag == false ){
return false ;
}
Bagother = (Bag)object;
if (other.size() != size()){
return false ;
}
for (Iteratorit = _map.keySet().iterator();it.hasNext();){
Objectelement = it.next();
if (other.getCount(element) != getCount(element)){
return false ;
}
}
return true ;
}
public int hashCode(){
return _map.hashCode();
}
public boolean isEmpty(){
return _map.isEmpty();
}
public Iteratoriterator(){
return new BagIterator( this ,extractList().iterator());
}
static class BagIterator implements Iterator{
private DefaultMapBag_parent = null ;
private Iterator_support = null ; // 原始迭代器
private Object_current = null ; // 当前元素
private int _mods = 0 ;
public BagIterator(DefaultMapBagparent,Iteratorsupport){
_parent = parent;
_support = support;
_current = null ;
_mods = parent.modCount();
}
public boolean hasNext(){
return _support.hasNext();
}
public Objectnext(){
if (_parent.modCount() != _mods){
throw new ConcurrentModificationException();
}
_current = _support.next();
return _current;
}
public void remove(){
if (_parent.modCount() != _mods){
throw new ConcurrentModificationException();
}
_support.remove();
_parent.remove(_current, 1 );
_mods ++ ;
}
}
public boolean remove(Objectobject){
return remove(object,getCount(object));
}
public boolean remove(Objectobject, int nCopies){
_mods ++ ;
boolean result = false ;
int count = getCount(object);
if (nCopies <= 0 ){
result = false ;
} else if (count > nCopies){
_map.put(object, new Integer(count - nCopies));
result = true ;
_total -= nCopies;
} else { // count>0&&count<=i
// needtoremoveall
result = (_map.remove(object) != null );
_total -= count;
}
return result;
}
public boolean removeAll(Collectioncoll){
boolean result = false ;
if (coll != null ){
Iteratori = coll.iterator();
while (i.hasNext()){
boolean changed = remove(i.next(), 1 );
result = result || changed;
}
}
return result;
}
public boolean retainAll(Collectioncoll){
return retainAll( new HashBag(coll));
}
public boolean retainAll(Bagother){
boolean result = false ;
Bagexcess = new HashBag();
Iteratori = uniqueSet().iterator();
while (i.hasNext()){
Objectcurrent = 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;
}
public Object[]toArray(){
return extractList().toArray();
}
public Object[]toArray(Object[]array){
return extractList().toArray(array);
}
public int getCount(Objectobject){
int result = 0 ;
Integercount = MapUtils.getInteger(_map,object);
if (count != null ){
result = count.intValue();
}
return result;
}
public SetuniqueSet(){
return UnmodifiableSet.decorate(_map.keySet());
}
public int size(){
return _total;
}
protected int calcTotalSize(){
_total = extractList().size();
return _total;
}
protected void setMap(Mapmap){
if (map == null || map.isEmpty() == false ){
throw new IllegalArgumentException( " Themapmustbenon-nullandempty " );
}
_map = map;
}
protected MapgetMap(){
return _map;
}
private ListextractList(){
Listresult = new ArrayList();
Iteratori = uniqueSet().iterator();
while (i.hasNext()){
Objectcurrent = i.next();
for ( int index = getCount(current);index > 0 ;index -- ){
result.add(current);
}
}
return result;
}
private int modCount(){
return _mods;
}
public StringtoString(){
StringBufferbuf = new StringBuffer();
buf.append( " [ " );
Iteratori = uniqueSet().iterator();
while (i.hasNext()){
Objectcurrent = i.next();
int count = getCount(current);
buf.append(count);
buf.append( " : " );
buf.append(current);
if (i.hasNext()){
buf.append( " , " );
}
}
buf.append( " ] " );
return buf.toString();
}
}
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
public
class
HashBag
extends
DefaultMapBag
implements
Bag
{
public HashBag(){
super ( new HashMap());
}
public HashBag(Collectioncoll){
this ();
addAll(coll);
}
}
{
public HashBag(){
super ( new HashMap());
}
public HashBag(Collectioncoll){
this ();
addAll(coll);
}
}
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
public
class
TreeBag
extends
DefaultMapBag
implements
SortedBag
{
public TreeBag(){
super ( new TreeMap());
}
public TreeBag(Comparatorcomparator){
super ( new TreeMap(comparator));
}
public TreeBag(Collectioncoll){
this ();
addAll(coll);
}
public Objectfirst(){
return ((SortedMap)getMap()).firstKey();
}
public Objectlast(){
return ((SortedMap)getMap()).lastKey();
}
public Comparatorcomparator(){
return ((SortedMap)getMap()).comparator();
}
}
{
public TreeBag(){
super ( new TreeMap());
}
public TreeBag(Comparatorcomparator){
super ( new TreeMap(comparator));
}
public TreeBag(Collectioncoll){
this ();
addAll(coll);
}
public Objectfirst(){
return ((SortedMap)getMap()).firstKey();
}
public Objectlast(){
return ((SortedMap)getMap()).lastKey();
}
public Comparatorcomparator(){
return ((SortedMap)getMap()).comparator();
}
}
使用decorate模式的Bag工具类
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
public
class
BagUtils
{
/**
*Anemptyunmodifiablebag.
*/
public static final BagEMPTY_BAG = UnmodifiableBag.decorate( new HashBag());
/**
*Anemptyunmodifiablesortedbag.
*/
public static final BagEMPTY_SORTED_BAG = UnmodifiableSortedBag.decorate( new TreeBag());
public BagUtils(){ // 这里按常理不应该是public,应该是private才对,作者应该有其他地方要用到吧
}
public static BagsynchronizedBag(Bagbag){
return SynchronizedBag.decorate(bag);
}
public static BagunmodifiableBag(Bagbag){
return UnmodifiableBag.decorate(bag);
}
public static BagpredicatedBag(Bagbag,Predicatepredicate){
return PredicatedBag.decorate(bag,predicate);
}
public static BagtypedBag(Bagbag,Classtype){
return TypedBag.decorate(bag,type);
}
public static BagtransformedBag(Bagbag,Transformertransformer){
return TransformedBag.decorate(bag,transformer);
}
public static SortedBagsynchronizedSortedBag(SortedBagbag){
return SynchronizedSortedBag.decorate(bag);
}
public static SortedBagunmodifiableSortedBag(SortedBagbag){
return UnmodifiableSortedBag.decorate(bag);
}
public static SortedBagpredicatedSortedBag(SortedBagbag,Predicatepredicate){
return PredicatedSortedBag.decorate(bag,predicate);
}
public static SortedBagtypedSortedBag(SortedBagbag,Classtype){
return TypedSortedBag.decorate(bag,type);
}
public static SortedBagtransformedSortedBag(SortedBagbag,Transformertransformer){
return TransformedSortedBag.decorate(bag,transformer);
}
}
{
/**
*Anemptyunmodifiablebag.
*/
public static final BagEMPTY_BAG = UnmodifiableBag.decorate( new HashBag());
/**
*Anemptyunmodifiablesortedbag.
*/
public static final BagEMPTY_SORTED_BAG = UnmodifiableSortedBag.decorate( new TreeBag());
public BagUtils(){ // 这里按常理不应该是public,应该是private才对,作者应该有其他地方要用到吧
}
public static BagsynchronizedBag(Bagbag){
return SynchronizedBag.decorate(bag);
}
public static BagunmodifiableBag(Bagbag){
return UnmodifiableBag.decorate(bag);
}
public static BagpredicatedBag(Bagbag,Predicatepredicate){
return PredicatedBag.decorate(bag,predicate);
}
public static BagtypedBag(Bagbag,Classtype){
return TypedBag.decorate(bag,type);
}
public static BagtransformedBag(Bagbag,Transformertransformer){
return TransformedBag.decorate(bag,transformer);
}
public static SortedBagsynchronizedSortedBag(SortedBagbag){
return SynchronizedSortedBag.decorate(bag);
}
public static SortedBagunmodifiableSortedBag(SortedBagbag){
return UnmodifiableSortedBag.decorate(bag);
}
public static SortedBagpredicatedSortedBag(SortedBagbag,Predicatepredicate){
return PredicatedSortedBag.decorate(bag,predicate);
}
public static SortedBagtypedSortedBag(SortedBagbag,Classtype){
return TypedSortedBag.decorate(bag,type);
}
public static SortedBagtransformedSortedBag(SortedBagbag,Transformertransformer){
return TransformedSortedBag.decorate(bag,transformer);
}
}