@@ -0,0 +1,1313 @@
+/**%0A * %0A */%0Apackage com.trendrr.oss;%0A%0Aimport java.util.Date;%0Aimport java.util.concurrent.atomic.AtomicLong;%0A%0Aimport org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A%0Aimport com.trendrr.oss.concurrent.Sleep;%0A%0A%0A/**%0A * %0A * Threadsafe counter that keeps counts of the past and current minute, second, ect.%0A * %0A * %0A * @author Dustin Norlander%0A * @created Mar 22, 2012%0A * %0A */%0Apublic class TimeframeCounter %7B%0A%0A%09protected static Log log = LogFactory.getLog(TimeframeCounter.class);%0A%09%0A%09AtomicLong current = new AtomicLong(0);%0A%09AtomicLong previous = new AtomicLong(0l);%0A%09Timeframe timeframe = Timeframe.MINUTES;%0A%09AtomicLong epoch = new AtomicLong(0);%0A%09%0A%09public TimeframeCounter(Timeframe frame) %7B%0A%09%09this.timeframe = frame;%0A%09%7D%0A%0A%09public long inc() %7B%0A%09%09return this.inc(1);%0A%09%7D%0A%09public long inc(long val) %7B%0A%09%09long curepoch = timeframe.toTrendrrEpoch(new Date()).longValue();%0A%09%09long oldepoch = this.epoch.getAndSet(curepoch);%0A%09%09if (oldepoch != curepoch) %7B%0A%09%09%09previous.set(current.getAndSet(0));%0A%09%09%7D%0A%09%09return current.addAndGet(val);%0A%09%7D%0A%09%0A%09/**%0A%09 * Gets the current value for this timeframe%0A%09 * @return%0A%09 */%0A%09public long getCurrent() %7B%0A%09%09return this.current.get();%0A%09%7D%0A%09%0A%09/**%0A%09 * gets the most recently completed value.%0A%09 * @return%0A%09 */%0A%09public long getPrevious() %7B%0A%09%09return this.previous.get();%0A%09%7D%0A%09%0A%7D%0A

add timeframe counter