From 343c26ecc815067796314fbdfe0c8ef10710ca42 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Thu, 4 Dec 2014 18:05:12 +0100 Subject: [PATCH] Cache isJar value in CodeEnvironment instead of looking it up everytime somebody asks. --- .../spacescooter/utility/CodeEnvironment.java | 57 ++++++++++--------- .../spacescooter/utility/Loader.java | 6 +- 2 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/de/teamteamteam/spacescooter/utility/CodeEnvironment.java b/src/de/teamteamteam/spacescooter/utility/CodeEnvironment.java index 2464696..20432c1 100644 --- a/src/de/teamteamteam/spacescooter/utility/CodeEnvironment.java +++ b/src/de/teamteamteam/spacescooter/utility/CodeEnvironment.java @@ -11,67 +11,71 @@ import java.util.zip.ZipInputStream; /** * This helper is used by the Loader to get a list of files of the whole Code - * and determine specific runtime configurations. - * Since there are some differences between OS and jar/non-jar environments, - * this helper makes sure, everything still works. + * and determine specific runtime configurations. Since there are some + * differences between OS and jar/non-jar environments, this helper makes sure, + * everything still works. */ public class CodeEnvironment { + /** + * Constant telling whether the Code is run from a jar or not. + */ + public static final boolean isJar = CodeEnvironment.class + .getProtectionDomain().getCodeSource().getLocation().toString() + .endsWith("jar"); + /** * Private constructor, this class will never be instantiated. */ - private CodeEnvironment() {} - - - /** - * Returns true if the program is run out of a jar. - */ - public static boolean isJar() { - return (CodeEnvironment.class.getProtectionDomain().getCodeSource().getLocation().toString().endsWith("jar")); + private CodeEnvironment() { } - + /** * Returns a String[] containing relative paths to files within the code. */ public static String[] getFileList() { - URL codeLocation = CodeEnvironment.class.getProtectionDomain().getCodeSource().getLocation(); - if(CodeEnvironment.isJar()) { + URL codeLocation = CodeEnvironment.class.getProtectionDomain() + .getCodeSource().getLocation(); + if (CodeEnvironment.isJar) { return CodeEnvironment.getFileListFromJar(codeLocation); } else { File codeFolder = null; try { codeFolder = new File(codeLocation.toURI()); } catch (URISyntaxException e) { - System.err.println("Could not convert codeLocation URL to File!"); + System.err + .println("Could not convert codeLocation URL to File!"); e.printStackTrace(); } return CodeEnvironment.getFileListFromFolder(codeFolder); } } - + /** * Return a relative path list of files based on a given folder. */ private static String[] getFileListFromFolder(File folder) { ArrayList fileList = new ArrayList(); String rootPath = folder.getAbsolutePath() + File.separator; - String[] folderContents = CodeEnvironment.getAbsoluteFileListFromFolder(folder); - for(String element : folderContents) { + String[] folderContents = CodeEnvironment + .getAbsoluteFileListFromFolder(folder); + for (String element : folderContents) { fileList.add(element.replace(rootPath, "")); } return fileList.toArray(new String[fileList.size()]); } - + /** * Recursively generate an absolute path list of elements within a folder. */ private static String[] getAbsoluteFileListFromFolder(File folder) { ArrayList fileList = new ArrayList(); File[] folderContents = folder.listFiles(); - for(File f : folderContents) { - if(f.isDirectory()) { - String[] filesInDirectory = CodeEnvironment.getAbsoluteFileListFromFolder(f); - for(String entry : filesInDirectory) { + for (File f : folderContents) { + if (f.isDirectory()) { + String[] filesInDirectory = CodeEnvironment + .getAbsoluteFileListFromFolder(f); + for (String entry : filesInDirectory) { fileList.add(entry); } } else { @@ -80,9 +84,10 @@ public class CodeEnvironment { } return fileList.toArray(new String[fileList.size()]); } - + /** - * Returns a relative path list of files that are contained within the current jar. + * Returns a relative path list of files that are contained within the + * current jar. */ private static String[] getFileListFromJar(URL jar) { List list = new ArrayList(); @@ -105,5 +110,5 @@ public class CodeEnvironment { } return list.toArray(new String[list.size()]); } - + } diff --git a/src/de/teamteamteam/spacescooter/utility/Loader.java b/src/de/teamteamteam/spacescooter/utility/Loader.java index e9c9c44..8d4b93b 100644 --- a/src/de/teamteamteam/spacescooter/utility/Loader.java +++ b/src/de/teamteamteam/spacescooter/utility/Loader.java @@ -67,7 +67,7 @@ public class Loader { */ public static BufferedImage getBufferedImageByFilename(String filename) { BufferedImage image = null; - if(CodeEnvironment.isJar()) { + if(CodeEnvironment.isJar) { image = Loader.images.get(filename); } else { image = Loader.images.get(filename.replace("/", File.separator)); @@ -83,7 +83,7 @@ public class Loader { * Return the loaded sound URL by its relative filename. */ public static URL getSoundURLByFilename(String filename) { - if(CodeEnvironment.isJar()) { + if(CodeEnvironment.isJar) { return Loader.sounds.get(filename); } else { return Loader.sounds.get(filename.replace("/", File.separator)); @@ -94,7 +94,7 @@ public class Loader { * Return the LevelConfig by its relative filename. */ public static LevelConfig getLevelConfigByFilename(String filename) { - if(CodeEnvironment.isJar()) { + if(CodeEnvironment.isJar) { return Loader.levelConfigs.get(filename); } else { return Loader.levelConfigs.get(filename.replace("/", File.separator));