From a2413cc4e54161204abc93192abae2cdbcef9c2e Mon Sep 17 00:00:00 2001 From: Steve Hannah Date: Sat, 30 May 2015 00:43:08 -0700 Subject: [PATCH] Added java.util.Observer and java.util.Observable. --- .../teavm/classlib/java/util/TObservable.java | 141 ++++++++++++++++++ .../teavm/classlib/java/util/TObserver.java | 35 +++++ 2 files changed, 176 insertions(+) create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/java/util/TObservable.java create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/java/util/TObserver.java diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TObservable.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TObservable.java new file mode 100644 index 000000000..dc65d1316 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TObservable.java @@ -0,0 +1,141 @@ +/* + * Copyright 2015 Steve Hannah. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.util; + +/** + * + * @author shannah + */ +public class TObservable { + TList observers = new TArrayList(); + + boolean changed = false; + + /** + * Constructs a new {@code Observable} object. + */ + public TObservable() { + super(); + } + + /** + * Adds the specified observer to the list of observers. If it is already + * registered, it is not added a second time. + * + * @param observer + * the Observer to add. + */ + public void addObserver(TObserver observer) { + if (observer == null) { + throw new NullPointerException(); + } + synchronized (this) { + if (!observers.contains(observer)) + observers.add(observer); + } + } + + /** + * Clears the changed flag for this {@code Observable}. After calling + * {@code clearChanged()}, {@code hasChanged()} will return {@code false}. + */ + protected void clearChanged() { + changed = false; + } + + /** + * Returns the number of observers registered to this {@code Observable}. + * + * @return the number of observers. + */ + public int countObservers() { + return observers.size(); + } + + /** + * Removes the specified observer from the list of observers. Passing null + * won't do anything. + * + * @param observer + * the observer to remove. + */ + public synchronized void deleteObserver(TObserver observer) { + observers.remove(observer); + } + + /** + * Removes all observers from the list of observers. + */ + public synchronized void deleteObservers() { + observers.clear(); + } + + /** + * Returns the changed flag for this {@code Observable}. + * + * @return {@code true} when the changed flag for this {@code Observable} is + * set, {@code false} otherwise. + */ + public boolean hasChanged() { + return changed; + } + + /** + * If {@code hasChanged()} returns {@code true}, calls the {@code update()} + * method for every observer in the list of observers using null as the + * argument. Afterwards, calls {@code clearChanged()}. + *

+ * Equivalent to calling {@code notifyObservers(null)}. + */ + public void notifyObservers() { + notifyObservers(null); + } + + /** + * If {@code hasChanged()} returns {@code true}, calls the {@code update()} + * method for every Observer in the list of observers using the specified + * argument. Afterwards calls {@code clearChanged()}. + * + * @param data + * the argument passed to {@code update()}. + */ + @SuppressWarnings("unchecked") + public void notifyObservers(Object data) { + int size = 0; + TObserver[] arrays = null; + synchronized (this) { + if (hasChanged()) { + clearChanged(); + size = observers.size(); + arrays = new TObserver[size]; + observers.toArray(arrays); + } + } + if (arrays != null) { + for (TObserver observer : arrays) { + observer.update(this, data); + } + } + } + + /** + * Sets the changed flag for this {@code Observable}. After calling + * {@code setChanged()}, {@code hasChanged()} will return {@code true}. + */ + protected void setChanged() { + changed = true; + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TObserver.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TObserver.java new file mode 100644 index 000000000..7de864f4f --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TObserver.java @@ -0,0 +1,35 @@ +/* + * Copyright 2015 Steve Hannah. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.classlib.java.util; + + +/** + * + * @author shannah + */ +public interface TObserver { + /** + * This method is called if the specified {@code Observable} object's + * {@code notifyObservers} method is called (because the {@code Observable} + * object has been updated. + * + * @param observable + * the {@link Observable} object. + * @param data + * the data passed to {@link Observable#notifyObservers(Object)}. + */ + void update(TObservable observable, Object data); +}