使用泛型与实现相同接口的枚举类的集合

时间:2011-12-28 15:14:18

标签: java generics enums guava

我试图通过使用Guava的reverse lookup迭代Field es列表,在实现相同Class接口的几个枚举类上执行Maps.uniqueIndex

  Field valueOfSearchName = null;
  for (final Class<? extends Enum<?>> clazz : ImmutableList.of(
      EntityField.class,
      AddressField.class,
      PersonFunctionType.class)) {
    valueOfSearchName = Fields.valueOfSearchName(clazz, term.field()); // error
    if (valueOfSearchName != null) {
      // do something...
      break;
    }
  }

我不想在所有枚举类中重复相同的代码(用于制作索引和执行查找),因此我使用包含Fields方法的辅助静态类Fields.valueOfSearchName

  public static <E extends Enum<E> & Field> Field valueOfSearchName(
      final Class<E> clazz, final String searchName) {
    // TODO: cache the index
    final ImmutableMap<String, E> index = Maps.uniqueIndex(
        EnumSet.allOf(clazz), GET_SEARCH_NAME_FUNCTION);
    return index.get(searchName);
  }

不幸的是,Eclipse显示错误:

Bound mismatch: 
The generic method valueOfSearchName(Class<E>, String) of type Fields is not 
applicable for the arguments (Class<capture#1-of ? extends Enum<?>>, String).
The inferred type capture#1-of ? extends Enum<?> is not a valid substitute
for the bounded parameter <E extends Enum<E> & Field>

for-each循环问题是Class<? extends Enum<?>> clazz(不匹配字段),但我不知道如何处理这种情况(显然我无法添加{{1}到& Field)。

3 个答案:

答案 0 :(得分:4)

考虑Class<? extends List<?>Class<? extends List<?>有两个通配符,而<E extends List<E>> Class<E>只有通用参数。前者将承认Class<ArrayList<String>>。因此,如果没有为枚举做一些额外的特殊操作,那么类型就不兼容了。

如何解决?额外的间接层!

public final class MetaEnum<E extends Enum<E>> {
    private final E clazz;
    public static <E extends Enum<E>> MetaEnum<E> of(E clazz) {
        return clazz;
    }
    private MetaEnum(E clazz) {
        this.clazz = clazz;
    }
    public E clazz() {
        return clazz;
    }
    // ...
}

for (final MetaEnum<?> meta : ImmutableList.of(
    MetaEnum.of(EntityField       .class),
    MetaEnum.of(AddressField      .class),
    MetaEnum.of(PersonFunctionType.class)
)) {
    Field valueOfSearchName = Fields.valueOfSearchName(
        meta.clazz(), term.field()
    );
    ...

(通常的堆栈溢出免责声明:与尝试编译不同。)

答案 1 :(得分:2)

Tom Hawtin's answer的启发我创建了包含Class es的包装类,但只包含带有签名<E extends Enum<E> & Field>的包装类:

public final static class FieldEnumWrapper<E extends Enum<E> & Field> {
  private final Class<E> clazz;
  private final ImmutableMap<String, E> index;

  public static <E extends Enum<E> & Field> 
      FieldEnumWrapper<E> of(final Class<E> clazz) {
    return new FieldEnumWrapper<E>(clazz);
  }

  private FieldEnumWrapper(final Class<E> clazz) {
    this.clazz = clazz;
    this.index = Maps.uniqueIndex(
        EnumSet.allOf(clazz), new Function<E, String>() {
          @Override
          public String apply(final E input) {
            return input.searchName();
          }
        });
  }

  public Class<E> clazz() {
    return clazz;
  }

  public Field valueOfSearchName(final String searchName) {
    return index.get(searchName);
  }
}

现在:

for (final FieldEnumWrapper<?> fieldEnum : ImmutableList.of(
    FieldEnumWrapper.of(EntityField.class),
    FieldEnumWrapper.of(AddressField.class),
    FieldEnumWrapper.of(PersonFunctionType.class))) {
  valueOfSearchName = fieldEnum.valueOfSearchName("POD_I_OS_PARTNER");
  // ...

类型安全不恰当地使用FieldEnumWrapper的静态工厂:

FieldEnumWrapper.of(NotEnumAndFieldClass.class)

生成编译错误。

此外,valueOfSearchName现在是FieldEnumWrapper的方法,使助手类更有意义。

答案 2 :(得分:0)

可能是这样的:

import java.util.*;
class N {
    static int n;
}
interface HasField {
    int getField();
}
enum Color implements HasField {
    r, g, b;
    public int getField() {
        return field;
    }
    private int field = N.n++;
}
enum Day implements HasField {
    m, t, w, th, f, sa, su;
    public int getField() {
        return field;
    }
    private int field = N.n++;
}
    class Helper {
    Helper(Set<HasField> set) {
        for (HasField hasField : set)
            if (hasField instanceof Enum) {
                Enum<?> e = (Enum<?>) hasField;
                for (Object o : e.getDeclaringClass().getEnumConstants()) {
                    map.put(((HasField) o).getField(), (Enum<?>) o);
                }
            } else
                throw new RuntimeException(hasField + " is not an enum!");
    }
    final Map<Integer, Enum<?>> map = new TreeMap<Integer, Enum<?>>();
}
public class Main {
    public static void main(String[] args) {
        Set<HasField> set = new LinkedHashSet<HasField>();
        set.add(Color.r);
        set.add(Day.m);
        Helper helper = new Helper(set);
        for (int i = 0; i < N.n; i++)
            System.out.println(i + " " + helper.map.get(i));
    }
}