Search in sources :

Example 11 with Decimal128

use of org.bson.types.Decimal128 in project mongo-java-driver by mongodb.

the class JsonReaderTest method testDecimal128Int64Constructor.

@Test
public void testDecimal128Int64Constructor() {
    String json = "NumberDecimal(" + Long.MAX_VALUE + ")";
    testStringAndStream(json, bsonReader -> {
        assertEquals(BsonType.DECIMAL128, bsonReader.readBsonType());
        assertEquals(new Decimal128(Long.MAX_VALUE), bsonReader.readDecimal128());
        assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
        return null;
    });
}
Also used : Decimal128(org.bson.types.Decimal128) Test(org.junit.Test)

Example 12 with Decimal128

use of org.bson.types.Decimal128 in project immutables by immutables.

the class TypeConversionTest method decimal128.

@Test
void decimal128() throws IOException {
    BsonDecimal128 value = new BsonDecimal128(new Decimal128(BigDecimal.valueOf(1.1)));
    check(Parsers.parserAt(value).getCurrentToken()).is(JsonToken.VALUE_NUMBER_FLOAT);
    check(Parsers.parserAt(value).getNumberType()).is(JsonParser.NumberType.BIG_DECIMAL);
    check(Parsers.parserAt(value).getDoubleValue()).is(1.1);
    check(Parsers.parserAt(value).getDecimalValue()).is(BigDecimal.valueOf(1.1));
}
Also used : BsonDecimal128(org.bson.BsonDecimal128) Decimal128(org.bson.types.Decimal128) BsonDecimal128(org.bson.BsonDecimal128) Test(org.junit.jupiter.api.Test)

Example 13 with Decimal128

use of org.bson.types.Decimal128 in project immutables by immutables.

the class BsonDecimal128Test method read.

@Test
public void read() throws Exception {
    BsonDocument doc = new BsonDocument();
    doc.put("int", new BsonDecimal128(Decimal128.parse(Integer.toString(Integer.MAX_VALUE))));
    doc.put("long", new BsonDecimal128(new Decimal128(Long.MAX_VALUE)));
    doc.put("double", new BsonDecimal128(Decimal128.parse("12.111")));
    JsonReader reader = Jsons.asGsonReader(doc);
    reader.beginObject();
    check(reader.nextName()).is("int");
    check(reader.peek()).is(JsonToken.NUMBER);
    check(reader.nextInt()).is(Integer.MAX_VALUE);
    check(reader.nextName()).is("long");
    check(reader.peek()).is(JsonToken.NUMBER);
    check(reader.nextLong()).is(Long.MAX_VALUE);
    check(reader.nextName()).is("double");
    check(reader.peek()).is(JsonToken.NUMBER);
    check(reader.nextDouble()).is(12.111D);
    reader.endObject();
    reader.close();
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDecimal128(org.bson.BsonDecimal128) JsonReader(com.google.gson.stream.JsonReader) BsonDecimal128(org.bson.BsonDecimal128) Decimal128(org.bson.types.Decimal128) Test(org.junit.Test)

Example 14 with Decimal128

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

the class DynamicRealmObjectTests method untypedSetter_usingStringConversion.

@Test
public void untypedSetter_usingStringConversion() {
    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(dObj.getBoolean(AllJavaTypes.FIELD_BOOLEAN));
                    break;
                case SHORT:
                    dObj.set(AllJavaTypes.FIELD_SHORT, "42");
                    assertEquals((short) 42, dObj.getShort(AllJavaTypes.FIELD_SHORT));
                    break;
                case INT:
                    dObj.set(AllJavaTypes.FIELD_INT, "42");
                    assertEquals(42, dObj.getInt(AllJavaTypes.FIELD_INT));
                    break;
                case LONG:
                    dObj.set(AllJavaTypes.FIELD_LONG, "42");
                    assertEquals((long) 42, dObj.getLong(AllJavaTypes.FIELD_LONG));
                    break;
                case FLOAT:
                    dObj.set(AllJavaTypes.FIELD_FLOAT, "1.23");
                    assertEquals(1.23f, dObj.getFloat(AllJavaTypes.FIELD_FLOAT), 0f);
                    break;
                case DOUBLE:
                    dObj.set(AllJavaTypes.FIELD_DOUBLE, "1.234");
                    assertEquals(1.234d, dObj.getDouble(AllJavaTypes.FIELD_DOUBLE), 0f);
                    break;
                case DATE:
                    dObj.set(AllJavaTypes.FIELD_DATE, "1000");
                    assertEquals(new Date(1000), dObj.getDate(AllJavaTypes.FIELD_DATE));
                    break;
                case DECIMAL128:
                    dObj.set(AllJavaTypes.FIELD_DECIMAL128, "1");
                    assertEquals(new Decimal128(BigDecimal.ONE), dObj.get(AllJavaTypes.FIELD_DECIMAL128));
                    break;
                case OBJECT_ID:
                    dObj.set(AllJavaTypes.FIELD_OBJECT_ID, 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;
                // These types don't have a string representation that can be parsed.
                case OBJECT:
                case LIST:
                case LIST_INTEGER:
                case LIST_STRING:
                case LIST_BOOLEAN:
                case LIST_FLOAT:
                case LIST_DOUBLE:
                case LIST_BINARY:
                case LIST_DATE:
                case LIST_DECIMAL128:
                case LIST_OBJECT_ID:
                case LIST_UUID:
                case STRING:
                case BINARY:
                case BYTE:
                    break;
                default:
                    fail("Unknown type: " + type);
                    break;
            }
        }
    } finally {
        realm.cancelTransaction();
    }
}
Also used : ObjectId(org.bson.types.ObjectId) Decimal128(org.bson.types.Decimal128) PrimaryKeyAsString(io.realm.entities.PrimaryKeyAsString) AllJavaTypes(io.realm.entities.AllJavaTypes) Date(java.util.Date) Test(org.junit.Test)

