aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpacien2018-02-04 21:30:35 +0100
committerpacien2018-02-04 21:30:35 +0100
commit2fc051711ab7edb2677d619bfffe526c81ef9d4f (patch)
tree32757374bf7cebc7a91262a8be840673ffddd076
parente7d7b2302cd836f4a9ddbde6fd3e71e7f14c0e9d (diff)
downloadwallj-2fc051711ab7edb2677d619bfffe526c81ef9d4f.tar.gz
Rewrite main, handling filesystems
Signed-off-by: pacien <pacien.trangirard@pacien.net>
-rw-r--r--src/main/java/fr/umlv/java/wallj/viewer/Main.java118
1 files changed, 63 insertions, 55 deletions
diff --git a/src/main/java/fr/umlv/java/wallj/viewer/Main.java b/src/main/java/fr/umlv/java/wallj/viewer/Main.java
index d2b041e..9e138a8 100644
--- a/src/main/java/fr/umlv/java/wallj/viewer/Main.java
+++ b/src/main/java/fr/umlv/java/wallj/viewer/Main.java
@@ -9,78 +9,86 @@ import java.awt.*;
9import java.io.IOException; 9import java.io.IOException;
10import java.net.URI; 10import java.net.URI;
11import java.net.URISyntaxException; 11import java.net.URISyntaxException;
12import java.nio.file.*; 12import java.nio.file.FileSystems;
13import java.util.*; 13import java.nio.file.Files;
14import java.nio.file.Path;
15import java.nio.file.Paths;
16import java.util.Collections;
14import java.util.List; 17import java.util.List;
18import java.util.stream.Collectors;
19import java.util.stream.Stream;
20import java.util.stream.StreamSupport;
15 21
16public class Main { 22/**
17 private static final String DEFAULT_MAP_NAME = "/maps/level0.txt"; 23 * The main class of the application.
24 *
25 * @author Adam NAILI
26 * @author Pacien TRAN-GIRARD
27 */
28public final class Main {
29 private static final String MAP_DIR_KEY = "levels";
30 private static final String DEFAULT_MAP_DIR = "/maps";
31 private static final String MAP_FILE_PATTERN = "level*.txt";
32 private static final String JAR_SCHEME = "jar";
18 33
19 private static FileSystem fileSystemForContext(URI uri) throws URISyntaxException, IOException { 34 private static Path getMapDirPath() {
20 boolean isInJar = Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().toString().endsWith(".jar"); 35 try {
21 if (isInJar) {//JAR from command line handling 36 if (System.getProperty(MAP_DIR_KEY) != null) {
22 Map<String, String> env = new HashMap<>(); 37 return Paths.get(System.getProperty(MAP_DIR_KEY));
23 env.put("create", "true"); 38 } else {
24 return FileSystems.newFileSystem(uri, env); 39 URI defaultMapDirUri = Main.class.getResource(DEFAULT_MAP_DIR).toURI();
40 if (defaultMapDirUri.getScheme().equals(JAR_SCHEME))
41 FileSystems.newFileSystem(defaultMapDirUri, Collections.emptyMap());
42
43 return Paths.get(defaultMapDirUri);
44 }
45 } catch (URISyntaxException | IOException e) {
46 throw new RuntimeException(e);
25 } 47 }
26 return FileSystems.getDefault();//IDE Handling
27 } 48 }
28 49
29 private static List<Path> getResourcePaths() throws URISyntaxException, IOException { 50 private static Stream<Path> listMaps(Path mapDir) {
30 final URI uri = Main.class.getResource(DEFAULT_MAP_NAME).toURI(); 51 try {
31 LinkedList<Path> paths = new LinkedList<>(); 52 return StreamSupport
32 String strFolder = System.getProperty("levels"); 53 .stream(Files.newDirectoryStream(mapDir, MAP_FILE_PATTERN).spliterator(), false)
33 if (strFolder != null) { 54 .sorted((l, r) -> String.CASE_INSENSITIVE_ORDER.compare(l.toString(), r.toString()));
34 Path pathFolder = Paths.get(strFolder); 55 } catch (IOException e) {
35 Files.newDirectoryStream(pathFolder).forEach(paths::add); 56 throw new RuntimeException(e);
36 } else {
37 FileSystem fileSystem = fileSystemForContext(uri);
38 paths.add(Paths.get(uri));
39 } 57 }
40 return paths;
41 } 58 }
42 59
43 //TODO Split Parse and validation + add useless return to satisfy this crazy compiler 60 private static Board loadBoard(Path path) {
44 private static Board validateBoardFromPath(Path path) {
45 try { 61 try {
46 BoardValidator boardValidator = new BoardValidator(BoardParser.parse(path)); 62 return BoardParser.parse(path);
47 return boardValidator.validate(BoardValidator.Constraint::hasActualReachableBlocks, "Some supposed reachable blocks are not reachable.")
48 .validate(BoardValidator.Constraint::hasMandatoryBlocks, "Some mandatory blocks are missing.")
49 .validate(BoardValidator.Constraint::isBounded, "The board is not correctly bounded.")
50 .validate(BoardValidator.Constraint::isHollow, "The board must have a unique and simple interior.")
51 .get();
52 } catch (IOException e) { 63 } catch (IOException e) {
53 System.err.println(e.getMessage()); 64 throw new RuntimeException(e);
54 System.exit(2);
55 return null;
56 } catch (BoardValidator.ValidationException e) {
57 System.err.println(path.toString() + ':');
58 for (Throwable throwable : e.getSuppressed()) {
59 System.err.println(throwable.getMessage());
60 }
61 System.exit(3);
62 return null;
63 } 65 }
64 } 66 }
65 67
66 private static List<Board> validateBoardsFromPaths(List<Path> boardPaths) { 68 private static Board validateBoard(Board board) {
67 List<Board> boards = new LinkedList<>(); 69 try {
68 for (Path path : boardPaths) { 70 return new BoardValidator(board)
69 boards.add(validateBoardFromPath(path)); 71 .validate(BoardValidator.Constraint::hasActualReachableBlocks, "Some supposed reachable blocks are not reachable.")
72 .validate(BoardValidator.Constraint::hasMandatoryBlocks, "Some mandatory blocks are missing.")
73 .validate(BoardValidator.Constraint::isBounded, "The board is not correctly bounded.")
74 .validate(BoardValidator.Constraint::isHollow, "The board must have a unique and simple interior.")
75 .get();
76 } catch (BoardValidator.ValidationException e) {
77 throw new RuntimeException(e);
70 } 78 }
71 return boards;
72 } 79 }
73 80
74 public static void main(String[] args) { 81 public static void main(String[] args) {
75 try { 82 List<Board> levels = listMaps(getMapDirPath())
76 Viewer viewer = new Viewer(validateBoardsFromPaths(Main.getResourcePaths())); 83 .map(Main::loadBoard)
77 Application.run(Color.WHITE, viewer::eventLoop); 84 .map(Main::validateBoard)
78 } catch (URISyntaxException e) { 85 .collect(Collectors.toList());
79 System.err.println(e.getMessage()); 86
80 System.exit(1); 87 Viewer viewer = new Viewer(levels);
81 } catch (IOException e) { 88 Application.run(Color.WHITE, viewer::eventLoop);
82 System.err.println(e.getMessage()); 89 }
83 System.exit(2); 90
84 } 91 private Main() {
92 // static class
85 } 93 }
86} 94}