Skip to content

Commit

Permalink
Replace Predicate<Long> with LongPredicate in *ChartDataModel
Browse files Browse the repository at this point in the history
Use the slightly more efficient primitive type specialisation for the
date filters in 'ChartDataModel' and its subclasses.
  • Loading branch information
stejbac committed Feb 20, 2024
1 parent d97636e commit e584c77
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.LongPredicate;
import java.util.function.ToLongFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public abstract class ChartDataModel extends ActivatableDataModel {
protected final TemporalAdjusterModel temporalAdjusterModel = new TemporalAdjusterModel();
protected Predicate<Long> dateFilter = e -> true;
protected LongPredicate dateFilter = e -> true;


///////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -76,7 +76,7 @@ public ToLongFunction<Instant> toCachedTimeIntervalFn() {
// Date filter predicate
///////////////////////////////////////////////////////////////////////////////////////////

public Predicate<Long> getDateFilter() {
public LongPredicate getDateFilter() {
return dateFilter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.NavigableMap;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.function.LongPredicate;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -238,7 +239,7 @@ private Map<Long, Double> getPriceByInterval(Predicate<TradeStatistics3> collect
private Map<Long, Double> getPriceByInterval(Collection<TradeStatistics3> collection,
Predicate<TradeStatistics3> collectionFilter,
Function<TradeStatistics3, Long> groupByDateFunction,
Predicate<Long> dateFilter,
LongPredicate dateFilter,
Function<List<TradeStatistics3>, Double> getAveragePriceFunction) {
return collection.stream()
.filter(collectionFilter)
Expand Down Expand Up @@ -274,7 +275,7 @@ private Map<Long, Double> getBsqMarketCapByInterval(Predicate<TradeStatistics3>
private Map<Long, Double> getBsqMarketCapByInterval(Collection<TradeStatistics3> tradeStatistics3s,
Predicate<TradeStatistics3> collectionFilter,
Function<TradeStatistics3, Long> groupByDateFunction,
Predicate<Long> dateFilter,
LongPredicate dateFilter,
Function<List<TradeStatistics3>, Double> getAveragePriceFunction) {
Map<Long, List<TradeStatistics3>> pricesGroupedByDate = tradeStatistics3s.stream()
.filter(collectionFilter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.LongPredicate;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -143,7 +143,7 @@ private Map<Long, Long> getVolumeByInterval(Function<List<TradeStatistics3>, Lon

private Map<Long, Long> getVolumeByInterval(Collection<TradeStatistics3> collection,
Function<TradeStatistics3, Long> groupByDateFunction,
Predicate<Long> dateFilter,
LongPredicate dateFilter,
Function<List<TradeStatistics3>, Long> getVolumeFunction) {
return collection.stream()
.collect(Collectors.groupingBy(groupByDateFunction))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import java.util.TimeZone;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.LongPredicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -322,32 +322,31 @@ Map<Long, Long> getSupplyChangeByInterval() {
// Aggregated collection data by interval
///////////////////////////////////////////////////////////////////////////////////////////

private Map<Long, Long> getIssuedBsqByInterval(Collection<Issuance> issuanceSet, Predicate<Long> dateFilter) {
private Map<Long, Long> getIssuedBsqByInterval(Collection<Issuance> issuanceSet, LongPredicate dateFilter) {
var allIssuedBsq = issuanceSet.stream()
.collect(Collectors.groupingBy(
issuance -> toTimeInterval(Instant.ofEpochMilli(blockTimeOfIssuanceFunction.apply(issuance))),
Collectors.summingLong(Issuance::getAmount)));
return getDateFilteredMap(allIssuedBsq, dateFilter);
}

private Map<Long, Long> getHistoricalIssuedBsqByInterval(Map<Long, Long> historicalData,
Predicate<Long> dateFilter) {
private Map<Long, Long> getHistoricalIssuedBsqByInterval(Map<Long, Long> historicalData, LongPredicate dateFilter) {
return historicalData.entrySet().stream()
.filter(e -> dateFilter.test(e.getKey()))
.collect(Collectors.toMap(e -> toTimeInterval(Instant.ofEpochSecond(e.getKey())),
Map.Entry::getValue,
Long::sum));
}

private Map<Long, Long> getBurntBsqByInterval(Stream<Tx> txStream, Predicate<Long> dateFilter) {
private Map<Long, Long> getBurntBsqByInterval(Stream<Tx> txStream, LongPredicate dateFilter) {
var toTimeIntervalFn = toCachedTimeIntervalFn();
var allBurntBsq = txStream.collect(Collectors.groupingBy(
tx -> toTimeIntervalFn.applyAsLong(Instant.ofEpochMilli(tx.getTime())),
Collectors.summingLong(Tx::getBurntBsq)));
return getDateFilteredMap(allBurntBsq, dateFilter);
}

private Predicate<Long> getPostTagDateFilter() {
private LongPredicate getPostTagDateFilter() {
// We filter out old dates as it only makes sense since Nov 2021
return date -> date >= TAG_DATE.getTimeInMillis() / 1000; // we use seconds
}
Expand All @@ -371,7 +370,7 @@ private Stream<Tx> getTradeFeeTxStream() {
// Utils
///////////////////////////////////////////////////////////////////////////////////////////

private static <V> Map<Long, V> getDateFilteredMap(Map<Long, V> map, Predicate<Long> dateFilter) {
private static <V> Map<Long, V> getDateFilteredMap(Map<Long, V> map, LongPredicate dateFilter) {
return map.entrySet().stream()
.filter(e -> dateFilter.test(e.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (u, v) -> v, HashMap::new));
Expand Down

0 comments on commit e584c77

Please sign in to comment.