java:针对Map或EnumMap的PropertyChangeSupport? [英] java: PropertyChangeSupport for Map or EnumMap?

查看:69
本文介绍了java:针对Map或EnumMap的PropertyChangeSupport?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有 java.beans.PropertyChangeSupport com.jgoodies.binding.beans.ExtendedPropertyChangeSupport 对支持Map或EnumMap的变更侦听器有帮助吗? (具有已知有限键的键值存储,并且更改了地图中所有值的侦听器)

Is there a version of either java.beans.PropertyChangeSupport or com.jgoodies.binding.beans.ExtendedPropertyChangeSupport which is helpful for supporting change listeners along the lines of a Map or EnumMap? (a key-value store with known limited keys and change listeners for all the values in the map)

我真的不需要bean类型的访问,我有一个各种不同统计信息的数量,例如:

I don't really need beans-type access, I have a number of different statistics sort of like:

interface hasName() {
    public String getName();
}

enum StatisticType implements hasName {
    MILES_DRIVEN, BURGERS_SERVED, CUSTOMERS_HELPED, BIRDS_WATCHED;
    @Override public String getName() { return name(); }
}

我要在其中发布界面的地方:

where I want to publish an interface like:

interface KeyValueStore<K extends hasName,V>
{
    void setValue(K key, V value);
    V getValue(K key);

    void addPropertyChangeListener(PropertyChangeListener listener);
    void removePropertyChangeListener(PropertyChangeListener listener);        
    /*
     * when setValue() is called, this should send PropertyChangeEvents
     * to all listeners, with the property name of K.getName()
     */
}

所以我可以使用 KeyValueStore< StatisticType,Integer> 在我的应用程序中。

so I could use a KeyValueStore<StatisticType, Integer> in my application.

有没有方便的方法?我的头转了一圈,正在浪费我的精力,试图重新发明这种东西。

Is there any convenient way to do this? My head is spinning around in circles and it's sapping my energy trying to reinvent the wheel on this stuff.

推荐答案

除非有迫切的理由,否则我将扩展 Map 并使用 put get 代替 setValue getValue

Unless there is a pressing reason not to, I would extend Map and use put and get instead of setValue and getValue.

我有一个经常使用的界面:

I have an interface I often use:

public interface PropertyChangeNotification {
    void addPropertyChangeListener(String property, PropertyChangeListener listener);
    void removePropertyChangeListener(String property, PropertyChangeListener listener);
    void addPropertyChangeListener(PropertyChangeListener listener);
    void removePropertyChangeListener(PropertyChangeListener listener);
}

这样,您的界面将变为:

With that, your interface becomes:

interface KeyValueStore<K extends hasName,V>
    extends Map<K,V>, PropertyChangeNotification
{
}

然后您的实现最终看起来像这样:

Then your implementation winds up looking something like this:

public class MyKeyStore<K extends hasName, V>
    extends HashMap<K,V>
    implements KeyValueStore<K,V>
{
    private PropertyChangeSupport changer = new PropertyChangeSupport(this);

    public void put(K key, V value)
    {
        V old = get(K);
        super.put(key,value);
        changer.firePropertyChange(key.getName(), value, old);
    }
}

未显示PropertyChangeNotification功能的4种方法,只需委派给 changeer

Not shown are the 4 methods for the PropertyChangeNotification functionality that simply delegate to changer.

这篇关于java:针对Map或EnumMap的PropertyChangeSupport?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