43 * Group of paths. |
43 * Group of paths. |
44 * Each path in the group is assigned a unique id. |
44 * Each path in the group is assigned a unique id. |
45 */ |
45 */ |
46 final class PathGroup { |
46 final class PathGroup { |
47 PathGroup(Map<Object, Path> paths) { |
47 PathGroup(Map<Object, Path> paths) { |
48 entries = Collections.unmodifiableMap(paths); |
48 entries = new HashMap<>(paths); |
49 } |
49 } |
50 |
50 |
51 Path getPath(Object id) { |
51 Path getPath(Object id) { |
|
52 if (id == null) { |
|
53 throw new NullPointerException(); |
|
54 } |
52 return entries.get(id); |
55 return entries.get(id); |
|
56 } |
|
57 |
|
58 void setPath(Object id, Path path) { |
|
59 if (path != null) { |
|
60 entries.put(id, path); |
|
61 } else { |
|
62 entries.remove(id); |
|
63 } |
53 } |
64 } |
54 |
65 |
55 /** |
66 /** |
56 * All configured entries. |
67 * All configured entries. |
57 */ |
68 */ |
96 Collectors.toMap(e -> e.getKey(), |
107 Collectors.toMap(e -> e.getKey(), |
97 e -> root.resolve(e.getValue())))); |
108 e -> root.resolve(e.getValue())))); |
98 } |
109 } |
99 |
110 |
100 void copy(PathGroup dst) throws IOException { |
111 void copy(PathGroup dst) throws IOException { |
101 copy(this, dst, false); |
112 copy(this, dst, null, false); |
102 } |
113 } |
103 |
114 |
104 void move(PathGroup dst) throws IOException { |
115 void move(PathGroup dst) throws IOException { |
105 copy(this, dst, true); |
116 copy(this, dst, null, true); |
|
117 } |
|
118 |
|
119 void transform(PathGroup dst, TransformHandler handler) throws IOException { |
|
120 copy(this, dst, handler, false); |
106 } |
121 } |
107 |
122 |
108 static interface Facade<T> { |
123 static interface Facade<T> { |
109 PathGroup pathGroup(); |
124 PathGroup pathGroup(); |
110 |
125 |
127 } |
142 } |
128 |
143 |
129 default void move(Facade<T> dst) throws IOException { |
144 default void move(Facade<T> dst) throws IOException { |
130 pathGroup().move(dst.pathGroup()); |
145 pathGroup().move(dst.pathGroup()); |
131 } |
146 } |
132 } |
147 |
133 |
148 default void transform(Facade<T> dst, TransformHandler handler) throws |
134 private static void copy(PathGroup src, PathGroup dst, boolean move) throws |
149 IOException { |
|
150 pathGroup().transform(dst.pathGroup(), handler); |
|
151 } |
|
152 } |
|
153 |
|
154 static interface TransformHandler { |
|
155 public void copyFile(Path src, Path dst) throws IOException; |
|
156 public void createDirectory(Path dir) throws IOException; |
|
157 } |
|
158 |
|
159 private static void copy(PathGroup src, PathGroup dst, |
|
160 TransformHandler handler, boolean move) throws IOException { |
|
161 List<Map.Entry<Path, Path>> copyItems = new ArrayList<>(); |
|
162 List<Path> excludeItems = new ArrayList<>(); |
|
163 |
|
164 for (var id: src.entries.keySet()) { |
|
165 Path srcPath = src.entries.get(id); |
|
166 if (dst.entries.containsKey(id)) { |
|
167 copyItems.add(Map.entry(srcPath, dst.entries.get(id))); |
|
168 } else { |
|
169 excludeItems.add(srcPath); |
|
170 } |
|
171 } |
|
172 |
|
173 copy(move, copyItems, excludeItems, handler); |
|
174 } |
|
175 |
|
176 private static void copy(boolean move, List<Map.Entry<Path, Path>> entries, |
|
177 List<Path> excludePaths, TransformHandler handler) throws |
135 IOException { |
178 IOException { |
136 copy(move, src.entries.keySet().stream().filter( |
179 |
137 id -> dst.entries.containsKey(id)).map(id -> Map.entry( |
180 if (handler == null) { |
138 src.entries.get(id), dst.entries.get(id))).collect( |
181 handler = new TransformHandler() { |
139 Collectors.toCollection(ArrayList::new))); |
182 @Override |
140 } |
183 public void copyFile(Path src, Path dst) throws IOException { |
141 |
184 Files.createDirectories(dst.getParent()); |
142 private static void copy(boolean move, List<Map.Entry<Path, Path>> entries) |
185 if (move) { |
143 throws IOException { |
186 Files.move(src, dst); |
|
187 } else { |
|
188 Files.copy(src, dst); |
|
189 } |
|
190 } |
|
191 |
|
192 @Override |
|
193 public void createDirectory(Path dir) throws IOException { |
|
194 Files.createDirectories(dir); |
|
195 } |
|
196 }; |
|
197 } |
144 |
198 |
145 // destination -> source file mapping |
199 // destination -> source file mapping |
146 Map<Path, Path> actions = new HashMap<>(); |
200 Map<Path, Path> actions = new HashMap<>(); |
147 for (var action: entries) { |
201 for (var action: entries) { |
148 Path src = action.getKey(); |
202 Path src = action.getKey(); |
149 Path dst = action.getValue(); |
203 Path dst = action.getValue(); |
150 if (src.toFile().isDirectory()) { |
204 if (src.toFile().isDirectory()) { |
151 try (Stream<Path> stream = Files.walk(src)) { |
205 try (Stream<Path> stream = Files.walk(src)) { |
152 stream.forEach(path -> actions.put(dst.resolve( |
206 stream.sequential().forEach(path -> actions.put(dst.resolve( |
153 src.relativize(path)).toAbsolutePath().normalize(), |
207 src.relativize(path)).normalize(), path)); |
154 path)); |
|
155 } |
208 } |
156 } else { |
209 } else { |
157 actions.put(dst.toAbsolutePath().normalize(), src); |
210 actions.put(dst.normalize(), src); |
158 } |
211 } |
159 } |
212 } |
160 |
213 |
161 for (var action : actions.entrySet()) { |
214 for (var action : actions.entrySet()) { |
162 Path dst = action.getKey(); |
215 Path dst = action.getKey(); |
163 Path src = action.getValue(); |
216 Path src = action.getValue(); |
164 |
217 |
|
218 if (excludePaths.stream().anyMatch(src::startsWith)) { |
|
219 continue; |
|
220 } |
|
221 |
165 if (src.equals(dst) || !src.toFile().exists()) { |
222 if (src.equals(dst) || !src.toFile().exists()) { |
166 continue; |
223 continue; |
167 } |
224 } |
168 |
225 |
169 if (src.toFile().isDirectory()) { |
226 if (src.toFile().isDirectory()) { |
170 Files.createDirectories(dst); |
227 handler.createDirectory(dst); |
171 } else { |
228 } else { |
172 Files.createDirectories(dst.getParent()); |
229 handler.copyFile(src, dst); |
173 if (move) { |
|
174 Files.move(src, dst); |
|
175 } else { |
|
176 Files.copy(src, dst); |
|
177 } |
|
178 } |
230 } |
179 } |
231 } |
180 |
232 |
181 if (move) { |
233 if (move) { |
182 // Delete source dirs. |
234 // Delete source dirs. |