Search in sources :

Example 16 with Decimal128

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

the class DynamicRealmObjectTests method callSetter.

// Helper method for calling setters with different field names.
private static void callSetter(DynamicRealmObject target, SupportedType type, List<String> fieldNames) {
    for (String fieldName : fieldNames) {
        switch(type) {
            case BOOLEAN:
                target.setBoolean(fieldName, false);
                break;
            case SHORT:
                target.setShort(fieldName, (short) 1);
                break;
            case INT:
                target.setInt(fieldName, 1);
                break;
            case LONG:
                target.setLong(fieldName, 1L);
                break;
            case BYTE:
                target.setByte(fieldName, (byte) 4);
                break;
            case FLOAT:
                target.setFloat(fieldName, 1.23f);
                break;
            case DOUBLE:
                target.setDouble(fieldName, 1.23d);
                break;
            case STRING:
                target.setString(fieldName, "foo");
                break;
            case BINARY:
                target.setBlob(fieldName, new byte[] {});
                break;
            case DATE:
                target.getDate(fieldName);
                break;
            case DECIMAL128:
                target.getDecimal128(fieldName);
                break;
            case OBJECT_ID:
                target.getObjectId(fieldName);
                break;
            case UUID:
                target.getUUID(fieldName);
                break;
            case OBJECT:
                target.setObject(fieldName, null);
                target.setObject(fieldName, target);
                break;
            case LIST:
                target.setList(fieldName, new RealmList<DynamicRealmObject>());
                break;
            case LIST_INTEGER:
                target.setList(fieldName, new RealmList<Integer>(1));
                break;
            case LIST_STRING:
                target.setList(fieldName, new RealmList<String>("foo"));
                break;
            case LIST_BOOLEAN:
                target.setList(fieldName, new RealmList<Boolean>(true));
                break;
            case LIST_FLOAT:
                target.setList(fieldName, new RealmList<Float>(1.23F));
                break;
            case LIST_DOUBLE:
                target.setList(fieldName, new RealmList<Double>(1.234D));
                break;
            case LIST_BINARY:
                target.setList(fieldName, new RealmList<byte[]>(new byte[] {}));
                break;
            case LIST_DATE:
                target.setList(fieldName, new RealmList<Date>(new Date()));
                break;
            case LIST_DECIMAL128:
                target.setList(fieldName, new RealmList<>(new Decimal128(BigDecimal.ONE)));
                break;
            case LIST_OBJECT_ID:
                target.setList(fieldName, new RealmList<>(new ObjectId(TestHelper.generateObjectIdHexString(7))));
                break;
            case LIST_UUID:
                target.setList(fieldName, new RealmList<>(UUID.randomUUID()));
                break;
            default:
                fail();
        }
    }
}
Also used : ObjectId(org.bson.types.ObjectId) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Decimal128(org.bson.types.Decimal128) Date(java.util.Date) PrimaryKeyAsBoxedInteger(io.realm.entities.PrimaryKeyAsBoxedInteger) PrimaryKeyAsInteger(io.realm.entities.PrimaryKeyAsInteger)

Example 17 with Decimal128

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

the class BulkInsertTests method insert_realmModel.

@Test
public void insert_realmModel() {
    AllTypesRealmModel allTypes = new AllTypesRealmModel();
    allTypes.columnLong = 10;
    allTypes.columnBoolean = false;
    allTypes.columnBinary = new byte[] { 1, 2, 3 };
    allTypes.columnDate = new Date();
    allTypes.columnDouble = Math.PI;
    allTypes.columnFloat = 1.234567f;
    allTypes.columnString = "test data";
    allTypes.columnByte = 0x2A;
    allTypes.columnDecimal128 = new Decimal128(BigDecimal.TEN);
    allTypes.columnObjectId = new ObjectId(TestHelper.generateObjectIdHexString(7));
    allTypes.columnUUID = UUID.fromString(TestHelper.generateUUIDString(7));
    realm.beginTransaction();
    realm.insert(allTypes);
    realm.commitTransaction();
    AllTypesRealmModel first = realm.where(AllTypesRealmModel.class).findFirst();
    assertNotNull(first);
    assertEquals(allTypes.columnString, first.columnString);
    assertEquals(allTypes.columnLong, first.columnLong);
    assertEquals(allTypes.columnBoolean, first.columnBoolean);
    assertArrayEquals(allTypes.columnBinary, first.columnBinary);
    assertEquals(allTypes.columnDate, first.columnDate);
    assertEquals(allTypes.columnDouble, first.columnDouble, 0.0000001);
    assertEquals(allTypes.columnFloat, first.columnFloat, 0.0000001);
    assertEquals(allTypes.columnByte, first.columnByte);
    assertEquals(allTypes.columnDecimal128, first.columnDecimal128);
    assertEquals(allTypes.columnObjectId, first.columnObjectId);
    assertEquals(allTypes.columnUUID, first.columnUUID);
}
Also used : AllTypesRealmModel(io.realm.entities.pojo.AllTypesRealmModel) ObjectId(org.bson.types.ObjectId) Decimal128(org.bson.types.Decimal128) Date(java.util.Date) Test(org.junit.Test)

Example 18 with Decimal128

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

the class DynamicRealmObjectTests method untypedGetterSetter.

