/**
  * Removes an object at a given index. This also deletes the object from the underlying Realm.
  *
  * <p>Using this method while iterating the list can result in a undefined behavior. Use {@link
  * io.realm.RealmResults.RealmResultsIterator#remove()} instead.
  *
  * @param index the array index identifying the object to be removed.
  * @return always return {@code null}.
  * @throws IllegalStateException if the corresponding Realm is closed or in an incorrect thread.
  */
 @Override
 public E remove(int index) {
   realm.checkIfValid();
   TableOrView table = getTable();
   table.remove(index);
   return null; // Returning the object doesn't make sense, since it could no longer access any
                // data.
 }
 /**
  * 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"));
   }
 }
 /**
  * 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"));
   }
 }
  private RealmResults(BaseRealm realm, TableOrView table, Class<E> classSpec) {
    this.realm = realm;
    this.classSpec = classSpec;
    this.table = table;

    this.pendingQuery = null;
    this.query = null;
    this.currentTableViewVersion = table.sync();
  }
 // 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;
 }
  /** Notifies all registered listeners. */
  void notifyChangeListeners() {
    if (listeners != null && !listeners.isEmpty()) {
      // table might be null (if the async query didn't complete
      // but we have already registered listeners for it)
      if (pendingQuery != null && !isCompleted) return;

      // FIXME: still waiting for Core to provide a fix
      //       for crash when calling _sync_if_needed on a cleared View.
      //       https://github.com/realm/realm-core/pull/1390
      long version = table.sync();
      if (currentTableViewVersion != version) {
        currentTableViewVersion = version;
        for (RealmChangeListener listener : listeners) {
          listener.onChange();
        }
      }
    }
  }
 /**
  * Removes all objects from the list. This also deletes the objects from the underlying Realm.
  *
  * @throws IllegalStateException if the corresponding Realm is closed or in an incorrect thread.
  */
 public void clear() {
   realm.checkIfValid();
   TableOrView table = getTable();
   table.clear();
 }
 /**
  * Removes and returns the last object in the list. This also deletes the object from the
  * underlying Realm.
  *
  * <p>Using this method while iterating the list can result in a undefined behavior. Use {@link
  * io.realm.RealmResults.RealmResultsListIterator#removeLast()} instead.
  *
  * @throws IllegalStateException if the corresponding Realm is closed or in an incorrect thread.
  */
 public void removeLast() {
   realm.checkIfValid();
   TableOrView table = getTable();
   table.removeLast();
 }