/** * Returns the element at the specified location in this list. * * @param location the index of the element to return. * @return the element at the specified index. * @throws IndexOutOfBoundsException if {@code location < 0 || location >= size()}. */ @Override public E get(int location) { E obj; realm.checkIfValid(); TableOrView table = getTable(); if (table instanceof TableView) { obj = realm.get(classSpec, className, ((TableView) table).getSourceRowIndex(location)); } else { obj = realm.get(classSpec, className, location); } return obj; }
/** * 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")); } }
/** * Adds a change listener to this RealmResults. * * @param listener the change listener to be notified. */ public void addChangeListener(RealmChangeListener listener) { if (listener == null) { throw new IllegalArgumentException("Listener should not be null"); } realm.checkIfValid(); if (realm.handler == null) { throw new IllegalStateException("You can't register a listener from a non-Looper thread "); } if (!listeners.contains(listener)) { listeners.add(listener); } }
/** * 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")); } }
/** * Sorts existing {@link io.realm.RealmResults}. * * @param fieldName the field name to sort by. Only fields of type boolean, short, int, long, * float, double, Date, and String are supported. * @param sortOrder the direction to sort by. * @throws java.lang.IllegalArgumentException if field name does not exist. */ public void sort(String fieldName, Sort sortOrder) { if (fieldName == null) { throw new IllegalArgumentException("fieldName must be provided"); } realm.checkIfValid(); TableOrView table = getTable(); if (table instanceof TableView) { long columnIndex = getColumnIndex(fieldName); ((TableView) table).sort(columnIndex, sortOrder); } else { throw new IllegalArgumentException( "Only RealmResults can be sorted - please use allObject() to create a RealmResults."); } }
/** * Sorts existing {@link io.realm.RealmResults}. * * @param fieldNames an array of field names to sort by. Only fields of type boolean, short, int, * long, float, double, Date, and String are supported. * @param sortOrders the directions to sort by. * @throws java.lang.IllegalArgumentException if a field name does not exist. */ public void sort(String fieldNames[], Sort sortOrders[]) { if (fieldNames == null) { throw new IllegalArgumentException("fieldNames must be provided."); } else if (sortOrders == null) { throw new IllegalArgumentException("sortOrder must be provided."); } if (fieldNames.length == 1 && sortOrders.length == 1) { sort(fieldNames[0], sortOrders[0]); } else { realm.checkIfValid(); TableOrView table = getTable(); if (table instanceof TableView) { List<Long> columnIndices = new ArrayList<Long>(); for (int i = 0; i < fieldNames.length; i++) { String fieldName = fieldNames[i]; long columnIndex = getColumnIndex(fieldName); columnIndices.add(columnIndex); } ((TableView) table).sort(columnIndices, sortOrders); } } }
/** Removes all registered listeners. */ public void removeChangeListeners() { realm.checkIfValid(); listeners.clear(); }
/** * Removes a previously registered listener. * * @param listener the instance to be removed. */ public void removeChangeListener(RealmChangeListener listener) { if (listener == null) throw new IllegalArgumentException("Listener should not be null"); realm.checkIfValid(); listeners.remove(listener); }
/** * Returns {@code true} if the results are not yet loaded, {@code false} if they are still * loading. Synchronous query methods like findAll() will always return {@code true}, while * asynchronous query methods like findAllAsync() will return {@code false} until the results are * available. This will return {@code true} if called for a standalone object (created outside of * Realm). * * @return {@code true} if the query has completed and the data is available {@code false} if the * query is still running. */ public boolean isLoaded() { realm.checkIfValid(); return pendingQuery == null || isCompleted; }
/** * 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(); }
/** * Returns a typed {@link io.realm.RealmQuery}, which can be used to query for specific objects of * this type. * * @return a typed RealmQuery. * @see io.realm.RealmQuery */ public RealmQuery<E> where() { realm.checkIfValid(); return RealmQuery.createQueryFromResult(this); }
/** * Checks if {@link io.realm.RealmResults} is still valid to use i.e. the {@link io.realm.Realm} * instance hasn't been closed. * * @return {@code true} if still valid to use, {@code false} otherwise. */ public boolean isValid() { return realm != null && !realm.isClosed(); }