use of com.mongodb.lang.Nullable in project mongo-java-driver by mongodb.
the class ChangeStreamBatchCursor method convertAndProduceLastId.
/**
* @param lastIdConsumer Is {@linkplain Consumer#accept(Object) called} iff {@code rawDocuments} is successfully converted
* and the returned {@link List} is neither {@code null} nor {@linkplain List#isEmpty() empty}.
*/
@Nullable
static <T> List<T> convertAndProduceLastId(@Nullable final List<RawBsonDocument> rawDocuments, final Decoder<T> decoder, final Consumer<BsonDocument> lastIdConsumer) {
List<T> results = null;
if (rawDocuments != null) {
results = new ArrayList<T>();
for (RawBsonDocument rawDocument : rawDocuments) {
if (!rawDocument.containsKey("_id")) {
throw new MongoChangeStreamException("Cannot provide resume functionality when the resume token is missing.");
}
results.add(rawDocument.decode(decoder));
}
lastIdConsumer.accept(rawDocuments.get(rawDocuments.size() - 1).getDocument("_id"));
}
return results;
}
use of com.mongodb.lang.Nullable in project mongo-java-driver by mongodb.
the class AggregatePublisherImpl method getOutNamespace.
@Nullable
private MongoNamespace getOutNamespace() {
BsonDocument lastPipelineStage = getLastPipelineStage();
if (lastPipelineStage == null) {
return null;
}
String databaseName = getNamespace().getDatabaseName();
if (lastPipelineStage.containsKey("$out")) {
if (lastPipelineStage.get("$out").isString()) {
return new MongoNamespace(databaseName, lastPipelineStage.getString("$out").getValue());
} else if (lastPipelineStage.get("$out").isDocument()) {
BsonDocument outDocument = lastPipelineStage.getDocument("$out");
if (!outDocument.containsKey("db") || !outDocument.containsKey("coll")) {
throw new IllegalStateException("Cannot return a cursor when the value for $out stage is not a namespace document");
}
return new MongoNamespace(outDocument.getString("db").getValue(), outDocument.getString("coll").getValue());
} else {
throw new IllegalStateException("Cannot return a cursor when the value for $out stage " + "is not a string or namespace document");
}
} else if (lastPipelineStage.containsKey("$merge")) {
if (lastPipelineStage.isString("$merge")) {
return new MongoNamespace(databaseName, lastPipelineStage.getString("$merge").getValue());
} else if (lastPipelineStage.isDocument("$merge")) {
BsonDocument mergeDocument = lastPipelineStage.getDocument("$merge");
if (mergeDocument.isDocument("into")) {
BsonDocument intoDocument = mergeDocument.getDocument("into");
return new MongoNamespace(intoDocument.getString("db", new BsonString(databaseName)).getValue(), intoDocument.getString("coll").getValue());
} else if (mergeDocument.isString("into")) {
return new MongoNamespace(databaseName, mergeDocument.getString("into").getValue());
}
} else {
throw new IllegalStateException("Cannot return a cursor when the value for $merge stage is not a string or a document");
}
}
return null;
}
use of com.mongodb.lang.Nullable in project mongo-java-driver by mongodb.
the class DBObjectCodec method readValue.
@Nullable
private Object readValue(final BsonReader reader, final DecoderContext decoderContext, @Nullable final String fieldName, final List<String> path) {
Object initialRetVal;
BsonType bsonType = reader.getCurrentBsonType();
if (bsonType.isContainer() && fieldName != null) {
// if we got into some new context like nested document or array
path.add(fieldName);
}
switch(bsonType) {
case DOCUMENT:
initialRetVal = verifyForDBRef(readDocument(reader, decoderContext, path));
break;
case ARRAY:
initialRetVal = readArray(reader, decoderContext, path);
break;
case // custom for driver-compat types
JAVASCRIPT_WITH_SCOPE:
initialRetVal = readCodeWScope(reader, decoderContext, path);
break;
case // custom for driver-compat types
DB_POINTER:
BsonDbPointer dbPointer = reader.readDBPointer();
initialRetVal = new DBRef(dbPointer.getNamespace(), dbPointer.getId());
break;
case BINARY:
initialRetVal = readBinary(reader, decoderContext);
break;
case NULL:
reader.readNull();
initialRetVal = null;
break;
default:
initialRetVal = bsonTypeCodecMap.get(bsonType).decode(reader, decoderContext);
}
if (bsonType.isContainer() && fieldName != null) {
// step out of current context to a parent
path.remove(fieldName);
}
return initialRetVal;
}
use of com.mongodb.lang.Nullable in project morphia by mongodb.
the class Filter method getValue.
@Nullable
protected Object getValue(Datastore datastore) {
if (!mapped) {
PathTarget target = pathTarget(datastore.getMapper());
OperationTarget operationTarget = new OperationTarget(pathTarget, value);
this.value = operationTarget.getValue();
PropertyModel property = target.getTarget();
if (property != null && property.specializeCodec(datastore) instanceof PropertyHandler) {
this.value = ((Document) operationTarget.encode(datastore)).get(field);
}
mapped = true;
}
return value;
}
use of com.mongodb.lang.Nullable in project morphia by mongodb.
the class PathTarget method resolveProperty.
@Nullable
private PropertyModel resolveProperty(String segment) {
if (context != null) {
PropertyModel model = context.getProperty(segment);
if (model == null) {
Iterator<EntityModel> subTypes = context.getSubtypes().iterator();
while (model == null && subTypes.hasNext()) {
context = subTypes.next();
model = resolveProperty(segment);
}
}
if (model != null) {
try {
context = mapper.getEntityModel(model.getNormalizedType());
} catch (NotMappableException ignored) {
context = null;
}
}
return model;
} else {
return null;
}
}
Aggregations