This is an automated email from the ASF dual-hosted git repository.
jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push:
new 68f334a PropertyStore refactoring.
68f334a is described below
commit 68f334a8cc53132a548d77d8db1e454efbd9019e
Author: JamesBognar <[email protected]>
AuthorDate: Mon Feb 8 11:00:56 2021 -0500
PropertyStore refactoring.
---
.../src/main/java/org/apache/juneau/Session.java | 2 +-
.../main/java/org/apache/juneau/SessionArgs.java | 28 ++------
.../java/org/apache/juneau/SessionProperties.java | 78 ++++++++++++++++++----
.../juneau/rest/Header_AcceptCharset_Test.java | 6 +-
4 files changed, 73 insertions(+), 41 deletions(-)
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/Session.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/Session.java
index 8b5b3ea..f77b367 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/Session.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/Session.java
@@ -52,7 +52,7 @@ public abstract class Session {
*/
protected Session(Context ctx, SessionArgs args) {
this.ctx = ctx;
- SessionProperties sp = this.properties = new
SessionProperties(args.properties == null ? OMap.EMPTY_MAP : args.properties);
+ SessionProperties sp = this.properties = args.properties;
debug = sp.get(CONTEXT_debug,
Boolean.class).orElse(ctx.isDebug());
locale = sp.get(CONTEXT_locale,
Locale.class).orElse(ctx.getDefaultLocale());
timeZone = sp.get(CONTEXT_timeZone,
TimeZone.class).orElse(ctx.getDefaultTimeZone());
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/SessionArgs.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/SessionArgs.java
index e464845..ed60c1c 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/SessionArgs.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/SessionArgs.java
@@ -26,12 +26,7 @@ import org.apache.juneau.json.*;
*/
public class SessionArgs {
- /**
- * Default empty session arguments.
- */
- public static final SessionArgs DEFAULT = new SessionArgs();
-
- OMap properties;
+ SessionProperties properties = SessionProperties.create();
/**
* Constructor.
@@ -138,7 +133,7 @@ public class SessionArgs {
*/
@FluentSetter
public SessionArgs properties(OMap value) {
- this.properties = value;
+ this.properties = SessionProperties.create(value);
return this;
}
@@ -152,28 +147,13 @@ public class SessionArgs {
@FluentSetter
public SessionArgs property(String key, Object value) {
if (value == null) {
- if (properties != null)
- properties.remove(key);
+ properties.remove(key);
} else {
- if (properties == null)
- properties = new OMap();
properties.put(key, value);
}
return this;
}
- /**
- * Returns a property on this session.
- *
- * @param key The property key.
- * @return The property value, or <jk>null</jk> if not set.
- */
- public Object getProperty(String key) {
- if (properties != null)
- return properties.get(key);
- return null;
- }
-
//-----------------------------------------------------------------------------------------------------------------
// Other methods
//-----------------------------------------------------------------------------------------------------------------
@@ -191,7 +171,7 @@ public class SessionArgs {
.create()
.filtered()
.append("SessionArgs", OMap.create().filtered()
- .append("properties", properties)
+ .append("properties", properties.asMap())
);
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/SessionProperties.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/SessionProperties.java
index 2a8860c..d4bc6c3 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/SessionProperties.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/SessionProperties.java
@@ -28,12 +28,29 @@ public class SessionProperties {
private final OMap map;
/**
- * Constructor.
+ * Static creator.
+ *
+ * @return A new instance of this class.
+ */
+ public static SessionProperties create() {
+ return new SessionProperties(null);
+ }
+
+ /**
+ * Static creator.
*
- * @param map The map containing the properties for this session.
+ * @param inner The initial contents of these properties.
+ * @return A new instance of this class.
*/
- public SessionProperties(OMap map) {
- this.map = map;
+ public static SessionProperties create(OMap inner) {
+ return new SessionProperties(inner);
+ }
+
+ /**
+ * Constructor.
+ */
+ private SessionProperties(OMap inner) {
+ this.map = inner == null ? new OMap() : inner;
}
/**
@@ -43,7 +60,7 @@ public class SessionProperties {
* @return <jk>true</jk> if this session has the specified property
defined.
*/
public final boolean contains(String key) {
- return map != null && map.containsKey(key);
+ return map.containsKey(key);
}
/**
@@ -53,7 +70,7 @@ public class SessionProperties {
* @return The property value. Never <jk>null</jk>.
*/
public final Optional<Object> get(String key) {
- return Optional.ofNullable(map == null ? null : map.get(key));
+ return Optional.ofNullable(map.get(key));
}
/**
@@ -105,8 +122,7 @@ public class SessionProperties {
* @return The session property.
*/
public <T> Optional<T> getInstance(String key, Class<T> type) {
- Object o = map == null ? null : map.get(key);
- return Optional.ofNullable(newInstance(type, o));
+ return Optional.ofNullable(newInstance(type, map.get(key)));
}
/**
@@ -118,11 +134,11 @@ public class SessionProperties {
*/
@SuppressWarnings("unchecked")
public <T> Optional<T[]> getInstanceArray(String key, Class<T> type) {
- Object o = map == null ? null : map.get(key);
- T[] t = null;
+ Object o = map.get(key);
if (o == null)
return Optional.empty();
- else if (o.getClass().isArray()) {
+ T[] t = null;
+ if (o.getClass().isArray()) {
if (o.getClass().getComponentType() == type)
t = (T[])o;
else {
@@ -146,7 +162,7 @@ public class SessionProperties {
* @return All the keys in these properties.
*/
public Set<String> keySet() {
- return map == null ? Collections.emptySet() : map.keySet();
+ return map.keySet();
}
/**
@@ -159,8 +175,7 @@ public class SessionProperties {
}
private <T> T find(String key, Class<T> c) {
- Object o = map == null ? null : map.get(key);
- return newInstance(c, o);
+ return newInstance(c, map.get(key));
}
@SuppressWarnings("unchecked")
@@ -178,4 +193,39 @@ public class SessionProperties {
return t;
throw new ConfigException("Could not instantiate type ''{0}''
as type {1}", o, type);
}
+
+ /**
+ * Removes a property from this store.
+ *
+ * @param key The property key.
+ * @return This object (for method chaining).
+ */
+ public SessionProperties remove(String key) {
+ map.remove(key);
+ return this;
+ }
+
+ /**
+ * Adds a property to this store.
+ *
+ * @param key The property key.
+ * @param value The property value.
+ * @return This object (for method chaining).
+ */
+ public SessionProperties put(String key, Object value) {
+ map.put(key, value);
+ return this;
+ }
+
+ /**
+ * Adds multiple properties to this store.
+ *
+ * @param values The map containing the properties to add.
+ * @return This object (for method chaining).
+ */
+ public SessionProperties putAll(Map<String,Object> values) {
+ if (values != null)
+ map.putAll(values);
+ return this;
+ }
}
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/rest/Header_AcceptCharset_Test.java
b/juneau-utest/src/test/java/org/apache/juneau/rest/Header_AcceptCharset_Test.java
index 9b450fc..690f94c 100644
---
a/juneau-utest/src/test/java/org/apache/juneau/rest/Header_AcceptCharset_Test.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/rest/Header_AcceptCharset_Test.java
@@ -80,7 +80,8 @@ public class Header_AcceptCharset_Test {
@Override /* ParserSession */
@SuppressWarnings("unchecked")
protected <T> T doParse(ParserPipe
pipe, ClassMeta<T> type) throws IOException, ParseException,
ExecutableException {
- return
(T)args.getProperty(ReaderParser.RPARSER_streamCharset).toString();
+ SessionProperties sp =
getSessionProperties();
+ return
(T)sp.get(ReaderParser.RPARSER_streamCharset).get().toString();
}
};
}
@@ -95,8 +96,9 @@ public class Header_AcceptCharset_Test {
return new OutputStreamSerializerSession(args) {
@Override /* SerializerSession */
protected void
doSerialize(SerializerPipe out, Object o) throws IOException,
SerializeException {
+ SessionProperties sp =
getSessionProperties();
try (Writer w = new
OutputStreamWriter(out.getOutputStream())) {
- Object sc =
args.getProperty(WriterSerializer.WSERIALIZER_streamCharset);
+ Object sc =
sp.get(WriterSerializer.WSERIALIZER_streamCharset).orElse(null);
w.append(o.toString()).append('/').append(sc == null ? null : sc.toString());
}
}