Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add syntax sugar to Optional matchers with is(Optional) / isNot(Optio…
…nal) methods Helpful for nullable arguments which get converted to isPresent / isAbsent equivalent depending on optional value. value == Optional.empty() changed to isAbsent() value == Optional.of(123) changed to is(123) This allows making calling API like: person.name.is(Optional.ofNullable(nullableName));
- Loading branch information
1 parent
ba79f0e
commit 0c2d47e
Showing
21 changed files
with
228 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
criteria/common/src/org/immutables/criteria/matcher/OptionalValueMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2020 Immutables Authors and Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.immutables.criteria.matcher; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Optional matcher for scalar (string, int, comparable etc.) attributes. | ||
* In addition to standard {@link ObjectMatcher} contract offers similar methods but with | ||
* {@link Optional} arguments. Depending on optional value (empty / present) matcher will | ||
* delegate to {@link #isPresent()}, {@link #isAbsent()} or {@link #is(Object)} | ||
*/ | ||
public interface OptionalValueMatcher<R, V> extends ObjectMatcher<R, V>, PresentAbsentMatcher<R> { | ||
|
||
/** | ||
* Match current attribute given an optional parameter. If optional is | ||
* empty, matching is equivalent to {@link #isAbsent()} otherwise standard | ||
* {@link #is(Object)} matching is used. | ||
* | ||
* @param optional argument to match with | ||
* @throws NullPointerException if argument is null | ||
*/ | ||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") | ||
default R is(Optional<? extends V> optional) { | ||
Objects.requireNonNull(optional, "optional"); | ||
return optional.map(this::is).orElseGet(this::isAbsent); | ||
} | ||
|
||
/** | ||
* Match current attribute given an optional parameter. If optional is | ||
* empty, matching is equivalent to {@link #isPresent()} ()} otherwise standard | ||
* {@link #isNot(Object)} matching is used. | ||
* | ||
* <p><strong>Note</strong> Different backends might treat null (missing/unknown) value differently. Negations | ||
* might or might not return missing value. | ||
* For SQL there is <a href="https://modern-sql.com/concept/three-valued-logic">Three-Valued-Logic</a>. | ||
* while Mongo is two-valued. | ||
* @throws NullPointerException if argument is null | ||
*/ | ||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") | ||
default R isNot(Optional<? extends V> optional) { | ||
Objects.requireNonNull(optional, "optional"); | ||
return optional.map(this::isNot).orElseGet(this::isPresent); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.