More improvements to flag methods.

This commit is contained in:
Steveice10 2020-05-30 15:33:56 -07:00
parent 12d26014fa
commit be70b05e3c
4 changed files with 54 additions and 10 deletions

View file

@ -11,6 +11,7 @@ import com.github.steveice10.packetlib.packet.PacketProtocol;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -117,7 +118,7 @@ public class Server {
* @return This server's flags. * @return This server's flags.
*/ */
public Map<String, Object> getGlobalFlags() { public Map<String, Object> getGlobalFlags() {
return new HashMap<String, Object>(this.flags); return Collections.unmodifiableMap(this.flags);
} }
/** /**
@ -131,20 +132,32 @@ public class Server {
} }
/** /**
* Gets the value of the given flag as an instance of the given type. If this * Gets the value of the given flag as an instance of the given type.
* session belongs to a server, the server's flags will be checked for the flag
* as well.
* *
* @param <T> Type of the flag. * @param <T> Type of the flag.
* @param key Key of the flag. * @param key Key of the flag.
* @return Value of the flag. * @return Value of the flag.
* @throws IllegalStateException If the flag's value isn't of the required type. * @throws IllegalStateException If the flag's value isn't of the required type.
*/ */
@SuppressWarnings("unchecked")
public <T> T getGlobalFlag(String key) { public <T> T getGlobalFlag(String key) {
return this.getGlobalFlag(key, null);
}
/**
* Gets the value of the given flag as an instance of the given type.
* If the flag is not set, the specified default value will be returned.
*
* @param <T> Type of the flag.
* @param key Key of the flag.
* @param def Default value of the flag.
* @return Value of the flag.
* @throws IllegalStateException If the flag's value isn't of the required type.
*/
@SuppressWarnings("unchecked")
public <T> T getGlobalFlag(String key, T def) {
Object value = this.flags.get(key); Object value = this.flags.get(key);
if(value == null) { if(value == null) {
return null; return def;
} }
try { try {
@ -171,7 +184,7 @@ public class Server {
* @return This server's listeners. * @return This server's listeners.
*/ */
public List<ServerListener> getListeners() { public List<ServerListener> getListeners() {
return new ArrayList<ServerListener>(this.listeners); return Collections.unmodifiableList(this.listeners);
} }
/** /**

View file

@ -92,7 +92,7 @@ public interface Session {
/** /**
* Gets the value of the given flag as an instance of the given type. If this * Gets the value of the given flag as an instance of the given type. If this
* session belongs to a server, the server's flags will be checked for the flag * session belongs to a server, the server's flags will be checked for the flag
* as well. If no flag is found, the specified default value will be returned. * as well. If the flag is not set, the specified default value will be returned.
* *
* @param <T> Type of the flag. * @param <T> Type of the flag.
* @param key Key of the flag. * @param key Key of the flag.

View file

@ -4,6 +4,7 @@ import com.github.steveice10.packetlib.Server;
import com.github.steveice10.packetlib.packet.PacketProtocol; import com.github.steveice10.packetlib.packet.PacketProtocol;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class TcpServerSession extends TcpSession { public class TcpServerSession extends TcpSession {
@ -16,11 +17,41 @@ public class TcpServerSession extends TcpSession {
@Override @Override
public Map<String, Object> getFlags() { public Map<String, Object> getFlags() {
Map<String, Object> ret = super.getFlags(); Map<String, Object> ret = new HashMap<>();
ret.putAll(this.server.getGlobalFlags()); ret.putAll(this.server.getGlobalFlags());
ret.putAll(super.getFlags());
return ret; return ret;
} }
@Override
public boolean hasFlag(String key) {
if(super.hasFlag(key)) {
return true;
}
return this.server.hasGlobalFlag(key);
}
@Override
public <T> T getFlag(String key) {
T ret = super.getFlag(key);
if(ret != null) {
return ret;
}
return this.server.getGlobalFlag(key);
}
@Override
public <T> T getFlag(String key, T def) {
T ret = super.getFlag(key);
if(ret != null) {
return ret;
}
return this.server.getGlobalFlag(key, def);
}
@Override @Override
public void channelActive(ChannelHandlerContext ctx) throws Exception { public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx); super.channelActive(ctx);

View file

@ -129,7 +129,7 @@ public abstract class TcpSession extends SimpleChannelInboundHandler<Packet> imp
@Override @Override
public List<SessionListener> getListeners() { public List<SessionListener> getListeners() {
return new ArrayList<SessionListener>(this.listeners); return Collections.unmodifiableList(this.listeners);
} }
@Override @Override