Commits

David Carr committed 3d5af49

Add DBO and DBL objects to faciliate easier construction of objects in Java

Comments (0)

Files changed (2)

src/main/java/org/bitbucket/davidm24/mongodb/aggregate/DBL.java

+package org.bitbucket.davidm24.mongodb.aggregate;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * A utility class to allow concisely constructing {@link List}s.  As noted in {@link org.bson.types.BasicBSONList},
+ * MongoDB will create arrays from {@link List}s directly.
+ *
+ * @since 0.1.2
+ */
+public class DBL {
+    private DBL() {
+        // Not intended for construction
+    }
+
+    /**
+     * Returns a {@link List} containing the specified elements.
+     *
+     * @since 0.1.2
+     */
+    public static List<Object> of(Object... elements) {
+        return Arrays.asList(elements);
+    }
+}

src/main/java/org/bitbucket/davidm24/mongodb/aggregate/DBO.java

+package org.bitbucket.davidm24.mongodb.aggregate;
+
+import com.mongodb.BasicDBObject;
+
+/**
+ * A utility class to allow concisely constructing {@link BasicDBObject}s.
+ *
+ * @since 0.1.2
+ */
+public class DBO {
+    private DBO() {
+        // Not intended for construction
+    }
+
+    /**
+     * Returns a {@link BasicDBObject} containing no entries.
+     *
+     * @since 0.1.2
+     */
+    public static BasicDBObject of() {
+        return new BasicDBObject();
+    }
+
+    /**
+     * Returns a {@link BasicDBObject} containing a single entry.
+     *
+     * @since 0.1.2
+     */
+    public static BasicDBObject of(String key, Object value) {
+        return new BasicDBObject(key, value);
+    }
+
+    /**
+     * Returns a {@link BasicDBObject} containing two entries based on the specified key-value pairs.
+     *
+     * @since 0.1.2
+     */
+    public static BasicDBObject of(String key1, Object value1, String key2, Object value2) {
+        return new BasicDBObject().append(key1, value1).append(key2, value2);
+    }
+
+    /**
+     * Returns a {@link BasicDBObject} containing three entries based on the specified key-value pairs.
+     *
+     * @since 0.1.2
+     */
+    public static BasicDBObject of(String key1, Object value1, String key2, Object value2, String key3, Object value3) {
+        return new BasicDBObject().append(key1, value1).append(key2, value2).append(key3, value3);
+    }
+
+    /**
+     * Returns a {@link BasicDBObject} containing four entries based on the specified key-value pairs.
+     *
+     * @since 0.1.2
+     */
+    public static BasicDBObject of(String key1, Object value1, String key2, Object value2, String key3, Object value3,
+                                   String key4, Object value4) {
+        return new BasicDBObject().append(key1, value1).append(key2, value2).append(key3, value3).append(key4, value4);
+    }
+
+    /**
+     * Returns a {@link BasicDBObject} containing five entries based on the specified key-value pairs.
+     *
+     * @since 0.1.2
+     */
+    public static BasicDBObject of(String key1, Object value1, String key2, Object value2, String key3, Object value3,
+                                   String key4, Object value4, String key5, Object value5) {
+        return new BasicDBObject().append(key1, value1).append(key2, value2).append(key3, value3).append(key4, value4)
+                .append(key5, value5);
+    }
+
+    /**
+     * Returns a {@link BasicDBObject} containing entries based on the specified key-value pairs.  Arguments must be
+     * specified in the form of key-value pairs, with the first of the pair being a String key, and the second an Object
+     * value.
+     *
+     * @throws IllegalArgumentException if the arguments are not valid key-value pairs
+     * @since 0.1.2
+     */
+    public static BasicDBObject of(String key1, Object value1, String key2, Object value2, String key3, Object value3,
+                                   String key4, Object value4, String key5, Object value5, Object... otherPairs) {
+        if (otherPairs.length % 2 != 0) {
+            throw new IllegalArgumentException("Arguments must be in pairs");
+        }
+        BasicDBObject dbo = new BasicDBObject();
+        dbo.append(key1, value1).append(key2, value2).append(key3, value3).append(key4, value4).append(key5, value5);
+        for (int i = 0; i < otherPairs.length; i += 2) {
+            String key = (String) otherPairs[i];
+            Object value = otherPairs[i + 1];
+            dbo.append(key, value);
+        }
+        return dbo;
+    }
+}