diff --git a/src/main/java/org/redkale/net/Request.java b/src/main/java/org/redkale/net/Request.java index 2d47a154d..e958fe9a2 100644 --- a/src/main/java/org/redkale/net/Request.java +++ b/src/main/java/org/redkale/net/Request.java @@ -10,6 +10,7 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Array; import java.nio.ByteBuffer; import java.util.*; +import java.util.function.Function; import org.redkale.convert.ConvertDisabled; import org.redkale.convert.bson.BsonConvert; import org.redkale.convert.json.JsonConvert; @@ -52,10 +53,10 @@ public abstract class Request { protected AsyncConnection channel; - // properties与attributes的区别在于: - // 调用recycle时, attributes会被清空而properties会保留; - // properties 通常存放需要永久绑定在request里的一些对象 - private final Map properties = new HashMap<>(); + // subobjects与attributes的区别在于: + // 调用recycle时, attributes会被清空而subobjects会保留; + // subobjects通常存放需要永久绑定在request里的一些对象 + private final Map subobjects = new HashMap<>(); /** 每次新请求都会清空 */ protected final Map attributes = new HashMap<>(); @@ -116,48 +117,53 @@ public abstract class Request { channel = null; // close it by response } - protected T setProperty(String name, T value) { - properties.put(name, value); + @SuppressWarnings("unchecked") + public V getSubobject(String name) { + return (V) this.subobjects.get(name); + } + + @SuppressWarnings("unchecked") + public V getSubobjectIfAbsent(String name, Function func) { + return (V) this.subobjects.computeIfAbsent(name, func); + } + + public V setSubobject(String name, V value) { + this.subobjects.put(name, value); return value; } @SuppressWarnings("unchecked") - protected T getProperty(String name) { - return (T) properties.get(name); + public V removeSubobject(String name) { + return (V) this.subobjects.remove(name); } - @SuppressWarnings("unchecked") - protected T removeProperty(String name) { - return (T) properties.remove(name); + protected Map getSubobjects() { + return subobjects; } - protected Map getProperties() { - return properties; - } - - protected InputStream newInputStream() { - return ((AsyncNioConnection) channel).newInputStream(); - } - - public T setAttribute(String name, T value) { + public V setAttribute(String name, V value) { attributes.put(name, value); return value; } @SuppressWarnings("unchecked") - public T getAttribute(String name) { - return (T) attributes.get(name); + public V getAttribute(String name) { + return (V) attributes.get(name); } @SuppressWarnings("unchecked") - public T removeAttribute(String name) { - return (T) attributes.remove(name); + public V removeAttribute(String name) { + return (V) attributes.remove(name); } public Map getAttributes() { return attributes; } + protected InputStream newInputStream() { + return ((AsyncNioConnection) channel).newInputStream(); + } + public boolean isKeepAlive() { return keepAlive; }