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 893125a Context API refactoring.
893125a is described below
commit 893125ac3933462af26ef1d09c487e5ebea83fb7
Author: JamesBognar <[email protected]>
AuthorDate: Sun Aug 22 11:12:05 2021 -0400
Context API refactoring.
---
.../java/org/apache/juneau/parser/ParserGroup.java | 29 ++++++---
.../apache/juneau/parser/ParserGroupBuilder.java | 55 +++++++++--------
.../apache/juneau/serializer/SerializerGroup.java | 27 ++++++---
.../juneau/serializer/SerializerGroupBuilder.java | 69 ++++++++++++++--------
4 files changed, 115 insertions(+), 65 deletions(-)
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/parser/ParserGroup.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/parser/ParserGroup.java
index 1328362..1bb2b28 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/parser/ParserGroup.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/parser/ParserGroup.java
@@ -13,6 +13,9 @@
package org.apache.juneau.parser;
import static org.apache.juneau.http.HttpHeaders.*;
+import static java.util.Arrays.*;
+import static java.util.Collections.*;
+
import java.util.*;
import java.util.concurrent.*;
@@ -20,6 +23,7 @@ import java.util.concurrent.*;
import org.apache.juneau.annotation.*;
import org.apache.juneau.collections.*;
import org.apache.juneau.http.header.*;
+import org.apache.juneau.internal.*;
/**
* Represents a group of {@link Parser Parsers} that can be looked up by media
type.
@@ -85,7 +89,8 @@ public final class ParserGroup {
private final List<MediaType> mediaTypes;
private final List<Parser> mediaTypeParsers;
- private final List<Parser> parsers;
+
+ final Parser[] parsers;
/**
* Instantiates a new clean-slate {@link ParserGroupBuilder} object.
@@ -102,13 +107,19 @@ public final class ParserGroup {
/**
* Constructor.
*
- * @param parsers
- * The parsers defined in this group.
- * The order is important because they will be tried in reverse
order (e.g. newer first) in which they will be
- * tried to match against media types.
+ * @param builder The builder for this bean.
*/
- public ParserGroup(Parser[] parsers) {
- this.parsers = AList.unmodifiable(parsers);
+ public ParserGroup(ParserGroupBuilder builder) {
+ List<Parser> x = new ArrayList<>();
+ for (Object p : builder.parsers) {
+ if (p instanceof ParserBuilder) {
+ x.add(((ParserBuilder)p).build());
+ } else {
+ x.add((Parser)p);
+ }
+ }
+
+ this.parsers = ArrayUtils.toReverseArray(Parser.class, x);
AList<MediaType> lmt = AList.create();
AList<Parser> l = AList.create();
@@ -207,7 +218,7 @@ public final class ParserGroup {
* @return An unmodifiable list of parsers in this group.
*/
public List<Parser> getParsers() {
- return parsers;
+ return unmodifiableList(asList(parsers));
}
/**
@@ -216,6 +227,6 @@ public final class ParserGroup {
* @return <jk>true</jk> if this group contains no parsers.
*/
public boolean isEmpty() {
- return parsers.isEmpty();
+ return parsers.length == 0;
}
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/parser/ParserGroupBuilder.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/parser/ParserGroupBuilder.java
index 5d9591a..9acf380 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/parser/ParserGroupBuilder.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/parser/ParserGroupBuilder.java
@@ -26,13 +26,13 @@ import org.apache.juneau.internal.*;
@FluentSetters
public class ParserGroupBuilder {
- private final AList<Object> parsers;
+ final AList<Object> parsers;
private BeanContextBuilder bcBuilder;
/**
* Create an empty parser group builder.
*/
- public ParserGroupBuilder() {
+ protected ParserGroupBuilder() {
this.parsers = AList.create();
}
@@ -41,11 +41,40 @@ public class ParserGroupBuilder {
*
* @param copyFrom The bean to copy from.
*/
- public ParserGroupBuilder(ParserGroup copyFrom) {
+ protected ParserGroupBuilder(ParserGroup copyFrom) {
this.parsers =
AList.create().appendReverse(copyFrom.getParsers());
}
/**
+ * Copy constructor.
+ *
+ * @param copyFrom The builder to copy from.
+ */
+ protected ParserGroupBuilder(ParserGroupBuilder copyFrom) {
+ bcBuilder = copyFrom.bcBuilder.copy();
+ parsers = AList.create();
+ copyFrom.parsers.stream().map(x -> copyBuilder(x)).forEach(x ->
parsers.add(x));
+ }
+
+ private Object copyBuilder(Object o) {
+ if (o instanceof ParserBuilder)
+ return
((ParserBuilder)o).copy().beanContextBuilder(bcBuilder);
+ return o;
+ }
+
+ /**
+ * Creates a new {@link ParserGroup} object using a snapshot of the
settings defined in this builder.
+ *
+ * <p>
+ * This method can be called multiple times to produce multiple parser
groups.
+ *
+ * @return A new {@link ParserGroup} object.
+ */
+ public ParserGroup build() {
+ return new ParserGroup(this);
+ }
+
+ /**
* Associates an existing bean context builder with all serializer
builders in this group.
*
* @param value The bean contest builder to associate.
@@ -112,26 +141,6 @@ public class ParserGroupBuilder {
}
/**
- * Creates a new {@link ParserGroup} object using a snapshot of the
settings defined in this builder.
- *
- * <p>
- * This method can be called multiple times to produce multiple parser
groups.
- *
- * @return A new {@link ParserGroup} object.
- */
- public ParserGroup build() {
- List<Parser> l = new ArrayList<>();
- for (Object p : parsers) {
- if (p instanceof ParserBuilder) {
- l.add(((ParserBuilder)p).build());
- } else {
- l.add((Parser)p);
- }
- }
- return new ParserGroup(ArrayUtils.toReverseArray(Parser.class,
l));
- }
-
- /**
* Performs an action on all parser builders in this group.
*
* @param action The action to perform.
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/serializer/SerializerGroup.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/serializer/SerializerGroup.java
index 52d44b7..3456f20 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/serializer/SerializerGroup.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/serializer/SerializerGroup.java
@@ -13,6 +13,8 @@
package org.apache.juneau.serializer;
import static org.apache.juneau.http.HttpHeaders.*;
+import static java.util.Arrays.*;
+import static java.util.Collections.*;
import java.util.*;
import java.util.concurrent.*;
@@ -20,6 +22,7 @@ import java.util.concurrent.*;
import org.apache.juneau.annotation.*;
import org.apache.juneau.collections.*;
import org.apache.juneau.http.header.*;
+import org.apache.juneau.internal.*;
/**
* Represents a group of {@link Serializer Serializers} that can be looked up
by media type.
@@ -79,7 +82,7 @@ public final class SerializerGroup {
private final List<Serializer> mediaTypeRangeSerializers;
private final List<MediaType> mediaTypesList;
- private final List<Serializer> serializers;
+ final Serializer[] serializers;
/**
* Instantiates a new clean-slate {@link SerializerGroupBuilder} object.
@@ -96,13 +99,19 @@ public final class SerializerGroup {
/**
* Constructor.
*
- * @param serializers
- * The serializers defined in this group.
- * The order is important because they will be tried in reverse
order (e.g.newer first) in which they will be tried
- * to match against media types.
+ * @param builder The builder for this bean.
*/
- public SerializerGroup(Serializer[] serializers) {
- this.serializers = AList.unmodifiable(serializers);
+ protected SerializerGroup(SerializerGroupBuilder builder) {
+
+ List<Serializer> x = new ArrayList<>();
+ for (Object s : builder.serializers) {
+ if (s instanceof SerializerBuilder) {
+ x.add(((SerializerBuilder)s).build());
+ } else {
+ x.add((Serializer)s);
+ }
+ }
+ this.serializers = ArrayUtils.toReverseArray(Serializer.class,
x);
AList<MediaRange> lmtr = AList.create();
ASet<MediaType> lmt = ASet.of();
@@ -261,7 +270,7 @@ public final class SerializerGroup {
* @return An unmodifiable list of serializers in this group.
*/
public List<Serializer> getSerializers() {
- return serializers;
+ return unmodifiableList(asList(serializers));
}
/**
@@ -270,6 +279,6 @@ public final class SerializerGroup {
* @return <jk>true</jk> if this group contains no serializers.
*/
public boolean isEmpty() {
- return serializers.isEmpty();
+ return serializers.length == 0;
}
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/serializer/SerializerGroupBuilder.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/serializer/SerializerGroupBuilder.java
index 2664541..ecb5daf 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/serializer/SerializerGroupBuilder.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/serializer/SerializerGroupBuilder.java
@@ -26,23 +26,64 @@ import org.apache.juneau.internal.*;
@FluentSetters
public class SerializerGroupBuilder {
- private final AList<Object> serializers;
+ final AList<Object> serializers;
private BeanContextBuilder bcBuilder;
/**
* Create an empty serializer group builder.
*/
- public SerializerGroupBuilder() {
+ protected SerializerGroupBuilder() {
this.serializers = AList.create();
}
/**
+ * Clone an existing serializer group.
+ *
+ * @param copyFrom The serializer group that we're copying settings and
serializers from.
+ */
+ protected SerializerGroupBuilder(SerializerGroup copyFrom) {
+ this.serializers =
AList.create().appendReverse(Arrays.asList(copyFrom.serializers));
+ }
+
+ /**
* Clone an existing serializer group builder.
*
+ * <p>
+ * Serializer builders will be cloned during this process.
+ *
* @param copyFrom The serializer group that we're copying settings and
serializers from.
*/
- public SerializerGroupBuilder(SerializerGroup copyFrom) {
- this.serializers =
AList.create().appendReverse(copyFrom.getSerializers());
+ protected SerializerGroupBuilder(SerializerGroupBuilder copyFrom) {
+ bcBuilder = copyFrom.bcBuilder.copy();
+ serializers = AList.create();
+ copyFrom.serializers.stream().map(x ->
copyBuilder(x)).forEach(x -> serializers.add(x));
+ }
+
+ private Object copyBuilder(Object o) {
+ if (o instanceof SerializerBuilder)
+ return
((SerializerBuilder)o).copy().beanContextBuilder(bcBuilder);
+ return o;
+ }
+
+ /**
+ * Copy creator.
+ *
+ * @return A new mutable copy of this builder.
+ */
+ public SerializerGroupBuilder copy() {
+ return new SerializerGroupBuilder(this);
+ }
+
+ /**
+ * Creates a new {@link SerializerGroup} object using a snapshot of the
settings defined in this builder.
+ *
+ * <p>
+ * This method can be called multiple times to produce multiple
serializer groups.
+ *
+ * @return A new {@link SerializerGroup} object.
+ */
+ public SerializerGroup build() {
+ return new SerializerGroup(this);
}
/**
@@ -113,26 +154,6 @@ public class SerializerGroupBuilder {
}
/**
- * Creates a new {@link SerializerGroup} object using a snapshot of the
settings defined in this builder.
- *
- * <p>
- * This method can be called multiple times to produce multiple
serializer groups.
- *
- * @return A new {@link SerializerGroup} object.
- */
- public SerializerGroup build() {
- List<Serializer> l = new ArrayList<>();
- for (Object s : serializers) {
- if (s instanceof SerializerBuilder) {
- l.add(((SerializerBuilder)s).build());
- } else {
- l.add((Serializer)s);
- }
- }
- return new
SerializerGroup(ArrayUtils.toReverseArray(Serializer.class, l));
- }
-
- /**
* Performs an action on all serializer builders in this group.
*
* @param action The action to perform.