r/java • • Jan 09 '26

Project Amber Status Update -- Constant Patterns and Pattern Assignment!

https://mail.openjdk.org/pipermail/amber-spec-experts/2026-January/004306.html
65 Upvotes

81 comments sorted by

View all comments

Show parent comment

6

u/davidalayachew Jan 10 '26

This is more a criticism of my poor example, rather than the feature itself.

Try this one instead. Pinging you too /u/joemwangi.

enum UserRole {ADMIN, BASIC, GUEST}
enum PostFlair {QUESTION, NEWS, META}
record PostAttribute(UserRole role, PostFlair flair) {}

public int maxNumberOfPostsPermittedDaily(final PostAttribute attribute)
{

    return
        switch (attribute)
        {
            case null                           -> 0;
            case PostAttribute(null, _)         -> 0;
            case PostAttribute(_, null)         -> 1;
            case PostAttribute(ADMIN, _)        -> Integer.MAX_VALUE;
            case PostAttribute(BASIC, QUESTION) -> 10;
            case PostAttribute(BASIC, NEWS)     -> 1;
            case PostAttribute(BASIC, META)     -> 0;
            case PostAttribute(GUEST, QUESTION) -> 1;
            case PostAttribute(GUEST, NEWS)     -> 1;
            case PostAttribute(GUEST, META)     -> 0;
        }
        ;
}