forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffCtx.java
More file actions
66 lines (56 loc) · 1.98 KB
/
DiffCtx.java
File metadata and controls
66 lines (56 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package graphql.schema.diff;
import graphql.Internal;
import graphql.language.Document;
import graphql.language.Type;
import graphql.language.TypeDefinition;
import graphql.schema.diff.reporting.DifferenceReporter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Stack;
/*
* A helper class that represents diff state (eg visited types) as well as helpers
*/
@Internal
class DiffCtx {
final List<String> examinedTypes = new ArrayList<>();
final Stack<String> currentTypes = new Stack<>();
private final DifferenceReporter reporter;
final Document oldDoc;
final Document newDoc;
DiffCtx(DifferenceReporter reporter, Document oldDoc, Document newDoc) {
this.reporter = reporter;
this.oldDoc = oldDoc;
this.newDoc = newDoc;
}
void report(DiffEvent differenceEvent) {
reporter.report(differenceEvent);
}
boolean examiningType(String typeName) {
if (examinedTypes.contains(typeName)) {
return true;
}
examinedTypes.add(typeName);
currentTypes.push(typeName);
return false;
}
void exitType() {
currentTypes.pop();
}
<T extends TypeDefinition> Optional<T> getOldTypeDef(Type type, Class<T> typeDefClass) {
return getType(SchemaDiff.getTypeName(type), typeDefClass, oldDoc);
}
<T extends TypeDefinition> Optional<T> getNewTypeDef(Type type, Class<T> typeDefClass) {
return getType(SchemaDiff.getTypeName(type), typeDefClass, newDoc);
}
private <T extends TypeDefinition> Optional<T> getType(String typeName, Class<T> typeDefClass, Document doc) {
if (typeName == null) {
return Optional.empty();
}
return doc.getDefinitions().stream()
.filter(def -> typeDefClass.isAssignableFrom(def.getClass()))
.map(typeDefClass::cast)
.filter(defT -> defT.getName().equals(typeName))
.findFirst();
}
}