cn.dreampie.security.Session Maven / Gradle / Ivy
package cn.dreampie.security;
import cn.dreampie.common.util.Maper;
import java.io.Serializable;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
/**
* Created by wangrenhui on 14/12/23.
*/
public class Session implements Serializable {
public static final String SESSION_DEF_KEY = "_session";
private final Map values;
private final String sessionKey;
private final String username;
private final long expires;
public Session(String sessionKey, String username) {
this(sessionKey, username, Maper.of());
}
public Session(String sessionKey, String username, Map values) {
this(sessionKey, username, values, -1);
}
public Session(String sessionKey, String username, Map values, long expires) {
this.sessionKey = sessionKey;
this.username = username;
this.values = values;
this.expires = expires;
}
String getSessionKey() {
return sessionKey;
}
long getExpires() {
return expires;
}
String getUsername() {
return username;
}
Map getValues() {
return values;
}
String get(String key) {
return values.get(key);
}
void set(Map values) {
Set> entrySet = values.entrySet();
for (Map.Entry entry : entrySet) {
set(entry.getKey(), entry.getValue());
}
}
void set(String key, String value) {
if (value == null) {
values.remove(key);
} else {
values.put(key, value);
}
}
String remove(String key) {
return values.remove(key);
}
}