8000 This moves the AST element builder to use immutable lists by bbakerman · Pull Request #2101 · graphql-java/graphql-java · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions src/main/java/graphql/collect/ImmutableKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import graphql.Assert;
import graphql.Internal;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import static graphql.Assert.assertNotNull;

@Internal
public final class ImmutableKit {

Expand Down Expand Up @@ -40,7 +41,7 @@ public static <K, V> ImmutableMap<K, V> emptyMap() {
*/

public static <K, V> ImmutableMap<K, ImmutableList<V>> toImmutableMapOfLists(Map<K, List<V>> startingMap) {
Assert.assertNotNull(startingMap);
assertNotNull(startingMap);
ImmutableMap.Builder<K, ImmutableList<V>> map = ImmutableMap.builder();
for (Map.Entry<K, List<V>> e : startingMap.entrySet()) {
ImmutableList<V> value = ImmutableList.copyOf(startingMap.getOrDefault(e.getKey(), emptyList()));
Expand All @@ -66,22 +67,46 @@ public static <T> ImmutableList<T> concatLists(List<T> l1, List<T> 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 <T> for two
* @param <R> for result
* @param iterable the iterable to map
* @param mapper the mapper function
* @param <T> for two
* @param <R> for result
*
* @return a map immutable list of results
*/
public static <T, R> ImmutableList<R> map(Collection<T> collection, Function<? super T, ? extends R> mapper) {
Assert.assertNotNull(collection);
Assert.assertNotNull(mapper);
public static <T, R> ImmutableList<R> map(Iterable<? extends T> iterable, Function<? super T, ? extends R> mapper) {
assertNotNull(iterable);
assertNotNull(mapper);
@SuppressWarnings("RedundantTypeArguments")
ImmutableList.Builder<R> builder = ImmutableList.<R>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 <T> for two
*
* @return an Immutable list with the extra effort.
*/
public static <T> ImmutableList<T> addToList(Collection<? extends T> existing, T newValue, T... extraValues) {
assertNotNull(existing);
assertNotNull(newValue);
int expectedSize = existing.size() + 1 + extraValues.length;
ImmutableList.Builder<T> newList = ImmutableList.builderWithExpectedSize(expectedSize);
newList.addAll(existing);
newList.add(newValue);
for (T extraValue : extraValues) {
newList.add(extraValue);
}
return newList.build();
}

}
26 changes: 12 additions & 14 deletions src/main/java/graphql/language/Argument.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,11 +21,10 @@
@PublicApi
public class Argument extends AbstractNode<Argument> implements NamedNode<Argument> {

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<Comment> comments, IgnoredChars ignoredChars, Map<String, String> additionalData) {
super(sourceLocation, comments, ignoredChars, additionalData);
Expand All @@ -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;
Expand Down Expand Up @@ -105,14 +111,6 @@ public TraversalControl accept(TraverserContext<Node> 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<Builder> builderConsumer) {
Builder builder = new Builder(this);
builderConsumer.accept(builder);
Expand All @@ -121,7 +119,7 @@ public Argument transform(Consumer<Builder> builderConsumer) {

public static final class Builder implements NodeBuilder {
private SourceLocation sourceLocation;
private List<Comment> comments = new ArrayList<>();
private ImmutableList<Comment> comments = emptyList();
private String name;
private Value value;
private IgnoredChars ignoredChars = IgnoredChars.EMPTY;
Expand All @@ -132,7 +130,7 @@ private Builder() {

private Buil 4D8B der(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();
Expand All @@ -155,7 +153,7 @@ public Builder value(Value value) {
}

public Builder comments(List<Comment> comments) {
this.comments = comments;
this.comments = ImmutableList.copyOf(comments);
return this;
}

Expand Down
27 changes: 13 additions & 14 deletions src/main/java/graphql/language/ArrayValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,9 +21,8 @@
@PublicApi
public class ArrayValue extends AbstractNode<ArrayValue> implements Value<ArrayValue> {

private final ImmutableList<Value> values;

public static final String CHILD_VALUES = "values";
private final ImmutableList<Value> values;

@Internal
protected ArrayValue(List<Value> values, SourceLocation sourceLocation, List<Comment> comments, IgnoredChars ignoredChars, Map<String, String> additionalData) {
Expand All @@ -40,6 +39,10 @@ public ArrayValue(List<Value> values) {
this(values, null, emptyList(), IgnoredChars.EMPTY, emptyMap());
}

public static Builder newArrayValue() {
return new Builder();
}

public List<Value> getValues() {
return values;
}
Expand Down Expand Up @@ -92,10 +95,6 @@ public TraversalControl accept(TraverserContext<Node> context, NodeVisitor visit
return visitor.visitArrayValue(this, context);
}

public static Builder newArrayValue() {
return new Builder();
}

public ArrayValue transform(Consumer<Builder> builderConsumer) {
Builder builder = new Builder(this);
builderConsumer.accept(builder);
Expand All @@ -104,8 +103,8 @@ public ArrayValue transform(Consumer<Builder> builderConsumer) {

public static final class Builder implements NodeBuilder {
private SourceLocation sourceLocation;
private List<Value> values = new ArrayList<>();
private List<Comment> comments = new ArrayList<>();
private ImmutableList&l 4D8B t;Value> values = emptyList();
private ImmutableList<Comment> comments = emptyList();
private IgnoredChars ignoredChars = IgnoredChars.EMPTY;
private Map<String, String> additionalData = new LinkedHashMap<>();

Expand All @@ -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());
}
Expand All @@ -126,17 +125,17 @@ public Builder sourceLocation(SourceLocation sourceLocation) {
}

public Builder values(List<Value> 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<Comment> comments) {
this.comments = comments;
this.comments = ImmutableList.copyOf(comments);
return this;
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/graphql/language/BooleanValue.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -108,7 +108,7 @@ public BooleanValue transform(Consumer<Builder> builderConsumer) {
public static final class Builder implements NodeBuilder {
private SourceLocation sourceLocation;
private boolean value;
private List<Comment> comments = new ArrayList<>();
private ImmutableList<Comment> comments = emptyList();
private IgnoredChars ignoredChars = IgnoredChars.EMPTY;
private Map<String, String> additionalData = new LinkedHashMap<>();

Expand All @@ -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());
Expand All @@ -135,7 +135,7 @@ public Builder value(boolean value) {
}

public Builder comments(List<Comment> comments) {
this.comments = comments;
this.comments = ImmutableList.copyOf(comments);
return this;
}

Expand Down
23 changes: 14 additions & 9 deletions src/main/java/graphql/language/Directive.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
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;
import java.util.Objects;
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<Directive> implements NamedNode<Directive> {
Expand Down Expand Up @@ -138,9 +138,9 @@ public Directive transform(Consumer<Builder> builderConsumer) {

public static final class Builder implements NodeBuilder {
private SourceLocation sourceLocation;
private List<Comment> comments = new ArrayList<>();
private ImmutableList<Comment> comments = emptyList();
private String name;
private List<Argument> arguments = new ArrayList<>();
private ImmutableList<Argument> arguments = emptyList();
private IgnoredChars ignoredChars = IgnoredChars.EMPTY;
private Map<String, String> additionalData = new LinkedHashMap<>();

Expand All @@ -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());
}
Expand All @@ -163,7 +163,7 @@ public Builder sourceLocation(SourceLocation sourceLocation) {
}

public Builder comments(List<Comment> comments) {
this.comments = comments;
this.comments = ImmutableList.copyOf(comments);
return this;
}

Expand All @@ -173,7 +173,12 @@ public Builder name(String name) {
}

public Builder arguments(List<Argument> arguments) {
this.arguments = arguments;
this.arguments = ImmutableList.copyOf(arguments);
return this;
}

public Builder argument(Argument argument) {
this.arguments = ImmutableKit.addToList(arguments,argument);
return this;
}

Expand Down
Loading
0