/*******************************************************************************
 * This file is part of TERMINAL RECALL
 * Copyright (c) 2015 Chuck Ritola
 * Part of the jTRFP.org project
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     chuck - initial API and implementation
 ******************************************************************************/

package org.jtrfp.trcl.coll;

import java.util.AbstractList;
import java.util.List;

public class SubList<E> extends AbstractList<E> {
    private final List<E> delegate;
    private int startIndex=0, endIndex=0;
    
    public SubList(List<E> delegate, int startIndex, int endIndex){
	this.delegate  =delegate;
	this.startIndex=startIndex;
	this.endIndex  =endIndex;
    }

    @Override
    public E get(int index) {
	checkRangeExclusive(index);
	return delegate.get(index+startIndex);
    }
    
    @Override
    public E remove(int index){
	checkRange(index);
	final E result = delegate.remove(startIndex+index);
	endIndex--;
	return result;
    }
    
    @Override
    public void add(int index, E element){
	checkRange(index);
	delegate.add(startIndex+index, element);
	endIndex++;
    }
    
    @Override
    public E set(int index, E element){
	checkRange(index);
	return delegate.set(startIndex+index, element);
    }

    @Override
    public int size() {
	return endIndex-startIndex;
    }
    
    private void checkRangeExclusive(int index){
	final int size = size();
	if(index>=size)
	    throw new IndexOutOfBoundsException("Index="+index+" size="+size);
	if(index<0)
	    throw new IndexOutOfBoundsException("Index must be positive integer. Got "+index);
    }//end checkRange()
    
    private void checkRange(int index){
	final int size = size();
	if(index>size)
	    throw new IndexOutOfBoundsException("Index="+index+" size="+size);
	if(index<0)
	    throw new IndexOutOfBoundsException("Index must be positive integer. Got "+index);
    }//end checkRange()
    
    @Override
    public List<E> subList(int start, int end){
	if(start<0)
	    throw new IllegalArgumentException("Start index must be positive integer. Got "+start);
	if(start>=size())
	    throw new IllegalArgumentException("Start index must be less than total size. Got "+start);
	if(end<1)
	    throw new IllegalArgumentException("End index must be positive nonzero integer. Got "+end);
	if(end>delegate.size())
	    throw new IllegalArgumentException("End index must be less than total size. Got "+end);
	return new SubList<E>(delegate,startIndex+start,startIndex+end);
    }//end subList(...)

}//end SubList<E>()

@@ -2755,16 +2755,248 @@
 ist(...)
+%0A    %0A    @SuppressWarnings(%22unchecked%22)%0A    @Override%0A    public void clear()%7B%0A%09if(delegate instanceof RangeClearable)%0A%09    ((RangeClearable%3CE%3E)delegate).clearRange(startIndex,endIndex);%0A%09else%0A%09    super.clear();%0A    %7D//end clear()
 %0A%0A%7D//end

Add RangeClearable support to SubList.