/*
 * Copyright (c) 2016 NECTEC
 *   National Electronics and Computer Technology Center, Thailand
 *
 * 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 org.tanrabad.survey.utils.collection;

import android.database.Cursor;

import java.util.AbstractList;
import java.util.List;

public class CursorList<T> extends AbstractList<T> {

    private Cursor cursor;
    private CursorMapper<T> mapper;

    public CursorList(Cursor cursor, CursorMapper<T> mapper) {
        this.cursor = cursor;
        this.mapper = mapper;
    }

    public static void close(List list) {
        if (list instanceof CursorList) {
            Cursor cursor = ((CursorList) list).cursor;
            if (cursor != null && !cursor.isClosed())
                cursor.close();
        }
    }

    public Cursor getCursor() {
        return cursor;
    }

    @Override
    public T get(int i) {
        cursor.moveToPosition(i);
        return mapper.map(cursor);
    }

    @Override
    public T set(int location, T object) {
        return null;
    }

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

    @Override
    protected void finalize() throws Throwable {
        try {
            cursor.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        super.finalize();
    }
}

@@ -1378,24 +1378,80 @@
 et(int i) %7B%0A
+        if (cursor.isClosed())%0A            return null;%0A
         curs
@@ -1472,16 +1472,16 @@
 ion(i);%0A
-
         
@@ -1633,24 +1633,77 @@
 nt size() %7B%0A
+        if (cursor.isClosed())%0A            return 0;%0A
         retu
@@ -1794,16 +1794,147 @@
 wable %7B%0A
+        if (!cursor.isClosed())%0A            tryCloseCursor();%0A        super.finalize();%0A    %7D%0A%0A    private void tryCloseCursor() %7B%0A
         
@@ -2030,24 +2030,24 @@
 ackTrace();%0A
+
         %7D%0A  
@@ -2048,34 +2048,8 @@
   %7D%0A
-        super.finalize();%0A
     

check ว่า cursor ปิดรึยังก่อนยุงกับ cursor