/** * Calculates the sum of a given field. * * @param fieldName the field to sum. Only number fields are supported. * @return the sum. 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 sum, objects with {@code null} values * are ignored. * @throws java.lang.IllegalArgumentException if the field is not a number type. */ public Number sum(String fieldName) { realm.checkIfValid(); long columnIndex = table.getColumnIndex(fieldName); switch (table.getColumnType(columnIndex)) { case INTEGER: return table.sumLong(columnIndex); case FLOAT: return table.sumFloat(columnIndex); case DOUBLE: return table.sumDouble(columnIndex); default: throw new IllegalArgumentException( String.format(TYPE_MISMATCH, fieldName, "int, float or double")); } }