Skip to content

Instantly share code, notes, and snippets.

@tonvanbart
Last active January 28, 2022 16:56
Show Gist options
  • Save tonvanbart/3fc3e1039674e9059289153da174f381 to your computer and use it in GitHub Desktop.
Save tonvanbart/3fc3e1039674e9059289153da174f381 to your computer and use it in GitHub Desktop.
Try out various Java enum (mis)uses
package io.axual.connect.plugins.kafka;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class Tryout {
@Test
void testEnumValues() {
Thing thing = Thing.fromType("fooBar");
assertEquals("hello from fooBar", thing.work());
assertEquals("hello from else", Thing.OTHER.work());
assertEquals("a horse with no name", Thing.UNNAMED.work());
assertThrows(IllegalArgumentException.class, () -> Thing.fromType("nonsense"));
assertEquals("hello from else", Thing.fromType("else").plainMethod());
}
interface Canwork {
String work();
}
enum Thing implements Canwork {
FOO("fooBar"),
OTHER("else"),
UNNAMED("") {
@Override
public String work() {
return "a horse with no name";
}
};
private final Canwork worker;
Thing(String type) {
this.worker = () -> "hello from " + type;
}
static Thing fromType(String typeName) {
if ("fooBar".equals(typeName)) {
return FOO;
} else if("else".equals(typeName)) {
return OTHER;
} else {
throw new IllegalArgumentException("unknown type name");
}
}
@Override
public String work() {
return worker.work();
}
public String plainMethod() {
return worker.work();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment