Skip to content

Instantly share code, notes, and snippets.

@carloseduardosx
Last active January 18, 2021 05:35
Show Gist options
  • Select an option

  • Save carloseduardosx/a7bd88d7337660cd10a2c5dcc580ebd0 to your computer and use it in GitHub Desktop.

Select an option

Save carloseduardosx/a7bd88d7337660cd10a2c5dcc580ebd0 to your computer and use it in GitHub Desktop.

Revisions

  1. carloseduardosx revised this gist Jun 15, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions RealmAutoIncrement.java
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    package com.carlosedurdo.database;

    import java.util.HashMap;
    import java.util.Map;
    import java.util.concurrent.atomic.AtomicInteger;
  2. carloseduardosx revised this gist Jun 15, 2016. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions RealmAutoIncrement.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    package com.greenmile.driver.database;

    import java.util.HashMap;
    import java.util.Map;
    import java.util.concurrent.atomic.AtomicInteger;
  3. carloseduardosx revised this gist Jun 15, 2016. 1 changed file with 0 additions and 8 deletions.
    8 changes: 0 additions & 8 deletions RealmAutoIncrement.java
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,5 @@
    package com.greenmile.driver.database;

    import android.provider.BaseColumns;
    import android.util.Log;

    import java.util.HashMap;
    import java.util.Map;
    import java.util.concurrent.atomic.AtomicInteger;
    @@ -43,9 +40,6 @@ private int getLastIdFromModel(Class<? extends RealmObject> clazz) {
    String primaryKeyColumnName = "id";
    Number lastId = realm.where(clazz).max(primaryKeyColumnName);

    if (lastId != null) {
    Log.i("RealmAutoIncrement", "getLastIdFromModel: " + lastId.intValue());
    }
    return lastId == null ? 0 : lastId.intValue();
    }

    @@ -64,10 +58,8 @@ public Integer getNextIdFromModel(Class<? extends RealmObject> clazz) {
    AtomicInteger modelId = modelMap.get(clazz);

    if (modelId == null) {
    Log.i("RealmAutoIncrement", "No model mapped to: " + clazz.getSimpleName());
    return 0;
    }

    return modelId.incrementAndGet();
    }
    return null;
  4. carloseduardosx revised this gist Jun 15, 2016. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions RealmAutoIncrement.java
    Original file line number Diff line number Diff line change
    @@ -39,8 +39,9 @@ private RealmAutoIncrement() {
    * @return The last id saved from model passed
    */
    private int getLastIdFromModel(Class<? extends RealmObject> clazz) {

    Number lastId = realm.where(clazz).max(BaseColumns._ID);

    String primaryKeyColumnName = "id";
    Number lastId = realm.where(clazz).max(primaryKeyColumnName);

    if (lastId != null) {
    Log.i("RealmAutoIncrement", "getLastIdFromModel: " + lastId.intValue());
  5. carloseduardosx created this gist Jun 15, 2016.
    102 changes: 102 additions & 0 deletions RealmAutoIncrement.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,102 @@
    package com.greenmile.driver.database;

    import android.provider.BaseColumns;
    import android.util.Log;

    import java.util.HashMap;
    import java.util.Map;
    import java.util.concurrent.atomic.AtomicInteger;

    import io.realm.Realm;
    import io.realm.RealmObject;

    /**
    * Implementation of Auto Increment feature for Realm
    * RealmAutoIncrement is a singleton which maintain the last id saved from each database model.
    *
    * To get next id from anyone model, simple call the method getNextIdFromModel.
    * @see #getNextIdFromModel(Class)
    */
    public final class RealmAutoIncrement {

    private Realm realm;
    private Map<Class<? extends RealmObject>, AtomicInteger> modelMap = new HashMap<>();
    private static RealmAutoIncrement autoIncrementMap;

    private RealmAutoIncrement() {
    }

    {
    realm = Realm.getDefaultInstance();
    modelMap.put(SampleModel.class, new AtomicInteger(getLastIdFromModel(SampleModel.class)));
    }

    /**
    * Utility method which query for all models saved and get the bigger model id saved
    * Used to guarantee which the last model id saved is really the last
    *
    * @param clazz Model which should get the last id
    * @return The last id saved from model passed
    */
    private int getLastIdFromModel(Class<? extends RealmObject> clazz) {

    Number lastId = realm.where(clazz).max(BaseColumns._ID);

    if (lastId != null) {
    Log.i("RealmAutoIncrement", "getLastIdFromModel: " + lastId.intValue());
    }
    return lastId == null ? 0 : lastId.intValue();
    }

    /**
    * Search in modelMap for the last saved id from model passed and return the next one
    *
    * @param clazz Model to search the last id
    * @return The next id which can be saved in database for that model,
    * {@code null} will be returned when this method is called by reflection
    */
    public Integer getNextIdFromModel(Class<? extends RealmObject> clazz) {


    if (isValidMethodCall()) {

    AtomicInteger modelId = modelMap.get(clazz);

    if (modelId == null) {
    Log.i("RealmAutoIncrement", "No model mapped to: " + clazz.getSimpleName());
    return 0;
    }

    return modelId.incrementAndGet();
    }
    return null;
    }

    /**
    * Utility method to validate if the method is called from reflection,
    * in this case is considered a not valid call otherwise is a valid call
    *
    * @return The boolean which define if the method call is valid or not
    */
    private boolean isValidMethodCall() {

    StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();

    for (StackTraceElement stackTraceElement : stackTraceElements) {

    if (stackTraceElement.getMethodName().equals("newInstance")) {
    return false;
    }
    }
    return true;
    }

    public static RealmAutoIncrement getInstance() {

    if (autoIncrementMap == null) {
    autoIncrementMap = new RealmAutoIncrement();
    }

    return autoIncrementMap;
    }
    }