-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for non-trivial Stream searching
- Loading branch information
1 parent
d6c3f99
commit 9ec20f0
Showing
5 changed files
with
280 additions
and
0 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
61 changes: 61 additions & 0 deletions
61
src/main/java/no/digipost/stream/AtomicReferenceFolder.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 (C) Posten Norge AS | ||
* | ||
* 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 no.digipost.stream; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.BiPredicate; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* A consumer function for "folding" a value with an {@link AtomicReference}. | ||
* | ||
* @param <T> The type of the value this function folds. | ||
*/ | ||
@FunctionalInterface | ||
interface AtomicReferenceFolder<T> extends BiConsumer<AtomicReference<T>, T> { | ||
|
||
static <T> AtomicReferenceFolder<T> clearReference() { | ||
return (ref, value) -> ref.set(null); | ||
} | ||
|
||
static <T> AtomicReferenceFolder<T> keepFirst(Predicate<? super T> predicate) { | ||
return (currentRef, candidateElement) -> | ||
currentRef.accumulateAndGet(candidateElement, (current, candidate) -> current == null && predicate.test(candidate) ? candidate : current); | ||
} | ||
|
||
static <T> AtomicReferenceFolder<T> keepLast(Predicate<? super T> predicate) { | ||
return (currentRef, candidateElement) -> { | ||
if (predicate.test(candidateElement)) { | ||
currentRef.set(candidateElement); | ||
} | ||
}; | ||
} | ||
|
||
default AtomicReferenceFolder<T> doInsteadIf(Predicate<? super T> valuePredicate, AtomicReferenceFolder<T> foldOperation) { | ||
return doInsteadIf((ref, value) -> valuePredicate.test(value), foldOperation); | ||
} | ||
|
||
default AtomicReferenceFolder<T> doInsteadIf(BiPredicate<? super AtomicReference<T>, ? super T> refAndValuePredicate, AtomicReferenceFolder<T> foldOperation) { | ||
return (ref, value) -> { | ||
if (refAndValuePredicate.test(ref, value)) { | ||
foldOperation.accept(ref, value); | ||
} else { | ||
this.accept(ref, value); | ||
} | ||
}; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/main/java/no/digipost/stream/AtomicReferenceFoldingCollector.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,78 @@ | ||
/* | ||
* Copyright (C) Posten Norge AS | ||
* | ||
* 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 no.digipost.stream; | ||
|
||
import java.util.EnumSet; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.function.BinaryOperator; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collector; | ||
|
||
import static java.util.Collections.unmodifiableSet; | ||
import static java.util.stream.Collector.Characteristics.CONCURRENT; | ||
|
||
final class AtomicReferenceFoldingCollector<T> implements Collector<T, AtomicReference<T>, Optional<T>> { | ||
|
||
private final AtomicReferenceFolder<T> accumulator; | ||
private final Set<Characteristics> characteristics; | ||
|
||
AtomicReferenceFoldingCollector(AtomicReferenceFolder<T> accumulator) { | ||
this(accumulator, EnumSet.of(CONCURRENT)); | ||
} | ||
|
||
AtomicReferenceFoldingCollector(AtomicReferenceFolder<T> accumulator, Set<Characteristics> characteristics) { | ||
this.accumulator = accumulator; | ||
this.characteristics = unmodifiableSet(characteristics); | ||
} | ||
|
||
|
||
@Override | ||
public AtomicReferenceFolder<T> accumulator() { | ||
return accumulator; | ||
} | ||
|
||
@Override | ||
public Set<Characteristics> characteristics() { | ||
return characteristics; | ||
} | ||
|
||
/** | ||
* The combiner, while thread-safe, has essentially undefined behavior if combining two | ||
* found elements, because there is no way to tell if one or the other should be prioritized. | ||
* In all cases of combining one found element with a not found, the found element will be | ||
* returned from the function. | ||
*/ | ||
@Override | ||
public BinaryOperator<AtomicReference<T>> combiner() { | ||
return (ref1, ref2) -> { | ||
ref1.compareAndSet(null, ref2.get()); | ||
return ref1; | ||
}; | ||
} | ||
|
||
@Override | ||
public Function<AtomicReference<T>, Optional<T>> finisher() { | ||
return ref -> Optional.ofNullable(ref.get()); | ||
} | ||
|
||
@Override | ||
public Supplier<AtomicReference<T>> supplier() { | ||
return AtomicReference::new; | ||
} | ||
} |
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,60 @@ | ||
/* | ||
* Copyright (C) Posten Norge AS | ||
* | ||
* 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 no.digipost.stream; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collector; | ||
|
||
import static java.util.function.Predicate.isEqual; | ||
import static no.digipost.stream.AtomicReferenceFolder.clearReference; | ||
import static no.digipost.stream.AtomicReferenceFolder.keepFirst; | ||
import static no.digipost.stream.AtomicReferenceFolder.keepLast; | ||
|
||
/** | ||
* The initial subject filter for building a searching {@code Collector}. | ||
* The {@link Collector} is acquired by invoking a method to finalize the | ||
* (compound) condition for accumulating the result across the elements | ||
* the collector will be applied to. | ||
* | ||
* @param <T> the type of elements this subject filter inspects | ||
* | ||
* @see #keepFirstNotFollowedBy(Predicate) | ||
* @see #keepLastNotFollowedBy(Predicate) | ||
*/ | ||
public final class SubjectFilter<T> { | ||
private final Predicate<T> subjectElement; | ||
|
||
public SubjectFilter(Predicate<T> subjectElement) { | ||
this.subjectElement = subjectElement; | ||
} | ||
|
||
public Collector<T, ?, Optional<T>> keepFirstNotFollowedBy(T cancellingElement) { | ||
return keepFirstNotFollowedBy(isEqual(cancellingElement)); | ||
} | ||
|
||
public Collector<T, ?, Optional<T>> keepFirstNotFollowedBy(Predicate<? super T> cancellingElement) { | ||
return new AtomicReferenceFoldingCollector<>(keepFirst(subjectElement).doInsteadIf(cancellingElement, clearReference())); | ||
} | ||
|
||
public Collector<T, ?, Optional<T>> keepLastNotFollowedBy(T cancellingElement) { | ||
return keepLastNotFollowedBy(isEqual(cancellingElement)); | ||
} | ||
|
||
public Collector<T, ?, Optional<T>> keepLastNotFollowedBy(Predicate<? super T> cancellingElement) { | ||
return new AtomicReferenceFoldingCollector<>(keepLast(subjectElement).doInsteadIf(cancellingElement, clearReference())); | ||
} | ||
} |
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