For simpler technique, but nice reading from Artur !
You could simply use a List to store every time needed. Then filter those when you get the list.
private static List<Long> counter = new LinkedList<Long>(); //Thanks to Artur to point that out. (Faster but a Queue takes more memory)
public static final long FILTER_TIME = 1000*60*60;
public static void add(){
add(System.currentTimeMillis());
}
public static List<Long> get(){
filter();
return counter;
}
private static void filter(){
int length = counter.size();
long lastHour = System.currentTimeMillis() - 1000*60*60;
//trim from left until value is correct or list is empty
while(length > 0 && counter.get(0) < lastHour){
counter.remove(0);
length--;
}
}
The result will be a list (sorted since the add method only add currentTime) without older value. This is basic implementation, better technics could be used but for a quick solution. This could do it.
The filter is done on the getter. This means that the list could explode in length if this reading is done rarely. So this could be done in the add method also.