100 lines
3.4 KiB
Java
100 lines
3.4 KiB
Java
package com.discord.utilities.frecency;
|
|
|
|
import com.discord.utilities.time.ClockFactory;
|
|
import d0.t.n;
|
|
import d0.t.u;
|
|
import d0.z.d.m;
|
|
import java.util.Collection;
|
|
import java.util.HashMap;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
/* compiled from: FrecencyTracker.kt */
|
|
public abstract class FrecencyTracker<T> {
|
|
private transient boolean dirty = true;
|
|
private final HashMap<T, List<Long>> history = new HashMap<>();
|
|
private final int maxSamples;
|
|
private final int minScoreThreshold;
|
|
private transient List<? extends T> sortedKeys = n.emptyList();
|
|
|
|
public FrecencyTracker(int i, int i2) {
|
|
this.minScoreThreshold = i;
|
|
this.maxSamples = i2;
|
|
}
|
|
|
|
public static final /* synthetic */ HashMap access$getHistory$p(FrecencyTracker frecencyTracker) {
|
|
return frecencyTracker.history;
|
|
}
|
|
|
|
private final void computeScores(long j) {
|
|
HashMap hashMap = new HashMap(this.history.size());
|
|
Iterator<Map.Entry<T, List<Long>>> it = this.history.entrySet().iterator();
|
|
while (it.hasNext()) {
|
|
Map.Entry<T, List<Long>> next = it.next();
|
|
T key = next.getKey();
|
|
int computeScore = computeScore(next.getValue(), j);
|
|
if (computeScore > this.minScoreThreshold) {
|
|
hashMap.put(key, Integer.valueOf(computeScore));
|
|
} else {
|
|
it.remove();
|
|
}
|
|
}
|
|
Set keySet = hashMap.keySet();
|
|
m.checkNotNullExpressionValue(keySet, "scores.keys");
|
|
this.sortedKeys = u.sortedWith(keySet, new FrecencyTracker$computeScores$1(this, hashMap));
|
|
this.dirty = false;
|
|
}
|
|
|
|
public static /* synthetic */ Collection getSortedKeys$default(FrecencyTracker frecencyTracker, long j, int i, Object obj) {
|
|
if (obj == null) {
|
|
if ((i & 1) != 0) {
|
|
j = ClockFactory.get().currentTimeMillis();
|
|
}
|
|
return frecencyTracker.getSortedKeys(j);
|
|
}
|
|
throw new UnsupportedOperationException("Super calls with default arguments not supported in this target, function: getSortedKeys");
|
|
}
|
|
|
|
public static /* synthetic */ void track$default(FrecencyTracker frecencyTracker, Object obj, long j, int i, Object obj2) {
|
|
if (obj2 == null) {
|
|
if ((i & 2) != 0) {
|
|
j = ClockFactory.get().currentTimeMillis();
|
|
}
|
|
frecencyTracker.track(obj, j);
|
|
return;
|
|
}
|
|
throw new UnsupportedOperationException("Super calls with default arguments not supported in this target, function: track");
|
|
}
|
|
|
|
public abstract int computeScore(List<Long> list, long j);
|
|
|
|
public final int getMaxSamples() {
|
|
return this.maxSamples;
|
|
}
|
|
|
|
public final int getMinScoreThreshold() {
|
|
return this.minScoreThreshold;
|
|
}
|
|
|
|
public final synchronized Collection<T> getSortedKeys(long j) {
|
|
if (this.dirty) {
|
|
computeScores(j);
|
|
}
|
|
return this.sortedKeys;
|
|
}
|
|
|
|
public final synchronized void removeEntry(T t) {
|
|
this.history.remove(t);
|
|
this.dirty = true;
|
|
}
|
|
|
|
public final synchronized void track(T t, long j) {
|
|
List<Long> list = this.history.get(t);
|
|
if (list == null) {
|
|
list = n.emptyList();
|
|
}
|
|
this.history.put(t, u.takeLast(u.plus((Collection<? extends Long>) list, Long.valueOf(j)), this.maxSamples));
|
|
this.dirty = true;
|
|
}
|
|
}
|