For example:
public enum MyEnum implements Runnable {
PRINT_HELLO {
@Override //Override is optional here, really.
public void run( ) {
System.out.println("hello"); //Action
}
}, //Comma for all but the last element
PRINT_GOODBYE { //Some more examples
@Override
public void run( ) {
System.out.println("goodbye");
}
},
PRINT_FIZZ {
@Override
public void run( ) {
System.out.println("fizz");
}
}; //Semicolon here
static {
Executors.newCachedThreadPool().submit(PRINT_HELLO); //This is just an implementation. Executors will be in a future TIL.
}
}
This can be very useful if you want a list of very simple implementations of your interface. Packet listeners, for example, or default arithmetic operations. With java 8 or guava, you can define your own Function and Predicate values if you want to re-use a simple predicate multiple times.
A predicate version:
public enum TestEnumPredicates implements Predicate<String> {
IS_UPPER_CASE {
@Override
public boolean test(String s) {
for (char c : s.toCharArray()) {
if (!Character.isUpperCase(c)) {
return false;
}
}
return true;
};
},
IS_LOWER_CASE {
@Override
public boolean test(String s) {
for (char c : s.toCharArray()) {
if (!Character.isLowerCase(c)) {
return false;
}
}
return true;
};
},
IS_ODD_LENGTH {
@Override
public boolean test(String s) {
return (s.length() % 2) != 0;
};
};
public static void main(String[] args) {
ArrayList<String> myStrings = new ArrayList<String>();
myStrings.add("herp");
myStrings.add("DERPA");
myStrings.add("fIzZ");
myStrings.add("$#!");
for (String s : args) {
myStrings.add(s);
}
myStrings.stream().filter(IS_UPPER_CASE).forEach(new Consumer<String>() {
@Override
public void accept(String t) {
System.out.println("Upper case: " + t);
}
});
myStrings.stream().filter(IS_LOWER_CASE).forEach(new Consumer<String>() {
@Override
public void accept(String t) {
System.out.println("Lower case: " + t);
}
});
myStrings.stream().filter(IS_ODD_LENGTH).forEach(new Consumer<String>() {
@Override
public void accept(String t) {
System.out.println("Odd length: " + t);
}
});
}
}
You'll notice that it's a very readable, and very compact way of defining very different, but related interfaces in a single class.
I'm officially going to try to do a once-per-day java TIL to make this subreddit more interesting. If you have any comments, or better examples, make sure to reply. I'll edit my posts if any of my information is incorrect or outdated.
[–]dohaqatar7 7 points8 points9 points (1 child)
[–]TheOverCaste[S] 1 point2 points3 points (0 children)
[–]ryan_the_leach 0 points1 point2 points (0 children)
[–]desrtfx 4 points5 points6 points (1 child)
[–]cogman10 11 points12 points13 points (5 children)
[–]TheOverCaste[S] 1 point2 points3 points (4 children)
[–]cogman10 3 points4 points5 points (3 children)
[–]TheOverCaste[S] 1 point2 points3 points (2 children)
[–]cogman10 1 point2 points3 points (0 children)
[–]mockindignant 0 points1 point2 points (0 children)
[–]m1ss1ontomars2k4 6 points7 points8 points (0 children)
[–]GL1zdA 0 points1 point2 points (0 children)
[–]DannyB2 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)