在下面的代码中,类型参数D
可以是List<Byte>
或List<List<Byte>>
(它是Fields<?, ?, D>
界面中的第三个通用参数,但我仍然是可能会在那里省略它 - 但它也存在于方法的返回类型中)。似乎无法找到告诉编译器的方法 - 在标记为Unchecked cast
的行中获取//*
警告:
public static <D, K, T extends Enum<T> & Fields<?, ?, D>> List<EnumMap<T, D>>
getEntries(InputStream is, Class<T> fields) throws IOException {
final List<List<Byte>> entries = new ArrayList<List<Byte>>();
// populate "entries"
final boolean hasLists = hasLists(fields);
List<K> daBytes;
if (hasLists) {
daBytes = (List<K>) new ArrayList<EnumMap<T, List<List<Byte>>>>(); //*
} else {
daBytes = (List<K>) new ArrayList<EnumMap<T, List<Byte>>>(); //*
}
final int numOfEntries = entries.size();
for (int currentEntry = 0; currentEntry < numOfEntries; ++currentEntry) {
// add an element in daBytes for this currentEntry
if (hasLists) {
daBytes.add((K) new EnumMap<T, List<List<Byte>>>(fields)); //*
} else {
daBytes.add((K) new EnumMap<T, List<Byte>>(fields)); //*
}
for (T daField : fields.getEnumConstants()) {
List<Byte> field = new ArrayList<Byte>();
// populate "field"
D map = (D) daBytes.get(currentEntry);
if (hasLists) {
List<List<Byte>> fieldEntries = new ArrayList<List<Byte>>();
// populate "fieldEntries"
((EnumMap<T, List<List<Byte>>>) map).put(daField,
fieldEntries); //*
} else {
((EnumMap<T, List<Byte>>) map).put(daField, field); //*
}
}
}
return (List<EnumMap<T, D>>) daBytes; //*
}
如果hasLists
为假,那么我需要D为List<Byte>
否则为List<List<Byte>>
。 daList
变量是List<EnumMap<T, D>>
。现在,对我来说,定义是很自然的:
List<EnumMap<T, D>> daBytes;
但是一旦我这样做并改变:
if (hasLists) {
daBytes = (List<EnumMap<T, D>>) new ArrayList<EnumMap<T, List<List<Byte>>>>();
}
我收到错误:
无法从
ArrayList<EnumMap<T,List<List<Byte>>>>
投射到List<EnumMap<T,D>>
围绕圈子使daBytes成为一个对象,一个List<?>
等,但总是使用直接强制转换或导致警告的通用强制转换。必须有一种方法可以完全编译,没有强制转换
答案 0 :(得分:2)
我做了一些重构,提取了一个&#34;方法对象&#34;正如其他海报所建议的那样。这是所谓的&#34;策略类&#34;的一个实例。设计模式。
无论你对hasLists
进行检查,我都引入了抽象方法。事实证明,您不再需要K
类型参数。
代码在顶部生成一个未经检查的警告,我在hasLists
检查以选择抽象类的实现。
public static <X, T extends Enum<T> & Fields<?, ?, List<X>>>
List<EnumMap<T, List<X>>> getEntries(InputStream is, Class<T> fields) throws IOException {
final List<List<Byte>> entries = new ArrayList<List<Byte>>();
// populate "entries"
FieldsStrategy<X, T> strategy = selectStrategy(fields);
return strategy.getEntries(entries);
}
private static <X, T extends Enum<T> & Fields<?, ?, List<X>>>
FieldsStrategy<X, T> selectStrategy(Class<T> fields) {
final boolean hasLists = hasLists(fields);
return hasLists
? (FieldsStrategy<X, T>) new ByteListFieldsStrategy(fields) //* this is the only unchecked warning
: (FieldsStrategy<X, T>) new ByteFieldsStrategy(fields); //* this is the only unchecked warning
}
private abstract static class FieldsStrategy<X, T extends Enum<T> & Fields<?, ?, List<X>>> {
private Class<T> fields;
public FieldsStrategy(Class<T> fields) {
this.fields = fields;
}
public List<EnumMap<T, List<X>>> getEntries(List<List<Byte>> entries) {
List<EnumMap<T, List<X>>> daBytes = new ArrayList<EnumMap<T, List<X>>>();
final int numOfEntries = entries.size();
for (int currentEntry = 0; currentEntry < numOfEntries; ++currentEntry) {
// add an element in daBytes for this currentEntry
daBytes.add(new EnumMap<T, List<X>>(fields));
for (T daField : fields.getEnumConstants()) {
EnumMap<T, List<X>> map = daBytes.get(currentEntry);
map.put(daField, getFieldData(daField));
}
}
return daBytes;
}
protected abstract List<X> getFieldData(T daField);
}
public static class ByteFieldsStrategy<T extends Enum<T> & Fields<?, ?, List<Byte>>>
extends FieldsStrategy<Byte, T> {
public ByteFieldsStrategy(Class<T> fields) {
super(fields);
}
protected List<Byte> getFieldData(T daField) {
ArrayList<Byte> field = new ArrayList<Byte>();
// populate "field"
return field;
}
}
public static class ByteListFieldsStrategy<T extends Enum<T> & Fields<?, ?, List<List<Byte>>>>
extends FieldsStrategy<List<Byte>, T> {
public ByteListFieldsStrategy(Class<T> fields) {
super(fields);
}
protected List<List<Byte>> getFieldData(T daField) {
List<List<Byte>> fieldEntries = new ArrayList<List<Byte>>();
// populate "fieldEntries"
return fieldEntries;
}
}
您可以通过移动hasLists()
逻辑和交换机来删除唯一剩余的未检查警告,以选择要用于Fields
接口的策略类并在enum
类中实现它。
更新:此处是selectStrategy
和Fields
的更新定义,其中未提出任何警告:
public static interface Fields<T extends Enum<T> & Fields<T, D, K>, D extends Data, K> {
// ....
GetEntries<K, T> selectStrategy();
}
private static <K, T extends Enum<T> & Fields<T, ?, K>>
GetEntries<K, T> selectStrategy(Class<T> fields) {
for (T field : fields.getEnumConstants()) {
return field.selectStrategy();
}
throw new IllegalArgumentException("Enum type has no instances: " + fields);
}
您需要在selectStrategy()
类型中实施enum
,并返回适当的GetByteEntries
或GetByteListEntries
。
您现在可以删除hasLists()
。