Skip to content

Instantly share code, notes, and snippets.

@yatharth
Created October 18, 2015 17:28
Show Gist options
  • Save yatharth/969a25c882b53ad36ae5 to your computer and use it in GitHub Desktop.
Save yatharth/969a25c882b53ad36ae5 to your computer and use it in GitHub Desktop.
Stripped down example of working with errorable static init blocks
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