Search in sources :

Example 76 with Decimal128

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

the class Decimal128QuickTour method main.

/**
 * Run this main method to see the output of this quick example.
 *
 * @param args takes an optional single argument for the connection string
 */
public static void main(final String[] args) {
    MongoClient mongoClient;
    if (args.length == 0) {
        // connect to the local database server
        mongoClient = MongoClients.create();
    } else {
        mongoClient = MongoClients.create(args[0]);
    }
    // get handle to "mydb" database
    MongoDatabase database = mongoClient.getDatabase("mydb");
    // get a handle to the "test" collection
    MongoCollection<Document> collection = database.getCollection("test");
    // drop all the data in it
    collection.drop();
    // make a document and insert it
    Document doc = new Document("name", "MongoDB").append("amount1", Decimal128.parse(".10")).append("amount2", new Decimal128(42L)).append("amount3", new Decimal128(new BigDecimal(".200")));
    collection.insertOne(doc);
    Document first = collection.find().filter(Filters.eq("amount1", new Decimal128(new BigDecimal(".10")))).first();
    Decimal128 amount3 = (Decimal128) first.get("amount3");
    BigDecimal amount2AsBigDecimal = amount3.bigDecimalValue();
    System.out.println(amount3.toString());
    System.out.println(amount2AsBigDecimal.toString());
}
Also used : MongoClient(com.mongodb.client.MongoClient) Decimal128(org.bson.types.Decimal128) Document(org.bson.Document) BigDecimal(java.math.BigDecimal) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 77 with Decimal128

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

the class NumberCodecHelper method decodeLong.

static long decodeLong(final BsonReader reader) {
    long longValue;
    BsonType bsonType = reader.getCurrentBsonType();
    switch(bsonType) {
        case INT32:
            longValue = reader.readInt32();
            break;
        case INT64:
            longValue = reader.readInt64();
            break;
        case DOUBLE:
            double doubleValue = reader.readDouble();
            longValue = (long) doubleValue;
            if (doubleValue != (double) longValue) {
                throw invalidConversion(Long.class, doubleValue);
            }
            break;
        case DECIMAL128:
            Decimal128 decimal128 = reader.readDecimal128();
            longValue = decimal128.longValue();
            if (!decimal128.equals(new Decimal128(longValue))) {
                throw invalidConversion(Long.class, decimal128);
            }
            break;
        default:
            throw new BsonInvalidOperationException(format("Invalid numeric type, found: %s", bsonType));
    }
    return longValue;
}
Also used : BsonType(org.bson.BsonType) BsonInvalidOperationException(org.bson.BsonInvalidOperationException) Decimal128(org.bson.types.Decimal128)

Example 78 with Decimal128

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

the class TestWindows method rangeBased.

