|
BasicDBObject already has a fluent method append(String key, Object val) returning itself. It would be very convenient to have more append methods like
- BasicDBObject appendIfNotBlank(String key, String val)
- BasicDBObject appendIfNotEmpty(String key, Collection val)
- BasicDBObject appendIfNotEmpty(String key, Object[] val)
- BasicDBObject appendIfNotEmpty(String key, String val)
- BasicDBObject appendIfNotNull(String key, Object val)
so that you could write code like this:
DBObject dbo = new BasicDBObject()
|
.append("k1", 42)
|
.appendIfNotNull("k2", somePossiblyNullValue)
|
.appendIfNotBlank("k3", somePossiblyBlankString)
|
.appendIfNotEmpty("k4", somePossiblyEmptyString)
|
.appendIfNotEmpty("k5", somePossibleEmptyCollection)
|
.appendIfNotEmpty("k6", somePossibleEmptyArray)
|
instead of
DBObject dbo = new BasicDBObject();
|
if (somePossibleNullValue != null) {
|
dbo.put("k2, somePossibleNullValue);
|
}
|
if (somePossibleEmptyCollection != null && !somePossibleEmptyCollection.isEmpty()) {
|
dbo.put("k5", somePossibleEmptyCollection)
|
}
|
...
|
|