List
内部メソッドの一部を拡張して上書きするクラスを返して、他のクラスをだましてそれが唯一のサブセットであると思わせてみませんか。
たとえば、サブリスト メソッドでは、これを行うことができます...
public List<E> subList(int startPosition, int endPosition) {
return new SmallerList(this,startPosition,endPosition);
}
SmallerList
そのようなクラスを作成します...
public class SmallerList extends List {
List parentList = null;
int startPosition = 0;
int endPosition = 0;
public SmallerList(List parentList, int startPosition, int endPosition){
this.parentList = parentList;
this.startPosition = startPosition;
this.endPosition = endPosition;
}
// overwrite some directly to appear smaller
public int size(){
return endPosition-startPosition;
}
// overwrite others to make adjustments to the correct position in the parentList
public void add(int index, Object object){
parentList.add(index+startPosition,object);
}
// overwrite others to only search between startPosition and endPosition
public boolean contains (Object object){
for (int i=startPosition;i<endPosition;i++){
if (parentList.get(i).equals(object)){
return true;
}
}
return false;
}
// etc. for all other methods of List.
}
このアプローチでは、すべてのメソッドは引き続き基になる で動作しますが、、、 、などparentList
へのクエリはすべて、より小さなものでのみ動作していると考えるようにだまされます。SmallerList
add()
get()
contains()
size()
List