@Test
void rangeBased() {
    assertAll(() -> assertEquals(new BsonDocument("range", new BsonArray(asList(new BsonInt64(-1), new BsonInt64(0)))), range(-1, 0).toBsonDocument()), () -> assertEquals(new BsonDocument("range", new BsonArray(asList(new BsonDouble(0), new BsonDouble(0)))), range(0d, 0d).toBsonDocument()), () -> assertEquals(new BsonDocument("range", new BsonArray(asList(new BsonDecimal128(new Decimal128(1)), new BsonDecimal128(new Decimal128(2))))), range(new Decimal128(1), new Decimal128(2)).toBsonDocument()), () -> assertEquals(new BsonDocument("range", new BsonArray(asList(new BsonString(CURRENT.value()), new BsonDouble(0.1)))), range(CURRENT, 0.1).toBsonDocument()), () -> assertEquals(new BsonDocument("range", new BsonArray(asList(new BsonDouble(0.1), new BsonString(UNBOUNDED.value())))), range(0.1, UNBOUNDED).toBsonDocument()), () -> assertEquals(new BsonDocument("range", new BsonArray(asList(new BsonString(CURRENT.value()), new BsonDecimal128(new Decimal128(Long.MAX_VALUE))))), range(CURRENT, new Decimal128(Long.MAX_VALUE)).toBsonDocument()), () -> assertEquals(new BsonDocument("range", new BsonArray(asList(new BsonDecimal128(new Decimal128(Long.MAX_VALUE)), new BsonString(UNBOUNDED.value())))), range(new Decimal128(Long.MAX_VALUE), UNBOUNDED).toBsonDocument()), () -> assertEquals(new BsonDocument("range", new BsonArray(asList(new BsonInt64(-1), new BsonInt64(0)))).append("unit", new BsonString("millisecond")), timeRange(-1, 0, MILLISECOND).toBsonDocument()), () -> assertEquals(new BsonDocument("range", new BsonArray(asList(new BsonString(CURRENT.value()), new BsonInt64(1)))).append("unit", new BsonString("hour")), timeRange(CURRENT, 1, HOUR).toBsonDocument()), () -> assertEquals(new BsonDocument("range", new BsonArray(asList(new BsonInt64(1), new BsonString(UNBOUNDED.value())))).append("unit", new BsonString("month")), timeRange(1, MONTH, UNBOUNDED).toBsonDocument()));
    assertAll(() -> assertThrows(IllegalArgumentException.class, () -> range(1, -1)), () -> assertThrows(IllegalArgumentException.class, () -> range(null, 1)), () -> assertThrows(IllegalArgumentException.class, () -> range(null, 0.1)), () -> assertThrows(IllegalArgumentException.class, () -> range((Bound) null, Decimal128.POSITIVE_ZERO)), () -> assertThrows(IllegalArgumentException.class, () -> range(1, null)), () -> assertThrows(IllegalArgumentException.class, () -> range(0.1, null)), () -> assertThrows(IllegalArgumentException.class, () -> range(Decimal128.POSITIVE_ZERO, (Bound) null)), () -> assertThrows(IllegalArgumentException.class, () -> range((Decimal128) null, Decimal128.POSITIVE_ZERO)), () -> assertThrows(IllegalArgumentException.class, () -> range(Decimal128.POSITIVE_ZERO, (Decimal128) null)), () -> assertThrows(IllegalArgumentException.class, () -> range((Decimal128) null, (Decimal128) null)), () -> assertThrows(IllegalArgumentException.class, () -> timeRange(1, -1, MongoTimeUnit.DAY)), () -> assertThrows(IllegalArgumentException.class, () -> timeRange(1, 2, null)));
}
Also used : BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonDecimal128(org.bson.BsonDecimal128) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonDouble(org.bson.BsonDouble) BsonDecimal128(org.bson.BsonDecimal128) Decimal128(org.bson.types.Decimal128) Test(org.junit.jupiter.api.Test)

Example 79 with Decimal128

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

the class JsonWriterTest method testDecimal128SShell.

@Test
public void testDecimal128SShell() {
    List<TestData<Decimal128>> tests = asList(new TestData<Decimal128>(Decimal128.parse("1.0"), "1.0"), new TestData<Decimal128>(Decimal128.POSITIVE_INFINITY, Decimal128.POSITIVE_INFINITY.toString()));
    for (final TestData<Decimal128> cur : tests) {
        stringWriter = new StringWriter();
        writer = new JsonWriter(stringWriter, JsonWriterSettings.builder().outputMode(JsonMode.SHELL).build());
        writer.writeStartDocument();
        writer.writeDecimal128("d", cur.value);
        writer.writeEndDocument();
        String expected = "{\"d\": NumberDecimal(\"" + cur.expected + "\")}";
        assertEquals(expected, stringWriter.toString());
    }
}
Also used : StringWriter(java.io.StringWriter) Decimal128(org.bson.types.Decimal128) Test(org.junit.Test)

Example 80 with Decimal128

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

the class JsonWriterTest method testDecimal128Relaxed.

@Test
public void testDecimal128Relaxed() {
    List<TestData<Decimal128>> tests = asList(new TestData<Decimal128>(Decimal128.parse("1.0"), "1.0"), new TestData<Decimal128>(Decimal128.POSITIVE_INFINITY, Decimal128.POSITIVE_INFINITY.toString()));
    for (final TestData<Decimal128> cur : tests) {
        stringWriter = new StringWriter();
        writer = new JsonWriter(stringWriter, JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build());
        writer.writeStartDocument();
        writer.writeDecimal128("d", cur.value);
        writer.writeEndDocument();
        String expected = "{\"d\": {\"$numberDecimal\": \"" + cur.expected + "\"}}";
        assertEquals(expected, stringWriter.toString());
    }
}
Also used : StringWriter(java.io.StringWriter) Decimal128(org.bson.types.Decimal128) Test(org.junit.Test)

Aggregations

Decimal128 (org.bson.types.Decimal128)80 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