diff --git a/core/src/main/java/dev/morphia/query/filters/Filters.java b/core/src/main/java/dev/morphia/query/filters/Filters.java index 24445861cb9..5aa421e2420 100644 --- a/core/src/main/java/dev/morphia/query/filters/Filters.java +++ b/core/src/main/java/dev/morphia/query/filters/Filters.java @@ -11,7 +11,6 @@ import dev.morphia.aggregation.expressions.impls.Expression; import dev.morphia.query.Type; -import org.bson.BsonBinary; import org.bson.Document; import static java.util.Arrays.asList; @@ -49,65 +48,30 @@ public static Filter and(Filter... filters) { return new LogicalFilter("$and", filters); } - /** - * Matches numeric or binary values in which a set of bit positions all have a value of 0. - * - * @param field the field to check - * @param positions the value to check - * @return the filter - * @query.filter $bitsAllClear - */ - public static Filter bitsAllClear(String field, int[] positions) { - return new Filter("$bitsAllClear", field, positions); - } - - /** - * Matches numeric or binary values in which a set of bit positions all have a value of 0. - * - * @param field the field to check - * @param bitMask the numeric bitmask to use - * @return the filter - * @query.filter $bitsAllClear - */ - public static Filter bitsAllClear(String field, int bitMask) { - return new Filter("$bitsAllClear", field, bitMask); - } - /** * Matches numeric or binary values in which a set of bit positions all have a value of 0. * * @param field the field to check - * @param data the data to use + * @param val the value to check * @return the filter * @query.filter $bitsAllClear - * @since 3.0 - */ - public static Filter bitsAllClear(String field, byte[] data) { - return new Filter("$bitsAllClear", field, new BsonBinary(data)); - } - - /** - * Matches numeric or binary values in which a set of bit positions all have a value of 1. - * - * @param field the field to check - * @param bitMask the numeric bitmask to use - * @return the filter - * @query.filter $bitsAllSet + * @since 3.0 changed to take a plain Object instead of overloading */ - public static Filter bitsAllSet(String field, int bitMask) { - return new Filter("$bitsAllSet", field, bitMask); + public static Filter bitsAllClear(String field, Object val) { + return new Filter("$bitsAllClear", field, val); } /** * Matches numeric or binary values in which a set of bit positions all have a value of 1. * - * @param field the field to check - * @param positions the value to check + * @param field the field to check + * @param val the value to check * @return the filter * @query.filter $bitsAllSet + * @since 3.0 changed to take a plain Object instead of overloading */ - public static Filter bitsAllSet(String field, int[] positions) { - return new Filter("$bitsAllSet", field, positions); + public static Filter bitsAllSet(String field, Object val) { + return new Filter("$bitsAllSet", field, val); } /** diff --git a/core/src/test/java/dev/morphia/test/query/filters/TestBitsAllSet.java b/core/src/test/java/dev/morphia/test/query/filters/TestBitsAllSet.java new file mode 100644 index 00000000000..de6067e76f6 --- /dev/null +++ b/core/src/test/java/dev/morphia/test/query/filters/TestBitsAllSet.java @@ -0,0 +1,28 @@ +package dev.morphia.test.query.filters; + +import dev.morphia.test.ServerVersion; + +import org.testng.annotations.Test; + +import static dev.morphia.query.filters.Filters.bitsAllSet; + +public class TestBitsAllSet extends FilterTest { + @Test + public void testExample1() { + testQuery(ServerVersion.ANY, false, true, (query) -> query.filter( + bitsAllSet("a", new int[] { 1, 5 }))); + } + + @Test + public void testExample2() { + testQuery(ServerVersion.ANY, false, true, (query) -> query.filter( + bitsAllSet("a", 50))); + } + + @Test + public void testExample3() { + testQuery(ServerVersion.ANY, false, true, (query) -> query.filter( + bitsAllSet("a", new byte[] { 48 }))); + } + +} diff --git a/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example1/action.json b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example1/action.json new file mode 100644 index 00000000000..7bd459dfc1a --- /dev/null +++ b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example1/action.json @@ -0,0 +1 @@ +db.collection.find( { a: { $bitsAllSet: [ 1, 5 ] } } ) \ No newline at end of file diff --git a/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example1/data.json b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example1/data.json new file mode 100644 index 00000000000..ec9a9173fff --- /dev/null +++ b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example1/data.json @@ -0,0 +1,4 @@ +{ _id: 1, a: 54, binaryValueofA: "00110110" }, +{ _id: 2, a: 20, binaryValueofA: "00010100" }, +{ _id: 3, a: 20.0, binaryValueofA: "00010100" }, +{ _id: 4, a: BinData(0, "Zg=="), binaryValueofA: "01100110" } \ No newline at end of file diff --git a/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example1/expected.json b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example1/expected.json new file mode 100644 index 00000000000..83ffee53965 --- /dev/null +++ b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example1/expected.json @@ -0,0 +1,2 @@ +{ "_id" : 1, "a" : 54, "binaryValueofA" : "00110110" } +{ "_id" : 4, "a" : BinData(0,"Zg=="), "binaryValueofA" : "01100110" } \ No newline at end of file diff --git a/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example1/name b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example1/name new file mode 100644 index 00000000000..bbb596dbc4a --- /dev/null +++ b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example1/name @@ -0,0 +1 @@ +Bit Position Array \ No newline at end of file diff --git a/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example2/action.json b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example2/action.json new file mode 100644 index 00000000000..7e1b739e3b6 --- /dev/null +++ b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example2/action.json @@ -0,0 +1 @@ +db.collection.find( { a: { $bitsAllSet: 50 } } ) \ No newline at end of file diff --git a/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example2/data.json b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example2/data.json new file mode 100644 index 00000000000..ec9a9173fff --- /dev/null +++ b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example2/data.json @@ -0,0 +1,4 @@ +{ _id: 1, a: 54, binaryValueofA: "00110110" }, +{ _id: 2, a: 20, binaryValueofA: "00010100" }, +{ _id: 3, a: 20.0, binaryValueofA: "00010100" }, +{ _id: 4, a: BinData(0, "Zg=="), binaryValueofA: "01100110" } \ No newline at end of file diff --git a/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example2/expected.json b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example2/expected.json new file mode 100644 index 00000000000..971efc37fa5 --- /dev/null +++ b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example2/expected.json @@ -0,0 +1 @@ +{ "_id" : 1, "a" : 54, "binaryValueofA" : "00110110" } \ No newline at end of file diff --git a/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example2/name b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example2/name new file mode 100644 index 00000000000..45884820524 --- /dev/null +++ b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example2/name @@ -0,0 +1 @@ +Integer Bitmask \ No newline at end of file diff --git a/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example3/action.json b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example3/action.json new file mode 100644 index 00000000000..8354ae26864 --- /dev/null +++ b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example3/action.json @@ -0,0 +1 @@ +db.collection.find( { a: { $bitsAllSet: BinData(0, "MA==") } } ) \ No newline at end of file diff --git a/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example3/data.json b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example3/data.json new file mode 100644 index 00000000000..ec9a9173fff --- /dev/null +++ b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example3/data.json @@ -0,0 +1,4 @@ +{ _id: 1, a: 54, binaryValueofA: "00110110" }, +{ _id: 2, a: 20, binaryValueofA: "00010100" }, +{ _id: 3, a: 20.0, binaryValueofA: "00010100" }, +{ _id: 4, a: BinData(0, "Zg=="), binaryValueofA: "01100110" } \ No newline at end of file diff --git a/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example3/expected.json b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example3/expected.json new file mode 100644 index 00000000000..eb962595dfd --- /dev/null +++ b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example3/expected.json @@ -0,0 +1 @@ +{ _id: 1, a: 54, binaryValueofA: "00110110" } \ No newline at end of file diff --git a/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example3/name b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example3/name new file mode 100644 index 00000000000..a90b4622323 --- /dev/null +++ b/core/src/test/resources/dev/morphia/test/query/filters/bitsAllSet/example3/name @@ -0,0 +1 @@ +BinData Bitmask \ No newline at end of file