@Override public int compareTo(BsonValue<?> obj) { if (obj == this) { return 0; } int diff = BsonTypeComparator.INSTANCE.compare(getType(), obj.getType()); if (diff != 0) { return diff; } assert obj instanceof BsonBoolean; BsonBoolean other = obj.asBoolean(); // TODO: Check how MongoDB compares booleans! return this.getValue().compareTo(other.getValue()); }
public static boolean getBooleanOrUndefined(Entry<?> entry, String fieldId, boolean defaultValue) throws TypesMismatchException { BsonValue object = entry.getValue(); if (object.isUndefined()) { return defaultValue; } if (object.isBoolean()) { return object.asBoolean().getPrimitiveValue(); } throw new TypesMismatchException( fieldId, BsonType.BOOLEAN, object.getType(), "Expected boolean or undefined type for field " + fieldId + ". Found " + object.getType().toString().toLowerCase(Locale.ROOT) ); }
public static boolean getBoolean(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue object = entry.getValue(); if (object.isBoolean()) { return object.asBoolean().getValue(); } String foundType = toStringBsonType(object.getType()); throw new TypesMismatchException( fieldId, "boolean", object.getType(), "Expected boolean type for field " + fieldId + ". Found " + foundType ); }
public static boolean getBooleanOrNumeric(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue object = entry.getValue(); if (object.isNumber()) { return object.asNumber().intValue() != 0; } if (object.isBoolean()) { return object.asBoolean().getPrimitiveValue(); } throw new TypesMismatchException( fieldId, BsonType.BOOLEAN, object.getType(), "Expected boolean or numeric type for field " + fieldId + ". Found " + object.getType().toString().toLowerCase(Locale.ROOT) ); }
public static boolean isPseudoTrue(Entry<?> entry) { if (entry == null) { return false; } BsonValue<?> value = entry.getValue(); switch (value.getType()) { case INT64: return !value.equals(DefaultBsonValues.newLong(0)); case DOUBLE: return !value.equals(DefaultBsonValues.newDouble(0)); case INT32: return !value.equals(DefaultBsonValues.newInt(0)); case BOOLEAN: return value.asBoolean().getPrimitiveValue(); case UNDEFINED: return false; default: return true; } }