package com.getbase.android.db.loaders;

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

import android.database.Cursor;

import java.util.AbstractList;

class LazyCursorList<T> extends AbstractList<T> {

  private final Cursor cursor;
  private final Function<Cursor, T> transformation;

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

  @Override
  public T get(int i) {
    // TODO - caching.
    cursor.moveToPosition(i);
    return transformation.apply(cursor);
  }

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

@@ -153,16 +153,57 @@
 .Cursor;
+%0Aimport android.support.v4.util.LruCache;
 %0A%0Aimport
@@ -361,16 +361,60 @@
 rmation;
+%0A  private final LruCache%3CInteger, T%3E cache;
 %0A%0A  publ
@@ -474,24 +474,154 @@
 function) %7B%0A
+    this(cursor, function, 256);%0A  %7D%0A%0A  public LazyCursorList(final Cursor cursor, Function%3CCursor, T%3E function, int cacheSize) %7B%0A
     this.cur
@@ -707,72 +707,119 @@
 on;%0A
+%0A
   
-%7D%0A%0A  @Override%0A  public T get(int i) %7B%0A    // TODO - caching.%0A
+  cache = new LruCache%3CInteger, T%3E(cacheSize) %7B%0A      @Override%0A      protected T create(Integer key) %7B%0A    
     
@@ -844,12 +844,18 @@
 ion(
-i);%0A
+key);%0A    
     
@@ -891,16 +891,97 @@
 ursor);%0A
+      %7D%0A    %7D;%0A  %7D%0A%0A  @Override%0A  public T get(int i) %7B%0A    return cache.get(i);%0A
   %7D%0A%0A  @

Add caching to LazyCursorList