Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-674

Add more fluent methods like appendIfNotNull() to BasicDBObject

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Won't Fix
    • Icon: Major - P3 Major - P3
    • None
    • None
    • API
    • None
    • Fully Compatible

    Description

      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)
      }
      ...

      Attachments

        Activity

          People

            Unassigned Unassigned
            christianbrensing Christian Brensing
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: