-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathNodeZipper.java
More file actions
150 lines (124 loc) · 5.08 KB
/
NodeZipper.java
File metadata and controls
150 lines (124 loc) · 5.08 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package graphql.util;
import com.google.common.collect.ImmutableList;
import graphql.PublicApi;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import static graphql.Assert.assertNotNull;
@PublicApi
public class NodeZipper<T> {
public enum ModificationType {
REPLACE,
DELETE,
INSERT_AFTER,
INSERT_BEFORE
}
private final T curNode;
private final NodeAdapter<T> nodeAdapter;
// reverse: the breadCrumbs start from curNode upwards
private final List<Breadcrumb<T>> breadcrumbs;
private final ModificationType modificationType;
public NodeZipper(T curNode, List<Breadcrumb<T>> breadcrumbs, NodeAdapter<T> nodeAdapter) {
this(curNode, breadcrumbs, nodeAdapter, ModificationType.REPLACE);
}
public NodeZipper(T curNode, List<Breadcrumb<T>> breadcrumbs, NodeAdapter<T> nodeAdapter, ModificationType modificationType) {
this.curNode = assertNotNull(curNode);
this.breadcrumbs = ImmutableList.copyOf(assertNotNull(breadcrumbs));
this.nodeAdapter = nodeAdapter;
this.modificationType = modificationType;
}
public ModificationType getModificationType() {
return modificationType;
}
public T getCurNode() {
return curNode;
}
public List<Breadcrumb<T>> getBreadcrumbs() {
return breadcrumbs;
}
public T getParent() {
return breadcrumbs.get(0).getNode();
}
public static <T> NodeZipper<T> rootZipper(T rootNode, NodeAdapter<T> nodeAdapter) {
return new NodeZipper<T>(rootNode, new ArrayList<>(), nodeAdapter);
}
public NodeZipper<T> modifyNode(Function<T, T> transform) {
return new NodeZipper<T>(transform.apply(curNode), breadcrumbs, nodeAdapter, this.modificationType);
}
public NodeZipper<T> deleteNode() {
return new NodeZipper<T>(this.curNode, breadcrumbs, nodeAdapter, ModificationType.DELETE);
}
public NodeZipper<T> insertAfter(T toInsertAfter) {
return new NodeZipper<T>(toInsertAfter, breadcrumbs, nodeAdapter, ModificationType.INSERT_AFTER);
}
public NodeZipper<T> insertBefore(T toInsertBefore) {
return new NodeZipper<T>(toInsertBefore, breadcrumbs, nodeAdapter, ModificationType.INSERT_BEFORE);
}
public NodeZipper<T> withNewNode(T newNode) {
return new NodeZipper<T>(newNode, breadcrumbs, nodeAdapter, this.modificationType);
}
public NodeZipper<T> moveUp() {
T node = getParent();
List<Breadcrumb<T>> newBreadcrumbs = breadcrumbs.subList(1, breadcrumbs.size());
return new NodeZipper<>(node, newBreadcrumbs, nodeAdapter, this.modificationType);
}
/**
* @return null if it is the root node and marked as deleted, otherwise never null
*/
public T toRoot() {
if (breadcrumbs.size() == 0 && modificationType != ModificationType.DELETE) {
return this.curNode;
}
if (breadcrumbs.size() == 0 && modificationType == ModificationType.DELETE) {
return null;
}
T curNode = this.curNode;
Breadcrumb<T> firstBreadcrumb = breadcrumbs.get(0);
Map<String, List<T>> childrenForParent = new HashMap<>(nodeAdapter.getNamedChildren(firstBreadcrumb.getNode()));
NodeLocation locationInParent = firstBreadcrumb.getLocation();
int ix = locationInParent.getIndex();
String name = locationInParent.getName();
List<T> childList = new ArrayList<>(childrenForParent.get(name));
switch (modificationType) {
case REPLACE:
childList.set(ix, curNode);
break;
case DELETE:
childList.remove(ix);
break;
case INSERT_BEFORE:
childList.add(ix, curNode);
break;
case INSERT_AFTER:
childList.add(ix + 1, curNode);
break;
}
childrenForParent.put(name, childList);
curNode = nodeAdapter.withNewChildren(firstBreadcrumb.getNode(), childrenForParent);
if (breadcrumbs.size() == 1) {
return curNode;
}
for (Breadcrumb<T> breadcrumb : breadcrumbs.subList(1, breadcrumbs.size())) {
// just handle replace
Map<String, List<T>> newChildren = new LinkedHashMap<>(nodeAdapter.getNamedChildren(breadcrumb.getNode()));
final T newChild = curNode;
NodeLocation location = breadcrumb.getLocation();
List<T> list = new ArrayList<>(newChildren.get(location.getName()));
list.set(location.getIndex(), newChild);
newChildren.put(location.getName(), list);
curNode = nodeAdapter.withNewChildren(breadcrumb.getNode(), newChildren);
}
return curNode;
}
@Override
public String toString() {
return "NodeZipper{" +
"curNode=" + curNode +
", breadcrumbs.size=" + breadcrumbs.size() +
", modificationType=" + modificationType +
'}';
}
}