package alobar.workout.db;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import org.apache.commons.lang3.Validate;

import java.util.ArrayList;
import java.util.List;

import alobar.workout.data.Exercise;

import static dagger.internal.Preconditions.checkNotNull;

/**
 * Repository for {@link Exercise} entities
 */
@SuppressWarnings("TryFinallyCanBeTryWithResources")
public class ExerciseRepo {

    private final SQLiteDatabase db;
    private final ContentResolver resolver;

    public ExerciseRepo(SQLiteDatabase db, ContentResolver resolver) {
        this.db = db;
        this.resolver = resolver;
    }

    public List<Exercise> all() {
        Cursor cursor = db.rawQuery("select * from " + DatabaseContract.Exercise.tableName, null);
        try {
            final int idIndex = cursor.getColumnIndexOrThrow(DatabaseContract.Exercise._ID);
            final int nameIndex = cursor.getColumnIndexOrThrow(DatabaseContract.Exercise.NAME);
            final int weightIndex = cursor.getColumnIndexOrThrow(DatabaseContract.Exercise.WEIGHT);
            List<Exercise> result = new ArrayList<>(cursor.getCount());
            while (cursor.moveToNext()) {
                result.add(new Exercise(
                        cursor.getLong(idIndex),
                        cursor.getString(nameIndex),
                        cursor.getDouble(weightIndex)
                ));
            }
            return result;
        } finally {
            cursor.close();
        }
    }

    public Exercise findById(long id) {
        final String query = "select * from " + DatabaseContract.Exercise.tableName +
                " where " + DatabaseContract.Exercise._ID + " = ?";
        Cursor cursor = db.rawQuery(query, new String[]{Long.toString(id)});
        try {
            if (cursor.moveToNext())
                return new Exercise(
                        cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseContract.Exercise._ID)),
                        cursor.getString(cursor.getColumnIndexOrThrow(DatabaseContract.Exercise.NAME)),
                        cursor.getDouble(cursor.getColumnIndexOrThrow(DatabaseContract.Exercise.WEIGHT))
                );
            else return null;
        } finally {
            cursor.close();
        }
    }

    public void save(Exercise exercise) {
        checkNotNull(exercise);
        ContentValues values = new ContentValues();
        values.put(DatabaseContract.Exercise.NAME, exercise.name);
        values.put(DatabaseContract.Exercise.WEIGHT, exercise.weight);
        long _id = exercise._id;
        if (exercise._id == 0) {
            _id = db.insert(DatabaseContract.Exercise.tableName, null, values);
            Validate.isTrue(_id != 0, "Exercise insert failed");
        } else {
            int affected = db.update(DatabaseContract.Exercise.tableName, values, DatabaseContract.Exercise._ID + " = ?", new String[]{Long.toString(exercise._id)});
            Validate.isTrue(affected == 1, "Exercise update failed");
        }
        resolver.notifyChange(DatabaseContract.Exercise.uri(_id), null);
    }

    public void deleteById(long _id) {
        Validate.isTrue(_id != 0, "Cannot delete exercise by id zero");
        int affected = db.delete(DatabaseContract.Exercise.tableName, DatabaseContract.Exercise._ID + " = ?", new String[]{Long.toString(_id)});
        Validate.isTrue(affected == 1, "Exercise delete failed");
        resolver.notifyChange(DatabaseContract.Exercise.uri(_id), null);
    }
}

@@ -427,61 +427,8 @@
  */%0A
-@SuppressWarnings(%22TryFinallyCanBeTryWithResources%22)%0A
 publ
@@ -702,32 +702,37 @@
 all() %7B%0A        
+try (
 Cursor cursor = 
@@ -804,29 +804,17 @@
 e, null)
-;%0A        try
+)
  %7B%0A     
@@ -1473,56 +1473,8 @@
 lt;%0A
-        %7D finally %7B%0A            cursor.close();%0A
     
@@ -1688,16 +1688,21 @@
         
+try (
 Cursor c
@@ -1760,29 +1760,17 @@
 ng(id)%7D)
-;%0A        try
+)
  %7B%0A     
@@ -2167,16 +2167,16 @@
      );%0A
+
         
@@ -2201,56 +2201,8 @@
 ll;%0A
-        %7D finally %7B%0A            cursor.close();%0A
     

Change to java 8 try-with-resources