use of io.realm.internal.Table 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]);
}
}
use of io.realm.internal.Table in project realm-java by realm.
the class RealmObjectSchema method getColumnIndices.
/**
* Returns the column indices for the given field name. If a linked field is defined, the column index for
* each field is returned.
*
* @param fieldDescription fieldName or link path to a field name.
* @param validColumnTypes valid field type for the last field in a linked field
* @return list of column indices.
*/
// TODO: consider another caching strategy so linked classes are included in the cache.
long[] getColumnIndices(String fieldDescription, RealmFieldType... validColumnTypes) {
if (fieldDescription == null || fieldDescription.equals("")) {
throw new IllegalArgumentException("Non-empty fieldname must be provided");
}
if (fieldDescription.startsWith(".") || fieldDescription.endsWith(".")) {
throw new IllegalArgumentException("Illegal field name. It cannot start or end with a '.': " + fieldDescription);
}
Table table = this.table;
boolean checkColumnType = validColumnTypes != null && validColumnTypes.length > 0;
if (fieldDescription.contains(".")) {
// Resolves field description down to last field name.
String[] names = fieldDescription.split("\\.");
long[] columnIndices = new long[names.length];
for (int i = 0; i < names.length - 1; i++) {
long index = table.getColumnIndex(names[i]);
if (index < 0) {
throw new IllegalArgumentException("Invalid query: " + names[i] + " does not refer to a class.");
}
RealmFieldType type = table.getColumnType(index);
if (type == RealmFieldType.OBJECT || type == RealmFieldType.LIST) {
table = table.getLinkTarget(index);
columnIndices[i] = index;
} else {
throw new IllegalArgumentException("Invalid query: " + names[i] + " does not refer to a class.");
}
}
// Checks if last field name is a valid field.
String columnName = names[names.length - 1];
long columnIndex = table.getColumnIndex(columnName);
columnIndices[names.length - 1] = columnIndex;
if (columnIndex < 0) {
throw new IllegalArgumentException(columnName + " is not a field name in class " + table.getName());
}
if (checkColumnType && !isValidType(table.getColumnType(columnIndex), validColumnTypes)) {
throw new IllegalArgumentException(String.format("Field '%s': type mismatch.", names[names.length - 1]));
}
return columnIndices;
} else {
Long fieldIndex = getFieldIndex(fieldDescription);
if (fieldIndex == null) {
throw new IllegalArgumentException(String.format("Field '%s' does not exist.", fieldDescription));
}
RealmFieldType tableColumnType = table.getColumnType(fieldIndex);
if (checkColumnType && !isValidType(tableColumnType, validColumnTypes)) {
throw new IllegalArgumentException(String.format("Field '%s': type mismatch. Was %s, expected %s.", fieldDescription, tableColumnType, Arrays.toString(validColumnTypes)));
}
return new long[] { fieldIndex };
}
}
use of io.realm.internal.Table in project realm-java by realm.
the class Realm method createObjectInternal.
/**
* Same as {@link #createObject(Class, Object)} but this does not check the thread.
*
* @param clazz the Class of the object to create.
* @param primaryKeyValue value for the primary key field.
* @param acceptDefaultValue if {@code true}, default value of the object will be applied and
* if {@code false}, it will be ignored.
* @return the new object.
* @throws RealmException if object could not be created due to the primary key being invalid.
* @throws IllegalStateException if the model class does not have an primary key defined.
* @throws IllegalArgumentException if the {@code primaryKeyValue} doesn't have a value that can be converted to the
* expected value.
*/
// Called from proxy classes.
<E extends RealmModel> E createObjectInternal(Class<E> clazz, Object primaryKeyValue, boolean acceptDefaultValue, List<String> excludeFields) {
Table table = schema.getTable(clazz);
long rowIndex = table.addEmptyRowWithPrimaryKey(primaryKeyValue);
return get(clazz, rowIndex, acceptDefaultValue, excludeFields);
}
use of io.realm.internal.Table in project realm-java by realm.
the class RealmSchema method getTable.
Table getTable(Class<? extends RealmModel> clazz) {
Table table = classToTable.get(clazz);
if (table == null) {
Class<? extends RealmModel> originalClass = Util.getOriginalModelClass(clazz);
if (isProxyClass(originalClass, clazz)) {
// If passed 'clazz' is the proxy, try again with model class.
table = classToTable.get(originalClass);
}
if (table == null) {
table = realm.sharedRealm.getTable(realm.configuration.getSchemaMediator().getTableName(originalClass));
classToTable.put(originalClass, table);
}
if (isProxyClass(originalClass, clazz)) {
// 'clazz' is the proxy class for 'originalClass'.
classToTable.put(clazz, table);
}
}
return table;
}
use of io.realm.internal.Table in project realm-java by realm.
the class RealmResults method createBacklinkResults.
static <T extends RealmModel> RealmResults<T> createBacklinkResults(BaseRealm realm, Row row, Class<T> srcTableType, String srcFieldName) {
if (!(row instanceof UncheckedRow)) {
throw new IllegalArgumentException("Row is " + row.getClass());
}
UncheckedRow uncheckedRow = (UncheckedRow) row;
Table srcTable = realm.getSchema().getTable(srcTableType);
return new RealmResults<T>(realm, Collection.createBacklinksCollection(realm.sharedRealm, uncheckedRow, srcTable, srcFieldName), srcTableType);
}
Aggregations