@Override public String toString() { if (row == null || !row.isAttached()) { return "Invalid object"; } StringBuilder sb = new StringBuilder(row.getTable().getName() + " = ["); String[] fields = getFieldNames(); for (String field : fields) { long columnIndex = row.getColumnIndex(field); ColumnType type = row.getColumnType(columnIndex); sb.append("{"); switch (type) { case BOOLEAN: sb.append(field + ": " + row.getBoolean(columnIndex)); break; case INTEGER: sb.append(field + ": " + row.getLong(columnIndex)); break; case FLOAT: sb.append(field + ": " + row.getFloat(columnIndex)); break; case DOUBLE: sb.append(field + ": " + row.getDouble(columnIndex)); break; case STRING: sb.append(field + ": " + row.getString(columnIndex)); break; case BINARY: sb.append(field + ": " + row.getBinaryByteArray(columnIndex)); break; case DATE: sb.append(field + ": " + row.getDate(columnIndex)); break; case LINK: if (row.isNullLink(columnIndex)) { sb.append("null"); } else { sb.append(field + ": " + row.getTable().getLinkTarget(columnIndex).getName()); } break; case LINK_LIST: String targetType = row.getTable().getLinkTarget(columnIndex).getName(); sb.append( String.format( "%s: RealmList<%s>[%s]", field, targetType, row.getLinkList(columnIndex).size())); break; case TABLE: case MIXED: default: sb.append(field + ": ?"); } sb.append("}, "); } sb.replace(sb.length() - 2, sb.length(), ""); sb.append("]"); return sb.toString(); }
/** * Returns the object being linked to from this field. * * @param fieldName Name of field. * @return the {@link DynamicRealmObject} representation of the linked object or {@code null} if * no object is linked. * @throws IllegalArgumentException if field name doesn't exists or it doesn't contain links to * other objects. */ public DynamicRealmObject getObject(String fieldName) { long columnIndex = row.getColumnIndex(fieldName); if (row.isNullLink(columnIndex)) { return null; } else { long linkRowIndex = row.getLink(columnIndex); CheckedRow linkRow = row.getTable().getCheckedRow(linkRowIndex); return new DynamicRealmObject(realm, linkRow); } }
@Override public int hashCode() { String realmName = realm.getPath(); String tableName = row.getTable().getName(); long rowIndex = row.getIndex(); int result = 17; result = 31 * result + ((realmName != null) ? realmName.hashCode() : 0); result = 31 * result + ((tableName != null) ? tableName.hashCode() : 0); result = 31 * result + (int) (rowIndex ^ (rowIndex >>> 32)); return result; }
/** * Sets a reference to another object on the given field. * * @param fieldName the field name. * @param value the object to link to. * @throws IllegalArgumentException if field name doesn't exists, it doesn't link to other Realm * objects, or the type of DynamicRealmObject doesn't match. */ public void setObject(String fieldName, DynamicRealmObject value) { long columnIndex = row.getColumnIndex(fieldName); if (value == null) { row.nullifyLink(columnIndex); } else { if (value.realm == null || value.row == null) { throw new IllegalArgumentException( "Cannot link to objects that are not part of the Realm."); } if (!realm.getConfiguration().equals(value.realm.getConfiguration())) { throw new IllegalArgumentException("Cannot add an object from another Realm"); } Table table = row.getTable(); Table inputTable = value.row.getTable(); if (!table.hasSameSchema(inputTable)) { throw new IllegalArgumentException( String.format( "Type of object is wrong. Was %s, expected %s", inputTable.getName(), table.getName())); } row.setLink(columnIndex, value.row.getIndex()); } }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } DynamicRealmObject other = (DynamicRealmObject) o; String path = realm.getPath(); String otherPath = other.realm.getPath(); if (path != null ? !path.equals(otherPath) : otherPath != null) { return false; } String tableName = row.getTable().getName(); String otherTableName = other.row.getTable().getName(); if (tableName != null ? !tableName.equals(otherTableName) : otherTableName != null) { return false; } return row.getIndex() == other.row.getIndex(); }
/** * Return the type of object. This will normally correspond to the name of a model class that is * extending {@link RealmObject}. * * @return this objects type. */ public String getType() { return row.getTable().getName().substring(Table.TABLE_PREFIX.length()); }
/** * Deletes this object from the Realm. Accessing any fields after removing the object will throw * an {@link IllegalStateException}. */ public void removeFromRealm() { row.getTable().moveLastOver(row.getIndex()); row = InvalidRow.INSTANCE; }