implement java/util/Observ*

This commit is contained in:
Joshua Warner 2014-01-20 10:17:22 -07:00
parent c4112db0b9
commit d2cc630736
3 changed files with 176 additions and 0 deletions

View File

@ -0,0 +1,73 @@
/* Copyright (c) 2008-2013, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util;
public class Observable {
private List<Observer> observers = new ArrayList<Observer>();
private boolean changed = false;
public void addObserver(Observer o) {
if(o == null) {
throw new NullPointerException();
}
synchronized(this) {
if(!observers.contains(o)) {
observers.add(o);
}
}
}
public synchronized int countObservers() {
return observers.size();
}
public void deleteObserver(Observer o) {
if(o == null) {
throw new NullPointerException();
}
synchronized(this) {
observers.remove(o);
}
}
public void notifyObservers() {
notifyObservers(null);
}
public synchronized void notifyObservers(Object value) {
Observer[] obsArray = null;
synchronized(this) {
if(hasChanged()) {
clearChanged();
obsArray = observers.toArray(new Observer[observers.size()]);
}
}
if(obsArray != null) {
for(Observer obs : obsArray) {
obs.update(this, value);
}
}
}
public boolean hasChanged() {
return changed;
}
protected void setChanged() {
changed = true;
}
protected void clearChanged() {
changed = false;
}
}

View File

@ -0,0 +1,15 @@
/* Copyright (c) 2008-2013, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util;
public interface Observer {
public void update(Observable o, Object arg);
}

88
test/Observe.java Normal file
View File

@ -0,0 +1,88 @@
import java.util.Observer;
import java.util.Observable;
public class Observe {
private static void expect(boolean v) {
if (! v) throw new RuntimeException();
}
private static class MyObservable extends Observable {
private String value;
public MyObservable(String value) {
this.value = value;
}
public void set(String newValue) {
if(!value.equals(newValue)) {
value = newValue;
setChanged();
notifyObservers(value);
}
}
}
private static class MyObserver implements Observer {
private int count = 0;
private Observable expectedObs;
private Object expectedValue = null;
private boolean expected = false;
public MyObserver(Observable expectedObs) {
this.expectedObs = expectedObs;
}
public void update(Observable obs, Object value) {
expect(expectedObs == expectedObs);
expect(expected);
expect(value == expectedValue);
expectNothing();
}
public void noUpdate() {
expect(!expected);
}
public void expect(Object value) {
expected = true;
expectedValue = value;
}
public void expectNothing() {
expected = false;
}
}
public static void main(String[] args) {
MyObservable obs = new MyObservable("test");
MyObserver o = new MyObserver(obs);
MyObserver o2 = new MyObserver(obs);
obs.set("a");
obs.addObserver(o);
o.expect("b");
obs.set("b");
o.noUpdate();
obs.addObserver(o2);
o.expect("c");
o2.expect("c");
obs.set("c");
o.noUpdate();
o2.noUpdate();
obs.deleteObserver(o);
o.expectNothing();
o2.expect("d");
obs.set("d");
o2.noUpdate();
obs.deleteObserver(o2);
o.expectNothing();
o2.expectNothing();
obs.set("e");
}
}