<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->publicinterfaceBagextendsCollection
{
intgetCount(Objectobject);
booleanadd(Objectobject);
booleanadd(Objectobject,intnCopies);
booleanremove(Objectobject);
booleanremove(Objectobject,intnCopies);
SetuniqueSet();
intsize();
booleancontainsAll(Collectioncoll);
booleanremoveAll(Collectioncoll);
booleanretainAll(Collectioncoll);
Iteratoriterator();
}
{
intgetCount(Objectobject);
booleanadd(Objectobject);
booleanadd(Objectobject,intnCopies);
booleanremove(Objectobject);
booleanremove(Objectobject,intnCopies);
SetuniqueSet();
intsize();
booleancontainsAll(Collectioncoll);
booleanremoveAll(Collectioncoll);
booleanretainAll(Collectioncoll);
Iteratoriterator();
}
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->publicinterfaceSortedBagextendsBag
{
publicComparatorcomparator();
publicObjectfirst();
publicObjectlast();
}
{
publicComparatorcomparator();
publicObjectfirst();
publicObjectlast();
}
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->publicabstractclassDefaultMapBagimplementsBag
{
privateMap_map=null;//底层数据存储区
privateint_total=0;//元素总个数
privateint_mods=0;//修改次数
publicDefaultMapBag(){
}
protectedDefaultMapBag(Mapmap){
setMap(map);
}
publicbooleanadd(Objectobject){
returnadd(object,1);
}
publicbooleanadd(Objectobject,intnCopies){
_mods++;
if(nCopies>0){
intcount=(nCopies+getCount(object));
_map.put(object,newInteger(count));
_total+=nCopies;
return(count==nCopies);
}else{
returnfalse;
}
}
publicbooleanaddAll(Collectioncoll){
booleanchanged=false;
Iteratori=coll.iterator();
while(i.hasNext()){
booleanadded=add(i.next());
changed=changed||added;
}
returnchanged;
}
publicvoidclear(){
_mods++;
_map.clear();
_total=0;
}
publicbooleancontains(Objectobject){
return_map.containsKey(object);
}
publicbooleancontainsAll(Collectioncoll){
returncontainsAll(newHashBag(coll));
}
publicbooleancontainsAll(Bagother){
booleanresult=true;
Iteratori=other.uniqueSet().iterator();
while(i.hasNext()){
Objectcurrent=i.next();
booleancontains=getCount(current)>=other.getCount(current);
result=result&&contains;
}
returnresult;
}
publicbooleanequals(Objectobject){
if(object==this){
returntrue;
}
if(objectinstanceofBag==false){
returnfalse;
}
Bagother=(Bag)object;
if(other.size()!=size()){
returnfalse;
}
for(Iteratorit=_map.keySet().iterator();it.hasNext();){
Objectelement=it.next();
if(other.getCount(element)!=getCount(element)){
returnfalse;
}
}
returntrue;
}
publicinthashCode(){
return_map.hashCode();
}
publicbooleanisEmpty(){
return_map.isEmpty();
}
publicIteratoriterator(){
returnnewBagIterator(this,extractList().iterator());
}
staticclassBagIteratorimplementsIterator{
privateDefaultMapBag_parent=null;
privateIterator_support=null;//原始迭代器
privateObject_current=null;//当前元素
privateint_mods=0;
publicBagIterator(DefaultMapBagparent,Iteratorsupport){
_parent=parent;
_support=support;
_current=null;
_mods=parent.modCount();
}
publicbooleanhasNext(){
return_support.hasNext();
}
publicObjectnext(){
if(_parent.modCount()!=_mods){
thrownewConcurrentModificationException();
}
_current=_support.next();
return_current;
}
publicvoidremove(){
if(_parent.modCount()!=_mods){
thrownewConcurrentModificationException();
}
_support.remove();
_parent.remove(_current,1);
_mods++;
}
}
publicbooleanremove(Objectobject){
returnremove(object,getCount(object));
}
publicbooleanremove(Objectobject,intnCopies){
_mods++;
booleanresult=false;
intcount=getCount(object);
if(nCopies<=0){
result=false;
}elseif(count>nCopies){
_map.put(object,newInteger(count-nCopies));
result=true;
_total-=nCopies;
}else{//count>0&&count<=i
//needtoremoveall
result=(_map.remove(object)!=null);
_total-=count;
}
returnresult;
}
publicbooleanremoveAll(Collectioncoll){
booleanresult=false;
if(coll!=null){
Iteratori=coll.iterator();
while(i.hasNext()){
booleanchanged=remove(i.next(),1);
result=result||changed;
}
}
returnresult;
}
publicbooleanretainAll(Collectioncoll){
returnretainAll(newHashBag(coll));
}
publicbooleanretainAll(Bagother){
booleanresult=false;
Bagexcess=newHashBag();
Iteratori=uniqueSet().iterator();
while(i.hasNext()){
Objectcurrent=i.next();
intmyCount=getCount(current);
intotherCount=other.getCount(current);
if(1<=otherCount&&otherCount<=myCount){
excess.add(current,myCount-otherCount);
}else{
excess.add(current,myCount);
}
}
if(!excess.isEmpty()){
result=removeAll(excess);
}
returnresult;
}
publicObject[]toArray(){
returnextractList().toArray();
}
publicObject[]toArray(Object[]array){
returnextractList().toArray(array);
}
publicintgetCount(Objectobject){
intresult=0;
Integercount=MapUtils.getInteger(_map,object);
if(count!=null){
result=count.intValue();
}
returnresult;
}
publicSetuniqueSet(){
returnUnmodifiableSet.decorate(_map.keySet());
}
publicintsize(){
return_total;
}
protectedintcalcTotalSize(){
_total=extractList().size();
return_total;
}
protectedvoidsetMap(Mapmap){
if(map==null||map.isEmpty()==false){
thrownewIllegalArgumentException("Themapmustbenon-nullandempty");
}
_map=map;
}
protectedMapgetMap(){
return_map;
}
privateListextractList(){
Listresult=newArrayList();
Iteratori=uniqueSet().iterator();
while(i.hasNext()){
Objectcurrent=i.next();
for(intindex=getCount(current);index>0;index--){
result.add(current);
}
}
returnresult;
}
privateintmodCount(){
return_mods;
}
publicStringtoString(){
StringBufferbuf=newStringBuffer();
buf.append("[");
Iteratori=uniqueSet().iterator();
while(i.hasNext()){
Objectcurrent=i.next();
intcount=getCount(current);
buf.append(count);
buf.append(":");
buf.append(current);
if(i.hasNext()){
buf.append(",");
}
}
buf.append("]");
returnbuf.toString();
}
}
{
privateMap_map=null;//底层数据存储区
privateint_total=0;//元素总个数
privateint_mods=0;//修改次数
publicDefaultMapBag(){
}
protectedDefaultMapBag(Mapmap){
setMap(map);
}
publicbooleanadd(Objectobject){
returnadd(object,1);
}
publicbooleanadd(Objectobject,intnCopies){
_mods++;
if(nCopies>0){
intcount=(nCopies+getCount(object));
_map.put(object,newInteger(count));
_total+=nCopies;
return(count==nCopies);
}else{
returnfalse;
}
}
publicbooleanaddAll(Collectioncoll){
booleanchanged=false;
Iteratori=coll.iterator();
while(i.hasNext()){
booleanadded=add(i.next());
changed=changed||added;
}
returnchanged;
}
publicvoidclear(){
_mods++;
_map.clear();
_total=0;
}
publicbooleancontains(Objectobject){
return_map.containsKey(object);
}
publicbooleancontainsAll(Collectioncoll){
returncontainsAll(newHashBag(coll));
}
publicbooleancontainsAll(Bagother){
booleanresult=true;
Iteratori=other.uniqueSet().iterator();
while(i.hasNext()){
Objectcurrent=i.next();
booleancontains=getCount(current)>=other.getCount(current);
result=result&&contains;
}
returnresult;
}
publicbooleanequals(Objectobject){
if(object==this){
returntrue;
}
if(objectinstanceofBag==false){
returnfalse;
}
Bagother=(Bag)object;
if(other.size()!=size()){
returnfalse;
}
for(Iteratorit=_map.keySet().iterator();it.hasNext();){
Objectelement=it.next();
if(other.getCount(element)!=getCount(element)){
returnfalse;
}
}
returntrue;
}
publicinthashCode(){
return_map.hashCode();
}
publicbooleanisEmpty(){
return_map.isEmpty();
}
publicIteratoriterator(){
returnnewBagIterator(this,extractList().iterator());
}
staticclassBagIteratorimplementsIterator{
privateDefaultMapBag_parent=null;
privateIterator_support=null;//原始迭代器
privateObject_current=null;//当前元素
privateint_mods=0;
publicBagIterator(DefaultMapBagparent,Iteratorsupport){
_parent=parent;
_support=support;
_current=null;
_mods=parent.modCount();
}
publicbooleanhasNext(){
return_support.hasNext();
}
publicObjectnext(){
if(_parent.modCount()!=_mods){
thrownewConcurrentModificationException();
}
_current=_support.next();
return_current;
}
publicvoidremove(){
if(_parent.modCount()!=_mods){
thrownewConcurrentModificationException();
}
_support.remove();
_parent.remove(_current,1);
_mods++;
}
}
publicbooleanremove(Objectobject){
returnremove(object,getCount(object));
}
publicbooleanremove(Objectobject,intnCopies){
_mods++;
booleanresult=false;
intcount=getCount(object);
if(nCopies<=0){
result=false;
}elseif(count>nCopies){
_map.put(object,newInteger(count-nCopies));
result=true;
_total-=nCopies;
}else{//count>0&&count<=i
//needtoremoveall
result=(_map.remove(object)!=null);
_total-=count;
}
returnresult;
}
publicbooleanremoveAll(Collectioncoll){
booleanresult=false;
if(coll!=null){
Iteratori=coll.iterator();
while(i.hasNext()){
booleanchanged=remove(i.next(),1);
result=result||changed;
}
}
returnresult;
}
publicbooleanretainAll(Collectioncoll){
returnretainAll(newHashBag(coll));
}
publicbooleanretainAll(Bagother){
booleanresult=false;
Bagexcess=newHashBag();
Iteratori=uniqueSet().iterator();
while(i.hasNext()){
Objectcurrent=i.next();
intmyCount=getCount(current);
intotherCount=other.getCount(current);
if(1<=otherCount&&otherCount<=myCount){
excess.add(current,myCount-otherCount);
}else{
excess.add(current,myCount);
}
}
if(!excess.isEmpty()){
result=removeAll(excess);
}
returnresult;
}
publicObject[]toArray(){
returnextractList().toArray();
}
publicObject[]toArray(Object[]array){
returnextractList().toArray(array);
}
publicintgetCount(Objectobject){
intresult=0;
Integercount=MapUtils.getInteger(_map,object);
if(count!=null){
result=count.intValue();
}
returnresult;
}
publicSetuniqueSet(){
returnUnmodifiableSet.decorate(_map.keySet());
}
publicintsize(){
return_total;
}
protectedintcalcTotalSize(){
_total=extractList().size();
return_total;
}
protectedvoidsetMap(Mapmap){
if(map==null||map.isEmpty()==false){
thrownewIllegalArgumentException("Themapmustbenon-nullandempty");
}
_map=map;
}
protectedMapgetMap(){
return_map;
}
privateListextractList(){
Listresult=newArrayList();
Iteratori=uniqueSet().iterator();
while(i.hasNext()){
Objectcurrent=i.next();
for(intindex=getCount(current);index>0;index--){
result.add(current);
}
}
returnresult;
}
privateintmodCount(){
return_mods;
}
publicStringtoString(){
StringBufferbuf=newStringBuffer();
buf.append("[");
Iteratori=uniqueSet().iterator();
while(i.hasNext()){
Objectcurrent=i.next();
intcount=getCount(current);
buf.append(count);
buf.append(":");
buf.append(current);
if(i.hasNext()){
buf.append(",");
}
}
buf.append("]");
returnbuf.toString();
}
}
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->publicclassHashBagextendsDefaultMapBagimplementsBag
{
publicHashBag(){
super(newHashMap());
}
publicHashBag(Collectioncoll){
this();
addAll(coll);
}
}
{
publicHashBag(){
super(newHashMap());
}
publicHashBag(Collectioncoll){
this();
addAll(coll);
}
}
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->publicclassTreeBagextendsDefaultMapBagimplementsSortedBag
{
publicTreeBag(){
super(newTreeMap());
}
publicTreeBag(Comparatorcomparator){
super(newTreeMap(comparator));
}
publicTreeBag(Collectioncoll){
this();
addAll(coll);
}
publicObjectfirst(){
return((SortedMap)getMap()).firstKey();
}
publicObjectlast(){
return((SortedMap)getMap()).lastKey();
}
publicComparatorcomparator(){
return((SortedMap)getMap()).comparator();
}
}
{
publicTreeBag(){
super(newTreeMap());
}
publicTreeBag(Comparatorcomparator){
super(newTreeMap(comparator));
}
publicTreeBag(Collectioncoll){
this();
addAll(coll);
}
publicObjectfirst(){
return((SortedMap)getMap()).firstKey();
}
publicObjectlast(){
return((SortedMap)getMap()).lastKey();
}
publicComparatorcomparator(){
return((SortedMap)getMap()).comparator();
}
}
使用decorate模式的Bag工具类
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->publicclassBagUtils
{
/**
*Anemptyunmodifiablebag.
*/
publicstaticfinalBagEMPTY_BAG=UnmodifiableBag.decorate(newHashBag());
/**
*Anemptyunmodifiablesortedbag.
*/
publicstaticfinalBagEMPTY_SORTED_BAG=UnmodifiableSortedBag.decorate(newTreeBag());
publicBagUtils(){//这里按常理不应该是public,应该是private才对,作者应该有其他地方要用到吧
}
publicstaticBagsynchronizedBag(Bagbag){
returnSynchronizedBag.decorate(bag);
}
publicstaticBagunmodifiableBag(Bagbag){
returnUnmodifiableBag.decorate(bag);
}
publicstaticBagpredicatedBag(Bagbag,Predicatepredicate){
returnPredicatedBag.decorate(bag,predicate);
}
publicstaticBagtypedBag(Bagbag,Classtype){
returnTypedBag.decorate(bag,type);
}
publicstaticBagtransformedBag(Bagbag,Transformertransformer){
returnTransformedBag.decorate(bag,transformer);
}
publicstaticSortedBagsynchronizedSortedBag(SortedBagbag){
returnSynchronizedSortedBag.decorate(bag);
}
publicstaticSortedBagunmodifiableSortedBag(SortedBagbag){
returnUnmodifiableSortedBag.decorate(bag);
}
publicstaticSortedBagpredicatedSortedBag(SortedBagbag,Predicatepredicate){
returnPredicatedSortedBag.decorate(bag,predicate);
}
publicstaticSortedBagtypedSortedBag(SortedBagbag,Classtype){
returnTypedSortedBag.decorate(bag,type);
}
publicstaticSortedBagtransformedSortedBag(SortedBagbag,Transformertransformer){
returnTransformedSortedBag.decorate(bag,transformer);
}
}
{
/**
*Anemptyunmodifiablebag.
*/
publicstaticfinalBagEMPTY_BAG=UnmodifiableBag.decorate(newHashBag());
/**
*Anemptyunmodifiablesortedbag.
*/
publicstaticfinalBagEMPTY_SORTED_BAG=UnmodifiableSortedBag.decorate(newTreeBag());
publicBagUtils(){//这里按常理不应该是public,应该是private才对,作者应该有其他地方要用到吧
}
publicstaticBagsynchronizedBag(Bagbag){
returnSynchronizedBag.decorate(bag);
}
publicstaticBagunmodifiableBag(Bagbag){
returnUnmodifiableBag.decorate(bag);
}
publicstaticBagpredicatedBag(Bagbag,Predicatepredicate){
returnPredicatedBag.decorate(bag,predicate);
}
publicstaticBagtypedBag(Bagbag,Classtype){
returnTypedBag.decorate(bag,type);
}
publicstaticBagtransformedBag(Bagbag,Transformertransformer){
returnTransformedBag.decorate(bag,transformer);
}
publicstaticSortedBagsynchronizedSortedBag(SortedBagbag){
returnSynchronizedSortedBag.decorate(bag);
}
publicstaticSortedBagunmodifiableSortedBag(SortedBagbag){
returnUnmodifiableSortedBag.decorate(bag);
}
publicstaticSortedBagpredicatedSortedBag(SortedBagbag,Predicatepredicate){
returnPredicatedSortedBag.decorate(bag,predicate);
}
publicstaticSortedBagtypedSortedBag(SortedBagbag,Classtype){
returnTypedSortedBag.decorate(bag,type);
}
publicstaticSortedBagtransformedSortedBag(SortedBagbag,Transformertransformer){
returnTransformedSortedBag.decorate(bag,transformer);
}
}
本文介绍了一种Bag接口及其几种实现方式,包括DefaultMapBag、HashBag和TreeBag等。这些实现支持元素的重复添加和移除操作,并提供了遍历、查询等功能。通过装饰器模式,可以进一步增强Bag的功能。
1212

被折叠的 条评论
为什么被折叠?



