mirror of
https://github.com/corda/corda.git
synced 2025-01-03 19:54:13 +00:00
implement java/util/Observ*
This commit is contained in:
parent
c4112db0b9
commit
d2cc630736
73
classpath/java/util/Observable.java
Normal file
73
classpath/java/util/Observable.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
15
classpath/java/util/Observer.java
Normal file
15
classpath/java/util/Observer.java
Normal 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
88
test/Observe.java
Normal 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");
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user