@Test
public void untypedGetterSetter() {
    realm.beginTransaction();
    AllJavaTypes obj = realm.createObject(AllJavaTypes.class, 0);
    DynamicRealmObject dObj = new DynamicRealmObject(obj);
    try {
        for (SupportedType type : SupportedType.values()) {
            switch(type) {
                case BOOLEAN:
                    dObj.set(AllJavaTypes.FIELD_BOOLEAN, true);
                    assertTrue((Boolean) dObj.get(AllJavaTypes.FIELD_BOOLEAN));
                    break;
                case SHORT:
                    dObj.set(AllJavaTypes.FIELD_SHORT, (short) 42);
                    assertEquals(Long.parseLong("42"), dObj.<Long>get(AllJavaTypes.FIELD_SHORT).longValue());
                    break;
                case INT:
                    dObj.set(AllJavaTypes.FIELD_INT, 42);
                    assertEquals(Long.parseLong("42"), dObj.<Long>get(AllJavaTypes.FIELD_INT).longValue());
                    break;
                case LONG:
                    dObj.set(AllJavaTypes.FIELD_LONG, 42L);
                    assertEquals(Long.parseLong("42"), dObj.<Long>get(AllJavaTypes.FIELD_LONG).longValue());
                    break;
                case BYTE:
                    dObj.set(AllJavaTypes.FIELD_BYTE, (byte) 4);
                    assertEquals(Long.parseLong("4"), dObj.<Long>get(AllJavaTypes.FIELD_BYTE).longValue());
                    break;
                case FLOAT:
                    dObj.set(AllJavaTypes.FIELD_FLOAT, 1.23f);
                    assertEquals(Float.parseFloat("1.23"), dObj.<Float>get(AllJavaTypes.FIELD_FLOAT), Float.MIN_NORMAL);
                    break;
                case DOUBLE:
                    dObj.set(AllJavaTypes.FIELD_DOUBLE, 1.234d);
                    assertEquals(Double.parseDouble("1.234"), dObj.<Double>get(AllJavaTypes.FIELD_DOUBLE), Double.MIN_NORMAL);
                    break;
                case STRING:
                    dObj.set(AllJavaTypes.FIELD_STRING, "str");
                    assertEquals("str", dObj.get(AllJavaTypes.FIELD_STRING));
                    break;
                case BINARY:
                    dObj.set(AllJavaTypes.FIELD_BINARY, new byte[] { 1, 2, 3 });
                    assertArrayEquals(new byte[] { 1, 2, 3 }, (byte[]) dObj.get(AllJavaTypes.FIELD_BINARY));
                    break;
                case DATE:
                    dObj.set(AllJavaTypes.FIELD_DATE, new Date(1000));
                    assertEquals(new Date(1000), dObj.get(AllJavaTypes.FIELD_DATE));
                    break;
                case DECIMAL128:
                    dObj.set(AllJavaTypes.FIELD_DECIMAL128, new Decimal128(BigDecimal.ONE));
                    assertEquals(new Decimal128(BigDecimal.ONE), dObj.get(AllJavaTypes.FIELD_DECIMAL128));
                    break;
                case OBJECT_ID:
                    dObj.set(AllJavaTypes.FIELD_OBJECT_ID, new ObjectId(TestHelper.generateObjectIdHexString(7)));
                    assertEquals(new ObjectId(TestHelper.generateObjectIdHexString(7)), dObj.get(AllJavaTypes.FIELD_OBJECT_ID));
                    break;
                case UUID:
                    String uuid = UUID.randomUUID().toString();
                    dObj.set(AllJavaTypes.FIELD_UUID, UUID.fromString(uuid));
                    assertEquals(UUID.fromString(uuid), dObj.get(AllJavaTypes.FIELD_UUID));
                    break;
                case OBJECT:
                    dObj.set(AllJavaTypes.FIELD_OBJECT, dObj);
                    assertEquals(dObj, dObj.get(AllJavaTypes.FIELD_OBJECT));
                    break;
                case LIST:
                    {
                        RealmList<DynamicRealmObject> newList = new RealmList<DynamicRealmObject>();
                        newList.add(dObj);
                        dObj.set(AllJavaTypes.FIELD_LIST, newList);
                        RealmList<DynamicRealmObject> list = dObj.getList(AllJavaTypes.FIELD_LIST);
                        assertEquals(1, list.size());
                        assertEquals(dObj, list.get(0));
                        break;
                    }
                case LIST_INTEGER:
                    {
                        RealmList<Integer> newList = new RealmList<>(null, 1);
                        dObj.set(AllJavaTypes.FIELD_INTEGER_LIST, newList);
                        RealmList<Integer> list = dObj.getList(AllJavaTypes.FIELD_INTEGER_LIST, Integer.class);
                        assertEquals(2, list.size());
                        assertArrayEquals(newList.toArray(), list.toArray());
                        break;
                    }
                case LIST_STRING:
                    {
                        RealmList<String> newList = new RealmList<>(null, "Foo");
                        dObj.set(AllJavaTypes.FIELD_STRING_LIST, newList);
                        RealmList<String> list = dObj.getList(AllJavaTypes.FIELD_STRING_LIST, String.class);
                        assertEquals(2, list.size());
                        assertArrayEquals(newList.toArray(), list.toArray());
                        break;
                    }
                case LIST_BOOLEAN:
                    {
                        RealmList<Boolean> newList = new RealmList<>(null, true);
                        dObj.set(AllJavaTypes.FIELD_BOOLEAN_LIST, newList);
                        RealmList<Boolean> list = dObj.getList(AllJavaTypes.FIELD_BOOLEAN_LIST, Boolean.class);
                        assertEquals(2, list.size());
                        assertArrayEquals(newList.toArray(), list.toArray());
                        break;
                    }
                case LIST_FLOAT:
                    {
                        RealmList<Float> newList = new RealmList<>(null, 1.23F);
                        dObj.set(AllJavaTypes.FIELD_FLOAT_LIST, newList);
                        RealmList<Float> list = dObj.getList(AllJavaTypes.FIELD_FLOAT_LIST, Float.class);
                        assertEquals(2, list.size());
                        assertArrayEquals(newList.toArray(), list.toArray());
                        break;
                    }
                case LIST_DOUBLE:
                    {
                        RealmList<Double> newList = new RealmList<>(null, 1.24D);
                        dObj.set(AllJavaTypes.FIELD_DOUBLE_LIST, newList);
                        RealmList<Double> list = dObj.getList(AllJavaTypes.FIELD_DOUBLE_LIST, Double.class);
                        assertEquals(2, list.size());
                        assertArrayEquals(newList.toArray(), list.toArray());
                        break;
                    }
                case LIST_BINARY:
                    {
                        RealmList<byte[]> newList = new RealmList<>(null, new byte[] { 1, 2, 3 });
                        dObj.set(AllJavaTypes.FIELD_BINARY_LIST, newList);
                        RealmList<byte[]> list = dObj.getList(AllJavaTypes.FIELD_BINARY_LIST, byte[].class);
                        assertEquals(2, list.size());
                        assertArrayEquals(newList.toArray(), list.toArray());
                        break;
                    }
                case LIST_DATE:
                    {
                        RealmList<Date> newList = new RealmList<>(null, new Date(1000));
                        dObj.set(AllJavaTypes.FIELD_DATE_LIST, newList);
                        RealmList<Date> list = dObj.getList(AllJavaTypes.FIELD_DATE_LIST, Date.class);
                        assertEquals(2, list.size());
                        assertArrayEquals(newList.toArray(), list.toArray());
                        break;
                    }
                case LIST_DECIMAL128:
                    {
                        RealmList<Decimal128> newList = new RealmList<>(null, new Decimal128(BigDecimal.ONE));
                        dObj.set(AllJavaTypes.FIELD_DECIMAL128_LIST, newList);
                        RealmList<Decimal128> list = dObj.getList(AllJavaTypes.FIELD_DECIMAL128_LIST, Decimal128.class);
                        assertEquals(2, list.size());
                        assertArrayEquals(newList.toArray(), list.toArray());
                        break;
                    }
                case LIST_OBJECT_ID:
                    {
                        RealmList<ObjectId> newList = new RealmList<>(null, new ObjectId(TestHelper.generateObjectIdHexString(0)));
                        dObj.set(AllJavaTypes.FIELD_OBJECT_ID_LIST, newList);
                        RealmList<ObjectId> list = dObj.getList(AllJavaTypes.FIELD_OBJECT_ID_LIST, ObjectId.class);
                        assertEquals(2, list.size());
                        assertArrayEquals(newList.toArray(), list.toArray());
                        break;
                    }
                case LIST_UUID:
                    {
                        RealmList<UUID> newList = new RealmList<>(null, UUID.randomUUID());
                        dObj.set(AllJavaTypes.FIELD_UUID_LIST, newList);
                        RealmList<UUID> list = dObj.getList(AllJavaTypes.FIELD_UUID_LIST, UUID.class);
                        assertEquals(2, list.size());
                        assertArrayEquals(newList.toArray(), list.toArray());
                        break;
                    }
                default:
                    fail();
            }
        }
    } finally {
        realm.cancelTransaction();
    }
}
Also used : ObjectId(org.bson.types.ObjectId) Decimal128(org.bson.types.Decimal128) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) Date(java.util.Date) PrimaryKeyAsBoxedInteger(io.realm.entities.PrimaryKeyAsBoxedInteger) PrimaryKeyAsInteger(io.realm.entities.PrimaryKeyAsInteger) PrimaryKeyAsLong(io.realm.entities.PrimaryKeyAsLong) PrimaryKeyAsBoxedLong(io.realm.entities.PrimaryKeyAsBoxedLong) AllJavaTypes(io.realm.entities.AllJavaTypes) UUID(java.util.UUID) Test(org.junit.Test)

