import java.util.HashMap;
import java.util.Map;
public class TokenMonitor {
private final Map<String, String> storedTokens = new HashMap<>(); // Store old tokens
private String currentToken; // Current token
public synchronized void setToken(String token) {
currentToken = token; // Update current token
storedTokens.put(currentToken, token); // Store the new token
}
public synchronized String getCurrentToken() {
return currentToken; // Return the current token
}
public synchronized boolean isTokenValid(String token) {
return storedTokens.containsKey(token);
}
public synchronized void removeToken(String token) {
storedTokens.remove(token); // Remove token from stored tokens
}
public static void main(String[] args) throws InterruptedException {
TokenMonitor monitor = new TokenMonitor();
// Simulate token changes
monitor.setToken("oldToken1");
System.out.println("Current token: " + monitor.getCurrentToken()); // Output: oldToken1
monitor.setToken("newToken1");
System.out.println("Current token: " + monitor.getCurrentToken()); // Output: newToken1
// Check if an old token is still valid
System.out.println("Is oldToken1 valid? " + monitor.isTokenValid("oldToken1")); // Output: true
//Remove an old token
monitor.removeToken("oldToken1");
System.out.println("Is oldToken1 valid after removal? " + monitor.isTokenValid("oldToken1")); //Output: false
}
}
Add your comment