Last active
August 29, 2015 13:57
-
-
Save darrend/9375829 to your computer and use it in GitHub Desktop.
noodling with flag parameters to methods instead of a buncha booleans
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
import java.util.Arrays; | |
public class FlagArgumentsDemo { | |
public static void main(String[] args) { | |
System.out.println(convertToInteger("5")); | |
System.out.println(convertToInteger("5", Flag.DEBUG)); | |
System.out.println(convertToInteger("5", Flag.IGNORE_EXCEPTIONS, Flag.DEBUG)); | |
System.out.println(convertToInteger("no number", Flag.IGNORE_EXCEPTIONS, Flag.DEBUG)); | |
System.out.println(convertToInteger("no number", Flag.IGNORE_EXCEPTIONS)); | |
try { | |
System.out.println(convertToInteger("no number", Flag.DEBUG)); | |
} catch(Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
public static Integer convertToInteger(String number, Flag... flags) { | |
if (Flag.DEBUG.isFlagged(flags)) { | |
System.out.println("DEBUG: Converting {"+number+"} to Integer (flags {"+Arrays.asList(flags)+"})"); | |
} | |
try { | |
return new Integer(number); | |
} catch (NumberFormatException e) { | |
if (Flag.IGNORE_EXCEPTIONS.isFlagged(flags)) { return null; } | |
throw e; | |
} | |
} | |
public static enum Flag { | |
DEBUG,IGNORE_EXCEPTIONS; | |
public boolean isFlagged(Flag... flags) { | |
for (Flag flag : flags) { if(flag.equals(this)) { return true; } } | |
return false; | |
} | |
} | |
} |
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
5 | |
DEBUG: Converting {5} to Integer (flags {[DEBUG]}) | |
5 | |
DEBUG: Converting {5} to Integer (flags {[IGNORE_EXCEPTIONS, DEBUG]}) | |
5 | |
DEBUG: Converting {no number} to Integer (flags {[IGNORE_EXCEPTIONS, DEBUG]}) | |
null | |
null | |
DEBUG: Converting {no number} to Integer (flags {[DEBUG]}) | |
java.lang.NumberFormatException: For input string: "no number" | |
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) | |
at java.lang.Integer.parseInt(Integer.java:492) | |
at java.lang.Integer.<init>(Integer.java:677) | |
at FlagArgumentsDemo.convertToInteger(FlagArgumentsDemo.java:28) | |
at FlagArgumentsDemo.main(FlagArgumentsDemo.java:16) | |
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | |
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) | |
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) | |
at java.lang.reflect.Method.invoke(Method.java:601) | |
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) | |
Process finished with exit code 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment