@Override public Value clone() { if (isArray()) return new ComparableValue(asArray().clone()); if (isDocument()) return new ComparableValue(asDocument().clone()); // All other values are immutable ... return this; }
@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()); }
@Override public Value clone() { if (isArray()) return new ComparableValue(asArray().clone()); if (isDocument()) return new ComparableValue(asDocument().clone()); // All other values are immutable ... return this; }
@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()); }