/** * Update and then reduce all given ActionFrequency instances by the given * amount, capped at a maximum of 0 for the resulting first bucket score. * * @param amount * The amount to subtract. * @param freqs */ public static void reduce(final long time, final float amount, final ActionFrequency... freqs) { for (int i = 0; i < freqs.length; i++) { final ActionFrequency freq = freqs[i]; freq.update(time); freq.setBucket(0, Math.max(0f, freq.bucketScore(0) - amount)); } }
/** * Deserialize from a string. * @param line * @return */ public static ActionFrequency fromLine(final String line) { // TODO: Backwards-compatible lastUpdate ? String[] split = line.split(","); if (split.length < 3) throw new RuntimeException("Bad argument length."); // TODO final int n = Integer.parseInt(split[0]); final long durBucket = Long.parseLong(split[1]); final long time = Long.parseLong(split[2]); final float[] buckets = new float[split.length -3]; if (split.length - 3 != buckets.length) throw new RuntimeException("Bad argument length."); // TODO for (int i = 3; i < split.length; i ++) { buckets[i - 3] = Float.parseFloat(split[i]); } ActionFrequency freq = new ActionFrequency(n, durBucket); freq.setTime(time); for (int i = 0; i < buckets.length; i ++) { freq.setBucket(i, buckets[i]); } return freq; } }
/** * Update and then reduce all given ActionFrequency instances by the given * amount, without capping the result. * * @param amount * The amount to subtract. * @param freqs */ public static void subtract(final long time, final float amount, final ActionFrequency... freqs) { for (int i = 0; i < freqs.length; i++) { final ActionFrequency freq = freqs[i]; freq.update(time); freq.setBucket(0, freq.bucketScore(0) - amount); } }