Java generics with multiple enums implementing an interface The Next CEO of Stack OverflowJava Generics Wildcarding With Multiple ClassesCast int to enum in C#Is Java “pass-by-reference” or “pass-by-value”?Create Generic method constraining T to an EnumHow do I enumerate an enum in C#?What is the preferred syntax for defining enums in JavaScript?How do I generate random integers within a specific range in Java?How do I make the method return type generic?How to get an enum value from a string value in Java?Comparing Java enum members: == or equals()?How should I have explained the difference between an Interface and an Abstract class?

Help/tips for a first time writer?

(How) Could a medieval fantasy world survive a magic-induced "nuclear winter"?

What can the phrase “is embedded in a whale of a bill” mean?

Do I need to write [sic] when including a quotation with a number less than 10 that isn't written out?

Can I board the first leg of the flight without having final country's visa?

Is there a way to save my career from absolute disaster?

Is French Guiana a (hard) EU border?

How did Beeri the Hittite come up with naming his daughter Yehudit?

IC has pull-down resistors on SMBus lines?

Does higher Oxidation/ reduction potential translate to higher energy storage in battery?

Inexact numbers as keys in Association?

0-rank tensor vs vector in 1D

Won the lottery - how do I keep the money?

Pulling the principal components out of a DimensionReducerFunction?

Physiological effects of huge anime eyes

What was Carter Burke's job for "the company" in Aliens?

Why is information "lost" when it got into a black hole?

Calculate the Mean mean of two numbers

Expectation in a stochastic differential equation

How to use ReplaceAll on an expression that contains a rule

Which one is the true statement?

In the "Harry Potter and the Order of the Phoenix" video game, what potion is used to sabotage Umbridge's speakers?

When "be it" is at the beginning of a sentence, what kind of structure do you call it?

What CSS properties can the br tag have?



Java generics with multiple enums implementing an interface



The Next CEO of Stack OverflowJava Generics Wildcarding With Multiple ClassesCast int to enum in C#Is Java “pass-by-reference” or “pass-by-value”?Create Generic method constraining T to an EnumHow do I enumerate an enum in C#?What is the preferred syntax for defining enums in JavaScript?How do I generate random integers within a specific range in Java?How do I make the method return type generic?How to get an enum value from a string value in Java?Comparing Java enum members: == or equals()?How should I have explained the difference between an Interface and an Abstract class?










4















I want to pass a list of enum classes to a method, where all of the enums implement a common interface, and have the method return one of the enum values.



Looking at Java Generics Wildcarding With Multiple Classes, it seems that



public class Main

interface Foo

enum First implements Foo
A, B, C;


enum Second implements Foo
X, Y, Z;


interface Bar

enum Third implements Bar
M, N, P;

enum Fourth implements Bar
A, X, Z;



public static <I, T extends Enum<?> & I>
I enumVarArgs(Class<? extends T>... classes)

// Do stuff and return some instance of T
return null;


public static void main(String[] args)
Foo foo = enumVarArgs(First.class,
Second.class);
Bar bar = enumVarArgs(Third.class,
Fourth.class);




should do what I want. However, this fails to compile under Java 10:



[ERROR] /me/test/src/main/java/test/Main.java:[17,42] error: unexpected type
required: class
found: type parameter I
where I,T are type-variables:
I extends Object declared in method <I,T>enumVarArgs(Class<? extends T>...)
T extends Enum<?>,I declared in method <I,T>enumVarArgs(Class<? extends T>...)
[INFO] 1 error


From the error message, I am guessing that Java wants me to do something like T extends Enum<?> & Serializable, where I pass an actual interface, rather than a type parameter. However, I need the API to be general so that I remains a generic parameter.



Is there a syntax that makes this work?



If it matters, we are using Java 10.










share|improve this question






















  • are you aware that your A's and X's will be different? i.e. First.a!=Fourth.a

    – Ray Tayek
    Mar 8 at 23:21















4















I want to pass a list of enum classes to a method, where all of the enums implement a common interface, and have the method return one of the enum values.



Looking at Java Generics Wildcarding With Multiple Classes, it seems that



public class Main

interface Foo

enum First implements Foo
A, B, C;


