Last active
May 11, 2025 16:25
-
-
Save fResult/0dafb93d41ebe42669088e4ef9c9d105 to your computer and use it in GitHub Desktop.
Try Union Type In Java
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
| sealed interface Permission permits Read, Write {} | |
| record Read() implements Permission {} | |
| record Write() implements Permission {} | |
| record Execute() {} | |
| class ResourceAction { | |
| public String perform(Permission permission) { | |
| return String.format("Performing action with %s...", permission.getClass().getSimpleName()); | |
| } | |
| } | |
| class TestSecureResource1 { | |
| public static void main(String[] args) { | |
| final var resourceAction = new ResourceAction(); | |
| System.out.println(resourceAction.perform(new Read())); // Performing action with Read... | |
| System.out.println(resourceAction.perform(new Write())); // Performing action with Write... | |
| System.out.println(resourceAction.perform(new Execute())); /* Compilation Error!!!! | |
| * Required type: Permission | |
| * Provided: Execute | |
| */ | |
| } | |
| } |
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.lang.reflect.InvocationTargetException; | |
| import java.util.Optional; | |
| sealed interface Permission permits Read, Write {} | |
| record Read() implements Permission {} | |
| record Write() implements Permission {} | |
| record Execute() {} | |
| class Reflection { | |
| public static <T> Optional<T> createInstance(Class<T> clazz) { | |
| try { | |
| return Optional.of(clazz.getDeclaredConstructor().newInstance()); | |
| } catch (InstantiationException | |
| | IllegalAccessException | |
| | InvocationTargetException | |
| | NoSuchMethodException e) { | |
| System.err.println("[WARN]: " + e.getMessage()); | |
| return Optional.empty(); | |
| } | |
| } | |
| } | |
| class ResourceAction { | |
| public static String perform(P permission) { | |
| return String.format("Performing action with %s...", permission.getClass().getSimpleName()); | |
| } | |
| } | |
| class SecureResource { | |
| public <P extends Permission> String access(Class<P> permission) { | |
| return Reflection.createInstance(permission) | |
| .map(ResourceAction::perform) | |
| .orElseThrow(() -> new RuntimeException("Failed to instantiate Permission class")); | |
| } | |
| } | |
| class TestSecureResource2 { | |
| public static void main(String[] args) { | |
| final var resource = new SecureResource(); | |
| System.out.println(resource.access(Read.class)); // Performing action with Read... | |
| System.out.println(resource.access(Write.class)); // Performing action with Write... | |
| System.out.println(resource.access(Execute.class)); /* Compilation Error!!!! | |
| * No instance(s) of type variable(s) exist | |
| * so that Execute conforms to Permission | |
| */ | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment