Permalink
Browse files

Remove document key checks.

The reason for this is that MongoDB uses keys that starts with '$' or
contains '.' internally  which have two consequences: (a) users aren't *normally*
permitted to create documents using such keys, (b) documents sent by the
database need to handled *differently* upon deserialization as they could contain such
keys quite often. The whole *checking-for-invalid-keys* should be handled at
MongoDB driver level (morelike at database level, but whatever ...).

 - Remove document key checks from DefaultBsonDocument, DefaultBsonDocumentBuilder
   and BsonDocuments.
 - Update BsonDocument, BsonDocument.Builder and BsonDocuments documentation
   accordingly.
 - Delete BsonPreconditions as the class was used only at one place of the code.
  • Loading branch information...
1 parent 8c6a935 commit 2638eb16b60f3515f482f6aa6291537a48346b21 @kohanyirobert kohanyirobert committed Feb 6, 2012
View
22 src/main/java/com/github/kohanyirobert/ebson/BsonDocument.java
@@ -76,9 +76,9 @@
* @param <T> the expected type of the value associated with {@code key}
* @return the value associated with {@code key} in a type-safe manner
* @throws NullPointerException if {@code key} is null
- * @throws IllegalArgumentException if this document does not contain it or it
- * is illegal (starts with {@code '$'} or contains {@code '.'}) or if the
- * value to be returned is not an instance of with {@code type}
+ * @throws IllegalArgumentException if this document does not contain
+ * {@code key} or if the value associated with it is not assignment-compatible
+ * with {@code type}
* @throws ClassCastException if {@code key} is not a string
*/
@Nullable
@@ -90,8 +90,8 @@
* @param key the key whose associated value is to be returned
* @return the value associated with {@code key}
* @throws NullPointerException if {@code key} is null
- * @throws IllegalArgumentException if this document does not contain it or it
- * is illegal (starts with {@code '$'} or contains {@code '.'})
+ * @throws IllegalArgumentException if this document does not contain
+ * {@code key}
* @throws ClassCastException if {@code key} is not a string
*/
@Override
@@ -102,12 +102,10 @@
* Returns <em>true</em> if {@code key} is contained by this document;
* <em>false</em> otherwise.
*
- * @param key the key to be tested if it's contained by this document
+ * @param key the key to be tested if it is contained by this document
* @return <em>true</em> if {@code key} is contained by this document;
* <em>false</em> otherwise
* @throws NullPointerException if {@code key} is null
- * @throws IllegalArgumentException if {@code key} is illegal (starts with
- * {@code '$'} or contains {@code '.'})
* @throws ClassCastException if {@code key} is not a string
*/
@Override
@@ -117,7 +115,7 @@
* Returns <em>true</em> if {@code value} is contained by this document;
* <em>false</em> otherwise.
*
- * @param value the value to be tested if it's contained by this document
+ * @param value the value to be tested if it is contained by this document
* @return <em>true</em> if {@code value} is contained by this document;
* <em>false</em> otherwise
*/
@@ -222,8 +220,7 @@
* @param value the value associated with {@code key}
* @return this builder
* @throws NullPointerException if {@code key} is null
- * @throws IllegalArgumentException if {@code key} is illegal (starts with
- * {@code '$'} or contains {@code '.'}) or if it is already present
+ * @throws IllegalArgumentException if {@code key} is already present
*/
Builder put(String key, @Nullable Object value);
@@ -235,8 +232,7 @@
* @return this builder
* @throws NullPointerException if {@code map} or any of its keys are null
* @throws IllegalArgumentException if {@code map} contains keys that are
- * illegal (starts with {@code '$'} or contains {@code '.'}) or already
- * present
+ * already present
*/
Builder putAll(Map<String, Object> map);
View
40 src/main/java/com/github/kohanyirobert/ebson/BsonDocuments.java
@@ -46,8 +46,6 @@ public static void writeTo(ByteBuffer buffer, BsonDocument document) {
* @return a new document containing {@code map}'s key-value pairs
* @throws NullPointerException if {@code map} or any of its keys are
* null
- * @throws IllegalArgumentException if {@code map} contains keys that are
- * illegal (starts with {@code '$'} or contains {@code '.'})
*/
public static BsonDocument copyOf(Map<String, Object> map) {
return builder().putAll(map).build();
@@ -62,9 +60,7 @@ public static BsonDocument copyOf(Map<String, Object> map) {
* @return a new document containing the key-value pairs:
* <em>k1: v1, k2: v2 etc.</em>
* @throws NullPointerException if any key-value pair's key is null
- * @throws IllegalArgumentException if any key-value pair's key is illegal
- * (starts with {@code '$'} or contains {@code '.'}) or if there are duplicate
- * keys
+ * @throws IllegalArgumentException if there are duplicate keys
*/
public static BsonDocument of(String k1, @Nullable Object v1,
String k2, @Nullable Object v2, String k3, @Nullable Object v3,
@@ -83,9 +79,7 @@ public static BsonDocument of(String k1, @Nullable Object v1,
* @return a new document containing the key-value pairs:
* <em>k1: v1, k2: v2 etc.</em>
* @throws NullPointerException if any key-value pair's key is null
- * @throws IllegalArgumentException if any key-value pair's key is illegal
- * (starts with {@code '$'} or contains {@code '.'}) or if there are duplicate
- * keys
+ * @throws IllegalArgumentException if there are duplicate keys
*/
public static BsonDocument of(String k1, @Nullable Object v1,
String k2, @Nullable Object v2, String k3, @Nullable Object v3,
@@ -103,9 +97,7 @@ public static BsonDocument of(String k1, @Nullable Object v1,
* @return a new document containing the key-value pairs:
* <em>k1: v1, k2: v2 etc.</em>
* @throws NullPointerException if any key-value pair's key is null
- * @throws IllegalArgumentException if any key-value pair's key is illegal
- * (starts with {@code '$'} or contains {@code '.'}) or if there are duplicate
- * keys
+ * @throws IllegalArgumentException if there are duplicate keys
*/
public static BsonDocument of(String k1, @Nullable Object v1,
String k2, @Nullable Object v2, String k3, @Nullable Object v3,
@@ -123,9 +115,7 @@ public static BsonDocument of(String k1, @Nullable Object v1,
* @return a new document containing the key-value pairs:
* <em>k1: v1, k2: v2 etc.</em>
* @throws NullPointerException if any key-value pair's key is null
- * @throws IllegalArgumentException if any key-value pair's key is illegal
- * (starts with {@code '$'} or contains {@code '.'}) or if there are duplicate
- * keys
+ * @throws IllegalArgumentException if there are duplicate keys
*/
public static BsonDocument of(String k1, @Nullable Object v1,
String k2, @Nullable Object v2, String k3, @Nullable Object v3,
@@ -142,9 +132,7 @@ public static BsonDocument of(String k1, @Nullable Object v1,
* @return a new document containing the key-value pairs:
* <em>k1: v1, k2: v2 etc.</em>
* @throws NullPointerException if any key-value pair's key is null
- * @throws IllegalArgumentException if any key-value pair's key is illegal
- * (starts with {@code '$'} or contains {@code '.'}) or if there are duplicate
- * keys
+ * @throws IllegalArgumentException if there are duplicate keys
*/
public static BsonDocument of(String k1, @Nullable Object v1,
String k2, @Nullable Object v2, String k3, @Nullable Object v3,
@@ -161,9 +149,7 @@ public static BsonDocument of(String k1, @Nullable Object v1,
* @return a new document containing the key-value pairs:
* <em>k1: v1, k2: v2 etc.</em>
* @throws NullPointerException if any key-value pair's key is null
- * @throws IllegalArgumentException if any key-value pair's key is illegal
- * (starts with {@code '$'} or contains {@code '.'}) or if there are duplicate
- * keys
+ * @throws IllegalArgumentException if there are duplicate keys
*/
public static BsonDocument of(String k1, @Nullable Object v1,
String k2, @Nullable Object v2, String k3, @Nullable Object v3,
@@ -178,9 +164,7 @@ public static BsonDocument of(String k1, @Nullable Object v1,
* @return a new document containing the key-value pairs:
* <em>k1: v1, k2: v2 etc.</em>
* @throws NullPointerException if any key-value pair's key is null
- * @throws IllegalArgumentException if any key-value pair's key is illegal
- * (starts with {@code '$'} or contains {@code '.'}) or if there are duplicate
- * keys
+ * @throws IllegalArgumentException if there are duplicate keys
*/
public static BsonDocument of(String k1, @Nullable Object v1,
String k2, @Nullable Object v2, String k3, @Nullable Object v3,
@@ -195,9 +179,7 @@ public static BsonDocument of(String k1, @Nullable Object v1,
* @return a new document containing the key-value pairs:
* <em>k1: v1, k2: v2 etc.</em>
* @throws NullPointerException if any key-value pair's key is null
- * @throws IllegalArgumentException if any key-value pair's key is illegal
- * (starts with {@code '$'} or contains {@code '.'}) or if there are duplicate
- * keys
+ * @throws IllegalArgumentException if there are duplicate keys
*/
public static BsonDocument of(String k1, @Nullable Object v1,
String k2, @Nullable Object v2, String k3, @Nullable Object v3) {
@@ -211,9 +193,7 @@ public static BsonDocument of(String k1, @Nullable Object v1,
* @return a new document containing the key-value pairs:
* <em>k1: v1, k2: v2</em>
* @throws NullPointerException if any key-value pair's key is null
- * @throws IllegalArgumentException if any key-value pair's key is illegal
- * (starts with {@code '$'} or contains {@code '.'}) or if there are duplicate
- * keys
+ * @throws IllegalArgumentException if there are duplicate keys
*/
public static BsonDocument of(String k1, @Nullable Object v1,
String k2, @Nullable Object v2) {
@@ -231,8 +211,6 @@ public static BsonDocument of(String k1, @Nullable Object v1,
* @return a new document containing {@code key} and its associated
* {@code value}
* @throws NullPointerException if {@code key} is null
- * @throws IllegalArgumentException if {@code key} is illegal (starts with
- * {@code '$'} or contains {@code '.'})
*/
public static BsonDocument of(String key, @Nullable Object value) {
return builder().put(key, value).build();
View
28 src/main/java/com/github/kohanyirobert/ebson/BsonPreconditions.java
@@ -1,28 +0,0 @@
-package com.github.kohanyirobert.ebson;
-
-import javax.annotation.Nullable;
-
-final class BsonPreconditions {
-
- private BsonPreconditions() {}
-
- static <T> T checkIsInstance(Object reference, Class<T> clazz) {
- if (!clazz.isInstance(reference))
- throw new ClassCastException();
- return clazz.cast(reference);
- }
-
- static <T> T checkIsInstance(Object reference, Class<T> clazz,
- @Nullable Object errorMessage) {
- if (!clazz.isInstance(reference))
- throw new ClassCastException(String.valueOf(errorMessage));
- return clazz.cast(reference);
- }
-
- static <T> T checkIsInstance(Object reference, Class<T> clazz,
- @Nullable String errorMessageTemplate, @Nullable Object... errorMessageArgs) {
- if (!clazz.isInstance(reference))
- throw new ClassCastException(String.format(errorMessageTemplate, errorMessageArgs));
- return clazz.cast(reference);
- }
-}
View
5 src/main/java/com/github/kohanyirobert/ebson/DefaultDocument.java
@@ -39,9 +39,8 @@ public Object get(Object key) {
@Override
public boolean containsKey(Object key) {
Preconditions.checkNotNull(key, "null key");
- BsonPreconditions.checkIsInstance(key, String.class, "key: '%s' is not a string", key);
- Preconditions.checkArgument(!((String) key).startsWith("$"), "key: '%s' starts with '$'", key);
- Preconditions.checkArgument(!((String) key).contains("."), "key: '%s' contains '.'", key);
+ if (!String.class.isInstance(key))
+ throw new ClassCastException(String.format("key: '%s' is not a string", key));
return super.containsKey(key);
}
View
2 src/main/java/com/github/kohanyirobert/ebson/DefaultDocumentBuilder.java
@@ -28,8 +28,6 @@
@Override
public BsonDocument.Builder put(String key, @Nullable Object value) {
Preconditions.checkNotNull(key, "null key");
- Preconditions.checkArgument(!key.startsWith("$"), "key: '%s' starts with '$'", key);
- Preconditions.checkArgument(!key.contains("."), "key: '%s' contains '.'", key);
Preconditions.checkArgument(!builder.containsKey(key), "key: '%s' is already present", key);
builder.put(key, value);
return this;
View
41 src/test/java/com/github/kohanyirobert/ebson/BsonDocumentsTest.java
@@ -18,9 +18,6 @@
private static final String KEY2 = "key2";
private static final String KEY3 = "key3";
- private static final String DOLLAR_KEY = "$key";
- private static final String DOT_KEY = "k.ey";
-
public BsonDocumentsTest() {}
@Test
@@ -68,24 +65,6 @@ public void copyOf_map_withNullKey() {
BsonDocuments.copyOf(map);
}
- @Test(expected = IllegalArgumentException.class)
- public void copyOf_map_withDollarKey() {
- Map<String, Object> map = new HashMap<String, Object>();
- map.put(KEY1, null);
- map.put(DOLLAR_KEY, null);
- map.put(KEY3, null);
- BsonDocuments.copyOf(map);
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void copyOf_map_withDotKey() {
- Map<String, Object> map = new HashMap<String, Object>();
- map.put(DOT_KEY, null);
- map.put(KEY2, null);
- map.put(KEY3, null);
- BsonDocuments.copyOf(map);
- }
-
@Test
public void of_singleKeyValuePair_withLegalKey() {
BsonDocument document = BsonDocuments.of(KEY1, null);
@@ -97,16 +76,6 @@ public void of_singleKeyValuePair_withNullKey() {
BsonDocuments.of(null, null);
}
- @Test(expected = IllegalArgumentException.class)
- public void of_singleKeyValuePair_withDollarKey() {
- BsonDocuments.of(DOLLAR_KEY, null);
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void of_singleKeyValuePair_withDotKey() {
- BsonDocuments.of(DOT_KEY, null);
- }
-
@Test
public void of_multipleKeyValuePairs_withLegalKeys() {
BsonDocument document = BsonDocuments.of(KEY1, null, KEY2, null, KEY3, null);
@@ -122,14 +91,4 @@ public void of_multipleKeyValuePairs_withNullKey() {
public void of_multipleKeyValuePairs_withDuplicateKeys() {
BsonDocuments.of(KEY1, null, KEY1, null, KEY3, null);
}
-
- @Test(expected = IllegalArgumentException.class)
- public void of_multipleKeyValuePairs_withDollarKey() {
- BsonDocuments.of(KEY1, null, KEY2, null, DOLLAR_KEY, null);
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void of_multipleKeyValuePairs_withDotKey() {
- BsonDocuments.of(DOT_KEY, null, KEY2, null, KEY3, null);
- }
}

0 comments on commit 2638eb1

Please sign in to comment.