-
Notifications
You must be signed in to change notification settings - Fork 1
/
DesignAuthenticationManager.java
42 lines (34 loc) · 1.12 KB
/
DesignAuthenticationManager.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
package com.smlnskgmail.jaman.leetcodejava.medium;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
// https://leetcode.com/problems/design-authentication-manager/
public class DesignAuthenticationManager {
private final int timeToLive;
private final Map<String, Integer> auth = new HashMap<>();
private final Set<String> tokens = new HashSet<>();
public DesignAuthenticationManager(int timeToLive) {
this.timeToLive = timeToLive;
}
public void generate(String tokenId, int currentTime) {
auth.put(tokenId, currentTime + timeToLive);
tokens.add(tokenId);
}
public void renew(String tokenId, int currentTime) {
int time = auth.getOrDefault(tokenId, 0);
if (time > currentTime) {
generate(tokenId, currentTime);
tokens.add(tokenId);
}
}
public int countUnexpiredTokens(int currentTime) {
int count = 0;
for (String token : tokens) {
if (auth.get(token) > currentTime) {
count++;
}
}
return count;
}
}