我正在尝试在地图中设置属性的值,但出现以下错误:
The method setValue(capture#7-of ?) in the type WritableValue<capture#7-of ?> is not applicable for the arguments (capture#8-of ?)
这是我的代码:
Map<String, Property<?>> map1 = new HashMap<String, Property<?>>();
Map<String, Property<?>> map2 = new HashMap<String, Property<?>>();
map1.put("key1", new SimpleIntegerProperty(5));
map1.put("key2", new SimpleStringProperty("hi")); //I need multiple property types in this Map, all of which implement Property
map2.put("key1", new SimpleIntegerProperty(5));
map2.put("key2", new SimpleStringProperty("hi"));
//I can assume that the value of the properties with the same key are of the same type
map2.get("key1").setValue(map1.get("key1").getValue()); //Error
map2.get("key2").setValue(map1.get("key2").getValue()); //Error
我不能这样做,只能复制它们的值:
map2.put("key1", map1.get("key1"));
map2.put("key2", map1.get("key2"));
没有地图,我可以进一步简化此操作:
Property<?> p1 = new SimpleIntegerProperty(5);
Property<?> p2 = new SimpleIntegerProperty(10);
p1.setValue(p2.getValue());//Same (basic) error
p1.setValue(new Object());//Same (basic) error
我正在使用Java 1.8 JDK
问题在于,您尝试执行的操作本质上不是类型安全的。您可能从编程逻辑中知道与"key1"
in关联的属性map1
的类型与与"key1"
in关联的属性的类型相同map2
,但是编译器无法保证这一事实。因此,使用当前的结构,唯一的选择就是放弃编译时安全性。
这里的根本问题是,Map
您的API不能满足您的要求(即使您需要的是基本功能)。Map
是同质容器,意味着给定映射中的所有值都属于同一类型。这是由API强制执行的:public V get(K key);
并且public void put(K key, V value);
始终使用相同的type V
,该类型对于任何单个Map
实例都是固定的。您真正想要的是一个异构容器,其中的值根据密钥而变化。因此,您需要一个API,该API在V
容器实例中不是固定的,而是在每次调用方法get
和时更改的put
,具体取决于键的值。所以,你需要的东西,其中get
和put
方法是通用方法:
public interface Container<K> { // K is the type of the key...
public <V> V get(K key) ;
public <V> void put(K key, V value);
}
Josh Bloch的“ Effective Java”中记录了这种实现,称为“ Typesafe异构容器”模式。
首先为您的密钥定义一个类型,该类型维护对应属性的类型:
/**
* @param <T> The type associated with this key
* @param <K> The actual type of the key itself
*/
public class TypedKey<T, K> {
private final Class<T> type ;
private final K key ;
public TypedKey(Class<T> type, K key) {
if (type == null || key == null) {
throw new NullPointerException("type and key must be non-null");
}
this.type = type ;
this.key = key ;
}
@Override
public boolean equals(Object o) {
if (o == null) return false ;
if (! (o instanceof TypedKey)) {
return false ;
}
TypedKey<?, ?> other = (TypedKey<?, ?>) o ;
return other.type.equals(type) && other.key.equals(key);
}
@Override
public int hashCode() {
return Objects.hash(type, key);
}
}
这T
将是属性的类型,并且K
是键的实际类型。因此,您将修改代码以
// String key to map to Property<Number>:
TypedKey<Number, String> key1 = new TypedKey<>(Number.class, "key1");
// String key to map to Property<String>:
TypedKey<String, String> key2 = new TypedKey<>(String.class, "key2");
现在定义一个容器类来充当地图。这里的基本思想是有两种方法:
public <T> void put(TypedKey<T, K> key, Property<T> property)
public <T> Property<T> get(TypedKey<T, K> key)
实现非常简单:
/**
* @param <K> The type of the key in the TypedKey
*/
public class TypedPropertyMap<K> {
private final Map<TypedKey<?, K>, Property<?>> map = new HashMap<>();
public <T> void put(TypedKey<T, K> key, Property<T> property) {
map.put(key, property);
}
@SuppressWarnings("unchecked")
public <T> Property<T> get(TypedKey<T, K> key) {
// by virtue of the API we defined, the property associated with
// key must be a Property<T> (even though the underlying map does not know it):
return (Property<T>) map.get(key);
}
}
这里有一些微妙之处。因为基础地图是private
,所以我们可以确保唯一的访问方法是通过put
和get
方法。因此,当我们get()
使用a调用基础映射时TypedKey<T,K>
,可以确保对应的属性必须是(null
或)a Property<T>
(因为这是唯一允许编译器早先插入的内容)。因此,即使编译器不知道它,我们也可以确保强制转换成功,并且可以@SuppressWarnings
证明是正确的。
现在,如果我创建一个TypedPropertyMap<K>
(K
这里只是实际键的类型),则可以保证在编译时map.get(key1)
返回Property<Number>
(因为key1
编译时类型为TypedKey<Number, String>
)并map.get(key2)
返回Property<String>
(因为key2
编译时类型为TypedKey<String, String>
)。
这是一个完整的可运行示例:
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class TypedPropertyMapTest {
public static void main(String[] args) {
TypedPropertyMap<String> map1 = new TypedPropertyMap<>();
TypedPropertyMap<String> map2 = new TypedPropertyMap<>();
TypedKey<Number, String> key1 = new TypedKey<>(Number.class, "key1");
TypedKey<String, String> key2 = new TypedKey<>(String.class, "key2");
map1.put(key1, new SimpleIntegerProperty(5));
map1.put(key2, new SimpleStringProperty("hi"));
map2.put(key1, new SimpleIntegerProperty());
map2.put(key2, new SimpleStringProperty());
map2.get(key1).setValue(map1.get(key1).getValue());
map2.get(key2).setValue(map1.get(key2).getValue());
System.out.println(map2.get(key1).getValue());
System.out.println(map2.get(key2).getValue());
}
/**
* @param <T> The type associated with this key
* @param <K> The actual type of the key itself
*/
public static class TypedKey<T, K> {
private final Class<T> type ;
private final K key ;
public TypedKey(Class<T> type, K key) {
if (type == null || key == null) {
throw new NullPointerException("type and key must be non-null");
}
this.type = type ;
this.key = key ;
}
@Override
public boolean equals(Object o) {
if (o == null) return false ;
if (! (o instanceof TypedKey)) {
return false ;
}
TypedKey<?, ?> other = (TypedKey<?, ?>) o ;
return other.type.equals(type) && other.key.equals(key);
}
@Override
public int hashCode() {
return Objects.hash(type, key);
}
}
/**
* @param <K> The type of the key in the TypedKey
*/
public static class TypedPropertyMap<K> {
private final Map<TypedKey<?, K>, Property<?>> map = new HashMap<>();
public <T> void put(TypedKey<T, K> key, Property<T> property) {
map.put(key, property);
}
@SuppressWarnings("unchecked")
public <T> Property<T> get(TypedKey<T, K> key) {
// by virtue of the API we defined, the property associated with
// key must be a Property<T> (even though the underlying map does not know it):
return (Property<T>) map.get(key);
}
}
}
请注意,ObservableMap
由于介绍中概述的相同原因,实际上很难做到这一点:ObservableMap
接口定义了同类方法(实际上是从Map
接口继承的),您无法以满足要求的方式来实现。但是,您可以轻松地实现此工具javafx.beans.Observable
,这将允许您向其注册InvalidationListener
,并在绑定中使用它:
public class TypedPropertyMap<K> implements Observable {
private final ObservableMap<TypedKey<?, K>, Property<?>> map = FXCollections.observableHashMap();
@Override
public void addListener(InvalidationListener listener) {
map.addListener(listener);
}
@Override
public void removeListener(InvalidationListener listener) {
map.removeListener(listener);
}
// remaining code as before...
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句