-
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.
+ Adding basic implementation of a `TriFunction`, which Java does not have
- Loading branch information
Showing
6 changed files
with
229 additions
and
16 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
70 changes: 70 additions & 0 deletions
70
src/main/java/com/ginsberg/gatherers4j/FoldIndexedGatherer.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,70 @@ | ||
/* | ||
* Copyright 2025 Todd Ginsberg | ||
* | ||
* 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 com.ginsberg.gatherers4j; | ||
|
||
import org.jspecify.annotations.Nullable; | ||
|
||
import java.util.function.BiConsumer; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Gatherer; | ||
|
||
import static com.ginsberg.gatherers4j.GathererUtils.mustNotBeNull; | ||
|
||
public class FoldIndexedGatherer<INPUT extends @Nullable Object, OUTPUT extends @Nullable Object> | ||
implements Gatherer<INPUT, FoldIndexedGatherer.State<OUTPUT>, OUTPUT> { | ||
|
||
private final TriFunction<Long, OUTPUT, INPUT, OUTPUT> foldFunction; | ||
private final Supplier<OUTPUT> initialValue; | ||
|
||
FoldIndexedGatherer( | ||
final Supplier<OUTPUT> initialValue, | ||
final TriFunction<Long, OUTPUT, INPUT, OUTPUT> foldFunction | ||
) { | ||
mustNotBeNull(initialValue, "Initial value supplier must not be null"); | ||
mustNotBeNull(foldFunction, "Fold function must not be null"); | ||
this.foldFunction = foldFunction; | ||
this.initialValue = initialValue; | ||
} | ||
|
||
@Override | ||
public Supplier<State<OUTPUT>> initializer() { | ||
return () -> new State<>(initialValue.get()); | ||
} | ||
|
||
@Override | ||
public Integrator<State<OUTPUT>, INPUT, OUTPUT> integrator() { | ||
return Integrator.ofGreedy((state, element, downstream) -> { | ||
state.carriedValue = foldFunction.apply(state.index++, state.carriedValue, element); | ||
return !downstream.isRejecting(); | ||
}); | ||
} | ||
|
||
@Override | ||
public BiConsumer<State<OUTPUT>, Downstream<? super OUTPUT>> finisher() { | ||
return (outputState, downstream) -> downstream.push(outputState.carriedValue); | ||
} | ||
|
||
public static class State<OUTPUT> { | ||
@Nullable | ||
OUTPUT carriedValue; | ||
long index; | ||
|
||
private State(@Nullable final OUTPUT initialValue) { | ||
carriedValue = initialValue; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2025 Todd Ginsberg | ||
* | ||
* 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 com.ginsberg.gatherers4j; | ||
|
||
import org.jspecify.annotations.Nullable; | ||
|
||
@FunctionalInterface | ||
public interface TriFunction< | ||
A extends @Nullable Object, | ||
B extends @Nullable Object, | ||
C extends @Nullable Object, | ||
R extends @Nullable Object> { | ||
|
||
/// Applies this function to the given arguments | ||
/// | ||
/// @param a the first function argument | ||
/// @param b the second function argument | ||
/// @param c the third function argument | ||
/// @return the function result | ||
R apply(A a, B b, C c); | ||
} |
Oops, something went wrong.