Uploaded image for project: 'Realm Java SDK'
  1. Realm Java SDK
  2. RJAVA-177

Default values instead of `null` for max/min

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None

      When writing code that depend on e.g max value being set you often end up having to do something like:

      Number maxValue = realm.where(Person.class).max("id");
      long id = (maxValue != null) ? maxValue.longValue() : -1;
      new Person(++id)
      

      We added default values to first()/last() when the collection was empty and doing the same for max/min/average would help removing annoying code like the above:

      Like this:

      Number maxValue = realm.where(Person.class).max("id", -1)
      new Person(++maxValue);
      

      Proposed API changes

      // Add 4 new methods to RealmQuery
      Number max(String fieldName, Number defaultValue);
      Number min(String fieldName, Number defaultValue);
      Date minimumDate(String fieldName, Date defaultValue);
      Date maximumDate(String fieldName, Date defaultValue);
      
      // Add 4 new methods to RealmCollection
      Number max(String fieldName, Number defaultValue);
      Number min(String fieldName, Number defaultValue);
      Date minDate(String fieldName, Date defaultValue);
      Date maxDate(String fieldName, Date defaultValue);
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            christian.melchior@mongodb.com Christian Melchior (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: