Search in sources :

Example 36 with Decimal128

use of org.bson.types.Decimal128 in project realm-java by realm.

the class RealmResultsTests method setValue.

@Test
public void setValue() {
    populateAllJavaTypes(5);
    RealmResults<AllJavaTypes> collection = realm.where(AllJavaTypes.class).findAll();
    realm.beginTransaction();
    for (BulkSetMethods type : BulkSetMethods.values()) {
        switch(type) {
            case STRING:
                collection.setValue(AllJavaTypes.FIELD_STRING, "foo");
                assertElements(collection, obj -> assertEquals("foo", obj.getFieldString()));
                collection.setValue(AllJavaTypes.FIELD_STRING, null);
                assertElements(collection, obj -> assertEquals(null, obj.getFieldString()));
                break;
            case BOOLEAN:
                collection.setValue(AllJavaTypes.FIELD_BOOLEAN, true);
                assertElements(collection, obj -> assertTrue(obj.isFieldBoolean()));
                break;
            case BYTE:
                collection.setValue(AllJavaTypes.FIELD_BYTE, (byte) 1);
                assertElements(collection, obj -> assertEquals((byte) 1, obj.getFieldByte()));
                break;
            case SHORT:
                collection.setValue(AllJavaTypes.FIELD_SHORT, (short) 2);
                assertElements(collection, obj -> assertEquals((short) 2, obj.getFieldShort()));
                break;
            case INTEGER:
                collection.setValue(AllJavaTypes.FIELD_INT, 3);
                assertElements(collection, obj -> assertEquals(3, obj.getFieldInt()));
                break;
            case LONG:
                collection.setValue(AllJavaTypes.FIELD_LONG, 4L);
                assertElements(collection, obj -> assertEquals(4L, obj.getFieldLong()));
                break;
            case FLOAT:
                collection.setValue(AllJavaTypes.FIELD_FLOAT, 1.23F);
                assertElements(collection, obj -> assertEquals(1.23F, obj.getFieldFloat(), 0F));
                break;
            case DOUBLE:
                collection.setValue(AllJavaTypes.FIELD_DOUBLE, 1.234);
                assertElements(collection, obj -> assertEquals(1.234, obj.getFieldDouble(), 0F));
                break;
            case BINARY:
                collection.setValue(AllJavaTypes.FIELD_BINARY, new byte[] { 1, 2, 3 });
                assertElements(collection, obj -> assertArrayEquals(new byte[] { 1, 2, 3 }, obj.getFieldBinary()));
                collection.setValue(AllJavaTypes.FIELD_BINARY, null);
                assertElements(collection, obj -> assertNull(obj.getFieldBinary()));
                break;
            case DATE:
                collection.setValue(AllJavaTypes.FIELD_DATE, new Date(1000));
                assertElements(collection, obj -> assertEquals(new Date(1000), obj.getFieldDate()));
                collection.setValue(AllJavaTypes.FIELD_DATE, null);
                assertElements(collection, obj -> assertNull(obj.getFieldDate()));
                break;
            case DECIMAL128:
                collection.setValue(AllJavaTypes.FIELD_DECIMAL128, new Decimal128(1000));
                assertElements(collection, obj -> assertEquals(new Decimal128(1000), obj.getFieldDecimal128()));
                collection.setValue(AllJavaTypes.FIELD_DECIMAL128, null);
                assertElements(collection, obj -> assertNull(obj.getFieldDecimal128()));
                break;
            case OBJECT_ID:
                String hex = TestHelper.randomObjectIdHexString();
                collection.setValue(AllJavaTypes.FIELD_OBJECT_ID, new ObjectId(hex));
                assertElements(collection, obj -> assertEquals(new ObjectId(hex), obj.getFieldObjectId()));
                collection.setValue(AllJavaTypes.FIELD_OBJECT_ID, null);
                assertElements(collection, obj -> assertNull(obj.getFieldObjectId()));
                break;
            case UUID:
                String uuid = UUID.randomUUID().toString();
                collection.setValue(AllJavaTypes.FIELD_UUID, UUID.fromString(uuid));
                assertElements(collection, obj -> assertEquals(UUID.fromString(uuid), obj.getFieldUUID()));
                collection.setValue(AllJavaTypes.FIELD_UUID, null);
                assertElements(collection, obj -> assertNull(obj.getFieldUUID()));
                break;
            case OBJECT:
                {
                    AllJavaTypes childObj = realm.createObject(AllJavaTypes.class, 42);
                    collection.setValue(AllJavaTypes.FIELD_OBJECT, childObj);
                    assertElements(collection, obj -> assertEquals(childObj, obj.getFieldObject()));
                    collection.setValue(AllJavaTypes.FIELD_OBJECT, null);
                    assertElements(collection, obj -> assertNull(obj.getFieldObject()));
                    break;
                }
            case MODEL_LIST:
                {
                    AllJavaTypes childObj = realm.createObject(AllJavaTypes.class, 43);
                    collection.setValue(AllJavaTypes.FIELD_LIST, new RealmList<>(childObj));
                    assertElements(collection, obj -> {
                        assertEquals(1, obj.getFieldList().size());
                        assertEquals(childObj, obj.getFieldList().first());
                    });
                    break;
                }
            case STRING_VALUE_LIST:
                {
                    RealmList<String> list = new RealmList<>("Foo", "Bar");
                    collection.setValue(AllJavaTypes.FIELD_STRING_LIST, list);
                    assertElements(collection, obj -> {
                        assertEquals("Foo", obj.getFieldStringList().first());
                        assertEquals("Bar", obj.getFieldStringList().last());
                    });
                    break;
                }
            case BOOLEAN_VALUE_LIST:
                {
                    RealmList<Boolean> list = new RealmList<>(true, false);
                    collection.setValue(AllJavaTypes.FIELD_BOOLEAN_LIST, list);
                    assertElements(collection, obj -> {
                        assertTrue(obj.getFieldBooleanList().first());
                        assertFalse(obj.getFieldBooleanList().last());
                    });
                    break;
                }
            case BYTE_VALUE_LIST:
                {
                    RealmList<Byte> list = new RealmList<>((byte) 1, (byte) 2);
                    collection.setValue(AllJavaTypes.FIELD_BYTE_LIST, list);
                    assertElements(collection, obj -> {
                        assertEquals(Byte.valueOf((byte) 1), obj.getFieldByteList().first());
                        assertEquals(Byte.valueOf((byte) 2), obj.getFieldByteList().last());
                    });
                    break;
                }
            case SHORT_VALUE_LIST:
                {
                    RealmList<Short> list = new RealmList<>((short) 1, (short) 2);
                    collection.setValue(AllJavaTypes.FIELD_SHORT_LIST, list);
                    assertElements(collection, obj -> {
                        assertEquals(Short.valueOf((short) 1), obj.getFieldShortList().first());
                        assertEquals(Short.valueOf((short) 2), obj.getFieldShortList().last());
                    });
                    break;
                }
            case INTEGER_VALUE_LIST:
                {
                    RealmList<Integer> list = new RealmList<>(1, 2);
                    collection.setValue(AllJavaTypes.FIELD_INTEGER_LIST, list);
                    assertElements(collection, obj -> {
                        assertEquals(Integer.valueOf(1), obj.getFieldIntegerList().first());
                        assertEquals(Integer.valueOf(2), obj.getFieldIntegerList().last());
                    });
                    break;
                }
            case LONG_VALUE_LIST:
                {
                    RealmList<Long> list = new RealmList<>(1L, 2L);
                    collection.setValue(AllJavaTypes.FIELD_LONG_LIST, list);
                    assertElements(collection, obj -> {
                        assertEquals(Long.valueOf(1), obj.getFieldLongList().first());
                        assertEquals(Long.valueOf(2), obj.getFieldLongList().last());
                    });
                    break;
                }
            case FLOAT_VALUE_LIST:
                {
                    RealmList<Float> list = new RealmList<>(1.1F, 2.2F);
                    collection.setValue(AllJavaTypes.FIELD_FLOAT_LIST, list);
                    assertElements(collection, obj -> {
                        assertEquals(1.1F, obj.getFieldFloatList().first(), 0F);
                        assertEquals(2.2F, obj.getFieldFloatList().last(), 0F);
                    });
                    break;
                }
            case DOUBLE_VALUE_LIST:
                {
                    RealmList<Double> list = new RealmList<>(1.1D, 2.2D);
                    collection.setValue(AllJavaTypes.FIELD_DOUBLE_LIST, list);
                    assertElements(collection, obj -> {
                        assertEquals(1.1D, obj.getFieldDoubleList().first(), 0D);
                        assertEquals(2.2D, obj.getFieldDoubleList().last(), 0D);
                    });
                    break;
                }
            case BINARY_VALUE_LIST:
                {
                    RealmList<byte[]> list = new RealmList<>(new byte[] { 1, 2, 3 }, new byte[] { 2, 3, 4 });
                    collection.setValue(AllJavaTypes.FIELD_BINARY_LIST, list);
                    assertElements(collection, obj -> {
                        assertArrayEquals(new byte[] { 1, 2, 3 }, obj.getFieldBinaryList().first());
                        assertArrayEquals(new byte[] { 2, 3, 4 }, obj.getFieldBinaryList().last());
                    });
                    break;
                }
            case DATE_VALUE_LIST:
                {
                    RealmList<Date> list = new RealmList<>(new Date(1000), new Date(2000));
                    collection.setValue(AllJavaTypes.FIELD_DATE_LIST, list);
                    assertElements(collection, obj -> {
                        assertEquals(new Date(1000), obj.getFieldDateList().first());
                        assertEquals(new Date(2000), obj.getFieldDateList().last());
                    });
                    break;
                }
            case DECIMAL128_VALUE_LIST:
                {
                    RealmList<Decimal128> list = new RealmList<>(new Decimal128(1000), new Decimal128(2000));
                    collection.setValue(AllJavaTypes.FIELD_DECIMAL128_LIST, list);
                    assertElements(collection, obj -> {
                        assertEquals(new Decimal128(1000), obj.getFieldDecimal128List().first());
                        assertEquals(new Decimal128(2000), obj.getFieldDecimal128List().last());
                    });
                    break;
                }
            case OBJECT_ID_VALUE_LIST:
                {
                    String hex1 = TestHelper.randomObjectIdHexString();
                    String hex2 = TestHelper.randomObjectIdHexString();
                    RealmList<ObjectId> list = new RealmList<>(new ObjectId(hex1), new ObjectId(hex2));
                    collection.setValue(AllJavaTypes.FIELD_OBJECT_ID_LIST, list);
                    assertElements(collection, obj -> {
                        assertEquals(new ObjectId(hex1), obj.getFieldObjectIdList().first());
                        assertEquals(new ObjectId(hex2), obj.getFieldObjectIdList().last());
                    });
                    break;
                }
            case UUID_VALUE_LIST:
                {
                    String uuid1 = UUID.randomUUID().toString();
                    String uuid2 = UUID.randomUUID().toString();
                    RealmList<UUID> list = new RealmList<>(UUID.fromString(uuid1), UUID.fromString(uuid2));
                    collection.setValue(AllJavaTypes.FIELD_UUID_LIST, list);
                    assertElements(collection, obj -> {
                        assertEquals(UUID.fromString(uuid1), obj.getFieldUUIDList().first());
                        assertEquals(UUID.fromString(uuid2), obj.getFieldUUIDList().last());
                    });
                    break;
                }
            default:
                fail("Unknown type: " + type);
        }
    }
}
Also used : Arrays(java.util.Arrays) AllJavaTypes(io.realm.entities.AllJavaTypes) Date(java.util.Date) Dog(io.realm.entities.Dog) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) BigDecimal(java.math.BigDecimal) JSONException(org.json.JSONException) RunInLooperThread(io.realm.rule.RunInLooperThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StringOnly(io.realm.entities.StringOnly) After(org.junit.After) Assert.fail(org.junit.Assert.fail) DefaultValueOfField(io.realm.entities.DefaultValueOfField) NonLatinFieldNames(io.realm.entities.NonLatinFieldNames) TimeZone(java.util.TimeZone) Collection(java.util.Collection) Set(java.util.Set) UUID(java.util.UUID) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) RandomPrimaryKey(io.realm.entities.RandomPrimaryKey) RunWith(org.junit.runner.RunWith) SimpleDateFormat(java.text.SimpleDateFormat) JSONAssert(org.skyscreamer.jsonassert.JSONAssert) MappedAllJavaTypes(io.realm.entities.MappedAllJavaTypes) Calendar(java.util.Calendar) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) ExpectedException(org.junit.rules.ExpectedException) Decimal128(org.bson.types.Decimal128) Before(org.junit.Before) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) PrimaryKeyAsLong(io.realm.entities.PrimaryKeyAsLong) RealmLog(io.realm.log.RealmLog) Assert.assertNotNull(org.junit.Assert.assertNotNull) AllTypes(io.realm.entities.AllTypes) DictionaryAllTypes(io.realm.entities.DictionaryAllTypes) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) CyclicType(io.realm.entities.CyclicType) TimeUnit(java.util.concurrent.TimeUnit) OsResults(io.realm.internal.OsResults) Mockito(org.mockito.Mockito) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) Owner(io.realm.entities.Owner) UiThreadTest(androidx.test.annotation.UiThreadTest) ObjectId(org.bson.types.ObjectId) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ObjectId(org.bson.types.ObjectId) Decimal128(org.bson.types.Decimal128) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) AllJavaTypes(io.realm.entities.AllJavaTypes) MappedAllJavaTypes(io.realm.entities.MappedAllJavaTypes) Date(java.util.Date) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 37 with Decimal128

use of org.bson.types.Decimal128 in project realm-java by realm.

the class RealmResultsTests method setValue_specificType_wrongTypeThrows.

@Test
public void setValue_specificType_wrongTypeThrows() {
    populateAllJavaTypes(5);
    RealmResults<AllJavaTypes> collection = realm.where(AllJavaTypes.class).findAll();
    realm.beginTransaction();
    for (BulkSetMethods type : BulkSetMethods.values()) {
        try {
            switch(type) {
                case STRING:
                    collection.setString(AllJavaTypes.FIELD_BOOLEAN, "foo");
                    break;
                case BOOLEAN:
                    collection.setBoolean(AllJavaTypes.FIELD_STRING, true);
                    break;
                case BYTE:
                    collection.setByte(AllJavaTypes.FIELD_STRING, (byte) 1);
                    break;
                case SHORT:
                    collection.setShort(AllJavaTypes.FIELD_STRING, (short) 2);
                    break;
                case INTEGER:
                    collection.setInt(AllJavaTypes.FIELD_STRING, 3);
                    break;
                case LONG:
                    collection.setLong(AllJavaTypes.FIELD_STRING, 4L);
                    break;
                case FLOAT:
                    collection.setFloat(AllJavaTypes.FIELD_STRING, 1.23F);
                    break;
                case DOUBLE:
                    collection.setDouble(AllJavaTypes.FIELD_STRING, 1.234);
                    break;
                case BINARY:
                    collection.setBlob(AllJavaTypes.FIELD_STRING, new byte[] { 1, 2, 3 });
                    break;
                case DATE:
                    collection.setDate(AllJavaTypes.FIELD_STRING, new Date(1000));
                    break;
                case DECIMAL128:
                    collection.setDecimal128(AllJavaTypes.FIELD_STRING, new Decimal128(1000));
                    break;
                case OBJECT_ID:
                    collection.setObjectId(AllJavaTypes.FIELD_STRING, new ObjectId(TestHelper.randomObjectIdHexString()));
                    break;
                case UUID:
                    collection.setUUID(AllJavaTypes.FIELD_STRING, UUID.randomUUID());
                    break;
                case OBJECT:
                    collection.setObject(AllJavaTypes.FIELD_STRING, realm.createObject(AllJavaTypes.class, 42));
                    break;
                case MODEL_LIST:
                    collection.setList(AllJavaTypes.FIELD_STRING, new RealmList<>(realm.createObject(AllJavaTypes.class, 43)));
                    break;
                case STRING_VALUE_LIST:
                    collection.setList(AllJavaTypes.FIELD_STRING, new RealmList<>("Foo"));
                    break;
                case BOOLEAN_VALUE_LIST:
                    collection.setList(AllJavaTypes.FIELD_STRING, new RealmList<>(true));
                    break;
                case BYTE_VALUE_LIST:
                    collection.setList(AllJavaTypes.FIELD_STRING, new RealmList<>((byte) 1));
                    break;
                case SHORT_VALUE_LIST:
                    collection.setList(AllJavaTypes.FIELD_STRING, new RealmList<>((short) 1));
                    break;
                case INTEGER_VALUE_LIST:
                    collection.setList(AllJavaTypes.FIELD_STRING, new RealmList<>(1));
                    break;
                case LONG_VALUE_LIST:
                    collection.setList(AllJavaTypes.FIELD_STRING, new RealmList<>(1L));
                    break;
                case FLOAT_VALUE_LIST:
                    collection.setList(AllJavaTypes.FIELD_STRING, new RealmList<>(1.1F));
                    break;
                case DOUBLE_VALUE_LIST:
                    collection.setList(AllJavaTypes.FIELD_STRING, new RealmList<>(2.2D));
                    break;
                case BINARY_VALUE_LIST:
                    collection.setList(AllJavaTypes.FIELD_STRING, new RealmList<>(new byte[] {}));
                    break;
                case DATE_VALUE_LIST:
                    collection.setList(AllJavaTypes.FIELD_STRING, new RealmList<>(new Date()));
                    break;
                case DECIMAL128_VALUE_LIST:
                    collection.setList(AllJavaTypes.FIELD_STRING, new RealmList<>(new Decimal128(1000)));
                    break;
                case OBJECT_ID_VALUE_LIST:
                    collection.setList(AllJavaTypes.FIELD_STRING, new RealmList<>(new ObjectId(TestHelper.randomObjectIdHexString())));
                    break;
                case UUID_VALUE_LIST:
                    collection.setList(AllJavaTypes.FIELD_STRING, new RealmList<>(UUID.randomUUID()));
                    break;
                default:
                    fail("Unknown type: " + type);
            }
            fail(type + " should have thrown an exception");
        } catch (IllegalArgumentException e) {
            RealmLog.error(type + " -> " + e.getMessage());
            assertTrue(type + " failed", e.getMessage().contains("is not of the expected type") || e.getMessage().contains("List contained the wrong type of elements") || e.getMessage().contains("is not a list"));
        }
    }
}
Also used : ObjectId(org.bson.types.ObjectId) Decimal128(org.bson.types.Decimal128) AllJavaTypes(io.realm.entities.AllJavaTypes) MappedAllJavaTypes(io.realm.entities.MappedAllJavaTypes) Date(java.util.Date) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 38 with Decimal128

use of org.bson.types.Decimal128 in project realm-java by realm.

the class RealmTests method copyToRealm_fromOtherRealm.

@Test
public void copyToRealm_fromOtherRealm() {
    realm.beginTransaction();
    AllTypes allTypes = realm.createObject(AllTypes.class);
    allTypes.setColumnString("Test");
    allTypes.setColumnDecimal128(new Decimal128(new BigDecimal("12345")));
    allTypes.setColumnObjectId(new ObjectId(TestHelper.randomObjectIdHexString()));
    allTypes.setColumnUUID(UUID.randomUUID());
    allTypes.setColumnRealmAny(RealmAny.valueOf(UUID.randomUUID()));
    realm.commitTransaction();
    RealmConfiguration realmConfig = configFactory.createConfiguration("other-realm");
    Realm otherRealm = Realm.getInstance(realmConfig);
    otherRealm.beginTransaction();
    AllTypes copiedAllTypes = otherRealm.copyToRealm(allTypes);
    otherRealm.commitTransaction();
    // Same object in different Realms is not the same.
    assertNotSame(allTypes, copiedAllTypes);
    // But data is still the same.
    assertEquals(allTypes.getColumnString(), copiedAllTypes.getColumnString());
    otherRealm.close();
}
Also used : ObjectId(org.bson.types.ObjectId) AllTypes(io.realm.entities.AllTypes) Decimal128(org.bson.types.Decimal128) OsSharedRealm(io.realm.internal.OsSharedRealm) BigDecimal(java.math.BigDecimal) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 39 with Decimal128

