From 8cbaffbd0f3db592f0be2b5cb0b1033b1cc4447e Mon Sep 17 00:00:00 2001 From: Brad Baker Date: Wed, 11 Nov 2020 23:08:48 +1100 Subject: [PATCH 1/2] This moves the AST element builder to use immutable lists with the ability to add items to individually --- .../java/graphql/collect/ImmutableKit.java | 45 ++++++++++++++----- src/main/java/graphql/language/Argument.java | 26 +++++------ .../java/graphql/language/ArrayValue.java | 27 ++++++----- .../java/graphql/language/BooleanValue.java | 8 ++-- src/main/java/graphql/language/Directive.java | 23 ++++++---- .../graphql/language/DirectiveDefinition.java | 24 +++++----- .../graphql/language/DirectiveLocation.java | 8 ++-- src/main/java/graphql/language/Document.java | 16 +++---- .../graphql/language/EnumTypeDefinition.java | 22 ++++----- .../language/EnumTypeExtensionDefinition.java | 27 ++++++----- src/main/java/graphql/language/EnumValue.java | 8 ++-- .../graphql/language/EnumValueDefinition.java | 12 ++--- .../graphql/language/FieldDefinition.java | 26 ++++++----- .../java/graphql/language/FloatValue.java | 8 ++-- .../graphql/language/FragmentDefinition.java | 20 ++++++--- .../java/graphql/language/FragmentSpread.java | 20 ++++++--- .../java/graphql/language/InlineFragment.java | 19 +++++--- .../language/InputObjectTypeDefinition.java | 24 +++++----- .../InputObjectTypeExtensionDefinition.java | 33 +++++++++----- .../language/InputValueDefinition.java | 15 ++++--- src/main/java/graphql/language/IntValue.java | 7 +-- .../language/InterfaceTypeDefinition.java | 31 ++++++------- .../InterfaceTypeExtensionDefinition.java | 40 +++++++++++------ src/main/java/graphql/language/ListType.java | 7 ++- .../java/graphql/language/NodeParentTree.java | 4 +- .../java/graphql/language/NonNullType.java | 6 +-- src/main/java/graphql/language/NullValue.java | 9 ++-- .../java/graphql/language/ObjectField.java | 10 ++--- .../language/ObjectTypeDefinition.java | 31 ++++++------- .../ObjectTypeExtensionDefinition.java | 36 ++++++++------- .../java/graphql/language/ObjectValue.java | 18 ++++---- .../graphql/language/OperationDefinition.java | 28 ++++++++---- .../language/OperationTypeDefinition.java | 7 +-- .../language/ScalarTypeDefinition.java | 16 +++---- .../ScalarTypeExtensionDefinition.java | 25 ++++++----- .../graphql/language/SchemaDefinition.java | 26 ++++++----- .../language/SchemaExtensionDefinition.java | 28 +++++++----- .../java/graphql/language/SelectionSet.java | 16 +++---- .../java/graphql/language/StringValue.java | 10 ++--- src/main/java/graphql/language/TypeName.java | 7 +-- .../graphql/language/UnionTypeDefinition.java | 23 +++++----- .../UnionTypeExtensionDefinition.java | 36 +++++++++------ .../graphql/language/VariableDefinition.java | 15 ++++--- .../graphql/language/VariableReference.java | 7 +-- .../parser/GraphqlAntlrToLanguage.java | 7 +-- .../graphql/collect/ImmutableKitTest.groovy | 14 ++++++ 46 files changed, 509 insertions(+), 366 deletions(-) diff --git a/src/main/java/graphql/collect/ImmutableKit.java b/src/main/java/graphql/collect/ImmutableKit.java index 87da2895a2..4682451fbb 100644 --- a/src/main/java/graphql/collect/ImmutableKit.java +++ b/src/main/java/graphql/collect/ImmutableKit.java @@ -2,7 +2,6 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import graphql.Assert; import graphql.Internal; import java.util.Collection; @@ -10,6 +9,8 @@ import java.util.Map; import java.util.function.Function; +import static graphql.Assert.assertNotNull; + @Internal public final class ImmutableKit { @@ -40,7 +41,7 @@ public static ImmutableMap emptyMap() { */ public static ImmutableMap> toImmutableMapOfLists(Map> startingMap) { - Assert.assertNotNull(startingMap); + assertNotNull(startingMap); ImmutableMap.Builder> map = ImmutableMap.builder(); for (Map.Entry> e : startingMap.entrySet()) { ImmutableList value = ImmutableList.copyOf(startingMap.getOrDefault(e.getKey(), emptyList())); @@ -66,22 +67,46 @@ public static ImmutableList concatLists(List l1, List l2) { * This is more efficient than `c.stream().map().collect()` because it does not create the intermediate objects needed * for the flexible style. Benchmarking has shown this to outperform `stream()`. * - * @param collection the collection to map - * @param mapper the mapper function - * @param for two - * @param for result + * @param iterable the iterable to map + * @param mapper the mapper function + * @param for two + * @param for result * * @return a map immutable list of results */ - public static ImmutableList map(Collection collection, Function mapper) { - Assert.assertNotNull(collection); - Assert.assertNotNull(mapper); + public static ImmutableList map(Iterable iterable, Function mapper) { + assertNotNull(iterable); + assertNotNull(mapper); @SuppressWarnings("RedundantTypeArguments") ImmutableList.Builder builder = ImmutableList.builder(); - for (T t : collection) { + for (T t : iterable) { R r = mapper.apply(t); builder.add(r); } return builder.build(); } + + /** + * This constructs a new Immutable list from an existing collection and adds a new element to it. + * + * @param existing the existing collection + * @param newValue the new value to add + * @param extraValues more values to add + * @param for two + * + * @return an Immutable list with the extra effort. + */ + public static ImmutableList addToList(Collection existing, T newValue, T... extraValues) { + assertNotNull(existing); + assertNotNull(newValue); + int expectedSize = existing.size() + 1 + extraValues.length; + ImmutableList.Builder newList = ImmutableList.builderWithExpectedSize(expectedSize); + newList.addAll(existing); + newList.add(newValue); + for (T extraValue : extraValues) { + newList.add(extraValue); + } + return newList.build(); + } + } diff --git a/src/main/java/graphql/language/Argument.java b/src/main/java/graphql/language/Argument.java index d1cc6ab4c1..296af8784f 100644 --- a/src/main/java/graphql/language/Argument.java +++ b/src/main/java/graphql/language/Argument.java @@ -7,7 +7,6 @@ import graphql.util.TraversalControl; import graphql.util.TraverserContext; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -22,11 +21,10 @@ @PublicApi public class Argument extends AbstractNode implements NamedNode { + public static final String CHILD_VALUE = "value"; private final String name; private final Value value; - public static final String CHILD_VALUE = "value"; - @Internal protected Argument(String name, Value value, SourceLocation sourceLocation, List comments, IgnoredChars ignoredChars, Map additionalData) { super(sourceLocation, comments, ignoredChars, additionalData); @@ -44,6 +42,14 @@ public Argument(String name, Value value) { this(name, value, null, emptyList(), IgnoredChars.EMPTY, emptyMap()); } + public static Builder newArgument() { + return new Builder(); + } + + public static Builder newArgument(String name, Value value) { + return new Builder().name(name).value(value); + } + @Override public String getName() { return name; @@ -105,14 +111,6 @@ public TraversalControl accept(TraverserContext context, NodeVisitor visit return visitor.visitArgument(this, context); } - public static Builder newArgument() { - return new Builder(); - } - - public static Builder newArgument(String name, Value value) { - return new Builder().name(name).value(value); - } - public Argument transform(Consumer builderConsumer) { Builder builder = new Builder(this); builderConsumer.accept(builder); @@ -121,7 +119,7 @@ public Argument transform(Consumer builderConsumer) { public static final class Builder implements NodeBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; private Value value; private IgnoredChars ignoredChars = IgnoredChars.EMPTY; @@ -132,7 +130,7 @@ private Builder() { private Builder(Argument existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.value = existing.getValue(); this.ignoredChars = existing.getIgnoredChars(); @@ -155,7 +153,7 @@ public Builder value(Value value) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } diff --git a/src/main/java/graphql/language/ArrayValue.java b/src/main/java/graphql/language/ArrayValue.java index dbf000c571..154c8b7746 100644 --- a/src/main/java/graphql/language/ArrayValue.java +++ b/src/main/java/graphql/language/ArrayValue.java @@ -4,10 +4,10 @@ import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -21,9 +21,8 @@ @PublicApi public class ArrayValue extends AbstractNode implements Value { - private final ImmutableList values; - public static final String CHILD_VALUES = "values"; + private final ImmutableList values; @Internal protected ArrayValue(List values, SourceLocation sourceLocation, List comments, IgnoredChars ignoredChars, Map additionalData) { @@ -40,6 +39,10 @@ public ArrayValue(List values) { this(values, null, emptyList(), IgnoredChars.EMPTY, emptyMap()); } + public static Builder newArrayValue() { + return new Builder(); + } + public List getValues() { return values; } @@ -92,10 +95,6 @@ public TraversalControl accept(TraverserContext context, NodeVisitor visit return visitor.visitArrayValue(this, context); } - public static Builder newArrayValue() { - return new Builder(); - } - public ArrayValue transform(Consumer builderConsumer) { Builder builder = new Builder(this); builderConsumer.accept(builder); @@ -104,8 +103,8 @@ public ArrayValue transform(Consumer builderConsumer) { public static final class Builder implements NodeBuilder { private SourceLocation sourceLocation; - private List values = new ArrayList<>(); - private List comments = new ArrayList<>(); + private ImmutableList values = emptyList(); + private ImmutableList comments = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -114,8 +113,8 @@ private Builder() { private Builder(ArrayValue existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); - this.values = existing.getValues(); + this.comments = ImmutableList.copyOf(existing.getComments()); + this.values = ImmutableList.copyOf(existing.getValues()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -126,17 +125,17 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder values(List values) { - this.values = values; + this.values = ImmutableList.copyOf(values); return this; } public Builder value(Value value) { - this.values.add(value); + this.values = ImmutableKit.addToList(this.values, value); return this; } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } diff --git a/src/main/java/graphql/language/BooleanValue.java b/src/main/java/graphql/language/BooleanValue.java index cef23e38c5..4f5de35a50 100644 --- a/src/main/java/graphql/language/BooleanValue.java +++ b/src/main/java/graphql/language/BooleanValue.java @@ -1,12 +1,12 @@ package graphql.language; +import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; import graphql.util.TraversalControl; import graphql.util.TraverserContext; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -108,7 +108,7 @@ public BooleanValue transform(Consumer builderConsumer) { public static final class Builder implements NodeBuilder { private SourceLocation sourceLocation; private boolean value; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -117,7 +117,7 @@ private Builder() { private Builder(BooleanValue existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.value = existing.isValue(); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); @@ -135,7 +135,7 @@ public Builder value(boolean value) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } diff --git a/src/main/java/graphql/language/Directive.java b/src/main/java/graphql/language/Directive.java index 9c41061491..bf1f212bfe 100644 --- a/src/main/java/graphql/language/Directive.java +++ b/src/main/java/graphql/language/Directive.java @@ -4,10 +4,10 @@ import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -15,11 +15,11 @@ import java.util.function.Consumer; import static graphql.Assert.assertNotNull; +import static graphql.collect.ImmutableKit.emptyList; +import static graphql.collect.ImmutableKit.emptyMap; import static graphql.language.NodeChildrenContainer.newNodeChildrenContainer; import static graphql.language.NodeUtil.argumentsByName; import static graphql.language.NodeUtil.getArgumentByName; -import static java.util.Collections.emptyList; -import static java.util.Collections.emptyMap; @PublicApi public class Directive extends AbstractNode implements NamedNode { @@ -138,9 +138,9 @@ public Directive transform(Consumer builderConsumer) { public static final class Builder implements NodeBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; - private List arguments = new ArrayList<>(); + private ImmutableList arguments = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -149,9 +149,9 @@ private Builder() { private Builder(Directive existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); - this.arguments = existing.getArguments(); + this.arguments = ImmutableList.copyOf(existing.getArguments()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -163,7 +163,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -173,7 +173,12 @@ public Builder name(String name) { } public Builder arguments(List arguments) { - this.arguments = arguments; + this.arguments = ImmutableList.copyOf(arguments); + return this; + } + + public Builder argument(Argument argument) { + this.arguments = ImmutableKit.addToList(arguments,argument); return this; } diff --git a/src/main/java/graphql/language/DirectiveDefinition.java b/src/main/java/graphql/language/DirectiveDefinition.java index 1ea09cb119..9665b8cd4e 100644 --- a/src/main/java/graphql/language/DirectiveDefinition.java +++ b/src/main/java/graphql/language/DirectiveDefinition.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; @@ -141,11 +142,11 @@ public DirectiveDefinition transform(Consumer builderConsumer) { public static final class Builder implements NodeBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; private Description description; - private List inputValueDefinitions = new ArrayList<>(); - private List directiveLocations = new ArrayList<>(); + private ImmutableList inputValueDefinitions = emptyList(); + private ImmutableList directiveLocations = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -154,11 +155,11 @@ private Builder() { private Builder(DirectiveDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.description = existing.getDescription(); - this.inputValueDefinitions = existing.getInputValueDefinitions(); - this.directiveLocations = existing.getDirectiveLocations(); + this.inputValueDefinitions = ImmutableList.copyOf(existing.getInputValueDefinitions()); + this.directiveLocations = ImmutableList.copyOf(existing.getDirectiveLocations()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -169,7 +170,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -184,22 +185,23 @@ public Builder description(Description description) { } public Builder inputValueDefinitions(List inputValueDefinitions) { - this.inputValueDefinitions = inputValueDefinitions; + this.inputValueDefinitions = ImmutableList.copyOf(inputValueDefinitions); return this; } public Builder inputValueDefinition(InputValueDefinition inputValueDefinition) { - this.inputValueDefinitions.add(inputValueDefinition); + this.inputValueDefinitions = ImmutableKit.addToList(inputValueDefinitions, inputValueDefinition); return this; } + public Builder directiveLocations(List directiveLocations) { - this.directiveLocations = directiveLocations; + this.directiveLocations = ImmutableList.copyOf(directiveLocations); return this; } public Builder directiveLocation(DirectiveLocation directiveLocation) { - this.directiveLocations.add(directiveLocation); + this.directiveLocations = ImmutableKit.addToList(directiveLocations, directiveLocation); return this; } diff --git a/src/main/java/graphql/language/DirectiveLocation.java b/src/main/java/graphql/language/DirectiveLocation.java index 4cc8094107..f9e5d920d0 100644 --- a/src/main/java/graphql/language/DirectiveLocation.java +++ b/src/main/java/graphql/language/DirectiveLocation.java @@ -1,12 +1,12 @@ package graphql.language; +import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; import graphql.util.TraversalControl; import graphql.util.TraverserContext; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -102,7 +102,7 @@ public DirectiveLocation transform(Consumer builderConsumer) { public static final class Builder implements NodeBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -112,7 +112,7 @@ private Builder() { private Builder(DirectiveLocation existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -123,7 +123,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } diff --git a/src/main/java/graphql/language/Document.java b/src/main/java/graphql/language/Document.java index d881e6c4fd..ae7895b935 100644 --- a/src/main/java/graphql/language/Document.java +++ b/src/main/java/graphql/language/Document.java @@ -4,10 +4,10 @@ import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -118,9 +118,9 @@ public Document transform(Consumer builderConsumer) { } public static final class Builder implements NodeBuilder { - private List definitions = new ArrayList<>(); + private ImmutableList definitions = emptyList(); private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -129,19 +129,19 @@ private Builder() { private Builder(Document existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); - this.definitions = existing.getDefinitions(); + this.comments = ImmutableList.copyOf(existing.getComments()); + this.definitions = ImmutableList.copyOf(existing.getDefinitions()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } public Builder definitions(List definitions) { - this.definitions = definitions; + this.definitions = ImmutableList.copyOf(definitions); return this; } public Builder definition(Definition definition) { - this.definitions.add(definition); + this.definitions = ImmutableKit.addToList(definitions,definition); return this; } @@ -151,7 +151,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } diff --git a/src/main/java/graphql/language/EnumTypeDefinition.java b/src/main/java/graphql/language/EnumTypeDefinition.java index 7f0ddb3985..243e20acaf 100644 --- a/src/main/java/graphql/language/EnumTypeDefinition.java +++ b/src/main/java/graphql/language/EnumTypeDefinition.java @@ -141,11 +141,11 @@ public EnumTypeDefinition transform(Consumer builderConsumer) { public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; private Description description; - private List enumValueDefinitions = new ArrayList<>(); - private List directives = new ArrayList<>(); + private ImmutableList enumValueDefinitions = emptyList(); + private ImmutableList directives = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -154,11 +154,11 @@ private Builder() { private Builder(EnumTypeDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.description = existing.getDescription(); - this.directives = existing.getDirectives(); - this.enumValueDefinitions = existing.getEnumValueDefinitions(); + this.directives = ImmutableList.copyOf(existing.getDirectives()); + this.enumValueDefinitions = ImmutableList.copyOf(existing.getEnumValueDefinitions()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -169,7 +169,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -184,23 +184,23 @@ public Builder description(Description description) { } public Builder enumValueDefinitions(List enumValueDefinitions) { - this.enumValueDefinitions = enumValueDefinitions; + this.enumValueDefinitions = ImmutableList.copyOf(enumValueDefinitions); return this; } public Builder enumValueDefinition(EnumValueDefinition enumValueDefinition) { - this.enumValueDefinitions.add(enumValueDefinition); + this.enumValueDefinitions = ImmutableKit.addToList(enumValueDefinitions, enumValueDefinition); return this; } @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); return this; } public Builder directive(Directive directive) { - this.directives.add(directive); + this.directives = ImmutableKit.addToList(directives, directive); return this; } diff --git a/src/main/java/graphql/language/EnumTypeExtensionDefinition.java b/src/main/java/graphql/language/EnumTypeExtensionDefinition.java index eaf1b42afe..830b2d847a 100644 --- a/src/main/java/graphql/language/EnumTypeExtensionDefinition.java +++ b/src/main/java/graphql/language/EnumTypeExtensionDefinition.java @@ -1,15 +1,17 @@ package graphql.language; +import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.function.Consumer; import static graphql.Assert.assertNotNull; +import static graphql.collect.ImmutableKit.emptyList; @PublicApi public class EnumTypeExtensionDefinition extends EnumTypeDefinition { @@ -67,11 +69,11 @@ public EnumTypeExtensionDefinition transformExtension(Consumer builderC public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; private Description description; - private List enumValueDefinitions; - private List directives; + private ImmutableList enumValueDefinitions = emptyList(); + private ImmutableList directives = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -80,11 +82,11 @@ private Builder() { private Builder(EnumTypeExtensionDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.description = existing.getDescription(); - this.directives = existing.getDirectives(); - this.enumValueDefinitions = existing.getEnumValueDefinitions(); + this.directives = ImmutableList.copyOf(existing.getDirectives()); + this.enumValueDefinitions = ImmutableList.copyOf(existing.getEnumValueDefinitions()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -95,7 +97,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -110,13 +112,18 @@ public Builder description(Description description) { } public Builder enumValueDefinitions(List enumValueDefinitions) { - this.enumValueDefinitions = enumValueDefinitions; + this.enumValueDefinitions = ImmutableList.copyOf(enumValueDefinitions); return this; } @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); + return this; + } + + public Builder directive(Directive directive) { + this.directives = ImmutableKit.addToList(directives, directive); return this; } diff --git a/src/main/java/graphql/language/EnumValue.java b/src/main/java/graphql/language/EnumValue.java index 5f8e8f3d0b..08eea9672d 100644 --- a/src/main/java/graphql/language/EnumValue.java +++ b/src/main/java/graphql/language/EnumValue.java @@ -1,12 +1,12 @@ package graphql.language; +import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; import graphql.util.TraversalControl; import graphql.util.TraverserContext; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -110,7 +110,7 @@ public EnumValue transform(Consumer builderConsumer) { public static final class Builder implements NodeBuilder { private SourceLocation sourceLocation; private String name; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -119,7 +119,7 @@ private Builder() { private Builder(EnumValue existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -136,7 +136,7 @@ public Builder name(String name) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } diff --git a/src/main/java/graphql/language/EnumValueDefinition.java b/src/main/java/graphql/language/EnumValueDefinition.java index d1d019e75b..93a0d066f0 100644 --- a/src/main/java/graphql/language/EnumValueDefinition.java +++ b/src/main/java/graphql/language/EnumValueDefinition.java @@ -132,10 +132,10 @@ public EnumValueDefinition transform(Consumer builderConsumer) { public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; private Description description; - private List directives; + private ImmutableList directives = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -144,10 +144,10 @@ private Builder() { private Builder(EnumValueDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.description = existing.getDescription(); - this.directives = existing.getDirectives(); + this.directives = ImmutableList.copyOf(existing.getDirectives()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -158,7 +158,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -174,7 +174,7 @@ public Builder description(Description description) { @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); return this; } diff --git a/src/main/java/graphql/language/FieldDefinition.java b/src/main/java/graphql/language/FieldDefinition.java index b2dd301bfd..52d5b97da9 100644 --- a/src/main/java/graphql/language/FieldDefinition.java +++ b/src/main/java/graphql/language/FieldDefinition.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; @@ -152,11 +153,11 @@ public FieldDefinition transform(Consumer builderConsumer) { public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; private String name; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private Type type; private Description description; - private List inputValueDefinitions = new ArrayList<>(); - private List directives = new ArrayList<>(); + private ImmutableList inputValueDefinitions = emptyList(); + private ImmutableList directives = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -166,11 +167,11 @@ private Builder() { private Builder(FieldDefinition existing) { this.sourceLocation = existing.getSourceLocation(); this.name = existing.getName(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.type = existing.getType(); this.description = existing.getDescription(); - this.inputValueDefinitions = existing.getInputValueDefinitions(); - this.directives = existing.getDirectives(); + this.inputValueDefinitions = ImmutableList.copyOf(existing.getInputValueDefinitions()); + this.directives = ImmutableList.copyOf(existing.getDirectives()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -187,7 +188,7 @@ public Builder name(String name) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -202,23 +203,24 @@ public Builder description(Description description) { } public Builder inputValueDefinitions(List inputValueDefinitions) { - this.inputValueDefinitions = inputValueDefinitions; + this.inputValueDefinitions = ImmutableList.copyOf(inputValueDefinitions); return this; } - public Builder inputValueDefinition(InputValueDefinition inputValueDefinitions) { - this.inputValueDefinitions.add(inputValueDefinitions); + public Builder inputValueDefinition(InputValueDefinition inputValueDefinition) { + this.inputValueDefinitions = ImmutableKit.addToList(inputValueDefinitions, inputValueDefinition); return this; } + @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); return this; } public Builder directive(Directive directive) { - this.directives.add(directive); + this.directives = ImmutableKit.addToList(directives, directive); return this; } diff --git a/src/main/java/graphql/language/FloatValue.java b/src/main/java/graphql/language/FloatValue.java index 02d541bacf..652df05bc0 100644 --- a/src/main/java/graphql/language/FloatValue.java +++ b/src/main/java/graphql/language/FloatValue.java @@ -1,13 +1,13 @@ package graphql.language; +import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; import graphql.util.TraversalControl; import graphql.util.TraverserContext; import java.math.BigDecimal; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -108,7 +108,7 @@ public static Builder newFloatValue(BigDecimal value) { public static final class Builder implements NodeBuilder { private SourceLocation sourceLocation; private BigDecimal value; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -117,7 +117,7 @@ private Builder() { private Builder(FloatValue existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.value = existing.getValue(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -134,7 +134,7 @@ public Builder value(BigDecimal value) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } diff --git a/src/main/java/graphql/language/FragmentDefinition.java b/src/main/java/graphql/language/FragmentDefinition.java index 340d9154d1..10ec71d237 100644 --- a/src/main/java/graphql/language/FragmentDefinition.java +++ b/src/main/java/graphql/language/FragmentDefinition.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; @@ -15,6 +16,7 @@ import java.util.function.Consumer; import static graphql.Assert.assertNotNull; +import static graphql.collect.ImmutableKit.emptyList; import static graphql.language.NodeChildrenContainer.newNodeChildrenContainer; /** @@ -149,10 +151,11 @@ public FragmentDefinition transform(Consumer builderConsumer) { public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); + private String name; private TypeName typeCondition; - private List directives = new ArrayList<>(); + private ImmutableList directives = emptyList(); private SelectionSet selectionSet; private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -162,10 +165,10 @@ private Builder() { private Builder(FragmentDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.typeCondition = existing.getTypeCondition(); - this.directives = existing.getDirectives(); + this.directives = ImmutableList.copyOf(existing.getDirectives()); this.selectionSet = existing.getSelectionSet(); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); @@ -178,7 +181,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -194,7 +197,12 @@ public Builder typeCondition(TypeName typeCondition) { @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); + return this; + } + + public Builder directive(Directive directive) { + this.directives = ImmutableKit.addToList(directives, directive); return this; } diff --git a/src/main/java/graphql/language/FragmentSpread.java b/src/main/java/graphql/language/FragmentSpread.java index 12f698dac4..421ba135cb 100644 --- a/src/main/java/graphql/language/FragmentSpread.java +++ b/src/main/java/graphql/language/FragmentSpread.java @@ -4,10 +4,10 @@ import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -122,9 +122,9 @@ public FragmentSpread transform(Consumer builderConsumer) { public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; - private List directives = new ArrayList<>(); + private ImmutableList directives = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -133,9 +133,9 @@ private Builder() { private Builder(FragmentSpread existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); - this.directives = existing.getDirectives(); + this.directives = ImmutableList.copyOf(existing.getDirectives()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -146,7 +146,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -157,10 +157,16 @@ public Builder name(String name) { @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); return this; } + public Builder directive(Directive directive) { + this.directives = ImmutableKit.addToList(directives, directive); + return this; + } + + public Builder ignoredChars(IgnoredChars ignoredChars) { this.ignoredChars = ignoredChars; return this; diff --git a/src/main/java/graphql/language/InlineFragment.java b/src/main/java/graphql/language/InlineFragment.java index d05a564abd..ebdaae2d30 100644 --- a/src/main/java/graphql/language/InlineFragment.java +++ b/src/main/java/graphql/language/InlineFragment.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; @@ -163,9 +164,9 @@ public InlineFragment transform(Consumer builderConsumer) { public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private TypeName typeCondition; - private List directives = new ArrayList<>(); + private ImmutableList directives = emptyList(); private SelectionSet selectionSet; private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -176,9 +177,9 @@ private Builder() { private Builder(InlineFragment existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.typeCondition = existing.getTypeCondition(); - this.directives = existing.getDirectives(); + this.directives = ImmutableList.copyOf(existing.getDirectives()); this.selectionSet = existing.getSelectionSet(); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); @@ -191,7 +192,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -202,10 +203,16 @@ public Builder typeCondition(TypeName typeCondition) { @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); return this; } + public Builder directive(Directive directive) { + this.directives = ImmutableKit.addToList(directives, directive); + return this; + } + + public Builder selectionSet(SelectionSet selectionSet) { this.selectionSet = selectionSet; return this; diff --git a/src/main/java/graphql/language/InputObjectTypeDefinition.java b/src/main/java/graphql/language/InputObjectTypeDefinition.java index e0201305fa..127d7da308 100644 --- a/src/main/java/graphql/language/InputObjectTypeDefinition.java +++ b/src/main/java/graphql/language/InputObjectTypeDefinition.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; @@ -15,6 +16,7 @@ import java.util.function.Consumer; import static graphql.Assert.assertNotNull; +import static graphql.collect.ImmutableKit.emptyList; import static graphql.language.NodeChildrenContainer.newNodeChildrenContainer; @PublicApi @@ -133,11 +135,11 @@ public InputObjectTypeDefinition transform(Consumer builderConsumer) { public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; private Description description; - private List directives = new ArrayList<>(); - private List inputValueDefinitions = new ArrayList<>(); + private ImmutableList directives = emptyList(); + private ImmutableList inputValueDefinitions = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -146,11 +148,11 @@ private Builder() { private Builder(InputObjectTypeDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.description = existing.getDescription(); - this.directives = existing.getDirectives(); - this.inputValueDefinitions = existing.getInputValueDefinitions(); + this.directives = ImmutableList.copyOf(existing.getDirectives()); + this.inputValueDefinitions = ImmutableList.copyOf(existing.getInputValueDefinitions()); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -161,7 +163,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -177,22 +179,22 @@ public Builder description(Description description) { @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); return this; } public Builder directive(Directive directive) { - this.directives.add(directive); + this.directives = ImmutableKit.addToList(directives, directive); return this; } public Builder inputValueDefinitions(List inputValueDefinitions) { - this.inputValueDefinitions = inputValueDefinitions; + this.inputValueDefinitions = ImmutableList.copyOf(inputValueDefinitions); return this; } public Builder inputValueDefinition(InputValueDefinition inputValueDefinition) { - this.inputValueDefinitions.add(inputValueDefinition); + this.inputValueDefinitions = ImmutableKit.addToList(inputValueDefinitions, inputValueDefinition); return this; } diff --git a/src/main/java/graphql/language/InputObjectTypeExtensionDefinition.java b/src/main/java/graphql/language/InputObjectTypeExtensionDefinition.java index b5113405a1..6fa8344656 100644 --- a/src/main/java/graphql/language/InputObjectTypeExtensionDefinition.java +++ b/src/main/java/graphql/language/InputObjectTypeExtensionDefinition.java @@ -1,15 +1,17 @@ package graphql.language; +import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.function.Consumer; import static graphql.Assert.assertNotNull; +import static graphql.collect.ImmutableKit.emptyList; @PublicApi public class InputObjectTypeExtensionDefinition extends InputObjectTypeDefinition { @@ -67,11 +69,11 @@ public InputObjectTypeExtensionDefinition transformExtension(Consumer b public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; private Description description; - private List directives = new ArrayList<>(); - private List inputValueDefinitions = new ArrayList<>(); + private ImmutableList directives = emptyList(); + private ImmutableList inputValueDefinitions = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -80,11 +82,11 @@ private Builder() { private Builder(InputObjectTypeDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.description = existing.getDescription(); - this.directives = existing.getDirectives(); - this.inputValueDefinitions = existing.getInputValueDefinitions(); + this.directives = ImmutableList.copyOf(existing.getDirectives()); + this.inputValueDefinitions = ImmutableList.copyOf(existing.getInputValueDefinitions()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -96,7 +98,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -112,12 +114,23 @@ public Builder description(Description description) { @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); return this; } + public Builder directive(Directive directive) { + this.directives = ImmutableKit.addToList(directives, directive); + return this; + } + + public Builder inputValueDefinitions(List inputValueDefinitions) { - this.inputValueDefinitions = inputValueDefinitions; + this.inputValueDefinitions = ImmutableList.copyOf(inputValueDefinitions); + return this; + } + + public Builder inputValueDefinition(InputValueDefinition inputValueDefinition) { + this.inputValueDefinitions = ImmutableKit.addToList(inputValueDefinitions, inputValueDefinition); return this; } diff --git a/src/main/java/graphql/language/InputValueDefinition.java b/src/main/java/graphql/language/InputValueDefinition.java index fb58f75b2b..6250a1c41b 100644 --- a/src/main/java/graphql/language/InputValueDefinition.java +++ b/src/main/java/graphql/language/InputValueDefinition.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; @@ -175,12 +176,12 @@ public InputValueDefinition transform(Consumer builderConsumer) { public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; private Type type; private Value defaultValue; private Description description; - private List directives = new ArrayList<>(); + private ImmutableList directives = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -189,12 +190,12 @@ private Builder() { private Builder(InputValueDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.type = existing.getType(); this.defaultValue = existing.getDefaultValue(); this.description = existing.getDescription(); - this.directives = existing.getDirectives(); + this.directives = ImmutableList.copyOf(existing.getDirectives()); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -205,7 +206,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -231,12 +232,12 @@ public Builder description(Description description) { @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); return this; } public Builder directive(Directive directive) { - this.directives.add(directive); + this.directives = ImmutableKit.addToList(directives, directive); return this; } diff --git a/src/main/java/graphql/language/IntValue.java b/src/main/java/graphql/language/IntValue.java index b938484672..4015d88156 100644 --- a/src/main/java/graphql/language/IntValue.java +++ b/src/main/java/graphql/language/IntValue.java @@ -1,6 +1,7 @@ package graphql.language; +import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; import graphql.util.TraversalControl; @@ -108,7 +109,7 @@ public IntValue transform(Consumer builderConsumer) { public static final class Builder implements NodeBuilder { private SourceLocation sourceLocation; private BigInteger value; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -117,7 +118,7 @@ private Builder() { private Builder(IntValue existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.value = existing.getValue(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -133,7 +134,7 @@ public Builder value(BigInteger value) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } diff --git a/src/main/java/graphql/language/InterfaceTypeDefinition.java b/src/main/java/graphql/language/InterfaceTypeDefinition.java index 4dbf50cb55..c86c0fa486 100644 --- a/src/main/java/graphql/language/InterfaceTypeDefinition.java +++ b/src/main/java/graphql/language/InterfaceTypeDefinition.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; @@ -159,12 +160,12 @@ public InterfaceTypeDefinition transform(Consumer builderConsumer) { public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; private Description description; - private List implementz = new ArrayList<>(); - private List definitions = new ArrayList<>(); - private List directives = new ArrayList<>(); + private ImmutableList implementz = emptyList(); + private ImmutableList definitions = emptyList(); + private ImmutableList directives = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -174,14 +175,14 @@ private Builder() { private Builder(InterfaceTypeDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.description = existing.getDescription(); - this.directives = existing.getDirectives(); - this.definitions = existing.getFieldDefinitions(); + this.directives = ImmutableList.copyOf(existing.getDirectives()); + this.definitions = ImmutableList.copyOf(existing.getFieldDefinitions()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); - this.implementz = existing.getImplements(); + this.implementz = ImmutableList.copyOf(existing.getImplements()); } @@ -191,7 +192,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -206,34 +207,34 @@ public Builder description(Description description) { } public Builder implementz(List implementz) { - this.implementz = implementz; + this.implementz = ImmutableList.copyOf(implementz); return this; } public Builder implementz(Type implement) { - this.implementz.add(implement); + this.implementz = ImmutableKit.addToList(implementz, implement); return this; } public Builder definitions(List definitions) { - this.definitions = definitions; + this.definitions = ImmutableList.copyOf(definitions); return this; } public Builder definition(FieldDefinition definition) { - this.definitions.add(definition); + this.definitions = ImmutableKit.addToList(definitions, definition); return this; } @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); return this; } public Builder directive(Directive directive) { - this.directives.add(directive); + this.directives = ImmutableKit.addToList(directives, directive); return this; } diff --git a/src/main/java/graphql/language/InterfaceTypeExtensionDefinition.java b/src/main/java/graphql/language/InterfaceTypeExtensionDefinition.java index dfc9ba0492..a693c52866 100644 --- a/src/main/java/graphql/language/InterfaceTypeExtensionDefinition.java +++ b/src/main/java/graphql/language/InterfaceTypeExtensionDefinition.java @@ -1,15 +1,17 @@ package graphql.language; +import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.function.Consumer; import static graphql.Assert.assertNotNull; +import static graphql.collect.ImmutableKit.emptyList; @PublicApi public class InterfaceTypeExtensionDefinition extends InterfaceTypeDefinition { @@ -70,12 +72,12 @@ public InterfaceTypeExtensionDefinition transformExtension(Consumer bui public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; private Description description; - private List implementz = new ArrayList<>(); - private List definitions = new ArrayList<>(); - private List directives = new ArrayList<>(); + private ImmutableList implementz = emptyList(); + private ImmutableList definitions = emptyList(); + private ImmutableList directives = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -84,12 +86,12 @@ private Builder() { private Builder(InterfaceTypeExtensionDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.description = existing.getDescription(); - this.directives = existing.getDirectives(); - this.implementz = existing.getImplements(); - this.definitions = existing.getFieldDefinitions(); + this.directives = ImmutableList.copyOf(existing.getDirectives()); + this.implementz = ImmutableList.copyOf(existing.getImplements()); + this.definitions = ImmutableList.copyOf(existing.getFieldDefinitions()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -100,7 +102,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -115,23 +117,33 @@ public Builder description(Description description) { } public Builder implementz(List implementz) { - this.implementz = implementz; + this.implementz = ImmutableList.copyOf(implementz); return this; } public Builder implementz(Type implementz) { - this.implementz.add(implementz); + this.implementz = ImmutableKit.addToList(this.implementz, implementz); return this; } public Builder definitions(List definitions) { - this.definitions = definitions; + this.definitions = ImmutableList.copyOf(definitions); + return this; + } + + public Builder definition(FieldDefinition definition) { + this.definitions = ImmutableKit.addToList(definitions, definition); return this; } @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); + return this; + } + + public Builder directive(Directive directive) { + this.directives = ImmutableKit.addToList(directives, directive); return this; } diff --git a/src/main/java/graphql/language/ListType.java b/src/main/java/graphql/language/ListType.java index 668d578222..f193179bb6 100644 --- a/src/main/java/graphql/language/ListType.java +++ b/src/main/java/graphql/language/ListType.java @@ -7,7 +7,6 @@ import graphql.util.TraversalControl; import graphql.util.TraverserContext; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -109,7 +108,7 @@ public ListType transform(Consumer builderConsumer) { public static final class Builder implements NodeBuilder { private Type type; private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -118,7 +117,7 @@ private Builder() { private Builder(ListType existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.type = existing.getType(); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); @@ -136,7 +135,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } diff --git a/src/main/java/graphql/language/NodeParentTree.java b/src/main/java/graphql/language/NodeParentTree.java index 86c1799144..fc78ea093d 100644 --- a/src/main/java/graphql/language/NodeParentTree.java +++ b/src/main/java/graphql/language/NodeParentTree.java @@ -24,7 +24,7 @@ public class NodeParentTree { private final T node; private final NodeParentTree parent; - private final List path; + private final ImmutableList path; @Internal public NodeParentTree(Deque nodeStack) { @@ -41,7 +41,7 @@ public NodeParentTree(Deque nodeStack) { } } - private List mkPath(Deque copy) { + private ImmutableList mkPath(Deque copy) { return copy.stream() .filter(node1 -> node1 instanceof NamedNode) .map(node1 -> ((NamedNode) node1).getName()) diff --git a/src/main/java/graphql/language/NonNullType.java b/src/main/java/graphql/language/NonNullType.java index 579b6df84b..6d5fdc4454 100644 --- a/src/main/java/graphql/language/NonNullType.java +++ b/src/main/java/graphql/language/NonNullType.java @@ -110,7 +110,7 @@ public NonNullType transform(Consumer builderConsumer) { public static final class Builder implements NodeBuilder { private SourceLocation sourceLocation; private Type type; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -119,7 +119,7 @@ private Builder() { private Builder(NonNullType existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.type = existing.getType(); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); @@ -150,7 +150,7 @@ public Builder type(Type type) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } diff --git a/src/main/java/graphql/language/NullValue.java b/src/main/java/graphql/language/NullValue.java index ab3393f820..e69fc40542 100644 --- a/src/main/java/graphql/language/NullValue.java +++ b/src/main/java/graphql/language/NullValue.java @@ -1,12 +1,12 @@ package graphql.language; +import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; import graphql.util.TraversalControl; import graphql.util.TraverserContext; -import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; @@ -14,6 +14,7 @@ import java.util.function.Consumer; import static graphql.Assert.assertNotNull; +import static graphql.collect.ImmutableKit.emptyList; import static graphql.language.NodeChildrenContainer.newNodeChildrenContainer; import static graphql.language.NodeUtil.assertNewChildrenAreEmpty; @@ -83,13 +84,13 @@ public NullValue transform(Consumer builderConsumer) { public static final class Builder implements NodeBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); private Builder(NullValue existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -103,7 +104,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } diff --git a/src/main/java/graphql/language/ObjectField.java b/src/main/java/graphql/language/ObjectField.java index 2a9edc0833..7e392f64a2 100644 --- a/src/main/java/graphql/language/ObjectField.java +++ b/src/main/java/graphql/language/ObjectField.java @@ -8,13 +8,13 @@ import graphql.util.TraversalControl; import graphql.util.TraverserContext; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.function.Consumer; import static graphql.Assert.assertNotNull; +import static graphql.collect.ImmutableKit.emptyList; import static graphql.language.NodeChildrenContainer.newNodeChildrenContainer; @PublicApi @@ -39,7 +39,7 @@ protected ObjectField(String name, Value value, SourceLocation sourceLocation, L * @param value of the field */ public ObjectField(String name, Value value) { - this(name, value, null, ImmutableKit.emptyList(), IgnoredChars.EMPTY, ImmutableKit.emptyMap()); + this(name, value, null, emptyList(), IgnoredChars.EMPTY, ImmutableKit.emptyMap()); } @Override @@ -116,7 +116,7 @@ public ObjectField transform(Consumer builderConsumer) { public static final class Builder implements NodeBuilder { private SourceLocation sourceLocation; private String name; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private Value value; private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -127,7 +127,7 @@ private Builder() { private Builder(ObjectField existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.value = existing.getValue(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); @@ -145,7 +145,7 @@ public Builder name(String name) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } diff --git a/src/main/java/graphql/language/ObjectTypeDefinition.java b/src/main/java/graphql/language/ObjectTypeDefinition.java index e2eed513b5..d24a970c08 100644 --- a/src/main/java/graphql/language/ObjectTypeDefinition.java +++ b/src/main/java/graphql/language/ObjectTypeDefinition.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; @@ -155,12 +156,12 @@ public ObjectTypeDefinition transform(Consumer builderConsumer) { public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; private Description description; - private List implementz = new ArrayList<>(); - private List directives = new ArrayList<>(); - private List fieldDefinitions = new ArrayList<>(); + private ImmutableList implementz = emptyList(); + private ImmutableList directives = emptyList(); + private ImmutableList fieldDefinitions = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -169,12 +170,12 @@ private Builder() { private Builder(ObjectTypeDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.description = existing.getDescription(); - this.directives = existing.getDirectives(); - this.implementz = existing.getImplements(); - this.fieldDefinitions = existing.getFieldDefinitions(); + this.directives = ImmutableList.copyOf(existing.getDirectives()); + this.implementz = ImmutableList.copyOf(existing.getImplements()); + this.fieldDefinitions = ImmutableList.copyOf(existing.getFieldDefinitions()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -185,7 +186,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -200,33 +201,33 @@ public Builder description(Description description) { } public Builder implementz(List implementz) { - this.implementz = implementz; + this.implementz = ImmutableList.copyOf(implementz); return this; } public Builder implementz(Type implement) { - this.implementz.add(implement); + this.implementz = ImmutableKit.addToList(implementz, implement); return this; } @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); return this; } public Builder directive(Directive directive) { - this.directives.add(directive); + this.directives = ImmutableKit.addToList(directives, directive); return this; } public Builder fieldDefinitions(List fieldDefinitions) { - this.fieldDefinitions = fieldDefinitions; + this.fieldDefinitions = ImmutableList.copyOf(fieldDefinitions); return this; } public Builder fieldDefinition(FieldDefinition fieldDefinition) { - this.fieldDefinitions.add(fieldDefinition); + this.fieldDefinitions = ImmutableKit.addToList(fieldDefinitions, fieldDefinition); return this; } diff --git a/src/main/java/graphql/language/ObjectTypeExtensionDefinition.java b/src/main/java/graphql/language/ObjectTypeExtensionDefinition.java index e5a1021f62..e0fb1f946a 100644 --- a/src/main/java/graphql/language/ObjectTypeExtensionDefinition.java +++ b/src/main/java/graphql/language/ObjectTypeExtensionDefinition.java @@ -1,8 +1,10 @@ package graphql.language; +import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -82,26 +84,26 @@ public ObjectTypeExtensionDefinition transformExtension(Consumer builde public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; private Description description; - private List implementz = new ArrayList<>(); - private List directives = new ArrayList<>(); - private List fieldDefinitions = new ArrayList<>(); + private ImmutableList implementz = emptyList(); + private ImmutableList directives = emptyList(); + private ImmutableList fieldDefinitions = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); private Builder() { } - private Builder(ObjectTypeExtensionDefinition existing) { + private Builder(ObjectTypeDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.description = existing.getDescription(); - this.directives = existing.getDirectives(); - this.implementz = existing.getImplements(); - this.fieldDefinitions = existing.getFieldDefinitions(); + this.directives = ImmutableList.copyOf(existing.getDirectives()); + this.implementz = ImmutableList.copyOf(existing.getImplements()); + this.fieldDefinitions = ImmutableList.copyOf(existing.getFieldDefinitions()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -112,7 +114,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -127,33 +129,33 @@ public Builder description(Description description) { } public Builder implementz(List implementz) { - this.implementz = implementz; + this.implementz = ImmutableList.copyOf(implementz); return this; } - public Builder implementz(Type implementz) { - this.implementz.add(implementz); + public Builder implementz(Type implement) { + this.implementz = ImmutableKit.addToList(implementz, implement); return this; } @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); return this; } public Builder directive(Directive directive) { - this.directives.add(directive); + this.directives = ImmutableKit.addToList(directives, directive); return this; } public Builder fieldDefinitions(List fieldDefinitions) { - this.fieldDefinitions = fieldDefinitions; + this.fieldDefinitions = ImmutableList.copyOf(fieldDefinitions); return this; } public Builder fieldDefinition(FieldDefinition fieldDefinition) { - this.fieldDefinitions.add(fieldDefinition); + this.fieldDefinitions = ImmutableKit.addToList(fieldDefinitions, fieldDefinition); return this; } diff --git a/src/main/java/graphql/language/ObjectValue.java b/src/main/java/graphql/language/ObjectValue.java index 751748b8e7..6c7ee98041 100644 --- a/src/main/java/graphql/language/ObjectValue.java +++ b/src/main/java/graphql/language/ObjectValue.java @@ -8,13 +8,13 @@ import graphql.util.TraversalControl; import graphql.util.TraverserContext; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.function.Consumer; import static graphql.Assert.assertNotNull; +import static graphql.collect.ImmutableKit.emptyList; import static graphql.language.NodeChildrenContainer.newNodeChildrenContainer; @PublicApi @@ -36,7 +36,7 @@ protected ObjectValue(List objectFields, SourceLocation sourceLocat * @param objectFields the list of field that make up this object value */ public ObjectValue(List objectFields) { - this(objectFields, null, ImmutableKit.emptyList(), IgnoredChars.EMPTY, ImmutableKit.emptyMap()); + this(objectFields, null, emptyList(), IgnoredChars.EMPTY, ImmutableKit.emptyMap()); } public List getObjectFields() { @@ -106,8 +106,8 @@ public ObjectValue transform(Consumer builderConsumer) { public static final class Builder implements NodeBuilder { private SourceLocation sourceLocation; - private List objectFields = new ArrayList<>(); - private List comments = new ArrayList<>(); + private ImmutableList objectFields = emptyList(); + private ImmutableList comments = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -116,8 +116,8 @@ private Builder() { private Builder(ObjectValue existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); - this.objectFields = existing.getObjectFields(); + this.comments = ImmutableList.copyOf(existing.getComments()); + this.objectFields = ImmutableList.copyOf(existing.getObjectFields()); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -127,17 +127,17 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder objectFields(List objectFields) { - this.objectFields = objectFields; + this.objectFields = ImmutableList.copyOf(objectFields); return this; } public Builder objectField(ObjectField objectField) { - this.objectFields.add(objectField); + this.objectFields = ImmutableKit.addToList(objectFields, objectField); return this; } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } diff --git a/src/main/java/graphql/language/OperationDefinition.java b/src/main/java/graphql/language/OperationDefinition.java index 3034423d7b..1a7197c88f 100644 --- a/src/main/java/graphql/language/OperationDefinition.java +++ b/src/main/java/graphql/language/OperationDefinition.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; @@ -168,11 +169,11 @@ public OperationDefinition transform(Consumer builderConsumer) { public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; private Operation operation; - private List variableDefinitions = new ArrayList<>(); - private List directives = new ArrayList<>(); + private ImmutableList variableDefinitions = emptyList(); + private ImmutableList directives = emptyList(); private SelectionSet selectionSet; private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -182,11 +183,11 @@ private Builder() { private Builder(OperationDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.operation = existing.getOperation(); - this.variableDefinitions = existing.getVariableDefinitions(); - this.directives = existing.getDirectives(); + this.variableDefinitions = ImmutableList.copyOf(existing.getVariableDefinitions()); + this.directives = ImmutableList.copyOf(existing.getDirectives()); this.selectionSet = existing.getSelectionSet(); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); @@ -199,7 +200,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -214,16 +215,25 @@ public Builder operation(Operation operation) { } public Builder variableDefinitions(List variableDefinitions) { - this.variableDefinitions = variableDefinitions; + this.variableDefinitions = ImmutableList.copyOf(variableDefinitions); + return this; + } + + public Builder variableDefinition(VariableDefinition variableDefinition) { + this.variableDefinitions = ImmutableKit.addToList(variableDefinitions, variableDefinition); return this; } @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); return this; } + public Builder directive(Directive directive) { + this.directives = ImmutableKit.addToList(directives, directive); + return this; + } public Builder selectionSet(SelectionSet selectionSet) { this.selectionSet = selectionSet; return this; diff --git a/src/main/java/graphql/language/OperationTypeDefinition.java b/src/main/java/graphql/language/OperationTypeDefinition.java index 4997bdca0c..5041e692cf 100644 --- a/src/main/java/graphql/language/OperationTypeDefinition.java +++ b/src/main/java/graphql/language/OperationTypeDefinition.java @@ -1,6 +1,7 @@ package graphql.language; +import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; import graphql.util.TraversalControl; @@ -117,7 +118,7 @@ public OperationTypeDefinition transform(Consumer builderConsumer) { public static final class Builder implements NodeBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; private TypeName typeName; private IgnoredChars ignoredChars = IgnoredChars.EMPTY; @@ -129,7 +130,7 @@ private Builder() { private Builder(OperationTypeDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.typeName = existing.getTypeName(); this.ignoredChars = existing.getIgnoredChars(); @@ -143,7 +144,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } diff --git a/src/main/java/graphql/language/ScalarTypeDefinition.java b/src/main/java/graphql/language/ScalarTypeDefinition.java index 774eabb422..ac2d0f427f 100644 --- a/src/main/java/graphql/language/ScalarTypeDefinition.java +++ b/src/main/java/graphql/language/ScalarTypeDefinition.java @@ -4,10 +4,10 @@ import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -122,10 +122,10 @@ public ScalarTypeDefinition transform(Consumer builderConsumer) { public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; private Description description; - private List directives = new ArrayList<>(); + private ImmutableList directives = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -134,10 +134,10 @@ private Builder() { private Builder(ScalarTypeDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.description = existing.getDescription(); - this.directives = existing.getDirectives(); + this.directives = ImmutableList.copyOf(existing.getDirectives()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -149,7 +149,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -165,12 +165,12 @@ public Builder description(Description description) { @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); return this; } public Builder directive(Directive directive) { - this.directives.add(directive); + this.directives = ImmutableKit.addToList(directives, directive); return this; } diff --git a/src/main/java/graphql/language/ScalarTypeExtensionDefinition.java b/src/main/java/graphql/language/ScalarTypeExtensionDefinition.java index 91dbbb27c0..447bd5a616 100644 --- a/src/main/java/graphql/language/ScalarTypeExtensionDefinition.java +++ b/src/main/java/graphql/language/ScalarTypeExtensionDefinition.java @@ -1,15 +1,17 @@ package graphql.language; +import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.function.Consumer; import static graphql.Assert.assertNotNull; +import static graphql.collect.ImmutableKit.emptyList; @PublicApi public class ScalarTypeExtensionDefinition extends ScalarTypeDefinition { @@ -57,23 +59,22 @@ public ScalarTypeExtensionDefinition transformExtension(Consumer builde public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; private Description description; - private List directives = new ArrayList<>(); + private ImmutableList directives = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); private Builder() { } - - private Builder(ScalarTypeExtensionDefinition existing) { + private Builder(ScalarTypeDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.description = existing.getDescription(); - this.directives = existing.getDirectives(); + this.directives = ImmutableList.copyOf(existing.getDirectives()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -85,7 +86,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -101,7 +102,12 @@ public Builder description(Description description) { @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); + return this; + } + + public Builder directive(Directive directive) { + this.directives = ImmutableKit.addToList(directives, directive); return this; } @@ -120,7 +126,6 @@ public Builder additionalData(String key, String value) { return this; } - public ScalarTypeExtensionDefinition build() { return new ScalarTypeExtensionDefinition(name, directives, diff --git a/src/main/java/graphql/language/SchemaDefinition.java b/src/main/java/graphql/language/SchemaDefinition.java index d7047bbaa1..53e5214bf8 100644 --- a/src/main/java/graphql/language/SchemaDefinition.java +++ b/src/main/java/graphql/language/SchemaDefinition.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; @@ -14,6 +15,7 @@ import java.util.function.Consumer; import static graphql.Assert.assertNotNull; +import static graphql.collect.ImmutableKit.emptyList; import static graphql.language.NodeChildrenContainer.newNodeChildrenContainer; import static graphql.language.NodeUtil.directiveByName; import static graphql.language.NodeUtil.directivesByName; @@ -128,9 +130,9 @@ public static Builder newSchemaDefinition() { public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); - private List directives = new ArrayList<>(); - private List operationTypeDefinitions = new ArrayList<>(); + private ImmutableList comments = emptyList(); + private ImmutableList directives = emptyList(); + private ImmutableList operationTypeDefinitions = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); private Description description; @@ -141,9 +143,9 @@ protected Builder() { protected Builder(SchemaDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); - this.directives = existing.getDirectives(); - this.operationTypeDefinitions = existing.getOperationTypeDefinitions(); + this.comments = ImmutableList.copyOf(existing.getComments()); + this.directives = ImmutableList.copyOf(existing.getDirectives()); + this.operationTypeDefinitions = ImmutableList.copyOf(existing.getOperationTypeDefinitions()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); this.description = existing.getDescription(); @@ -160,28 +162,28 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); return this; } public Builder directive(Directive directive) { - this.directives.add(directive); + this.directives = ImmutableKit.addToList(directives, directive); return this; } public Builder operationTypeDefinitions(List operationTypeDefinitions) { - this.operationTypeDefinitions = operationTypeDefinitions; + this.operationTypeDefinitions = ImmutableList.copyOf(operationTypeDefinitions); return this; } - public Builder operationTypeDefinition(OperationTypeDefinition operationTypeDefinitions) { - this.operationTypeDefinitions.add(operationTypeDefinitions); + public Builder operationTypeDefinition(OperationTypeDefinition operationTypeDefinition) { + this.operationTypeDefinitions = ImmutableKit.addToList(operationTypeDefinitions, operationTypeDefinition); return this; } diff --git a/src/main/java/graphql/language/SchemaExtensionDefinition.java b/src/main/java/graphql/language/SchemaExtensionDefinition.java index 1f71ab8590..2ac4917c3d 100644 --- a/src/main/java/graphql/language/SchemaExtensionDefinition.java +++ b/src/main/java/graphql/language/SchemaExtensionDefinition.java @@ -1,6 +1,8 @@ package graphql.language; +import com.google.common.collect.ImmutableList; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -9,6 +11,7 @@ import java.util.function.Consumer; import static graphql.Assert.assertNotNull; +import static graphql.collect.ImmutableKit.emptyList; @PublicApi public class SchemaExtensionDefinition extends SchemaDefinition { @@ -56,20 +59,21 @@ public static Builder newSchemaExtensionDefinition() { public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); - private List directives = new ArrayList<>(); - private List operationTypeDefinitions = new ArrayList<>(); + private ImmutableList comments = emptyList(); + private ImmutableList directives = emptyList(); + private ImmutableList operationTypeDefinitions = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); + protected Builder() { } protected Builder(SchemaDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); - this.directives = existing.getDirectives(); - this.operationTypeDefinitions = existing.getOperationTypeDefinitions(); + this.comments = ImmutableList.copyOf(existing.getComments()); + this.directives = ImmutableList.copyOf(existing.getDirectives()); + this.operationTypeDefinitions = ImmutableList.copyOf(existing.getOperationTypeDefinitions()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -81,28 +85,28 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); return this; } public Builder directive(Directive directive) { - this.directives.add(directive); + this.directives = ImmutableKit.addToList(directives, directive); return this; } public Builder operationTypeDefinitions(List operationTypeDefinitions) { - this.operationTypeDefinitions = operationTypeDefinitions; + this.operationTypeDefinitions = ImmutableList.copyOf(operationTypeDefinitions); return this; } - public Builder operationTypeDefinition(OperationTypeDefinition operationTypeDefinitions) { - this.operationTypeDefinitions.add(operationTypeDefinitions); + public Builder operationTypeDefinition(OperationTypeDefinition operationTypeDefinition) { + this.operationTypeDefinitions = ImmutableKit.addToList(operationTypeDefinitions, operationTypeDefinition); return this; } diff --git a/src/main/java/graphql/language/SelectionSet.java b/src/main/java/graphql/language/SelectionSet.java index bb2381845f..2ff152657c 100644 --- a/src/main/java/graphql/language/SelectionSet.java +++ b/src/main/java/graphql/language/SelectionSet.java @@ -4,10 +4,10 @@ import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; -import java.util.ArrayList; import java.util.Collection; import java.util.LinkedHashMap; import java.util.List; @@ -124,9 +124,9 @@ public SelectionSet transform(Consumer builderConsumer) { public static final class Builder implements NodeBuilder { - private List selections = new ArrayList<>(); + private ImmutableList selections = emptyList(); private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -135,19 +135,19 @@ private Builder() { private Builder(SelectionSet existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); - this.selections = new ArrayList<>(existing.getSelections()); + this.comments = ImmutableList.copyOf(existing.getComments()); + this.selections = ImmutableList.copyOf(existing.getSelections()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } public Builder selections(Collection selections) { - this.selections = new ArrayList<>(selections); + this.selections = ImmutableList.copyOf(selections); return this; } public Builder selection(Selection selection) { - this.selections.add(selection); + this.selections = ImmutableKit.addToList(selections, selection); return this; } @@ -157,7 +157,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } diff --git a/src/main/java/graphql/language/StringValue.java b/src/main/java/graphql/language/StringValue.java index 322082eab3..aa85c3387c 100644 --- a/src/main/java/graphql/language/StringValue.java +++ b/src/main/java/graphql/language/StringValue.java @@ -1,6 +1,7 @@ package graphql.language; +import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; import graphql.util.TraversalControl; @@ -107,7 +108,7 @@ public StringValue transform(Consumer builderConsumer) { public static final class Builder implements NodeBuilder { private SourceLocation sourceLocation; private String value; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -116,7 +117,7 @@ private Builder() { private Builder(StringValue existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.value = existing.getValue(); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); @@ -134,7 +135,7 @@ public Builder value(String value) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -155,8 +156,7 @@ public Builder additionalData(String key, String value) { public StringValue build() { - StringValue stringValue = new StringValue(value, sourceLocation, comments, ignoredChars, additionalData); - return stringValue; + return new StringValue(value, sourceLocation, comments, ignoredChars, additionalData); } } } diff --git a/src/main/java/graphql/language/TypeName.java b/src/main/java/graphql/language/TypeName.java index 2e385c158b..313a0b9abb 100644 --- a/src/main/java/graphql/language/TypeName.java +++ b/src/main/java/graphql/language/TypeName.java @@ -1,6 +1,7 @@ package graphql.language; +import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; import graphql.util.TraversalControl; @@ -109,7 +110,7 @@ public TypeName transform(Consumer builderConsumer) { public static final class Builder implements NodeBuilder { private String name; private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -118,7 +119,7 @@ private Builder() { private Builder(TypeName existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -135,7 +136,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } diff --git a/src/main/java/graphql/language/UnionTypeDefinition.java b/src/main/java/graphql/language/UnionTypeDefinition.java index e11372d86b..00f9e6fc90 100644 --- a/src/main/java/graphql/language/UnionTypeDefinition.java +++ b/src/main/java/graphql/language/UnionTypeDefinition.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; @@ -153,11 +154,11 @@ public UnionTypeDefinition transform(Consumer builderConsumer) { public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; private Description description; - private List directives = new ArrayList<>(); - private List memberTypes = new ArrayList<>(); + private ImmutableList directives = emptyList(); + private ImmutableList memberTypes = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -166,11 +167,11 @@ private Builder() { private Builder(UnionTypeDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.description = existing.getDescription(); - this.directives = existing.getDirectives(); - this.memberTypes = existing.getMemberTypes(); + this.directives = ImmutableList.copyOf(existing.getDirectives()); + this.memberTypes = ImmutableList.copyOf(existing.getMemberTypes()); this.ignoredChars = existing.getIgnoredChars(); } @@ -180,7 +181,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -196,22 +197,22 @@ public Builder description(Description description) { @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); return this; } public Builder directive(Directive directive) { - this.directives.add(directive); + this.directives = ImmutableKit.addToList(directives, directive); return this; } public Builder memberTypes(List memberTypes) { - this.memberTypes = memberTypes; + this.memberTypes = ImmutableList.copyOf(memberTypes); return this; } public Builder memberType(Type memberType) { - this.memberTypes.add(memberType); + this.memberTypes = ImmutableKit.addToList(memberTypes, memberType); return this; } diff --git a/src/main/java/graphql/language/UnionTypeExtensionDefinition.java b/src/main/java/graphql/language/UnionTypeExtensionDefinition.java index e0c4b11f4f..85032e4f07 100644 --- a/src/main/java/graphql/language/UnionTypeExtensionDefinition.java +++ b/src/main/java/graphql/language/UnionTypeExtensionDefinition.java @@ -1,15 +1,17 @@ package graphql.language; +import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.function.Consumer; import static graphql.Assert.assertNotNull; +import static graphql.collect.ImmutableKit.emptyList; @PublicApi public class UnionTypeExtensionDefinition extends UnionTypeDefinition { @@ -73,27 +75,25 @@ public UnionTypeExtensionDefinition transformExtension(Consumer builder public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; private Description description; - private List directives = new ArrayList<>(); - private List memberTypes = new ArrayList<>(); + private ImmutableList directives = emptyList(); + private ImmutableList memberTypes = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); private Builder() { } - - private Builder(UnionTypeExtensionDefinition existing) { + private Builder(UnionTypeDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.description = existing.getDescription(); - this.directives = existing.getDirectives(); - this.memberTypes = existing.getMemberTypes(); + this.directives = ImmutableList.copyOf(existing.getDirectives()); + this.memberTypes = ImmutableList.copyOf(existing.getMemberTypes()); this.ignoredChars = existing.getIgnoredChars(); - this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } public Builder sourceLocation(SourceLocation sourceLocation) { @@ -102,7 +102,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -118,12 +118,22 @@ public Builder description(Description description) { @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); + return this; + } + + public Builder directive(Directive directive) { + this.directives = ImmutableKit.addToList(directives, directive); return this; } public Builder memberTypes(List memberTypes) { - this.memberTypes = memberTypes; + this.memberTypes = ImmutableList.copyOf(memberTypes); + return this; + } + + public Builder memberType(Type memberType) { + this.memberTypes = ImmutableKit.addToList(memberTypes, memberType); return this; } diff --git a/src/main/java/graphql/language/VariableDefinition.java b/src/main/java/graphql/language/VariableDefinition.java index b962338900..16fc258a62 100644 --- a/src/main/java/graphql/language/VariableDefinition.java +++ b/src/main/java/graphql/language/VariableDefinition.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; @@ -185,10 +186,10 @@ public VariableDefinition transform(Consumer builderConsumer) { public static final class Builder implements NodeDirectivesBuilder { private SourceLocation sourceLocation; private String name; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private Type type; private Value defaultValue; - private List directives = new ArrayList<>(); + private ImmutableList directives = emptyList(); private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -197,11 +198,11 @@ private Builder() { private Builder(VariableDefinition existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.type = existing.getType(); this.defaultValue = existing.getDefaultValue(); - this.directives = existing.getDirectives(); + this.directives = ImmutableList.copyOf(existing.getDirectives()); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); } @@ -217,7 +218,7 @@ public Builder name(String name) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } @@ -233,12 +234,12 @@ public Builder defaultValue(Value defaultValue) { @Override public Builder directives(List directives) { - this.directives = directives; + this.directives = ImmutableList.copyOf(directives); return this; } public Builder directive(Directive directive) { - this.directives.add(directive); + this.directives = ImmutableKit.addToList(directives, directive); return this; } diff --git a/src/main/java/graphql/language/VariableReference.java b/src/main/java/graphql/language/VariableReference.java index 775ff9ca1c..14555402bb 100644 --- a/src/main/java/graphql/language/VariableReference.java +++ b/src/main/java/graphql/language/VariableReference.java @@ -1,6 +1,7 @@ package graphql.language; +import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; import graphql.util.TraversalControl; @@ -103,7 +104,7 @@ public VariableReference transform(Consumer builderConsumer) { public static final class Builder implements NodeBuilder { private SourceLocation sourceLocation; - private List comments = new ArrayList<>(); + private ImmutableList comments = emptyList(); private String name; private IgnoredChars ignoredChars = IgnoredChars.EMPTY; private Map additionalData = new LinkedHashMap<>(); @@ -113,7 +114,7 @@ private Builder() { private Builder(VariableReference existing) { this.sourceLocation = existing.getSourceLocation(); - this.comments = existing.getComments(); + this.comments = ImmutableList.copyOf(existing.getComments()); this.name = existing.getName(); this.ignoredChars = existing.getIgnoredChars(); this.additionalData = new LinkedHashMap<>(existing.getAdditionalData()); @@ -125,7 +126,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) { } public Builder comments(List comments) { - this.comments = comments; + this.comments = ImmutableList.copyOf(comments); return this; } diff --git a/src/main/java/graphql/parser/GraphqlAntlrToLanguage.java b/src/main/java/graphql/parser/GraphqlAntlrToLanguage.java index d2e37c502c..3f66b45916 100644 --- a/src/main/java/graphql/parser/GraphqlAntlrToLanguage.java +++ b/src/main/java/graphql/parser/GraphqlAntlrToLanguage.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList; import graphql.Assert; import graphql.Internal; +import graphql.collect.ImmutableKit; import graphql.language.Argument; import graphql.language.ArrayValue; import graphql.language.BooleanValue; @@ -852,12 +853,12 @@ protected List getComments(ParserRuleContext ctx) { return getCommentOnChannel(refChannel); } } - return Collections.emptyList(); + return ImmutableKit.emptyList(); } protected List getCommentOnChannel(List refChannel) { - List comments = new ArrayList<>(); + ImmutableList.Builder comments = ImmutableList.builder(); for (Token refTok : refChannel) { String text = refTok.getText(); // we strip the leading hash # character but we don't trim because we don't @@ -873,7 +874,7 @@ protected List getCommentOnChannel(List refChannel) { int line = sourceAndLine.getLine() + 1; comments.add(new Comment(text, new SourceLocation(line, column, sourceAndLine.getSourceName()))); } - return ImmutableList.copyOf(comments); + return comments.build(); } diff --git a/src/test/groovy/graphql/collect/ImmutableKitTest.groovy b/src/test/groovy/graphql/collect/ImmutableKitTest.groovy index 89332bb428..2c8cdcefd2 100644 --- a/src/test/groovy/graphql/collect/ImmutableKitTest.groovy +++ b/src/test/groovy/graphql/collect/ImmutableKitTest.groovy @@ -28,4 +28,18 @@ class ImmutableKitTest extends Specification { outputList == ["kciuq", "nworb", "xof"] outputList instanceof ImmutableList } + + def "can add to lists"() { + def list = ["a"] + + when: + list = ImmutableKit.addToList(list, "b") + then: + list == ["a", "b"] + + when: + list = ImmutableKit.addToList(list, "c", "d", "e") + then: + list == ["a", "b", "c", "d", "e"] + } } From cd88aeea5926fd70f3b79a2a3911de8d897654c5 Mon Sep 17 00:00:00 2001 From: Brad Baker Date: Thu, 12 Nov 2020 09:40:19 +1100 Subject: [PATCH 2/2] Missing `directive` method --- src/main/java/graphql/language/EnumValueDefinition.java | 7 ++++++- src/main/java/graphql/language/Field.java | 6 ++++++ src/main/java/graphql/language/NodeDirectivesBuilder.java | 3 +++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/graphql/language/EnumValueDefinition.java b/src/main/java/graphql/language/EnumValueDefinition.java index 93a0d066f0..2a7ef13f10 100644 --- a/src/main/java/graphql/language/EnumValueDefinition.java +++ b/src/main/java/graphql/language/EnumValueDefinition.java @@ -4,10 +4,10 @@ import com.google.common.collect.ImmutableList; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; -import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -178,6 +178,11 @@ public Builder directives(List directives) { return this; } + public Builder directive(Directive directive) { + this.directives = ImmutableKit.addToList(directives, directive); + return this; + } + public Builder ignoredChars(IgnoredChars ignoredChars) { this.ignoredChars = ignoredChars; return this; diff --git a/src/main/java/graphql/language/Field.java b/src/main/java/graphql/language/Field.java index 0803f58e9a..0f1e8d3c69 100644 --- a/src/main/java/graphql/language/Field.java +++ b/src/main/java/graphql/language/Field.java @@ -5,6 +5,7 @@ import com.google.common.collect.ImmutableMap; import graphql.Internal; import graphql.PublicApi; +import graphql.collect.ImmutableKit; import graphql.util.TraversalControl; import graphql.util.TraverserContext; @@ -270,6 +271,11 @@ public Builder directives(List directives) { return this; } + public Builder directive(Directive directive) { + this.directives = ImmutableKit.addToList(directives, directive); + return this; + } + public Builder selectionSet(SelectionSet selectionSet) { this.selectionSet = selectionSet; return this; diff --git a/src/main/java/graphql/language/NodeDirectivesBuilder.java b/src/main/java/graphql/language/NodeDirectivesBuilder.java index ebbd5b8d08..d7a7ee7214 100644 --- a/src/main/java/graphql/language/NodeDirectivesBuilder.java +++ b/src/main/java/graphql/language/NodeDirectivesBuilder.java @@ -9,4 +9,7 @@ public interface NodeDirectivesBuilder extends NodeBuilder { NodeDirectivesBuilder directives(List directives); + NodeDirectivesBuilder directive(Directive directive); + + }