Example 19 with Decimal128

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

the class FrozenObjectsTests method createDataForLiveRealm.

private Realm createDataForLiveRealm(int dataSize) {
    realm.executeTransaction(r -> {
        RealmList<Dog> list = new RealmList<>();
        for (int i = 0; i < 5; i++) {
            list.add(r.copyToRealm(new Dog("Dog " + i)));
        }
        for (int i = 0; i < dataSize; i++) {
            AllTypes obj = new AllTypes();
            obj.setColumnString("String " + i);
            obj.setColumnLong(i);
            obj.setColumnRealmList(list);
            obj.setColumnStringList(new RealmList<>("Foo", "Bar", "Baz"));
            obj.setColumnRealmObject(r.copyToRealm(new Dog("Dog 42")));
            obj.setColumnObjectId(new ObjectId(TestHelper.randomObjectIdHexString()));
            obj.setColumnDecimal128(new Decimal128(new BigDecimal(i + ".23456789")));
            r.insert(obj);
        }
    });
    return realm;
}
Also used : ObjectId(org.bson.types.ObjectId) AllTypes(io.realm.entities.AllTypes) Decimal128(org.bson.types.Decimal128) Dog(io.realm.entities.Dog) BigDecimal(java.math.BigDecimal)

Example 20 with Decimal128

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

