@Override public String toString() { StringBuilder sb = new StringBuilder(size() * 20); sb.append('{'); for (Entry<?> entry : this) { sb.append(entry.getKey()).append(": ").append(entry.getValue()); sb.append(", "); } if (!isEmpty()) { sb.delete(sb.length() - 2, sb.length()); } sb.append('}'); return sb.toString(); }
public BsonDocumentBuilder copy(@Nonnull BsonDocument otherDoc) { Preconditions.checkState(!built); for (Entry<?> entrySet : otherDoc) { map.put(entrySet.getKey(), entrySet.getValue()); } return this; }
public BsonDocumentBuilder(BsonDocument doc) { this.map = new LinkedHashMap<>(doc.size()); for (Entry<?> entry : doc) { map.put(entry.getKey(), entry.getValue()); } built = false; }
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) ); }
@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 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 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(); }
@Override public BsonValue<?> get(String key) { Entry<?> entry = getEntry(key); if (entry == null) { return null; } return entry.getValue(); }
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); } }
@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(); }
public static KvDocument toEagerDocument(BsonDocument doc) { LinkedHashMap<String, KvValue<?>> map = new LinkedHashMap<>(doc.size()); for (Entry<?> entry : doc) { map.put(entry.getKey(), MongoWpConverter.translate(entry.getValue())); } return new MapKvDocument(map); }
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 ); }
@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(); }
@SuppressWarnings("unchecked") @Override public KvValue<V> getValue() { return (KvValue<V>) MongoWpConverter.translate(entry.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 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 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(); }
@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 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 final BsonDocument newDocument(@NotMutable List<Entry<?>> list) { switch (list.size()) { case 0: return EMPTY_DOC; case 1: Entry<?> entry = list.get(0); return new SingleEntryBsonDocument(entry.getKey(), entry.getValue()); default: return new ListBasedBsonDocument(list); } }