문제

I have a class and function defined as the following:

public class Site {
    public EnumSet<?> contents;

    public void determineStates(Site a, Site b) {
        this.contents.clear();
        this.contents.addAll(a.contents);
        this.contents.addAll(b.contents);
        a.contents.removeAll(b.contents);
        this.contents.removeAll(a.contents);
    }
}

For both addAll operations, Eclipse is giving me the following error message:

The method addAll(Collection<? extends capture#6-of ?>) in the type AbstractCollection<capture#6-of ?> is not applicable for the arguments (EnumSet<capture#7-of ?>)

Essentially I need some form of Enum generics and I have just been frustrated with what appear to be limitations. I have a number of Enum types that are incompatible but I desire some container that can hold any type.

I recognize that in this code there is no way to be certain that a, b, and contents are all of the same Enum type, however in actual implementation this should not be problematic.

Any thoughts and possible methods to address my generic Enum problem would be valuable and I would be immensely grateful for your help. Thanks in advance.

도움이 되었습니까?

해결책

The Site class should be generic:

public class Site<E extends Enum<E>> {
    public EnumSet<E> contents;

    public void determineStates(Site<E> a, Site<E> b) {
        this.contents.clear();
        this.contents.addAll(a.contents);
        this.contents.addAll(b.contents);
        a.contents.removeAll(b.contents);
        this.contents.removeAll(a.contents);
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top