@Override public String apply(String s) { AtomicLong count = Utils.computeIfAbsent(values, s, k -> new AtomicLong(0L)); long num = count.incrementAndGet(); if (num >= cutoff) { updateCutoff(); // cutoff may have been updated, double check it would still make the cut String v = limiter.apply(s); return num >= cutoff ? v : OTHERS; } else { return OTHERS; } }
/** * Allows the clock to be specified for testing. See {@link #mostFrequent(int)} for * details on the usage. */ static Function<String, String> mostFrequent(int n, Clock clock) { return new MostFrequentLimiter(Math.min(n, MAX_LIMIT), clock); }
/** * Allows the clock to be specified for testing. See {@link #mostFrequent(int)} for * details on the usage. */ static Function<String, String> mostFrequent(int n, Clock clock) { return new MostFrequentLimiter(Math.min(n, MAX_LIMIT), clock); }
@Override public String apply(String s) { AtomicLong count = Utils.computeIfAbsent(values, s, k -> new AtomicLong(0L)); long num = count.incrementAndGet(); if (num >= cutoff) { updateCutoff(); // cutoff may have been updated, double check it would still make the cut String v = limiter.apply(s); return num >= cutoff ? v : OTHERS; } else { return OTHERS; } }