/**
  * 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"));
   }
 }