From 2fc051711ab7edb2677d619bfffe526c81ef9d4f Mon Sep 17 00:00:00 2001 From: pacien Date: Sun, 4 Feb 2018 21:30:35 +0100 Subject: Rewrite main, handling filesystems Signed-off-by: pacien --- src/main/java/fr/umlv/java/wallj/viewer/Main.java | 118 ++++++++++++---------- 1 file changed, 63 insertions(+), 55 deletions(-) (limited to 'src/main') 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.*; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; -import java.nio.file.*; -import java.util.*; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; -public class Main { - private static final String DEFAULT_MAP_NAME = "/maps/level0.txt"; +/** + * The main class of the application. + * + * @author Adam NAILI + * @author Pacien TRAN-GIRARD + */ +public final class Main { + private static final String MAP_DIR_KEY = "levels"; + private static final String DEFAULT_MAP_DIR = "/maps"; + private static final String MAP_FILE_PATTERN = "level*.txt"; + private static final String JAR_SCHEME = "jar"; - private static FileSystem fileSystemForContext(URI uri) throws URISyntaxException, IOException { - boolean isInJar = Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().toString().endsWith(".jar"); - if (isInJar) {//JAR from command line handling - Map env = new HashMap<>(); - env.put("create", "true"); - return FileSystems.newFileSystem(uri, env); + private static Path getMapDirPath() { + try { + if (System.getProperty(MAP_DIR_KEY) != null) { + return Paths.get(System.getProperty(MAP_DIR_KEY)); + } else { + URI defaultMapDirUri = Main.class.getResource(DEFAULT_MAP_DIR).toURI(); + if (defaultMapDirUri.getScheme().equals(JAR_SCHEME)) + FileSystems.newFileSystem(defaultMapDirUri, Collections.emptyMap()); + + return Paths.get(defaultMapDirUri); + } + } catch (URISyntaxException | IOException e) { + throw new RuntimeException(e); } - return FileSystems.getDefault();//IDE Handling } - private static List getResourcePaths() throws URISyntaxException, IOException { - final URI uri = Main.class.getResource(DEFAULT_MAP_NAME).toURI(); - LinkedList paths = new LinkedList<>(); - String strFolder = System.getProperty("levels"); - if (strFolder != null) { - Path pathFolder = Paths.get(strFolder); - Files.newDirectoryStream(pathFolder).forEach(paths::add); - } else { - FileSystem fileSystem = fileSystemForContext(uri); - paths.add(Paths.get(uri)); + private static Stream listMaps(Path mapDir) { + try { + return StreamSupport + .stream(Files.newDirectoryStream(mapDir, MAP_FILE_PATTERN).spliterator(), false) + .sorted((l, r) -> String.CASE_INSENSITIVE_ORDER.compare(l.toString(), r.toString())); + } catch (IOException e) { + throw new RuntimeException(e); } - return paths; } - //TODO Split Parse and validation + add useless return to satisfy this crazy compiler - private static Board validateBoardFromPath(Path path) { + private static Board loadBoard(Path path) { try { - BoardValidator boardValidator = new BoardValidator(BoardParser.parse(path)); - return boardValidator.validate(BoardValidator.Constraint::hasActualReachableBlocks, "Some supposed reachable blocks are not reachable.") - .validate(BoardValidator.Constraint::hasMandatoryBlocks, "Some mandatory blocks are missing.") - .validate(BoardValidator.Constraint::isBounded, "The board is not correctly bounded.") - .validate(BoardValidator.Constraint::isHollow, "The board must have a unique and simple interior.") - .get(); + return BoardParser.parse(path); } catch (IOException e) { - System.err.println(e.getMessage()); - System.exit(2); - return null; - } catch (BoardValidator.ValidationException e) { - System.err.println(path.toString() + ':'); - for (Throwable throwable : e.getSuppressed()) { - System.err.println(throwable.getMessage()); - } - System.exit(3); - return null; + throw new RuntimeException(e); } } - private static List validateBoardsFromPaths(List boardPaths) { - List boards = new LinkedList<>(); - for (Path path : boardPaths) { - boards.add(validateBoardFromPath(path)); + private static Board validateBoard(Board board) { + try { + return new BoardValidator(board) + .validate(BoardValidator.Constraint::hasActualReachableBlocks, "Some supposed reachable blocks are not reachable.") + .validate(BoardValidator.Constraint::hasMandatoryBlocks, "Some mandatory blocks are missing.") + .validate(BoardValidator.Constraint::isBounded, "The board is not correctly bounded.") + .validate(BoardValidator.Constraint::isHollow, "The board must have a unique and simple interior.") + .get(); + } catch (BoardValidator.ValidationException e) { + throw new RuntimeException(e); } - return boards; } public static void main(String[] args) { - try { - Viewer viewer = new Viewer(validateBoardsFromPaths(Main.getResourcePaths())); - Application.run(Color.WHITE, viewer::eventLoop); - } catch (URISyntaxException e) { - System.err.println(e.getMessage()); - System.exit(1); - } catch (IOException e) { - System.err.println(e.getMessage()); - System.exit(2); - } + List levels = listMaps(getMapDirPath()) + .map(Main::loadBoard) + .map(Main::validateBoard) + .collect(Collectors.toList()); + + Viewer viewer = new Viewer(levels); + Application.run(Color.WHITE, viewer::eventLoop); + } + + private Main() { + // static class } } -- cgit v1.2.3