/** * If a value is a variable-sized decimal, invoke the specified consumer with the value, otherwise do nothing. * * @param consumer block to be executed if the value is a big decimal * @return true if the block was called, or false otherwise */ default boolean ifBigDecimal(Consumer<BigDecimal> consumer) { if (isBigDecimal()) { consumer.accept(asBigDecimal()); return true; } return false; }
/** * Get the big decimal value in this document for the given field name. * * @param fieldName The name of the field * @param defaultValue the default value to return if there is no such field or if the value is not a big decimal * @return The big decimal field value, if found, or null if there is no such field or if the value is not a big decimal */ default BigDecimal getBigDecimal(CharSequence fieldName, BigDecimal defaultValue) { Value value = get(fieldName); return value != null && value.isBigDecimal() ? value.asBigDecimal() : defaultValue; }
@Override public BigInteger asBigInteger() { if (value.isBigInteger()) return value.asBigInteger(); if (value.isBigDecimal()) return value.asBigDecimal().toBigInteger(); if (value instanceof Number) return BigInteger.valueOf(asLong().longValue()); if (value.isString()) { try { return new BigInteger(asString()); } catch (NumberFormatException e) { } } return null; }
@Override public BigDecimal asBigDecimal() { if (value.isBigDecimal()) return value.asBigDecimal(); if (value.isBigInteger()) return new BigDecimal(value.asBigInteger()); if (value.isInteger() || value.isLong()) return BigDecimal.valueOf(asLong().longValue()); if (value.isFloat() || value.isDouble()) return BigDecimal.valueOf(asDouble().doubleValue()); if (value.isString()) { try { return new BigDecimal(asString()); } catch (NumberFormatException e) { } } return null; }
@Override public boolean equals(Object obj) { if (obj == this) return true; if (obj instanceof Value) { Value that = (Value) obj; if (this.isNumber() && that.isNumber()) { if (this.isLong()) return this.asLong().equals(that.asLong()); if (this.isDouble()) return this.asDouble().equals(that.asDouble()); if (this.isInteger()) return this.asInteger().equals(that.asInteger()); if (this.isFloat()) return this.asFloat().equals(that.asFloat()); if (this.isBigDecimal()) return this.asBigDecimal().equals(that.asBigDecimal()); if (this.isBigInteger()) return this.asBigInteger().equals(that.asBigInteger()); } return this.value.equals(that.asObject()); } // Compare the value straight away ... return this.value.equals(obj); }
break; case DECIMAL: generator.writeNumber(value.asBigDecimal()); break; case DOCUMENT:
@SuppressWarnings("unchecked") @Override public int compareTo(Value that) { if (Value.isNull(that)) return 1; if ( this.isBoolean() && that.isBoolean() ) { return this.asBoolean().compareTo(that.asBoolean()); } if (this.isNumber() && that.isNumber()) { if (this.isLong()) return this.asLong().compareTo(that.asLong()); if (this.isDouble()) return this.asDouble().compareTo(that.asDouble()); if (this.isInteger()) return this.asInteger().compareTo(that.asInteger()); if (this.isFloat()) return this.asFloat().compareTo(that.asFloat()); if (this.isBigDecimal()) return this.asBigDecimal().compareTo(that.asBigDecimal()); return this.asBigInteger().compareTo(that.asBigInteger()); } if ( this.isDocument() && that.isDocument()) { return this.asDocument().compareTo(that.asDocument()); } if ( this.isArray() && that.isArray() ) { return this.asArray().compareTo(that.asArray()); } Comparable<Object> thisValue = (Comparable<Object>)this.asObject(); Comparable<Object> thatValue = (Comparable<Object>)((ComparableValue)that.comparable()).asObject(); if ( thisValue.getClass().isAssignableFrom(thatValue.getClass())) { return thisValue.compareTo(thatValue); } else if (thatValue.getClass().isAssignableFrom(thisValue.getClass())) { return thatValue.compareTo(thisValue) * -1; // correct for the reversed comparison } return ((Comparable<Object>) this.value).compareTo(that.asObject()); }
/** * If a value is a variable-sized decimal, invoke the specified consumer with the value, otherwise do nothing. * * @param consumer block to be executed if the value is a big decimal * @return true if the block was called, or false otherwise */ default boolean ifBigDecimal(Consumer<BigDecimal> consumer) { if (isBigDecimal()) { consumer.accept(asBigDecimal()); return true; } return false; }
/** * Get the big decimal value in this document for the given field name. * * @param fieldName The name of the field * @param defaultValue the default value to return if there is no such field or if the value is not a big decimal * @return The big decimal field value, if found, or null if there is no such field or if the value is not a big decimal */ default BigDecimal getBigDecimal(CharSequence fieldName, BigDecimal defaultValue) { Value value = get(fieldName); return value != null && value.isBigDecimal() ? value.asBigDecimal() : defaultValue; }
@Override public BigInteger asBigInteger() { if (value.isBigInteger()) return value.asBigInteger(); if (value.isBigDecimal()) return value.asBigDecimal().toBigInteger(); if (value instanceof Number) return BigInteger.valueOf(asLong().longValue()); if (value.isString()) { try { return new BigInteger(asString()); } catch (NumberFormatException e) { } } return null; }
@Override public BigDecimal asBigDecimal() { if (value.isBigDecimal()) return value.asBigDecimal(); if (value.isBigInteger()) return new BigDecimal(value.asBigInteger()); if (value.isInteger() || value.isLong()) return BigDecimal.valueOf(asLong().longValue()); if (value.isFloat() || value.isDouble()) return BigDecimal.valueOf(asDouble().doubleValue()); if (value.isString()) { try { return new BigDecimal(asString()); } catch (NumberFormatException e) { } } return null; }
@Override public boolean equals(Object obj) { if (obj == this) return true; if (obj instanceof Value) { Value that = (Value) obj; if (this.isNumber() && that.isNumber()) { if (this.isLong()) return this.asLong().equals(that.asLong()); if (this.isDouble()) return this.asDouble().equals(that.asDouble()); if (this.isInteger()) return this.asInteger().equals(that.asInteger()); if (this.isFloat()) return this.asFloat().equals(that.asFloat()); if (this.isBigDecimal()) return this.asBigDecimal().equals(that.asBigDecimal()); if (this.isBigInteger()) return this.asBigInteger().equals(that.asBigInteger()); } return this.value.equals(that.asObject()); } // Compare the value straight away ... return this.value.equals(obj); }
break; case DECIMAL: generator.writeNumber(value.asBigDecimal()); break; case DOCUMENT:
@SuppressWarnings("unchecked") @Override public int compareTo(Value that) { if (Value.isNull(that)) return 1; if ( this.isBoolean() && that.isBoolean() ) { return this.asBoolean().compareTo(that.asBoolean()); } if (this.isNumber() && that.isNumber()) { if (this.isLong()) return this.asLong().compareTo(that.asLong()); if (this.isDouble()) return this.asDouble().compareTo(that.asDouble()); if (this.isInteger()) return this.asInteger().compareTo(that.asInteger()); if (this.isFloat()) return this.asFloat().compareTo(that.asFloat()); if (this.isBigDecimal()) return this.asBigDecimal().compareTo(that.asBigDecimal()); return this.asBigInteger().compareTo(that.asBigInteger()); } if ( this.isDocument() && that.isDocument()) { return this.asDocument().compareTo(that.asDocument()); } if ( this.isArray() && that.isArray() ) { return this.asArray().compareTo(that.asArray()); } Comparable<Object> thisValue = (Comparable<Object>)this.asObject(); Comparable<Object> thatValue = (Comparable<Object>)((ComparableValue)that.comparable()).asObject(); if ( thisValue.getClass().isAssignableFrom(thatValue.getClass())) { return thisValue.compareTo(thatValue); } else if (thatValue.getClass().isAssignableFrom(thisValue.getClass())) { return thatValue.compareTo(thisValue) * -1; // correct for the reversed comparison } return ((Comparable<Object>) this.value).compareTo(that.asObject()); }