Skip to content
Browse files

implement java/util/Observ*

  • Loading branch information...
1 parent c4112db commit d2cc630736aa9efa7ce10c94b75865aede0b6709 @joshuawarner32 joshuawarner32 committed Jan 20, 2014
Showing with 176 additions and 0 deletions.
  1. +73 −0 classpath/java/util/Observable.java
  2. +15 −0 classpath/java/util/Observer.java
  3. +88 −0 test/Observe.java
View
73 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<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
15 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);
+}
View
88 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");
+
+ }
+}

0 comments on commit d2cc630

Please sign in to comment.
Something went wrong with that request. Please try again.