the class RealmJsonTests method checkNullableValuesAreNotNull.

// Checks the imported object from nulltyps.json[1].
private void checkNullableValuesAreNotNull(NullTypes nullTypes2) {
    // 1 String
    assertEquals("", nullTypes2.getFieldStringNull());
    assertEquals("", nullTypes2.getFieldStringNotNull());
    // 2 Bytes
    assertTrue(Arrays.equals(new byte[0], nullTypes2.getFieldBytesNull()));
    assertTrue(Arrays.equals(new byte[0], nullTypes2.getFieldBytesNotNull()));
    // 3 Boolean
    assertFalse(nullTypes2.getFieldBooleanNull());
    assertFalse(nullTypes2.getFieldBooleanNotNull());
    // 4 Byte
    assertEquals(0, nullTypes2.getFieldByteNull().byteValue());
    assertEquals(0, nullTypes2.getFieldByteNotNull().byteValue());
    // 5 Short
    assertEquals(0, nullTypes2.getFieldShortNull().shortValue());
    assertEquals(0, nullTypes2.getFieldShortNotNull().shortValue());
    // 6 Integer
    assertEquals(0, nullTypes2.getFieldIntegerNull().intValue());
    assertEquals(0, nullTypes2.getFieldIntegerNotNull().intValue());
    // 7 Long
    assertEquals(0, nullTypes2.getFieldLongNull().longValue());
    assertEquals(0, nullTypes2.getFieldLongNotNull().longValue());
    // 8 Float
    assertEquals((Float) 0F, nullTypes2.getFieldFloatNull());
    assertEquals((Float) 0F, nullTypes2.getFieldFloatNotNull());
    // 9 Double
    assertEquals((Double) 0D, nullTypes2.getFieldDoubleNull());
    assertEquals((Double) 0D, nullTypes2.getFieldDoubleNotNull());
    // 10 Date
    assertEquals(new Date(0), nullTypes2.getFieldDateNull());
    assertEquals(new Date(0), nullTypes2.getFieldDateNotNull());
    // 11 RealmObject
    assertTrue(nullTypes2.getFieldObjectNull() != null);
    // 12 ObjectId
    assertTrue(nullTypes2.getFieldObjectIdNull() != null);
    assertEquals(new ObjectId("789ABCDEF0123456789ABCDE"), nullTypes2.getFieldObjectIdNotNull());
    // 13 Decimal128
    assertTrue(nullTypes2.getFieldDecimal128Null() != null);
    assertEquals(new Decimal128(0), nullTypes2.getFieldDecimal128NotNull());
    // 14 UUID
    assertTrue(nullTypes2.getFieldUUIDNull() != null);
    assertEquals(UUID.fromString("027ba5ca-aa12-4afa-9219-e20cc3018599"), nullTypes2.getFieldUUIDNotNull());
}
Also used : PrimaryKeyAsObjectId(io.realm.entities.PrimaryKeyAsObjectId) ObjectId(org.bson.types.ObjectId) Decimal128(org.bson.types.Decimal128) Date(java.util.Date)

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