[ 
https://issues.apache.org/jira/browse/LANG-819?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13862742#comment-13862742
 ] 

Shevek commented on LANG-819:
-----------------------------

You misunderstand type bounds. Anyway, here is an example.

public class EnumTest {
    enum Foo {
        A, B, C;
    }

    public static void main(String[] args) {
        Set<? extends Foo> set = EnumSet.allOf(Foo.class);
        // set.add(Foo.B);  // Fails to compile: Set is typed read-only. See 
references above.
        EnumUtils.generateBitVector(Foo.class, set);    // Also fails to 
compile, even though legitimate.
        generateBitVector(Foo.class, set); // Compiles fine, as method has 
correct type.
    }

    // Note method signature now has correct type for a method which only reads 
from the given set.
    public static <E extends Enum<E>> long generateBitVector(Class<E> 
enumClass, Iterable<? extends E> values) {
        long total = 0;
        for (E constant : values)
            total |= 1 << constant.ordinal();
        return total;
    }
}


> EnumUtils.generateBitVector needs a "? extends"
> -----------------------------------------------
>
>                 Key: LANG-819
>                 URL: https://issues.apache.org/jira/browse/LANG-819
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.*
>    Affects Versions: 3.0.1
>            Reporter: Shevek
>            Priority: Minor
>
>     public static <E extends Enum<E>> long generateBitVector(Class<E> 
> enumClass, Iterable<E> values) {
> Should be Iterable<? extends E>.
> This is because although no subclasses of E can exist, the "? extends" is a 
> common idiom for marking the collection as readonly, or not "owned" by the 
> current object.



--
This message was sent by Atlassian JIRA
(v6.1.5#6160)

Reply via email to