/*******************************************************************************
* Copyright (c)                                                                *
*           2005   Tristan Allwood,                                            *
*           2005   Daniel Burke,                                               *
*           2005   Marc Hull,                                                  *
*           2005   Ekaterina Itskova,                                          *
*           2005   Steve Zymler                                                *
*                                                                              *
* Permission is hereby granted, free of charge, to any person obtaining a      *
* copy of this software and associated documentation files (the                *
* "Software"), to deal in the Software without restriction, including          *
* without limitation the rights to use, copy, modify, merge, publish,          *
* distribute, sublicense, and/or sell copies of the Software, and to           *
* permit persons to whom the Software is furnished to do so, subject to        *
* the following conditions:                                                    *
*                                                                              *
* The above copyright notice and this permission notice shall be included      *
* in all copies or substantial portions of the Software.                       *
*                                                                              *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS      *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF                   *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.       *
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY         *
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,         *
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE            *
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                       *
*******************************************************************************/
/*
 * Created on 19-Oct-2004
 *
 */
package uk.ac.ic.doc.kevlar.publicUtil.collections;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import uk.ac.ic.doc.kevlar.publicUtil.strings.StringUtil;


/**
 * @author Daniel Burke
 */
public final class ImmutableArrayList<T> implements IImmutableList<T>, Serializable {
	
	/**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = -3296468311616695676L;
   
    private List<T> backend;
	public ImmutableArrayList(List<T> data)
	{
		backend = new ArrayList<T>(data);
	}
	
	public ImmutableArrayList(Collection<T> data)
	{
		backend = new ArrayList<T>();
		for(T item : data)
		{
		    backend.add(item);
		}
	}
	
	public boolean contains(Object o) {
		return backend.contains(o);
	}

	public boolean containsAll(Collection< T > c) {
		return backend.containsAll(c);
	}

	public T get(int index) {
		return backend.get(index);
	}

	public int indexOf(Object o) {
		return backend.indexOf(o);
	}

	public boolean isEmpty() {
		return backend.isEmpty();
	}

	public Iterator<T> iterator() {
		return new ImmutableIterator<T>(backend.iterator());
	}

	public int lastIndexOf(Object o) {
		return backend.lastIndexOf(o);
	}
	
	public int size() {
	
		return backend.size();
	}
	
	public T[] toArray(T[] a) {
		
		return backend.toArray(a);
	}
	
	public String toString()
	{
		StringBuilder str = new StringBuilder(200);
		String nl = StringUtil.NEWLINE;
		str.append("[").append(nl);
		
		for(Object o : backend)
		{
			str.append(o).append(",").append(nl);
		}
		
		if(backend.size() > 0)
		{
			str.delete(
			        str.length() - 1 - StringUtil.NEWLINE.length(),
			        str.length());
			str.append(nl);
		}
		
		str.append("]").append(nl);
		
		return str.toString();
	}

}
