@Nonnull public static BsonObjectId getObjectId(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue<?> object = entry.getValue(); if (!object.isObjectId()) { throw new TypesMismatchException(fieldId, "objectId", object.getType()); } return object.asObjectId(); }
public TypesMismatchException newWithMessage(String customeMessage) { return new TypesMismatchException(getFieldId(), getExpectedType(), getFoundType(), customeMessage); } }
@Nonnull public static BsonArray getArray(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue<?> object = entry.getValue(); if (!object.isArray()) { throw new TypesMismatchException(fieldId, BsonType.ARRAY, object.getType()); } return object.asArray(); }
@Nonnull public static Instant getInstant(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue<?> value = entry.getValue(); if (!value.isDateTime()) { throw new TypesMismatchException( fieldId, "date", value.getType(), "Expected date type for field " + fieldId + ". Found" + value ); } return value.asDateTime().getValue(); }
@Nonnull public static BsonDocument getDocument(Entry<?> entry) throws TypesMismatchException { BsonValue<?> object = entry.getValue(); if (!object.isDocument()) { throw new TypesMismatchException(entry.getKey(), BsonType.DOCUMENT, object.getType()); } return object.asDocument(); }
@Nonnull public static BsonNumber<?> getNumeric(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue<?> object = entry.getValue(); if (!object.isNumber()) { String foundType = object.getType().toString().toLowerCase(Locale.ROOT); throw new TypesMismatchException( fieldId, "numeric", object.getType(), fieldId + " field has non-numeric type " + foundType ); } return object.asNumber(); }
@Nonnull public static String getString(Entry<?> entry) throws TypesMismatchException { BsonValue object = entry.getValue(); if (!object.isString()) { throw new TypesMismatchException(entry.getKey(), "string", object.getType()); } return object.asString().getValue(); }
public static BsonTimestamp getTimestamp(BsonDocument doc, String fieldId) throws NoSuchKeyException, TypesMismatchException { Entry<?> entry = doc.getEntry(fieldId); if (entry == null) { throw new NoSuchKeyException(fieldId); } BsonValue<?> value = entry.getValue(); if (!value.isTimestamp()) { throw new TypesMismatchException(fieldId, "timestamp", value.getType()); } return value.asTimestamp(); }
public static BsonTimestamp getTimestampFromDateTime(Entry<?> entry) throws TypesMismatchException { BsonValue<?> value = entry.getValue(); if (!value.isDateTime()) { throw new TypesMismatchException(entry.getKey(), BsonType.DATETIME, value.getType()); } return TimestampToDateTime.toTimestamp( value.asDateTime(), DefaultBsonValues::newTimestamp); } }
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 double getDouble(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue object = entry.getValue(); if (!object.isDouble()) { String foundType = toStringBsonType(object.getType()); throw new TypesMismatchException( fieldId, "integer", object.getType(), "Expected double type for field " + fieldId + ". Found" + foundType ); } return object.asDouble().doubleValue(); }
public static long getLong(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue object = entry.getValue(); if (!object.isNumber()) { String foundType = toStringBsonType(object.getType()); throw new TypesMismatchException( fieldId, "integer", object.getType(), "Expected long type for field " + fieldId + ". Found" + foundType ); } return object.asNumber().longValue(); }
public static int getInteger(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue<?> object = entry.getValue(); if (!object.isInt32()) { String foundType = toStringBsonType(object.getType()); throw new TypesMismatchException( fieldId, "integer", object.getType(), "Expected integer type for field " + fieldId + ". Found" + foundType ); } return object.asInt32().intValue(); }
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) ); }