Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constantly check for change in Java?

How can I check for a boolean to change state over a given period of time, and if a change is made over that time period perform a method?

Can any help please be given in Java.

Thanks.

like image 350
user329671 Avatar asked Apr 30 '10 10:04

user329671


3 Answers

Sounds like you want to wrap a boolean in a class which you can listen for changes on.

class ObservableBoolean {

    // "CopyOnWrite" to avoid concurrent modification exceptions in loop below.
    private final List<ChangeListener> listeners =
            new CopyOnWriteArrayList<ChangeListener>();

    private boolean value;

    public boolean getValue() {
        return value;
    }

    public synchronized void setValue(boolean b) {
        value = b;
        for (ChangeListener cl : listeners)
            cl.stateChanged(new ChangeEvent(this));
    }

    public synchronized void addChangeListener(ChangeListener cl) {
        listeners.add(cl);
    }

    public synchronized void removeChangeListener(ChangeListener cl) {
        listeners.remove(cl);
    }
}

Then simply do:

ObservableBoolean b = new ObservableBoolean();

//...

// Start the "period of time":
b.addChangeListener(iWantToBeNotifiedOfChanges);

// ...

// End the "period of time":
b.removeChangeListener(iWantToBeNotifiedOfChanges);

This is actually a simple case of the MVC pattern (and the observer pattern). The model in this case is the ObservableBoolean and the view would be the "view" that wants to be notified of the changes.

You could also write your own ChangeListener interface if you want to avoid a strange looking javax.swing... import

like image 168
aioobe Avatar answered Nov 11 '22 03:11

aioobe


The easiest way to do this is with a wrapper class...

public class Bool
{
    public Bool(){ _val = false; }
    public Bool(boolean val) { _val = val; }

    public boolean getValue(){ return _val; }
    public void setValue(boolean val){ 
         _changesupport.firePropertyChange("value",_val,_val=val);
    }

    public void addPropertyChangeListener(PropertyChangeListener listener ){
        _changesupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener){
        _changesupport.removePropertyChangeListener( listener );
    }

    private boolean _val = false;
    private final PropertyChangeSupport _changesupport = new PropertyChangeSupport(this);
}

This is a common pattern in Swing, and you can use PropertyChangeSupport to simplify the creation of objects on which you can observe and listen for property changes. With such classes, you can register a PropertyChangeListener to handle the resulting PropertyChangeEvents that result.

like image 36
Michael Aaron Safyan Avatar answered Nov 11 '22 04:11

Michael Aaron Safyan


use Timer or write your own class extending Thread.

Googling those things should give you good start

edit : From question it is not quite clear what are his intends. Maybe he wants to invoke some method in exact period of time, not immediatelly after event. For this pattern, I'd still rather stick to timer, or write my own Thread. Writing own Thread is still best way of interpreting specific user requirements. On the other hand, if this is really case of listening to events, my Timer pattern would be completely wrong.

example : I want to update database (imagine web browser game) every 5 minutes, if users requests so (reques = true). With false request, I don't need to update database. Immediatelly update database (f.e. stats in game Tribal Wars with thoushands of players) is totally overkill.

like image 43
Xorty Avatar answered Nov 11 '22 02:11

Xorty