Search in sources :

Example 21 with RealmObjectProxy

use of io.realm.internal.RealmObjectProxy in project realm-java by realm.

the class DynamicRealmObject method setList.

/**
     * Sets the reference to a {@link RealmList} on the given field.
     *
     * @param fieldName field name.
     * @param list list of references.
     * @throws IllegalArgumentException if field name doesn't exist, it is not a list field, the type
     * of the object represented by the DynamicRealmObject doesn't match or any element in the list belongs to a
     * different Realm.
     */
public void setList(String fieldName, RealmList<DynamicRealmObject> list) {
    proxyState.getRealm$realm().checkIfValid();
    if (list == null) {
        throw new IllegalArgumentException("Null values not allowed for lists");
    }
    long columnIndex = proxyState.getRow$realm().getColumnIndex(fieldName);
    LinkView links = proxyState.getRow$realm().getLinkList(columnIndex);
    Table linkTargetTable = links.getTargetTable();
    final String linkTargetTableName = Table.tableNameToClassName(linkTargetTable.getName());
    boolean typeValidated;
    if (list.className == null && list.clazz == null) {
        // Unmanaged lists don't know anything about the types they contain. They might even hold objects of
        // multiple types :(, so we have to check each item in the list.
        typeValidated = false;
    } else {
        String listType = list.className != null ? list.className : Table.tableNameToClassName(proxyState.getRealm$realm().schema.getTable(list.clazz).getName());
        if (!linkTargetTableName.equals(listType)) {
            throw new IllegalArgumentException(String.format(Locale.ENGLISH, "The elements in the list are not the proper type. " + "Was %s expected %s.", listType, linkTargetTableName));
        }
        typeValidated = true;
    }
    final int listLength = list.size();
    final long[] indices = new long[listLength];
    for (int i = 0; i < listLength; i++) {
        RealmObjectProxy obj = list.get(i);
        if (obj.realmGet$proxyState().getRealm$realm() != proxyState.getRealm$realm()) {
            throw new IllegalArgumentException("Each element in 'list' must belong to the same Realm instance.");
        }
        if (!typeValidated && !linkTargetTable.hasSameSchema(obj.realmGet$proxyState().getRow$realm().getTable())) {
            throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Element at index %d is not the proper type. " + "Was '%s' expected '%s'.", i, Table.tableNameToClassName(obj.realmGet$proxyState().getRow$realm().getTable().getName()), linkTargetTableName));
        }
        indices[i] = obj.realmGet$proxyState().getRow$realm().getIndex();
    }
    links.clear();
    for (int i = 0; i < listLength; i++) {
        links.add(indices[i]);
    }
}
Also used : RealmObjectProxy(io.realm.internal.RealmObjectProxy) Table(io.realm.internal.Table) LinkView(io.realm.internal.LinkView)

Example 22 with RealmObjectProxy

use of io.realm.internal.RealmObjectProxy in project realm-java by realm.

the class RealmList method add.

/**
     * Adds the specified object at the end of this List.
     * <ol>
     * <li><b>Unmanaged RealmLists</b>: It is possible to add both managed and unmanaged objects. If adding managed
     * objects to an unmanaged RealmList they will not be copied to the Realm again if using
     * {@link Realm#copyToRealm(RealmModel)} afterwards.</li>
     *
     * <li><b>Managed RealmLists</b>: It is possible to add unmanaged objects to a RealmList that is already managed. In
     * that case the object will transparently be copied to Realm using {@link Realm#copyToRealm(RealmModel)}
     * or {@link Realm#copyToRealmOrUpdate(RealmModel)} if it has a primary key.</li>
     * </ol>
     *
     * @param object the object to add.
     * @return always {@code true}.
     * @throws IllegalStateException if Realm instance has been closed or parent object has been removed.
     */
@Override
public boolean add(E object) {
    checkValidObject(object);
    if (isManaged()) {
        checkValidView();
        RealmObjectProxy proxy = (RealmObjectProxy) copyToRealmIfNeeded(object);
        view.add(proxy.realmGet$proxyState().getRow$realm().getIndex());
    } else {
        unmanagedList.add(object);
    }
    modCount++;
    return true;
}
Also used : RealmObjectProxy(io.realm.internal.RealmObjectProxy)

Example 23 with RealmObjectProxy

use of io.realm.internal.RealmObjectProxy in project realm-java by realm.

the class RealmList method set.