enum Second implements Foo
X, Y, Z;


interface Bar

enum Third implements Bar
M, N, P;

enum Fourth implements Bar
A, X, Z;



public static <I, T extends Enum<?> & I>
I enumVarArgs(Class<? extends T>... classes)

// Do stuff and return some instance of T
return null;


public static void main(String[] args)
Foo foo = enumVarArgs(First.class,
Second.class);
Bar bar = enumVarArgs(Third.class,
Fourth.class);




should do what I want. However, this fails to compile under Java 10:



[ERROR] /me/test/src/main/java/test/Main.java:[17,42] error: unexpected type
required: class
found: type parameter I
where I,T are type-variables:
I extends Object declared in method <I,T>enumVarArgs(Class<? extends T>...)
T extends Enum<?>,I declared in method <I,T>enumVarArgs(Class<? extends T>...)
[INFO] 1 error


From the error message, I am guessing that Java wants me to do something like T extends Enum<?> & Serializable, where I pass an actual interface, rather than a type parameter. However, I need the API to be general so that I remains a generic parameter.



Is there a syntax that makes this work?



If it matters, we are using Java 10.










share|improve this question






















  • are you aware that your A's and X's will be different? i.e. First.a!=Fourth.a

    – Ray Tayek
    Mar 8 at 23:21













4












4








4








I want to pass a list of enum classes to a method, where all of the enums implement a common interface, and have the method return one of the enum values.



Looking at Java Generics Wildcarding With Multiple Classes, it seems that



public class Main

interface Foo

enum First implements Foo
A, B, C;


enum Second implements Foo
X, Y, Z;


interface Bar

enum Third implements Bar
M, N, P;

enum Fourth implements Bar
A, X, Z;



public static <I, T extends Enum<?> & I>
I enumVarArgs(Class<? extends T>... classes)

// Do stuff and return some instance of T
return null;


public static void main(String[] args)
Foo foo = enumVarArgs(First.class,
Second.class);
Bar bar = enumVarArgs(Third.class,
Fourth.class);




should do what I want. However, this fails to compile under Java 10:



[ERROR] /me/test/src/main/java/test/Main.java:[17,42] error: unexpected type
required: class
found: type parameter I
where I,T are type-variables:
I extends Object declared in method <I,T>enumVarArgs(Class<? extends T>...)
T extends Enum<?>,I declared in method <I,T>enumVarArgs(Class<? extends T>...)
[INFO] 1 error


From the error message, I am guessing that Java wants me to do something like T extends Enum<?> & Serializable, where I pass an actual interface, rather than a type parameter. However, I need the API to be general so that I remains a generic parameter.



Is there a syntax that makes this work?



If it matters, we are using Java 10.










share|improve this question














I want to pass a list of enum classes to a method, where all of the enums implement a common interface, and have the method return one of the enum values.



Looking at Java Generics Wildcarding With Multiple Classes, it seems that



public class Main

interface Foo

enum First implements Foo
A, B, C;


enum Second implements Foo
X, Y, Z;


interface Bar

enum Third implements Bar
M, N, P;

enum Fourth implements Bar
A, X, Z;



public static <I, T extends Enum<?> & I>
I enumVarArgs(Class<? extends T>... classes)

// Do stuff and return some instance of T
return null;


public static void main(String[] args)
Foo foo = enumVarArgs(First.class,
Second.class);
Bar bar = enumVarArgs(Third.class,
Fourth.class);




should do what I want. However, this fails to compile under Java 10:



[ERROR] /me/test/src/main/java/test/Main.java:[17,42] error: unexpected type
required: class
found: type parameter I
where I,T are type-variables:
I extends Object declared in method <I,T>enumVarArgs(Class<? extends T>...)
T extends Enum<?>,I declared in method <I,T>enumVarArgs(Class<? extends T>...)
[INFO] 1 error


From the error message, I am guessing that Java wants me to do something like T extends Enum<?> & Serializable, where I pass an actual interface, rather than a type parameter. However, I need the API to be general so that I remains a generic parameter.



Is there a syntax that makes this work?



If it matters, we are using Java 10.







java generics enums






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 17:06









Troy DanielsTroy Daniels

