/** * Create an instance with the given high and low order bits representing this Decimal128 as an IEEE 754-2008 128-bit decimal * floating point using the BID encoding scheme. * * @param high the high-order 64 bits * @param low the low-order 64 bits * @return the Decimal128 value representing the given high and low order bits */ public static Decimal128 fromIEEE754BIDEncoding(final long high, final long low) { return new Decimal128(high, low); }
@Override public Decimal128 decimal128Value() { return new Decimal128(value); }
@Override public Decimal128 decimal128Value() { return new Decimal128(value); }
@Override public Decimal128 decimal128Value() { if (Double.isNaN(value)) { return Decimal128.NaN; } if (Double.isInfinite(value)) { return value > 0 ? Decimal128.POSITIVE_INFINITY : Decimal128.NEGATIVE_INFINITY; } return new Decimal128(new BigDecimal(value)); }
@Override public Object encode(final Object value, final MappedField optionalExtraInfo) { if (value instanceof BigDecimal) { return new Decimal128((BigDecimal) value); } return super.encode(value, optionalExtraInfo); } }
@Override public void encode(final BsonWriter writer, final BigDecimal value, final EncoderContext encoderContext) { writer.writeDecimal128(new Decimal128(value)); }
@Override public void writeNumber(BigDecimal number) throws IOException { try { writer.writeDecimal128(new Decimal128(number)); } catch (NumberFormatException e) { writer.writeString(number.toString()); } }
/** * Returns a Decimal128 value representing the given String. * * @param value the Decimal128 value represented as a String * @return the Decimal128 value representing the given String * @throws NumberFormatException if the value is out of the Decimal128 range * @see * <a href="https://github.com/mongodb/specifications/blob/master/source/bson-decimal128/decimal128.rst#from-string-representation"> * From-String Specification</a> */ public static Decimal128 parse(final String value) { String lowerCasedValue = value.toLowerCase(); if (NaN_STRINGS.contains(lowerCasedValue)) { return NaN; } if (NEGATIVE_NaN_STRINGS.contains(lowerCasedValue)) { return NEGATIVE_NaN; } if (POSITIVE_INFINITY_STRINGS.contains(lowerCasedValue)) { return POSITIVE_INFINITY; } if (NEGATIVE_INFINITY_STRINGS.contains(lowerCasedValue)) { return NEGATIVE_INFINITY; } return new Decimal128(new BigDecimal(value), value.charAt(0) == '-'); }
return clazz.cast(new Decimal128((Integer) value)); } else if (value instanceof Long) { return clazz.cast(new Decimal128((Long) value)); } else if (value instanceof Double) { return clazz.cast(new BsonDouble((Double) value).decimal128Value());
final BigDecimal decimal = (BigDecimal) value; try { return value(new Decimal128(decimal)); } catch (NumberFormatException ex) { return value(new Decimal128(decimal)); } catch (NumberFormatException ex) {
try { doubleValue = decimal128.doubleValue(); if (!decimal128.equals(new Decimal128(new BigDecimal(doubleValue)))) { throw invalidConversion(Double.class, decimal128);
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; }
Decimal128 decimal128 = reader.readDecimal128(); intValue = decimal128.intValue(); if (!decimal128.equals(new Decimal128(intValue))) { throw invalidConversion(Integer.class, decimal128);
@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(); }
/** * Serializing big numbers which can't be stored in {@link org.bson.types.Decimal128} format. * They should be serialized as strings (or binary) in mongo. */ @Test public void larger_than_decimal128() { final AdvancedRepository repo = new AdvancedRepository(context.setup()); final ObjectId id = ObjectId.get(); final BigInteger integer = BigInteger.valueOf(Long.MAX_VALUE).pow(4); try { new Decimal128(new BigDecimal(integer)); fail("Should fail for " + integer); } catch (NumberFormatException ignore) { // expected } final Advanced doc = ImmutableAdvanced.builder() .id(id) .bigDecimal(new BigDecimal(integer)) // make number big .bigInteger(integer) // make it also big .atomicBoolean(new AtomicBoolean(false)) .atomicInteger(new AtomicInteger(1)) .atomicLong(new AtomicLong(2)) .build(); repo.insert(doc).getUnchecked(); final Advanced doc2 = repo.findById(id).fetchFirst().getUnchecked().get(); check(doc2.bigDecimal().unscaledValue()).is(integer); check(doc2.bigInteger()).is(integer); }
.append("_id", new Decimal128(new BigDecimal("123.45678"))) .append("name", "Sally"); event = new Document().append("o", obj)
@Override public Object encode(final Object value, final MappedField optionalExtraInfo) { if (value instanceof BigDecimal) { return new Decimal128((BigDecimal) value); } return super.encode(value, optionalExtraInfo); } }
@Override public void encode(BsonWriter writer, BigDecimal value, EncoderContext encoderContext) { writer.writeDecimal128(new Decimal128(value)); }
@Override public BsonValue toBson(Object data) { if(data instanceof BigDecimal) { if(format.equals(Format.DECIMAL128)) return new BsonDecimal128(new Decimal128((BigDecimal)data)); if(format.equals(Format.LEGACYDOUBLE)) return new BsonDouble(((BigDecimal)data).doubleValue()); } throw new DataException("error: decimal conversion not possible when data is" + " of type "+data.getClass().getName() + " and format is "+format); } }
@Override public BsonValue toBson(Object data) { if(data instanceof BigDecimal) { if(format.equals(Format.DECIMAL128)) return new BsonDecimal128(new Decimal128((BigDecimal)data)); if(format.equals(Format.LEGACYDOUBLE)) return new BsonDouble(((BigDecimal)data).doubleValue()); } throw new DataException("error: decimal conversion not possible when data is" + " of type "+data.getClass().getName() + " and format is "+format); } }