Example 15 with Decimal128

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

the class DynamicRealmObjectTests method setUp.

@Before
public void setUp() {
    RealmConfiguration realmConfig = configFactory.createConfiguration();
    realm = Realm.getInstance(realmConfig);
    realm.beginTransaction();
    typedObj = realm.createObject(AllJavaTypes.class, 1);
    typedObj.setFieldString("str");
    typedObj.setFieldShort((short) 1);
    typedObj.setFieldInt(1);
    typedObj.setFieldLong(1);
    typedObj.setFieldByte((byte) 4);
    typedObj.setFieldFloat(1.23f);
    typedObj.setFieldDouble(1.234d);
    typedObj.setFieldBinary(new byte[] { 1, 2, 3 });
    typedObj.setFieldBoolean(true);
    typedObj.setFieldDate(new Date(1000));
    typedObj.setFieldDecimal128(new Decimal128(BigDecimal.TEN));
    typedObj.setFieldObjectId(new ObjectId(TestHelper.generateObjectIdHexString(7)));
    typedObj.setFieldUUID(UUID.randomUUID());
    typedObj.setFieldObject(typedObj);
    typedObj.getFieldList().add(typedObj);
    typedObj.getFieldIntegerList().add(1);
    typedObj.getFieldStringList().add("str");
    typedObj.getFieldBooleanList().add(true);
    typedObj.getFieldFloatList().add(1.23F);
    typedObj.getFieldDoubleList().add(1.234D);
    typedObj.getFieldBinaryList().add(new byte[] { 1, 2, 3 });
    typedObj.getFieldDateList().add(new Date(1000));
    dObjTyped = new DynamicRealmObject(typedObj);
    realm.commitTransaction();
    dynamicRealm = DynamicRealm.getInstance(realm.getConfiguration());
    dObjDynamic = dynamicRealm.where(AllJavaTypes.CLASS_NAME).findFirst();
}
Also used : ObjectId(org.bson.types.ObjectId) Decimal128(org.bson.types.Decimal128) AllJavaTypes(io.realm.entities.AllJavaTypes) Date(java.util.Date) Before(org.junit.Before)

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