package org.flymine.objectstore.proxy;

/*
 * Copyright (C) 2002-2003 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.util.List;
import java.util.Set;
import java.util.AbstractList;
import java.util.Iterator;
import java.util.ListIterator;

import org.flymine.objectstore.query.Query;
import org.flymine.objectstore.query.QueryClass;

/**
 * LazyCollection is used in paging of business object collections in materialization.
 *
 * @author Richard Smith
 */

public class LazyCollection extends AbstractList implements Set
{
    private Query query;

    /**
     * Construct with a flymine Query object.  NullPointerException if this
     * is null.  Checks that query returns one and only one object type.
     *
     * @param query a flymine query to get contents of the collection
     */
    public LazyCollection(Query query) {
        if (query == null) {
            throw (new NullPointerException("Query cannot be null"));
        }

        List select = query.getSelect();
        if (select.size() < 1) {
            throw (new IllegalArgumentException("Query has no items in select list."));
        }
        if (select.size() > 1) {
            throw (new IllegalArgumentException("Query has more than item in select list."));
        }
        if (!(select.get(0) instanceof QueryClass)) {
            throw (new IllegalArgumentException("Query can only select a QueryClass"));
        }

        this.query = query;
    }


    /**
     * Get the query that populates collection
     *
     * @return the query
     */
    public Query getQuery() {
        return query;
    }

    /**
     * iterator method not valid for a LazyCollection, throws an UnsupportedOperationException
     *
     * @return nothing
     */
    public Iterator iterator() {
        throw (new UnsupportedOperationException("Method not supported by LazyCollection"));
    }

    /**
     * get method not valid for a LazyCollection, throws an UnsupportedOperationException
     *
     * @param i index of object to get
     * @return nothing
     */
    public Object get(int i) {
        throw (new UnsupportedOperationException("Method not supported by LazyCollection"));
    }

    /**
     * size method not valid for a LazyCollection, throws an UnsupportedOperationException
     *
     * @return nothing
     */
    public int size() {
        throw (new UnsupportedOperationException("Method not supported by LazyCollection"));
    }

    /**
     * ListIterator method not valid for a LazyCollection, throws an UnsupportedOperationException
     *
     * @return nothing
     */
    public ListIterator listIterator() {
        throw (new UnsupportedOperationException("Method not supported by LazyCollection"));
    }

    /**
     * ListIterator method not valid for a LazyCollection, throws an UnsupportedOperationException
     *
     * @param index teh index
     * @return nothing
     */
    public ListIterator listIterator(int index) {
        throw (new UnsupportedOperationException("Method not supported by LazyCollection"));
    }

    /**
     * subList method not valid for a LazyCollection, throws an UnsupportedOperationException
     *
     * @param fromIndex from here
     * @param toIndex toHere
     * @return nothing
     */
    public List subList(int fromIndex, int toIndex) {
        throw (new UnsupportedOperationException("Method not supported by LazyCollection"));
    }

   /**
     * removeRange method not valid for a LazyCollection, throws an UnsupportedOperationException
     *
     * @param fromIndex from here
     * @param toIndex toHere
     */
    public void removeRange(int fromIndex, int toIndex) {
        throw (new UnsupportedOperationException("Method not supported by LazyCollection"));
    }

    /**
     * indexOf method not valid for a LazyCollection, throws an UnsupportedOperationException
     *
     * @param obj an object
     * @return nothing
     */
    public int indexOf(Object obj) {
        throw (new UnsupportedOperationException("Method not supported by LazyCollection"));
    }

    /**
     * lastIndexOf method not valid for a LazyCollection, throws an UnsupportedOperationException
     *
     * @param obj an object
     * @return nothing
     */
    public int lastIndexOf(Object obj) {
        throw (new UnsupportedOperationException("Method not supported by LazyCollection"));
    }

}

@@ -410,66 +410,8 @@
 ist;
-%0Aimport java.util.Iterator;%0Aimport java.util.ListIterator;
 %0A%0Aim
@@ -1608,17 +1608,16 @@
 %0A    %7D%0A%0A
-%0A
     /**%0A
@@ -1766,282 +1766,8 @@
  %7D%0A%0A
-    /**%0A     * iterator method not valid for a LazyCollection, throws an UnsupportedOperationException%0A     *%0A     * @return nothing%0A     */%0A    public Iterator iterator() %7B%0A        throw (new UnsupportedOperationException(%22Method not supported by LazyCollection%22));%0A    %7D%0A%0A
     
@@ -1974,33 +1974,32 @@
 %7B%0A        throw 
-(
 new UnsupportedO
@@ -2009,33 +2009,39 @@
 ationException(%22
-M
+get() m
 ethod not suppor
@@ -2055,33 +2055,32 @@
 LazyCollection%22)
-)
 ;%0A    %7D%0A%0A    /**
@@ -2239,33 +2239,32 @@
 %7B%0A        throw 
-(
 new UnsupportedO
@@ -2286,620 +2286,16 @@
 on(%22
-Method not supported by LazyCollection%22));%0A    %7D%0A%0A    /**%0A     * ListIterator method not valid for a LazyCollection, throws an UnsupportedOperationException%0A     *%0A     * @return nothing%0A     */%0A    public ListIterator listIterator() %7B%0A        throw (new UnsupportedOperationException(%22Method not supported by LazyCollection%22));%0A    %7D%0A%0A    /**%0A     * ListIterator method not valid for a LazyCollection, throws an UnsupportedOperationException%0A     *%0A     * @param index teh index%0A     * @return nothing%0A     */%0A    public ListIterator listIterator(int index) %7B%0A        throw (new UnsupportedOperationException(%22M
+size() m
 etho
@@ -2325,33 +2325,32 @@
 Collection%22)
-)
 ;%0A    %7D%0A
 %0A    /**%0A   
@@ -2341,1323 +2341,6 @@
   %7D%0A
-%0A    /**%0A     * subList method not valid for a LazyCollection, throws an UnsupportedOperationException%0A     *%0A     * @param fromIndex from here%0A     * @param toIndex toHere%0A     * @return nothing%0A     */%0A    public List subList(int fromIndex, int toIndex) %7B%0A        throw (new UnsupportedOperationException(%22Method not supported by LazyCollection%22));%0A    %7D%0A%0A   /**%0A     * removeRange method not valid for a LazyCollection, throws an UnsupportedOperationException%0A     *%0A     * @param fromIndex from here%0A     * @param toIndex toHere%0A     */%0A    public void removeRange(int fromIndex, int toIndex) %7B%0A        throw (new UnsupportedOperationException(%22Method not supported by LazyCollection%22));%0A    %7D%0A%0A    /**%0A     * indexOf method not valid for a LazyCollection, throws an UnsupportedOperationException%0A     *%0A     * @param obj an object%0A     * @return nothing%0A     */%0A    public int indexOf(Object obj) %7B%0A        throw (new UnsupportedOperationException(%22Method not supported by LazyCollection%22));%0A    %7D%0A%0A    /**%0A     * lastIndexOf method not valid for a LazyCollection, throws an UnsupportedOperationException%0A     *%0A     * @param obj an object%0A     * @return nothing%0A     */%0A    public int lastIndexOf(Object obj) %7B%0A        throw (new UnsupportedOperationException(%22Method not supported by LazyCollection%22));%0A    %7D%0A%0A
 %7D%0A

tidy up