From d2cc630736aa9efa7ce10c94b75865aede0b6709 Mon Sep 17 00:00:00 2001 From: Joshua Warner Date: Mon, 20 Jan 2014 10:17:22 -0700 Subject: [PATCH] implement java/util/Observ* --- classpath/java/util/Observable.java | 73 ++++++++++++++++++++++++ classpath/java/util/Observer.java | 15 +++++ test/Observe.java | 88 +++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 classpath/java/util/Observable.java create mode 100644 classpath/java/util/Observer.java create mode 100644 test/Observe.java diff --git a/classpath/java/util/Observable.java b/classpath/java/util/Observable.java new file mode 100644 index 0000000000..12ca1199d8 --- /dev/null +++ b/classpath/java/util/Observable.java @@ -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 observers = new ArrayList(); + 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; + } + +} \ No newline at end of file diff --git a/classpath/java/util/Observer.java b/classpath/java/util/Observer.java new file mode 100644 index 0000000000..e63424864a --- /dev/null +++ b/classpath/java/util/Observer.java @@ -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); +} \ No newline at end of file diff --git a/test/Observe.java b/test/Observe.java new file mode 100644 index 0000000000..75dc0063a8 --- /dev/null +++ b/test/Observe.java @@ -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"); + + } +} \ No newline at end of file