1,14511133




1,14511133












  • are you aware that your A's and X's will be different? i.e. First.a!=Fourth.a

    – Ray Tayek
    Mar 8 at 23:21

















  • are you aware that your A's and X's will be different? i.e. First.a!=Fourth.a

    – Ray Tayek
    Mar 8 at 23:21
















are you aware that your A's and X's will be different? i.e. First.a!=Fourth.a

– Ray Tayek
Mar 8 at 23:21





are you aware that your A's and X's will be different? i.e. First.a!=Fourth.a

– Ray Tayek
Mar 8 at 23:21












2 Answers
2






active

oldest

votes


















0














If interfaces are your design, then you can make the two interfaces extend a marker interface:



interface FooBar

interface Foo extends FooBar

interface Bar extends FooBar


And you can use this marker interface:



public static <T extends Enum<?> & FooBar> 
FooBar enumVarArgs(Class<? extends T>... classes)
return null;






share|improve this answer




















  • 1





    Declaring I extends FooBar and returns an I means you need an unchecked cast from FooBar to I in this method

    – yyyy
    Mar 8 at 18:14











  • @yyyy That's absolutely right. I didn't catch that surely because I didn't have to implement the method. Thanks, I've removed the second solution.

    – ernest_k
    Mar 8 at 18:22


















0














I think what you need is:



interface OnlyImpelents<T> 
interface Foo extends OnlyImpelents<Foo>
interface Bar extends OnlyImpelents<Bar>

static <T extends Enum<?> & OnlyImpelents<? super T>> T enumVarArgs(T... values)
return values[0];

public static void main(String[] args)
Foo foo = enumVarArgs(First.A, Second.X);



The marker interface OnlyImpelents<T> prevents any class from implementing both Foo and Bar



