Design Authentication Manager LeetCode solution in Java

1
class AuthenticationManager {
2
private int ttl;
3
private Map<String, Integer> map;
4

5
public AuthenticationManager(int timeToLive) {
6
this.ttl = timeToLive;
7
this.map = new HashMap<>();
8
}
9

10
public void generate(String tokenId, int currentTime) {
11
map.put(tokenId, currentTime + this.ttl);
12
}
13

14
public void renew(String tokenId, int currentTime) {
15
Integer expirationTime = this.map.getOrDefault(tokenId, null);
16
if (expirationTime == null || expirationTime <= currentTime) return;
17

18
generate(tokenId, currentTime);
19
}
20

21
public int countUnexpiredTokens(int currentTime) {
22
int count = 0;
23
for (Map.Entry<String, Integer> entry : this.map.entrySet())
24
if (entry.getValue() > currentTime) count++;
25

26
return count;
27
}
28
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0