Last active
August 22, 2022 09:47
-
-
Save Koboo/4f3640d1353a88885452d2bdc0565e38 to your computer and use it in GitHub Desktop.
Switch and case class of an object
This file contains hidden or 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.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