Skip to content

Instantly share code, notes, and snippets.

@Koboo
Last active August 22, 2022 09:47
Show Gist options
  • Save Koboo/4f3640d1353a88885452d2bdc0565e38 to your computer and use it in GitHub Desktop.
Save Koboo/4f3640d1353a88885452d2bdc0565e38 to your computer and use it in GitHub Desktop.
Switch and case class of an object
import java.util.Optional;
import java.util.function.Consumer;
@SuppressWarnings("all")
public class SwitchClass {
static public <T> void cswitch(Object object, Consumer... consumers) {
for (Consumer consumer : consumers) {
consumer.accept(object);
}
}
static public <T> Consumer ccase(Class<T> clazz, Consumer<T> consumer) {
return object -> Optional.of(object).filter(clazz::isInstance).map(clazz::cast)
.ifPresent(consumer);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment