package com.getbase.android.db.loaders;

import com.getbase.android.db.cursors.Cursors;
import com.google.common.base.Function;

import android.database.Cursor;
import android.support.v4.util.LruCache;

import java.util.AbstractList;
import java.util.RandomAccess;

public class LazyCursorList<T> extends AbstractList<T> implements RandomAccess {

  private final Cursor cursor;
  private final LruCache<Integer, T> cache;

  public LazyCursorList(final Cursor cursor, final Function<Cursor, T> function) {
    this.cursor = Cursors.returnSameOrEmptyIfNull(cursor);

    cache = new LruCache<Integer, T>(256) {
      @Override
      protected T create(Integer key) {
        cursor.moveToPosition(key);
        return function.apply(cursor);
      }
    };
  }

  @Override
  public T get(int i) {
    return cache.get(i);
  }

  @Override
  public int size() {
    return cursor.getCount();
  }
}

@@ -196,16 +196,42 @@
 Cache;%0A%0A
+import java.io.Closeable;%0A
 import j
@@ -363,16 +363,27 @@
 omAccess
+, Closeable
  %7B%0A%0A  pr
@@ -925,11 +925,72 @@
 t();%0A  %7D
+%0A%0A  @Override%0A  public void close() %7B%0A    cursor.close();%0A  %7D
 %0A%7D%0A

LazyCursorList should be Closeable