/*
 * Copyright 2014 Realm Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.realm;


import java.util.AbstractList;
import java.util.HashMap;
import java.util.Map;

import io.realm.internal.TableOrView;
import io.realm.internal.TableView;

/**
 *
 * @param <E> The class of objects in this list
 */
public class RealmTableOrViewList<E extends RealmObject> extends AbstractList<E> implements RealmList<E> {

    private Class<E> classSpec;
    private Realm realm;
    private TableOrView table = null;

    RealmTableOrViewList(Realm realm, Class<E> classSpec) {
        this.realm = realm;
        this.classSpec = classSpec;
    }

    RealmTableOrViewList(Realm realm, TableOrView table, Class<E> classSpec) {
        this(realm, classSpec);
        this.table = table;
    }

    Realm getRealm() {
        return realm;
    }

    TableOrView getTable() {

        if(table == null) {
            return realm.getTable(classSpec);
        } else {
            return table;
        }
    }

    @Override
    public void move(int oldPos, int newPos) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean remove(Object o) {
        throw new UnsupportedOperationException();
    }

    Map<String, Class<?>> cache = new HashMap<String, Class<?>>();


    @Override
    public RealmQuery<E> where() {
        return new RealmQuery<E>(this, classSpec);
    }


    @Override
    public E get(int rowIndex) {

        E obj;

        TableOrView table = getTable();
        if(table instanceof TableView) {
            obj = realm.get(classSpec, ((TableView)table).getSourceRowIndex(rowIndex));
        } else {
            obj = realm.get(classSpec, rowIndex);
        }

        return obj;
    }

    @Override
    public E first() {
        return get(0);
    }

    @Override
    public E last() {
        return get(size()-1);
    }

    // Aggregates


    @Override
    public int size() {
        return ((Long)getTable().size()).intValue();
    }

}

@@ -757,16 +757,87 @@
 bleView;
+%0Aimport sun.reflect.generics.reflectiveObjects.NotImplementedException;
 %0A%0A/**%0A *
@@ -1709,118 +1709,8 @@
  %7D%0A%0A
-    @Override%0A    public boolean remove(Object o) %7B%0A        throw new UnsupportedOperationException();%0A    %7D%0A%0A
     
@@ -2478,10 +2478,1430 @@
 %0A    %7D%0A%0A
+%0A%0A    // Sorting%0A    public static enum Order %7B%0A        ascending, descending%0A    %7D%0A%0A    /**%0A     * Get a sorted (ascending) RealmList from an existing RealmList.%0A     *%0A     * @param propName   The property name to sort by.%0A     * @return           A sorted RealmList%0A     */%0A    public RealmList%3CE%3E sort(String propName) %7B%0A        return sort(propName, Order.ascending);%0A    %7D%0A%0A    /**%0A     * Get a sorted RealmList from an existing RealmList.%0A     *%0A     * @param propName   The property name to sort by.%0A     * @param sortOrder  The direction to sort by.%0A     * @return           A sorted RealmList.%0A     */%0A    public RealmList%3CE%3E sort(String propName, Order sortOrder) %7B%0A        throw new NotImplementedException();%0A    %7D%0A%0A%0A    // Deleting%0A    /**%0A     * Removes an object at a given index.%0A     *%0A     * @param index      The array index identifying the object to be removed.%0A     * @return           Always return null.%0A     */%0A    @Override%0A    public E remove(int index) %7B%0A        TableOrView table = getTable();%0A        table.remove(index);%0A        return null;%0A    %7D%0A%0A    /**%0A     * Removes the last object in a RealmList.%0A     *%0A     */%0A    public void removeLast() %7B%0A        TableOrView table = getTable();%0A        table.removeLast();%0A    %7D%0A%0A    /**%0A     * Removes all objects from an RealmList.%0A     *%0A     */%0A    public void clear() %7B%0A        TableOrView table = getTable();%0A        table.clear();%0A    %7D%0A
 %7D%0A

Delete methods