Created
October 18, 2015 17:28
-
-
Save yatharth/969a25c882b53ad36ae5 to your computer and use it in GitHub Desktop.
Stripped down example of working with errorable static init blocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EncodingHelperCharNames { | |
private static HashMap<Integer, String> CHARACTER_NAMES; | |
private static boolean enabled = false; | |
static { | |
readFile(false); | |
} | |
/** | |
* Extract reading functionality to be able to pretend to fail for testing | |
* | |
* @param testFail Whether method should pretend to fail | |
*/ | |
static void readFile(boolean testFail) { | |
// stuff | |
} | |
public static boolean isEnabled() { | |
return enabled; | |
} | |
private static void checkEnabled() throws IOException { | |
if (!enabled) { | |
throw new IOException("Character names weren't loaded"); | |
} | |
} | |
public static String getName(int codepoint) throws IOException { | |
checkEnabled(); | |
// actual stuff | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment