编辑:
我删除了答案的先前内容,因为是错误的。
我找到了一个可以满足您需求的解决方案,它完全是类型安全的并且避免了原始类型,但会产生更复杂的键声明。地图的使用将根据您的需要变得简单。
首先定义两个不同的 Key 接口:第一个声明接受一个泛型类型,它不是键值的类型,而是键本身的类型
public interface Key<T extends Key<T>> {
}
第二个声明定义了一个 Key 类型:
public interface TypedKey<T, K extends Key<K>> extends Key<K> {
}
现在我们可以这样定义 Map:
public class Map<K extends Key<K>> {
<T, K1 extends TypedKey<T, K>> T get(K1 key) {
return null; // TODO implementation
}
}
这需要在实例化时实现 Key 的类型,并且每次调用 get 方法时都需要一个 TypedKey,它与第二个泛型类型的 Key 相同。
通过这个简单的测试类,您可以看到结果:
public class Tester {
static final class PersonKey implements Key<PersonKey> {
private PersonKey() {}
}
static final class HouseKey implements Key<HouseKey> {
private HouseKey() {}
}
static final class WrongKey implements Key<PersonKey> {
private WrongKey() {}
}
static class ExtendableKey implements Key<ExtendableKey> {
}
static class ExtensionKey extends ExtendableKey {
}
static class PersonTypedKey<T> implements TypedKey<T, PersonKey> {
}
static class HouseTypedKey<T> implements TypedKey<T, HouseKey> {
}
/*static class ExtensionTypedKey<T> implements TypedKey<T, ExtensionKey> { // wrong type
}
static class WrongTypedKey<T> implements TypedKey<T, WrongKey> { // wrong type
}*/
public static void main(String[] args) {
Map<PersonKey> personMap = new Map<>();
Map<HouseKey> houseMap = new Map<>();
//Map<WrongKey> wrongMap = new Map<>(); // wrong type
//Map<ExtensionKey> extMap = new Map<>(); // wrong type
PersonTypedKey<String> name = new PersonTypedKey<>();
PersonTypedKey<Integer> age = new PersonTypedKey<>();
HouseTypedKey<String> houseName = new HouseTypedKey<>();
String nameString = personMap.get(name);
Integer ageInt = personMap.get(age);
//String houseString = personMap.get(houseName); // wrong type
//ageInt = personMap.get(name); wrong type
Map<ExtendableKey> extMap = new Map<>();
whatMayBeWrongWithThis();
}
static class OtherPersonTypedKey<T> implements TypedKey<T, PersonKey> {
}
static class ExtendedPersonTypedKey<T> extends PersonTypedKey<T> {
}
public static void whatMayBeWrongWithThis() {
Map<PersonKey> map = new Map<>();
String val1 = map.get(new OtherPersonTypedKey<String>());
String val2 = map.get(new ExtendedPersonTypedKey<String>());
/*
* TypedKey inheritance can be disallowed be declaring the class final, OtherPersonTypedKey can not be disallowed
* with those declarations
*/
}
// if needed you can allow Key inheritance by declaring key, typedKey and map with extends Key<? super K>
}
我还评论了一些无法编译的示例代码。在上面的代码中,您可以看到我按照您的示例定义了 PersonKey 和 HouseKey,然后我定义了它们的类型化版本,这是您将实际使用的实现。我定义了 PersonKey 和 HouseKey final 来防止扩展(我添加了一条注释来解释如何添加继承支持)和一个私有构造函数来防止实例化(如果不需要,可以将其删除)。还有一种名为 whatMayBeWrongWithThis 的方法,它解释了为什么此解决方案可能不是您真正需要的解决方案。
但我也找到了解决这些问题的方法:
首先我们要修改一下TypedKey的定义和Map中的get方法:
public interface TypedKey<T, K extends TypedKey<T, K>> {
}
public class Map<K extends Key<K>> {
<T, K1 extends TypedKey<T, ? extends K>> T get(K1 key) {
return null; // TODO implementation
}
}
现在 TypedKey 不扩展 Key 并且第二个泛型类型必须是 TypedKey 本身的扩展。所以 get 方法更严格,只允许扩展 K 的 TypedKeys。
我们还必须更改 PersonKey 和 HouseKey 的定义,但是为了防止对我们的 Key 进行不必要的扩展,我们必须将它们定义为内部类,以这种方式:
public class PersonKeyWrapper {
public static class PersonKey implements Key<PersonKey> {
private PersonKey() {}
}
public static class PersonTypedKey<T> extends PersonKey implements TypedKey<T, PersonTypedKey<T>> {
}
}
public class HouseKeyWrapper {
public static class HouseKey implements Key<HouseKey> {
private HouseKey() {}
}
public static class HouseTypedKey<T> extends HouseKey implements TypedKey<T, HouseTypedKey<T>> {
}
}
因此 PersonKey 和 HouseKey 可以从外部看到,但由于私有构造函数无法扩展,因此唯一可能的扩展是我们提供的 TypedKeys。
这里有一个示例使用代码:
public class Tester {
/*static class PersonTypedKey<T> implements TypedKey<T, PersonKey> { // no more allowed
}
static class HouseTypedKey<T> implements TypedKey<T, HouseKey> { // no more allowed
}*/
public static void main(String[] args) {
Map<PersonKey> personMap = new Map<>();
Map<HouseKey> houseMap = new Map<>();
PersonTypedKey<String> name = new PersonTypedKey<>();
PersonTypedKey<Integer> age = new PersonTypedKey<>();
HouseTypedKey<String> houseName = new HouseTypedKey<>();
String nameString = personMap.get(name);
Integer ageInt = personMap.get(age);
//String houseString = personMap.get(houseName); // wrong type
//ageInt = personMap.get(name); wrong type
whatMayBeWrongWithThis();
}
/*static class OtherPersonTypedKey<T> implements TypedKey<T, PersonKey> { no more allowed
}*/
static class ExtendedPersonTypedKey<T> extends PersonTypedKey<T> { // allowed, you can declare PersonTypedKey final if you don't wont't to allow this
}
static class OtherPersonTypedKey<T> implements TypedKey<T, PersonTypedKey<T>> {
}
public static void whatMayBeWrongWithThis() {
Map<PersonKey> map = new Map<>();
String val1 = map.get(new OtherPersonTypedKey<String>());
String val2 = map.get(new ExtendedPersonTypedKey<String>());
/*
* OtherPersonTypedKey can not be disallowed with this declaration of PersonTypedKey
*/
}
}
正如您从示例中看到的那样,现在不再允许以前的一些不需要的行为,但我们仍然遇到问题。
这是最终的解决方案:
您需要的唯一更改是在 Wrapper 类中,将它们变成 TypedKeys 的工厂:
public class PersonKeyWrapper {
public static class PersonKey implements Key<PersonKey> {
private PersonKey() {
}
}
private static class PersonTypedKey<T> extends PersonKey implements TypedKey<T, PersonTypedKey<T>> {
}
public static <T> TypedKey<T, ? extends PersonKey> get() {
return new PersonTypedKey<T>();
}
}
public class HouseKeyWrapper {
public static class HouseKey implements Key<HouseKey> {
private HouseKey() {}
}
private static class HouseTypedKey<T> extends HouseKey implements TypedKey<T, HouseTypedKey<T>> {
}
public static <T> TypedKey<T, ? extends HouseKey> get() {
return new HouseTypedKey<T>();
}
}
现在 PersonKey 和 HouseKey 的唯一扩展是隐藏的(私有修饰符),获取它们实例的唯一方法是通过工厂方法。
现在测试类:
public class Tester {
public static void main(String[] args) {
Map<PersonKey> personMap = new Map<>();
Map<HouseKey> houseMap = new Map<>();
TypedKey<String, ? extends PersonKey> name = PersonKeyWrapper.get();
TypedKey<Integer, ? extends PersonKey> age = PersonKeyWrapper.get();
TypedKey<String, ? extends HouseKey> houseName = HouseKeyWrapper.get();
String nameString = personMap.get(name);
Integer ageInt = personMap.get(age);
//String houseString = personMap.get(houseName); // wrong type
//ageInt = personMap.get(name); wrong type
}
/*static class ExtendedPersonTypedKey<T> extends PersonTypedKey<T> { // no more allowed
}
static class OtherPersonTypedKey<T> implements TypedKey<T, PersonTypedKey<T>> { // no more allowed
}*/
}
如您所见,我们将 Map 绑定到了一个非常特定的类型,只能通过 Wrapper 类获得。如果您想在兼容的 TypedKeys 上创建层次结构,唯一的限制是您必须将它们声明为 Wrapper 内的私有内部类并通过工厂方法公开它们。