Pergunta

Existe uma versão de qualquer java.beans.PropertyChangeSupport ou com.jgoodies.binding.beans.ExtendedPropertyChangeSupport O que é útil para apoiar os ouvintes de mudança ao longo de um mapa ou enummap? (Uma loja de valores-chave com teclas limitadas conhecidas e muda os ouvintes para todos os valores no mapa)

Eu realmente não preciso de acesso do tipo feijão, tenho várias estatísticas diferentes como:

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

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

onde eu quero publicar uma interface como:

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()
     */
}

Então eu poderia usar um KeyValueStore<StatisticType, Integer> no meu aplicativo.

Existe alguma maneira conveniente de fazer isso? Minha cabeça está girando em círculos e está enrolando minha energia tentando reinventar a roda sobre essas coisas.

Foi útil?

Solução

A menos que haja uma razão premente não, eu estenderia Map E use put e get ao invés de setValue e getValue.

Eu tenho uma interface que costumo usar:

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

Com isso, sua interface se torna:

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

Então sua implementação acaba parecendo algo assim:

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);
    }
}

Não são mostrados os 4 métodos para a funcionalidade de reprodução da propriedade que simplesmente delega a changer.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top