use of org.bson.types.Decimal128 in project realm-java by realm.

the class JNIRowTest method nonNullValues.

@Test
public void nonNullValues() {
    final byte[] data = new byte[2];
    final AtomicLong colKey1 = new AtomicLong(-1);
    final AtomicLong colKey2 = new AtomicLong(-1);
    final AtomicLong colKey3 = new AtomicLong(-1);
    final AtomicLong colKey4 = new AtomicLong(-1);
    final AtomicLong colKey5 = new AtomicLong(-1);
    final AtomicLong colKey6 = new AtomicLong(-1);
    final AtomicLong colKey7 = new AtomicLong(-1);
    final AtomicLong colKey8 = new AtomicLong(-1);
    final AtomicLong colKey9 = new AtomicLong(-1);
    Table table = TestHelper.createTable(sharedRealm, "temp", new TestHelper.AdditionalTableSetup() {

        @Override
        public void execute(Table table) {
            colKey1.set(table.addColumn(RealmFieldType.STRING, "string"));
            colKey2.set(table.addColumn(RealmFieldType.INTEGER, "integer"));
            colKey3.set(table.addColumn(RealmFieldType.FLOAT, "float"));
            colKey4.set(table.addColumn(RealmFieldType.DOUBLE, "double"));
            colKey5.set(table.addColumn(RealmFieldType.BOOLEAN, "boolean"));
            colKey6.set(table.addColumn(RealmFieldType.DATE, "date"));
            colKey7.set(table.addColumn(RealmFieldType.BINARY, "binary"));
            colKey8.set(table.addColumn(RealmFieldType.DECIMAL128, "decimal128"));
            colKey9.set(table.addColumn(RealmFieldType.OBJECT_ID, "object_id"));
            TestHelper.addRowWithValues(table, new long[] { colKey1.get(), colKey2.get(), colKey3.get(), colKey4.get(), colKey5.get(), colKey6.get(), colKey7.get(), colKey8.get(), colKey9.get() }, new Object[] { "abc", 3, (float) 1.2, 1.3, true, new Date(0), data, new Decimal128(1), new ObjectId(TestHelper.generateObjectIdHexString(1)) });
        }
    });
    UncheckedRow row = table.getUncheckedRow(0);
    assertEquals("abc", row.getString(colKey1.get()));
    assertEquals(3, row.getLong(colKey2.get()));
    assertEquals(1.2F, row.getFloat(colKey3.get()), Float.MIN_NORMAL);
    assertEquals(1.3, row.getDouble(colKey4.get()), Double.MIN_NORMAL);
    assertEquals(true, row.getBoolean(colKey5.get()));
    assertEquals(new Date(0), row.getDate(colKey6.get()));
    assertArrayEquals(data, row.getBinaryByteArray(colKey7.get()));
    assertEquals(new Decimal128(1), row.getDecimal128(colKey8.get()));
    assertEquals(new ObjectId(TestHelper.generateObjectIdHexString(1)), row.getObjectId(colKey9.get()));
    row.setString(colKey1.get(), "a");
    row.setLong(colKey2.get(), 1);
    row.setFloat(colKey3.get(), (float) 8.8);
    row.setDouble(colKey4.get(), 9.9);
    row.setBoolean(colKey5.get(), false);
    row.setDate(colKey6.get(), new Date(10000));
    row.setDecimal128(colKey8.get(), new Decimal128(2));
    row.setObjectId(colKey9.get(), new ObjectId(TestHelper.generateObjectIdHexString(2)));
    byte[] newData = new byte[3];
    row.setBinaryByteArray(colKey7.get(), newData);
    assertEquals("a", row.getString(colKey1.get()));
    assertEquals(1, row.getLong(colKey2.get()));
    assertEquals(8.8F, row.getFloat(colKey3.get()), Float.MIN_NORMAL);
    assertEquals(9.9, row.getDouble(colKey4.get()), Double.MIN_NORMAL);
    assertEquals(false, row.getBoolean(colKey5.get()));
    assertEquals(new Date(10000), row.getDate(colKey6.get()));
    assertArrayEquals(newData, row.getBinaryByteArray(colKey7.get()));
    assertEquals(new Decimal128(2), row.getDecimal128(colKey8.get()));
    assertEquals(new ObjectId(TestHelper.generateObjectIdHexString(2)), row.getObjectId(colKey9.get()));
}
Also used : TestHelper(io.realm.TestHelper) AtomicLong(java.util.concurrent.atomic.AtomicLong) ObjectId(org.bson.types.ObjectId) Decimal128(org.bson.types.Decimal128) Date(java.util.Date) Test(org.junit.Test)

