-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
354 additions
and
61 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
86 changes: 86 additions & 0 deletions
86
src/main/java/com/ginsberg/gatherers4j/FrequencyGatherer.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,86 @@ | ||
/* | ||
* Copyright 2024 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.Comparator; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.BinaryOperator; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Gatherer; | ||
|
||
import static com.ginsberg.gatherers4j.GathererUtils.mustNotBeNull; | ||
|
||
public class FrequencyGatherer<INPUT extends @Nullable Object> | ||
implements Gatherer<INPUT, FrequencyGatherer.State<INPUT>, WithCount<INPUT>> { | ||
|
||
public enum Order { | ||
Ascending, | ||
Descending | ||
} | ||
private final Order order; | ||
|
||
FrequencyGatherer(final Order order) { | ||
mustNotBeNull(order, "Order must be specified"); | ||
this.order = order; | ||
} | ||
|
||
@Override | ||
public Supplier<State<INPUT>> initializer() { | ||
return State::new; | ||
} | ||
|
||
@Override | ||
public Integrator<State<INPUT>, INPUT, WithCount<INPUT>> integrator() { | ||
return Integrator.ofGreedy((state, element, downstream) -> { | ||
state.counts.compute(element, (_, count) -> count == null ? 1 : count + 1); | ||
return !downstream.isRejecting(); | ||
}); | ||
} | ||
|
||
@Override | ||
public BinaryOperator<State<INPUT>> combiner() { | ||
return (state1, state2) -> { | ||
state2.counts.forEach((key, value) -> state1.counts.merge(key, value, Long::sum)); | ||
return state1; | ||
}; | ||
} | ||
|
||
@Override | ||
public BiConsumer<State<INPUT>, Downstream<? super WithCount<INPUT>>> finisher() { | ||
return (inputState, downstream) -> inputState.counts | ||
.entrySet() | ||
.stream().map(it -> new WithCount<>(it.getKey(), it.getValue())) | ||
.sorted(comparator()) | ||
.forEach(downstream::push); | ||
} | ||
|
||
private Comparator<WithCount<INPUT>> comparator() { | ||
if(order == Order.Descending) { | ||
return (o1, o2) -> (int)(o2.count() - o1.count()); | ||
} else { | ||
return (o1, o2) -> (int)(o1.count() - o2.count()); | ||
} | ||
} | ||
|
||
public static class State<INPUT> { | ||
final Map<INPUT, Long> counts = new HashMap<>(); | ||
} | ||
} |
Oops, something went wrong.