Skip to content

Commit 9f4083a

Browse files
committed
Ability to clear constraints for a given key
1 parent bc94bcc commit 9f4083a

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Parse/src/main/java/com/parse/ParseQuery.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,12 @@ private Builder<T> whereSatifiesAnyOf(List<QueryConstraints> constraints) {
527527
return this;
528528
}
529529

530+
// Used by clear
531+
/* package */ Builder<T> clear(String key) {
532+
where.remove(key);
533+
return this;
534+
}
535+
530536
//endregion
531537

532538
//region Order
@@ -2087,6 +2093,18 @@ public String getClassName() {
20872093
return builder.getClassName();
20882094
}
20892095

2096+
/**
2097+
* Clears constraints related to the given key, if any was set previously.
2098+
* Order, includes and selected keys are not affected by this operation.
2099+
*
2100+
* @param key key to be cleared from current constraints.
2101+
* @return this, so you can chain this call.
2102+
*/
2103+
public ParseQuery<T> clear(String key) {
2104+
builder.clear(key);
2105+
return this;
2106+
}
2107+
20902108
/**
20912109
* Turn on performance tracing of finds.
20922110
* <p/>

Parse/src/test/java/com/parse/ParseQueryTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,18 @@ public void testWhereWithinKilometers() throws Exception {
563563
verifyCondition(query, "key", "$maxDistance", 100.0 / ParseGeoPoint.EARTH_MEAN_RADIUS_KM);
564564
}
565565

566+
@Test
567+
public void testClear() throws Exception {
568+
ParseQuery<ParseObject> query = new ParseQuery<>("Test");
569+
query.whereEqualTo("key", "value");
570+
query.whereEqualTo("otherKey", "otherValue");
571+
verifyCondition(query, "key", "value");
572+
verifyCondition(query, "otherKey", "otherValue");
573+
query.clear("key");
574+
verifyCondition(query, "key", null);
575+
verifyCondition(query, "otherKey", "otherValue"); // still.
576+
}
577+
566578
@Test
567579
public void testOr() throws Exception {
568580
ParseQuery<ParseObject> query = new ParseQuery<>("Test");
@@ -684,6 +696,13 @@ public void testSkip() throws Exception {
684696
assertEquals(5, query.getSkip());
685697
}
686698

699+
private static void verifyCondition(ParseQuery query, String key, Object value) {
700+
// We generate a state to verify the content of the builder
701+
ParseQuery.State state = query.getBuilder().build();
702+
ParseQuery.QueryConstraints queryConstraints = state.constraints();
703+
assertEquals(value, queryConstraints.get(key));
704+
}
705+
687706
private static void verifyCondition(
688707
ParseQuery query, String key, String conditionKey, Object value) {
689708
// We generate a state to verify the content of the builder

0 commit comments

Comments
 (0)