Skip to content

Commit bacddba

Browse files
committed
Deep copy of language nodes
1 parent f0a6c24 commit bacddba

47 files changed

Lines changed: 515 additions & 200 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/main/java/graphql/language/AbstractNode.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
import java.util.Collections;
55
import java.util.List;
6+
import java.util.function.Function;
7+
import java.util.stream.Collectors;
68

79
import static graphql.Assert.assertNotNull;
810

9-
public abstract class AbstractNode implements Node {
11+
public abstract class AbstractNode<T extends Node> implements Node<T> {
1012

1113
private SourceLocation sourceLocation;
1214
private List<Comment> comments = Collections.emptyList();
@@ -29,4 +31,31 @@ public void setComments(List<Comment> comments) {
2931
assertNotNull(comments, "You must provide non null comments");
3032
this.comments = comments;
3133
}
34+
35+
protected boolean isEqualTo(String thisStr, String thatStr) {
36+
if (null == thisStr) {
37+
if (null != thatStr) {
38+
return false;
39+
}
40+
} else if (!thisStr.equals(thatStr)) {
41+
return false;
42+
}
43+
return true;
44+
}
45+
46+
protected <V> V deepCopy(V nullableObj, Function<V, V> copyFunction) {
47+
if (nullableObj == null) {
48+
return null;
49+
}
50+
return copyFunction.apply(nullableObj);
51+
}
52+
53+
@SuppressWarnings("unchecked")
54+
protected <V extends Node> List<V> deepCopy(List<? extends Node> list) {
55+
if (list == null) {
56+
return null;
57+
}
58+
return list.stream().map(Node::deepCopy).map(node -> (V) node).collect(Collectors.toList());
59+
}
60+
3261
}

src/main/java/graphql/language/Argument.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.ArrayList;
55
import java.util.List;
66

7-
public class Argument extends AbstractNode {
7+
public class Argument extends AbstractNode<Argument> {
88

99
private final String name;
1010
private final Value value;
@@ -35,12 +35,16 @@ public boolean isEqualTo(Node o) {
3535
if (this == o) return true;
3636
if (o == null || getClass() != o.getClass()) return false;
3737

38-
Argument argument = (Argument) o;
38+
Argument that = (Argument) o;
3939

40-
return !(name != null ? !name.equals(argument.name) : argument.name != null);
40+
return isEqualTo(this.name, that.name);
4141

4242
}
4343

44+
@Override
45+
public Argument deepCopy() {
46+
return new Argument(name, deepCopy(value, Value::deepCopy));
47+
}
4448

4549
@Override
4650
public String toString() {

src/main/java/graphql/language/ArrayValue.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import java.util.ArrayList;
55
import java.util.List;
66

7-
public class ArrayValue extends AbstractNode implements Value {
7+
public class ArrayValue extends AbstractNode<ArrayValue> implements Value<ArrayValue> {
88

99
private List<Value> values = new ArrayList<>();
1010

1111
public ArrayValue() {
12+
this(new ArrayList<>());
1213
}
1314

1415
public ArrayValue(List<Value> values) {
@@ -43,4 +44,9 @@ public String toString() {
4344
"values=" + values +
4445
'}';
4546
}
47+
48+
@Override
49+
public ArrayValue deepCopy() {
50+
return new ArrayValue(deepCopy(values));
51+
}
4652
}

src/main/java/graphql/language/BooleanValue.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.ArrayList;
55
import java.util.List;
66

7-
public class BooleanValue extends AbstractNode implements Value {
7+
public class BooleanValue extends AbstractNode<BooleanValue> implements Value<BooleanValue> {
88

99
private boolean value;
1010

@@ -37,6 +37,11 @@ public boolean isEqualTo(Node o) {
3737

3838
}
3939

40+
@Override
41+
public BooleanValue deepCopy() {
42+
return new BooleanValue(value);
43+
}
44+
4045
@Override
4146
public String toString() {
4247
return "BooleanValue{" +
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package graphql.language;
22

33

4-
public interface Definition extends Node {
4+
public interface Definition<T extends Definition> extends Node<T> {
5+
6+
/**
7+
* @return a deep copy of this definition
8+
*/
9+
T deepCopy();
510
}

src/main/java/graphql/language/Directive.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import static graphql.language.NodeUtil.argumentsByName;
1010

11-
public class Directive extends AbstractNode {
11+
public class Directive extends AbstractNode<Directive> {
1212
private final String name;
1313
private final List<Argument> arguments = new ArrayList<>();
1414

@@ -49,12 +49,17 @@ public boolean isEqualTo(Node o) {
4949
if (this == o) return true;
5050
if (o == null || getClass() != o.getClass()) return false;
5151

52-
Directive directive = (Directive) o;
52+
Directive that = (Directive) o;
5353

54-
return !(name != null ? !name.equals(directive.name) : directive.name != null);
54+
return isEqualTo(this.name, that.name);
5555

5656
}
5757

58+
@Override
59+
public Directive deepCopy() {
60+
return new Directive(name, deepCopy(arguments));
61+
}
62+
5863
@Override
5964
public String toString() {
6065
return "Directive{" +

src/main/java/graphql/language/DirectiveDefinition.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44
import java.util.ArrayList;
55
import java.util.List;
66

7-
public class DirectiveDefinition extends AbstractNode implements Definition {
7+
public class DirectiveDefinition extends AbstractNode<DirectiveDefinition> implements Definition<DirectiveDefinition> {
88
private final String name;
9-
private final List<InputValueDefinition> inputValueDefinitions = new ArrayList<>();
10-
private final List<DirectiveLocation> directiveLocations = new ArrayList<>();
9+
private final List<InputValueDefinition> inputValueDefinitions;
10+
private final List<DirectiveLocation> directiveLocations;
1111

1212
public DirectiveDefinition(String name) {
13+
this(name, new ArrayList<>(), new ArrayList<>());
14+
}
15+
16+
public DirectiveDefinition(String name, List<InputValueDefinition> inputValueDefinitions, List<DirectiveLocation> directiveLocations) {
1317
this.name = name;
18+
this.inputValueDefinitions = inputValueDefinitions;
19+
this.directiveLocations = directiveLocations;
1420
}
1521

1622
public String getName() {
@@ -40,14 +46,15 @@ public boolean isEqualTo(Node o) {
4046

4147
DirectiveDefinition that = (DirectiveDefinition) o;
4248

43-
if (null == name) {
44-
if (null != that.name) return false;
45-
} else if (!name.equals(that.name)) {
46-
return false;
47-
}
48-
return true;
49+
return isEqualTo(this.name, that.name);
4950
}
5051

52+
@Override
53+
public DirectiveDefinition deepCopy() {
54+
return new DirectiveDefinition(name,
55+
deepCopy(inputValueDefinitions),
56+
deepCopy(directiveLocations));
57+
}
5158

5259
@Override
5360
public String toString() {

src/main/java/graphql/language/DirectiveLocation.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// FRAGMENT_DEFINITION
1313
// FRAGMENT_SPREAD
1414
// INLINE_FRAGMENT
15-
public class DirectiveLocation extends AbstractNode {
15+
public class DirectiveLocation extends AbstractNode<DirectiveLocation> {
1616
private final String name;
1717

1818
public DirectiveLocation(String name) {
@@ -25,8 +25,7 @@ public String getName() {
2525

2626
@Override
2727
public List<Node> getChildren() {
28-
List<Node> result = new ArrayList<>();
29-
return result;
28+
return new ArrayList<>();
3029
}
3130

3231
@Override
@@ -36,14 +35,13 @@ public boolean isEqualTo(Node o) {
3635

3736
DirectiveLocation that = (DirectiveLocation) o;
3837

39-
if ( null == name ) {
40-
if ( null != that.name ) return false;
41-
} else if ( !name.equals(that.name) ) {
42-
return false;
43-
}
44-
return true;
38+
return isEqualTo(this.name,that.name);
4539
}
4640

41+
@Override
42+
public DirectiveLocation deepCopy() {
43+
return new DirectiveLocation(name);
44+
}
4745

4846
@Override
4947
public String toString() {

src/main/java/graphql/language/Document.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import java.util.ArrayList;
55
import java.util.List;
66

7-
public class Document extends AbstractNode {
7+
public class Document extends AbstractNode<Document> {
88

9-
private List<Definition> definitions = new ArrayList<>();
9+
private List<Definition> definitions;
1010

1111
public Document() {
12-
12+
this(new ArrayList<>());
1313
}
1414

1515
public Document(List<Definition> definitions) {
@@ -36,11 +36,13 @@ public boolean isEqualTo(Node o) {
3636
if (this == o) return true;
3737
if (o == null || getClass() != o.getClass()) return false;
3838

39-
4039
return true;
41-
4240
}
4341

42+
@Override
43+
public Document deepCopy() {
44+
return new Document(deepCopy(definitions));
45+
}
4446

4547
@Override
4648
public String toString() {

src/main/java/graphql/language/EnumTypeDefinition.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import static graphql.language.NodeUtil.directivesByName;
88

9-
public class EnumTypeDefinition extends AbstractNode implements TypeDefinition {
9+
public class EnumTypeDefinition extends AbstractNode<EnumTypeDefinition> implements TypeDefinition<EnumTypeDefinition> {
1010
private final String name;
1111
private final List<EnumValueDefinition> enumValueDefinitions;
1212
private final List<Directive> directives;
@@ -16,9 +16,13 @@ public EnumTypeDefinition(String name) {
1616
}
1717

1818
public EnumTypeDefinition(String name, List<Directive> directives) {
19+
this(name, new ArrayList<>(), directives);
20+
}
21+
22+
public EnumTypeDefinition(String name, List<EnumValueDefinition> enumValueDefinitions, List<Directive> directives) {
1923
this.name = name;
2024
this.directives = (null == directives) ? new ArrayList<>() : directives;
21-
this.enumValueDefinitions = new ArrayList<>();
25+
this.enumValueDefinitions = enumValueDefinitions;
2226
}
2327

2428
public List<EnumValueDefinition> getEnumValueDefinitions() {
@@ -58,15 +62,16 @@ public boolean isEqualTo(Node o) {
5862

5963
EnumTypeDefinition that = (EnumTypeDefinition) o;
6064

61-
if (null == name) {
62-
if (null != that.name) return false;
63-
} else if (!name.equals(that.name)) {
64-
return false;
65-
}
66-
return true;
67-
65+
return isEqualTo(this.name, that.name);
6866
}
6967

68+
@Override
69+
public EnumTypeDefinition deepCopy() {
70+
return new EnumTypeDefinition(name,
71+
deepCopy(enumValueDefinitions),
72+
deepCopy(directives)
73+
);
74+
}
7075

7176
@Override
7277
public String toString() {

0 commit comments

Comments
 (0)