The only way to get enum values from a enum class is via reflection, which doesn't need to have a static type. Personally I think this is not a good object-oriented design, using T... as args should be better






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55067839%2fjava-generics-with-multiple-enums-implementing-an-interface%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    If interfaces are your design, then you can make the two interfaces extend a marker interface:



    interface FooBar

    interface Foo extends FooBar

    interface Bar extends FooBar


    And you can use this marker interface:



    public static <T extends Enum<?> & FooBar> 
    FooBar enumVarArgs(Class<? extends T>... classes)
    return null;






    share|improve this answer




















    • 1





      Declaring I extends FooBar and returns an I means you need an unchecked cast from FooBar to I in this method

      – yyyy
      Mar 8 at 18:14











    • @yyyy That's absolutely right. I didn't catch that surely because I didn't have to implement the method. Thanks, I've removed the second solution.

      – ernest_k
      Mar 8 at 18:22















    0














    If interfaces are your design, then you can make the two interfaces extend a marker interface:



    interface FooBar

    interface Foo extends FooBar

    interface Bar extends FooBar


    And you can use this marker interface:



    public static <T extends Enum<?> & FooBar> 
    FooBar enumVarArgs(Class<? extends T>... classes)
    return null;






    share|improve this answer




















    • 1





      Declaring I extends FooBar and returns an I means you need an unchecked cast from FooBar to I in this method

      – yyyy
      Mar 8 at 18:14











    • @yyyy That's absolutely right. I didn't catch that surely because I didn't have to implement the method. Thanks, I've removed the second solution.

      – ernest_k
      Mar 8 at 18:22













    0












    0








    0







    If interfaces are your design, then you can make the two interfaces extend a marker interface:



    interface FooBar

    interface Foo extends FooBar

    interface Bar extends FooBar


    And you can use this marker interface:



    public static <T extends Enum<?> & FooBar> 
    FooBar enumVarArgs(Class<? extends T>... classes)
    return null;






    share|improve this answer















    If interfaces are your design, then you can make the two interfaces extend a marker interface:



    interface FooBar

    interface Foo extends FooBar

    interface Bar extends FooBar


    And you can use this marker interface:



    public static <T extends Enum<?> & FooBar> 
    FooBar enumVarArgs(Class<? extends T>... classes)
    return null;







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 8 at 18:20

























    answered Mar 8 at 17:26









    ernest_kernest_k

    24.4k43050




    24.4k43050







    • 1





      Declaring I extends FooBar and returns an I means you need an unchecked cast from FooBar to I in this method

      – yyyy
      Mar 8 at 18:14











    • @yyyy That's absolutely right. I didn't catch that surely because I didn't have to implement the method. Thanks, I've removed the second solution.

      – ernest_k
      Mar 8 at 18:22












    • 1





      Declaring I extends FooBar and returns an I means you need an unchecked cast from FooBar to I in this method

      – yyyy
      Mar 8 at 18:14











    • @yyyy That's absolutely right. I didn't catch that surely because I didn't have to implement the method. Thanks, I've removed the second solution.

      – ernest_k
      Mar 8 at 18:22







    1




    1





    Declaring I extends FooBar and returns an I means you need an unchecked cast from FooBar to I in this method

    – yyyy
    Mar 8 at 18:14





    Declaring I extends FooBar and returns an I means you need an unchecked cast from FooBar to I in this method

    – yyyy
    Mar 8 at 18:14













    @yyyy That's absolutely right. I didn't catch that surely because I didn't have to implement the method. Thanks, I've removed the second solution.

    – ernest_k
    Mar 8 at 18:22





    @yyyy That's absolutely right. I didn't catch that surely because I didn't have to implement the method. Thanks, I've removed the second solution.

    – ernest_k
    Mar 8 at 18:22













    0














    I think what you need is:



    interface OnlyImpelents<T> 
    interface Foo extends OnlyImpelents<Foo>
    interface Bar extends OnlyImpelents<Bar>

    static <T extends Enum<?> & OnlyImpelents<? super T>> T enumVarArgs(T... values)
    return values[0];

    public static void main(String[] args)
    Foo foo = enumVarArgs(First.A, Second.X);



    The marker interface OnlyImpelents<T> prevents any class from implementing both Foo and Bar



    The only way to get enum values from a enum class is via reflection, which doesn't need to have a static type. Personally I think this is not a good object-oriented design, using T... as args should be better






    share|improve this answer





























      0














      I think what you need is:



      interface OnlyImpelents<T> 
      interface Foo extends OnlyImpelents<Foo>
      interface Bar extends OnlyImpelents<Bar>

      static <T extends Enum<?> & OnlyImpelents<? super T>> T enumVarArgs(T... values)
      return values[0];

      public static void main(String[] args)
      Foo foo = enumVarArgs(First.A, Second.X);



      The marker interface OnlyImpelents<T> prevents any class from implementing both Foo and Bar



      The only way to get enum values from a enum class is via reflection, which doesn't need to have a static type. Personally I think this is not a good object-oriented design, using T... as args should be better






      share|improve this answer



























        0












        0








        0







        I think what you need is:



        interface OnlyImpelents<T> 
        interface Foo extends OnlyImpelents<Foo>
        interface Bar extends OnlyImpelents<Bar>

        static <T extends Enum<?> & OnlyImpelents<? super T>> T enumVarArgs(T... values)
        return values[0];

        public static void main(String[] args)
        Foo foo = enumVarArgs(First.A, Second.X);



        The marker interface OnlyImpelents<T> prevents any class from implementing both Foo and Bar



        The only way to get enum values from a enum class is via reflection, which doesn't need to have a static type. Personally I think this is not a good object-oriented design, using T... as args should be better






        share|improve this answer















        I think what you need is:



        interface OnlyImpelents<T> 
        interface Foo extends OnlyImpelents<Foo>
        interface Bar extends OnlyImpelents<Bar>

        static <T extends Enum<?> & OnlyImpelents<? super T>> T enumVarArgs(T... values)
        return values[0];

        public static void main(String[] args)
        Foo foo = enumVarArgs(First.A, Second.X);



        The marker interface OnlyImpelents<T> prevents any class from implementing both Foo and Bar



        The only way to get enum values from a enum class is via reflection, which doesn't need to have a static type. Personally I think this is not a good object-oriented design, using T... as args should be better







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 8 at 19:24

























        answered Mar 8 at 18:09









        yyyyyyyy

        885




        885



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55067839%2fjava-generics-with-multiple-enums-implementing-an-interface%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

            List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229