实现ObservableValue

时间:2014-10-11 10:41:11

标签: javafx listener observable changelistener

我有这个对象:

public class Oggetto{
    private int value;
    private boolean valid;

    public Oggetto(int value, boolean valid) {
        this.value = value;
        this.valid = valid;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public boolean isValid() {
        return valid;
    }

    public void setValid(boolean valid) {
        this.valid = valid;
    }    
}

我希望实现一个Observable对象,当内部更改时触发事件 这里是可观察对象:

public class OggettoOsservabile implements ObservableValue<Oggetto>{

    private Oggetto value;

    OggettoOsservabile(int i, boolean b) {
        this.value=new Oggetto(i, b);
    }
    @Override
    public void addListener(ChangeListener<? super Oggetto> listener) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    @Override
    public void removeListener(ChangeListener<? super Oggetto> listener) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    @Override
    public Oggetto getValue() {
        return value;
    }
    @Override
    public void addListener(InvalidationListener listener) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    @Override
    public void removeListener(InvalidationListener listener) {
        throw new UnsupportedOperationException("Not supported yet."); 
    }  
}

我不知道如何继续检测班级“Oggetto”中的变化并向registeres听众发送通知。

OggettoOsservabile oggetto = new OggettoOsservabile(1, false);              
oggetto.addListener(new ChangeListener<Oggetto>() {
     public void changed(ObservableValue<? extends Oggetto> observable, Oggetto oldValue, Oggetto newValue) {
           System.out.println("changed " + oldValue + "->" + newValue);   
     }
});

2 个答案:

答案 0 :(得分:6)

使用标准JavaFX属性实现您的Oggetto类:

import javafx.beans.property.BooleanProperty ;
import javafx.beans.property.IntegerProperty ;
import javafx.beans.property.SimpleBooleanProperty ;
import javafx.beans.property.SimpleIntegerProperty ;

public class Oggetto {

    private final IntegerProperty value = new SimpleIntegerProperty() ;

    public final IntegerProperty valueProperty() {
        return value ;
    }

    public final int getValue() {
        return value.get();
    }

    public final void setValue(int value) {
        this.value.set(value);
    }

    private final BooleanProperty valid = new SimpleBooleanProperty();

    public final BooleanProperty validProperty() {
        return valid ;
    }

    public final boolean isValid() {
        return valid.get();
    }

    public final void setValid(boolean valid) {
        this.valid.set(valid);
    }

    public Oggetto(int value, boolean valid) {
        setValue(value);
        setValid(valid);
    }
}

这可能就是您所需要的,因为您可以只观察各个属性。但是如果你想要一个通知失效监听器的类,如果其中一个属性发生了变化,你可以扩展ObjectBinding

import javafx.beans.binding.ObjectBinding ;

public class OggettoObservable extends ObjectBinding {

    private final Oggetto value ;

    public OggettoObservable(int value, boolean valid) {
        this.value = new Oggetto(value, valid);
        bind(this.value.valueProperty(), this.value.validProperty());
    }

    @Override
    public Oggetto computeValue() {
        return value ;
    }
}

答案 1 :(得分:-1)

# cutoff function
from functools import reduce
from operator import mul

threshold = 1e-5

def cutoff(args):
    if args:
        return reduce(mul, args) < threshold
    return False

# alternative implementation of itertools.product with cutoff
def product(*args, **kwds):
    def cycle(values, uplevel):
        for prefix in uplevel:       # cycle through all upper levels
            if cutoff(prefix):
                break
            for current in values:   # restart iteration of current level
                result = prefix + (current,)
                if cutoff(result):
                    break
                yield result

    stack = iter(((),))             
    for level in tuple(map(tuple, args)) * kwds.get('repeat', 1):
        stack = cycle(level, stack)  # build stack of iterators
    return stack

# your code here
li=[[0.99, 0.002],
    [0.98, 0.0008, 0.0007],
    [0.97, 0.009, 0.001],
    [0.86, 0.001]]

for a in product(*li):
    p = reduce(mul, a)
    print (p, a)