-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthenticationManager.java
More file actions
40 lines (34 loc) · 1.14 KB
/
AuthenticationManager.java
File metadata and controls
40 lines (34 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.leetcode.impl;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
final class AuthenticationManager {
private final Map<String, Integer> storage;
private int timeToLive;
AuthenticationManager(int timeToLive) {
this.timeToLive = timeToLive;
this.storage = new HashMap<>();
}
public void generate(String tokenId, int currentTime) {
this.storage.putIfAbsent(tokenId, currentTime);
}
public void renew(String tokenId, int currentTime) {
Integer tokenStartTime = storage.get(tokenId);
if (tokenStartTime != null && tokenStartTime + timeToLive < currentTime) {
storage.put(tokenId, currentTime);
}
}
public int countUnexpiredTokens(int currentTime) {
int count = 0;
Iterator<Map.Entry<String, Integer>> it = storage.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Integer> kv = it.next();
if (kv.getValue() + timeToLive <= currentTime) {
it.remove();
} else {
count++;
}
}
return count;
}
}