forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1797.java
45 lines (38 loc) · 1.4 KB
/
_1797.java
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
41
42
43
44
45
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.Map;
public class _1797 {
public static class Solution1 {
public static class AuthenticationManager {
int timeToLive;
int currentTime;
Map<String, Integer> map;//tokenId -> expireTime
public AuthenticationManager(int timeToLive) {
this.timeToLive = timeToLive;
this.currentTime = 0;
this.map = new HashMap<>();
}
public void generate(String tokenId, int currentTime) {
map.put(tokenId, currentTime + timeToLive);
}
public void renew(String tokenId, int currentTime) {
Integer expireTime = map.getOrDefault(tokenId, -1);
if (expireTime == -1 || expireTime <= currentTime) {
return;
}
map.put(tokenId, currentTime + timeToLive);
}
public int countUnexpiredTokens(int currentTime) {
Map<String, Integer> tmp = new HashMap<>();
for (String token : map.keySet()) {
if (map.get(token) > currentTime) {
tmp.put(token, map.get(token));
}
}
map.clear();
map.putAll(tmp);
return map.size();
}
}
}
}