OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE.md file. | |
4 | |
5 // Generated file. Do not edit. | |
6 | |
7 package immi; | |
8 | |
9 import java.util.ArrayList; | |
10 import java.util.Collections; | |
11 import java.util.List; | |
12 | |
13 import dartino.ListPatchData; | |
14 import dartino.ListRegionData; | |
15 import dartino.ListRegionDataList; | |
16 import dartino.NodeDataList; | |
17 import dartino.NodePatchDataList; | |
18 | |
19 public final class ListPatch<N extends Node> implements Patch { | |
20 | |
21 // Public interface. | |
22 | |
23 @Override | |
24 public boolean hasChanged() { return changed; } | |
25 | |
26 public List<N> getCurrent() { return current; } | |
27 public List<N> getPrevious() { return previous; } | |
28 | |
29 public List<RegionPatch> getRegions() { return regions; } | |
30 | |
31 public static abstract class RegionPatch { | |
32 public int getIndex() { return index; } | |
33 public abstract int getCount(); | |
34 public boolean isRemove() { return false; } | |
35 public boolean isInsert() { return false; } | |
36 public boolean isUpdate() { return false; } | |
37 | |
38 RegionPatch(int index) { | |
39 this.index = index; | |
40 } | |
41 | |
42 static RegionPatch fromData( | |
43 ListRegionData data, | |
44 Type type, | |
45 List<Node> previous, | |
46 ImmiRoot root) { | |
47 if (data.isRemove()) return new RemovePatch(data); | |
48 if (data.isInsert()) return new InsertPatch(data, type, root); | |
49 assert data.isUpdate(); | |
50 return new UpdatePatch(data, type, previous, root); | |
51 } | |
52 | |
53 // Returns the change in size resulting from this region patch. | |
54 abstract int delta(); | |
55 | |
56 // Applies a region patch to the output list and returns the index increment | |
57 // of the input list. | |
58 abstract int apply(List<Node> output); | |
59 | |
60 private int index; | |
61 } | |
62 | |
63 public static class RemovePatch extends RegionPatch { | |
64 @Override public int getCount() { return count; } | |
65 @Override public boolean isRemove() { return false; } | |
66 | |
67 int delta() { return -count; } | |
68 | |
69 int apply(List<Node> output) { return count; } | |
70 | |
71 private RemovePatch(ListRegionData data) { | |
72 super(data.getIndex()); | |
73 count = data.getRemove(); | |
74 } | |
75 | |
76 private int count; | |
77 } | |
78 | |
79 public static class InsertPatch extends RegionPatch { | |
80 @Override public int getCount() { return nodes.size(); } | |
81 @Override public boolean isInsert() { return true; } | |
82 | |
83 public List<Node> getNodes() { return nodes; } | |
84 | |
85 int delta() { return nodes.size(); } | |
86 | |
87 int apply(List<Node> output) { | |
88 output.addAll(nodes); | |
89 return 0; | |
90 } | |
91 | |
92 private InsertPatch(ListRegionData data, Type type, ImmiRoot root) { | |
93 super(data.getIndex()); | |
94 NodeDataList insertData = data.getInsert(); | |
95 int length = insertData.size(); | |
96 List<Node> mutableNodes = new ArrayList<Node>(length); | |
97 if (type == Type.ANY_NODE) { | |
98 for (int i = 0; i < length; ++i) { | |
99 mutableNodes.add(new AnyNode(insertData.get(i), root)); | |
100 } | |
101 } else { | |
102 assert type == Type.SPECIFIC_NODE; | |
103 for (int i = 0; i < length; ++i) { | |
104 mutableNodes.add(AnyNode.fromData(insertData.get(i), root)); | |
105 } | |
106 } | |
107 nodes = Collections.unmodifiableList(mutableNodes); | |
108 } | |
109 | |
110 private List<Node> nodes; | |
111 } | |
112 | |
113 public static class UpdatePatch extends RegionPatch { | |
114 @Override public int getCount() { return updates.size(); } | |
115 @Override public boolean isUpdate() { return true; } | |
116 | |
117 public List<NodePatch> getUpdates() { return updates; } | |
118 | |
119 int delta() { return 0; } | |
120 | |
121 int apply(List<Node> output) { | |
122 int length = updates.size(); | |
123 for (int i = 0; i < length; ++i) { | |
124 output.add(updates.get(i).getCurrent()); | |
125 } | |
126 return length; | |
127 } | |
128 | |
129 private UpdatePatch( | |
130 ListRegionData data, | |
131 Type type, | |
132 List<Node> previous, | |
133 ImmiRoot root) { | |
134 super(data.getIndex()); | |
135 NodePatchDataList updateData = data.getUpdate(); | |
136 int length = updateData.size(); | |
137 List<NodePatch> mutableUpdates = new ArrayList<NodePatch>(length); | |
138 if (type == Type.ANY_NODE) { | |
139 for (int i = 0; i < length; ++i) { | |
140 mutableUpdates.add(new AnyNodePatch( | |
141 updateData.get(i), (AnyNode)previous.get(getIndex() + i), root)); | |
142 } | |
143 } else { | |
144 assert type == Type.SPECIFIC_NODE; | |
145 for (int i = 0; i < length; ++i) { | |
146 mutableUpdates.add(AnyNodePatch.fromData( | |
147 updateData.get(i), previous.get(getIndex() + i), root)); | |
148 } | |
149 } | |
150 updates = Collections.unmodifiableList(mutableUpdates); | |
151 } | |
152 | |
153 private List<NodePatch> updates; | |
154 } | |
155 | |
156 // Package private implementation. | |
157 | |
158 ListPatch(List<N> previous) { | |
159 changed = false; | |
160 this.previous = previous; | |
161 current = previous; | |
162 } | |
163 | |
164 ListPatch(ListPatchData data, List<N> previous, ImmiRoot root) { | |
165 changed = true; | |
166 this.previous = previous; | |
167 Type type = Type.fromInt(data.getType()); | |
168 ListRegionDataList regions = data.getRegions(); | |
169 List<RegionPatch> patches = new ArrayList<RegionPatch>(regions.size()); | |
170 for (int i = 0; i < regions.size(); ++i) { | |
171 // TODO(zerny): Separate ListPatch<AnyNode> from ListPatch<Node> and avoid | |
172 // dynamic casts. | |
173 patches.add(RegionPatch.fromData( | |
174 regions.get(i), type, (List<Node>)previous, root)); | |
175 } | |
176 this.regions = patches; | |
177 this.current = applyWith(previous); | |
178 } | |
179 | |
180 List<N> applyWith(List<N> previous) { | |
181 int newSize = previous.size(); | |
182 for (int i = 0; i < regions.size(); ++i) { | |
183 newSize += regions.get(i).delta(); | |
184 } | |
185 int sourceIndex = 0; | |
186 List<N> newArray = new ArrayList<N>(newSize); | |
187 for (int i = 0; i < regions.size(); ++i) { | |
188 RegionPatch patch = regions.get(i); | |
189 while (sourceIndex < patch.index) { | |
190 newArray.add(previous.get(sourceIndex++)); | |
191 } | |
192 // TODO(zerny): Separate ListPatch<AnyNode> from ListPatch<Node> and avoid | |
193 // dynamic casts. | |
194 sourceIndex += patch.apply((List<Node>)newArray); | |
195 } | |
196 while (sourceIndex < previous.size()) { | |
197 newArray.add(previous.get(sourceIndex++)); | |
198 } | |
199 return Collections.unmodifiableList(newArray); | |
200 } | |
201 | |
202 private enum Type { | |
203 ANY_NODE, SPECIFIC_NODE; | |
204 | |
205 public static Type fromInt(int tag) { | |
206 return tag == 0 ? ANY_NODE : SPECIFIC_NODE; | |
207 } | |
208 } | |
209 | |
210 private boolean changed; | |
211 private List<N> previous; | |
212 private List<N> current; | |
213 private List<RegionPatch> regions; | |
214 } | |
OLD | NEW |