Skip to content

Add NullableCondition extension for Condition#817

Open
nickolasdeluca wants to merge 1 commit into
objectbox:mainfrom
nickolasdeluca:feat/nullable-condition-helper
Open

Add NullableCondition extension for Condition#817
nickolasdeluca wants to merge 1 commit into
objectbox:mainfrom
nickolasdeluca:feat/nullable-condition-helper

Conversation

@nickolasdeluca

Copy link
Copy Markdown

Introduce a NullableCondition<T> extension on Condition<T>? providing and(Condition<T>) and or(Condition<T>) helpers.

These methods return the provided condition when the receiver is null, or delegate to the existing and/or logic otherwise, simplifying conditional accumulation of query conditions and removing boilerplate null checks. Doc examples included to demonstrate usage when building queries.

Usefull when building queries with multiple condition options that can all be supplied as null.

Example:

Condition<City>? condition;

if (id != null ) condition = condition.and(City_.id.equals(id));
if (name != null && name.isNotEmpty) condition = condition.and(City_.name.contains(name));

query = box.query(condition).build();

I always write this extension on all my projects because it makes it much easier to work with lots of conditions. Since Condition itself is not nullable, I'd always have to kick start it with something like City_.id.greaterThan(0) and then add my conditional conditions (haha).

Providing this extension natively should help everyone, I hope.

Introduce a NullableCondition<T> extension on Condition<T>? providing and(Condition<T>) and or(Condition<T>) helpers. These methods return the provided condition when the receiver is null, or delegate to the existing and/or logic otherwise, simplifying conditional accumulation of query conditions and removing boilerplate null checks. Doc examples included to demonstrate usage when building queries.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant