package coll;
/* Immutable Collection
 * by Martin Robinson
 * \__________________/
 *       /
 * \ ('<
 * (<=)
 * */

/*
 * array list, this one
 * will hold an array and
 * return value by index
 * \___________________/
 *      /
 * \ ('<
 * (<=)
 * */
/**
 * @author martin
 * Array List
 * @param <E> as type
 */
public final class ArrLst<E> extends Coll<E>{

	private final E[] _arr;

	/**
	 * private constructor
	 * @param arr as array
	 */
	private ArrLst(final E[] arr){
		_arr = arr;
	}

	/**
	 * static factory
	 * @param s as size of the array
	 * @return new list
	 */
	public static <E> Lst<E> fromSize(int size){
		if (size < 0)
			throw new IllegalArgumentException();
		//return new ArrLst(new Object[size]);
		return EmptyLst.fromSize(size);
	}
	/**
	 * static factory
	 * @param lst as list to duplicate
	 * @return new duplicated and optimised list
	 */
	public static <E> Lst<E> fromList(final Lst<E> lst){
		final Object [] nlst = new Object[lst.size()];
		for (int i=0;i<lst.size();i++)
			nlst[i] = lst.get(i);
		return new ArrLst(nlst);
	}
	/* (non-Javadoc)
	 * @see array.Lst#get(int)
	 */
	public E get(int i){
		if (i>=_arr.length)
			return null;
			//throw new Exception();
		return _arr[i];
	}
	/* (non-Javadoc)
	 * @see array.Lst#size()
	 */
	public int size() {
		return _arr.length;
	}
	/* (non-Javadoc)
	 * @see array.Lst#undo()
	 */
	public Lst<E> undo(){
		return null;
	}
}
