Example #1
0
 /**
  * Finds the maximum date.
  *
  * @param fieldName the field to look for the maximum date. If fieldName is not of Date type, an
  *     exception is thrown.
  * @return if no objects exist or they all have {@code null} as the value for the given date
  *     field, {@code null} will be returned. Otherwise the maximum date is returned. When
  *     determining the maximum date, objects with {@code null} values are ignored.
  * @throws java.lang.IllegalArgumentException if fieldName is not a Date field.
  */
 public Date maxDate(String fieldName) {
   realm.checkIfValid();
   long columnIndex = table.getColumnIndex(fieldName);
   if (table.getColumnType(columnIndex) == RealmFieldType.DATE) {
     return table.maximumDate(columnIndex);
   } else {
     throw new IllegalArgumentException(String.format(TYPE_MISMATCH, fieldName, "Date"));
   }
 }
Example #2
0
 // aux. method used by sort methods
 private long getColumnIndex(String fieldName) {
   if (fieldName.contains(".")) {
     throw new IllegalArgumentException(
         "Sorting using child object properties is not supported: " + fieldName);
   }
   long columnIndex = table.getColumnIndex(fieldName);
   if (columnIndex < 0) {
     throw new IllegalArgumentException(String.format("Field '%s' does not exist.", fieldName));
   }
   return columnIndex;
 }
Example #3
0
 /**
  * Returns the average of a given field.
  *
  * @param fieldName the field to calculate average on. Only number fields are supported.
  * @return the average for the given field amongst objects in query results. This will be of type
  *     double for all types of number fields. If no objects exist or they all have {@code null} as
  *     the value for the given field, {@code 0} will be returned. When computing the average,
  *     objects with {@code null} values are ignored.
  * @throws java.lang.IllegalArgumentException if the field is not a number type.
  */
 public double average(String fieldName) {
   realm.checkIfValid();
   long columnIndex = table.getColumnIndex(fieldName);
   switch (table.getColumnType(columnIndex)) {
     case INTEGER:
       return table.averageLong(columnIndex);
     case DOUBLE:
       return table.averageDouble(columnIndex);
     case FLOAT:
       return table.averageFloat(columnIndex);
     default:
       throw new IllegalArgumentException(
           String.format(TYPE_MISMATCH, fieldName, "int, float or double"));
   }
 }