-
Type:
Improvement
-
Resolution: Won't Fix
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: API
-
None
-
None
-
Fully Compatible
-
None
-
None
-
None
-
None
-
None
-
None
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) } ...