Untitled

mail@pastecode.io avatar
unknown
java
a year ago
810 B
4
Indexable
Never
    private static boolean accept(Long timestamp, String token) {
        Deque<Long> queue = map.computeIfAbsent(token, q -> new ArrayDeque<>(n));
        queue.addLast(timestamp); // ???
        while (queue.size() > 0 && timestamp - queue.peekFirst() >= 1000) {
            queue.pollFirst();
        }

        if (queue.size() > n) {
            return false;
        }

//        queue.addLast(timestamp); ???
        return true;
    }
}

Test data:
n = 2
1679982033000 441079aa62dc3cd57df3 
1679982033998 441079aa62dc3cd57df3 
1679982033999 441079aa62dc3cd57df3 
1679982034000 441079aa62dc3cd57df3

Will return:
1679982033000 441079aa62dc3cd57df3
1679982033998 441079aa62dc3cd57df3

Instead of:
1679982033000 441079aa62dc3cd57df3 
1679982033998 441079aa62dc3cd57df3 
1679982034000 441079aa62dc3cd57df3