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());
}
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;
}
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)));
}
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());
}
}
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());
}
}
Aggregations