/**
     * Replaces the element at the specified location in this list with the specified object.
     * <ol>
     * <li><b>Unmanaged RealmLists</b>: It is possible to add both managed and unmanaged objects. If adding managed
     * objects to an unmanaged RealmList they will not be copied to the Realm again if using
     * {@link Realm#copyToRealm(RealmModel)} afterwards.</li>
     *
     * <li><b>Managed RealmLists</b>: It is possible to add unmanaged objects to a RealmList that is already managed.
     * In that case the object will transparently be copied to Realm using {@link Realm#copyToRealm(RealmModel)} or
     * {@link Realm#copyToRealmOrUpdate(RealmModel)} if it has a primary key.</li>
     * </ol>
     *
     * @param location the index at which to put the specified object.
     * @param object the object to add.
     * @return the previous element at the index.
     * @throws IllegalStateException if Realm instance has been closed or parent object has been removed.
     * @throws IndexOutOfBoundsException if {@code location < 0 || location >= size()}.
     */
@Override
public E set(int location, E object) {
    checkValidObject(object);
    E oldObject;
    if (isManaged()) {
        checkValidView();
        RealmObjectProxy proxy = (RealmObjectProxy) copyToRealmIfNeeded(object);
        oldObject = get(location);
        view.set(location, proxy.realmGet$proxyState().getRow$realm().getIndex());
        return oldObject;
    } else {
        oldObject = unmanagedList.set(location, object);
    }
    return oldObject;
}
Also used : RealmObjectProxy(io.realm.internal.RealmObjectProxy)

Example 24 with RealmObjectProxy

use of io.realm.internal.RealmObjectProxy in project realm-java by realm.

the class RealmObject method removeAllChangeListeners.

/**
     * Removes all registered listeners from the given RealmObject.
     *
     * @param object RealmObject to remove all listeners from.
     * @throws IllegalArgumentException if object is {@code null} or isn't managed by Realm.
     */
public static <E extends RealmModel> void removeAllChangeListeners(E object) {
    if (object instanceof RealmObjectProxy) {
        RealmObjectProxy proxy = (RealmObjectProxy) object;
        BaseRealm realm = proxy.realmGet$proxyState().getRealm$realm();
        realm.checkIfValid();
        realm.sharedRealm.capabilities.checkCanDeliverNotification(BaseRealm.LISTENER_NOT_ALLOWED_MESSAGE);
        proxy.realmGet$proxyState().removeAllChangeListeners();
    } else {
        throw new IllegalArgumentException("Cannot remove listeners from this unmanaged RealmObject (created outside of Realm)");
    }
}
Also used : RealmObjectProxy(io.realm.internal.RealmObjectProxy)

Example 25 with RealmObjectProxy

use of io.realm.internal.RealmObjectProxy in project realm-java by realm.

the class RealmObject method isValid.

/**
     * Checks if the RealmObject is still valid to use i.e., the RealmObject hasn't been deleted nor has the
     * {@link io.realm.Realm} been closed. It will always return {@code true} for unmanaged objects.
     *
     * @param object RealmObject to check validity for.
     * @return {@code true} if the object is still accessible or an unmanaged object, {@code false} otherwise.
     */
public static <E extends RealmModel> boolean isValid(E object) {
    if (object instanceof RealmObjectProxy) {
        RealmObjectProxy proxy = (RealmObjectProxy) object;
        Row row = proxy.realmGet$proxyState().getRow$realm();
        return row != null && row.isAttached();
    } else {
        return true;
    }
}
Also used : RealmObjectProxy(io.realm.internal.RealmObjectProxy) Row(io.realm.internal.Row) InvalidRow(io.realm.internal.InvalidRow)

Aggregations

RealmObjectProxy (io.realm.internal.RealmObjectProxy)43 Table (io.realm.internal.Table)20 Date (java.util.Date)8 Row (io.realm.internal.Row)6 InvalidRow (io.realm.internal.InvalidRow)2 LinkView (io.realm.internal.LinkView)2 UiThreadTest (android.support.test.annotation.UiThreadTest)1 AllTypes (io.realm.entities.AllTypes)1 Collection (io.realm.internal.Collection)1 PendingRow (io.realm.internal.PendingRow)1 SharedRealm (io.realm.internal.SharedRealm)1 UncheckedRow (io.realm.internal.UncheckedRow)1 Test (org.junit.Test)1 Observable (rx.Observable)1