Example 40 with Decimal128

use of org.bson.types.Decimal128 in project realm-java by realm.

the class RealmResults method setValue.

/**
 * Updates the field given by {@code fieldName} in all objects inside the query result.
 * <p>
 * This method will automatically try to convert numbers and booleans that are given as
 * {@code String} to their appropriate type. For example {@code "10"} will be converted to
 * {@code 10} if the field type is {@link RealmFieldType#INTEGER}.
 * <p>
 * Using the typed setters like {@link #setInt(String, int)} will be faster than using
 * this method.
 *
 * @param fieldName field to update
 * @param value value to update with.
 * @throws IllegalArgumentException if the field could not be found, could not be updated or
 * the argument didn't match the field type or could not be converted to match the underlying
 * field type.
 */
public void setValue(String fieldName, @Nullable Object value) {
    checkNonEmptyFieldName(fieldName);
    baseRealm.checkIfValidAndInTransaction();
    fieldName = mapFieldNameToInternalName(fieldName);
    boolean isString = (value instanceof String);
    String strValue = isString ? (String) value : null;
    String className = osResults.getTable().getClassName();
    RealmObjectSchema schema = getRealm().getSchema().get(className);
    if (!schema.hasField(fieldName)) {
        throw new IllegalArgumentException(String.format("Field '%s' could not be found in class '%s'", fieldName, className));
    }
    // null values exit early
    if (value == null) {
        osResults.setNull(fieldName);
        return;
    }
    // Does implicit conversion if needed.
    RealmFieldType type = schema.getFieldType(fieldName);
    if (isString && type != RealmFieldType.STRING) {
        switch(type) {
            case BOOLEAN:
                value = Boolean.parseBoolean(strValue);
                break;
            case INTEGER:
                value = Long.parseLong(strValue);
                break;
            case FLOAT:
                value = Float.parseFloat(strValue);
                break;
            case DOUBLE:
                value = Double.parseDouble(strValue);
                break;
            case DATE:
                value = JsonUtils.stringToDate(strValue);
                break;
            case DECIMAL128:
                value = Decimal128.parse(strValue);
                break;
            case OBJECT_ID:
                value = new ObjectId(strValue);
                break;
            case UUID:
                value = UUID.fromString(strValue);
                break;
            default:
                throw new IllegalArgumentException(String.format(Locale.US, "Field %s is not a String field, " + "and the provide value could not be automatically converted: %s. Use a typed" + "setter instead", fieldName, value));
        }
    }
    // noinspection ConstantConditions
    Class<?> valueClass = value.getClass();
    if (valueClass == Boolean.class) {
        setBoolean(fieldName, (Boolean) value);
    } else if (valueClass == Short.class) {
        setShort(fieldName, (Short) value);
    } else if (valueClass == Integer.class) {
        setInt(fieldName, (Integer) value);
    } else if (valueClass == Long.class) {
        setLong(fieldName, (Long) value);
    } else if (valueClass == Byte.class) {
        setByte(fieldName, (Byte) value);
    } else if (valueClass == Float.class) {
        setFloat(fieldName, (Float) value);
    } else if (valueClass == Double.class) {
        setDouble(fieldName, (Double) value);
    } else if (valueClass == String.class) {
        // noinspection ConstantConditions
        setString(fieldName, (String) value);
    } else if (value instanceof Date) {
        setDate(fieldName, (Date) value);
    } else if (value instanceof Decimal128) {
        setDecimal128(fieldName, (Decimal128) value);
    } else if (value instanceof ObjectId) {
        setObjectId(fieldName, (ObjectId) value);
    } else if (value instanceof UUID) {
        setUUID(fieldName, (UUID) value);
    } else if (value instanceof byte[]) {
        setBlob(fieldName, (byte[]) value);
    } else if (value instanceof RealmModel) {
        setObject(fieldName, (RealmModel) value);
    } else if (valueClass == RealmList.class) {
        RealmList<?> list = (RealmList<?>) value;
        setList(fieldName, list);
    } else {
        throw new IllegalArgumentException("Value is of a type not supported: " + value.getClass());
    }
}
Also used : ObjectId(org.bson.types.ObjectId) Decimal128(org.bson.types.Decimal128) Date(java.util.Date) UUID(java.util.UUID)

Aggregations

Decimal128 (org.bson.types.Decimal128)83 ObjectId (org.bson.types.ObjectId)50 Date (java.util.Date)46 Test (org.junit.Test)46 AllTypes (io.realm.entities.AllTypes)22 BigDecimal (java.math.BigDecimal)20 UUID (java.util.UUID)17 UiThreadTest (androidx.test.annotation.UiThreadTest)12 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)11 AllJavaTypes (io.realm.entities.AllJavaTypes)10 DictionaryAllTypes (io.realm.entities.DictionaryAllTypes)10 Dog (io.realm.entities.Dog)9 PrimaryKeyAsObjectId (io.realm.entities.PrimaryKeyAsObjectId)8 JSONObject (org.json.JSONObject)8 MappedAllJavaTypes (io.realm.entities.MappedAllJavaTypes)7 NonLatinFieldNames (io.realm.entities.NonLatinFieldNames)6 PrimaryKeyAsLong (io.realm.entities.PrimaryKeyAsLong)6 SimpleDateFormat (java.text.SimpleDateFormat)6 Calendar (java.util.Calendar)6 DefaultValueOfField (io.realm.entities.DefaultValueOfField)5