From c2516c02cc1bd79ce11638488acc088d25cf6748 Mon Sep 17 00:00:00 2001 From: Brian Vosburgh Date: Wed, 21 Oct 2015 11:42:00 -0400 Subject: rework ObjectReferences --- .../AbstractModifiableObjectReference.java | 57 ++++ .../reference/AbstractObjectReference.java | 42 ++- .../internal/reference/SimpleObjectReference.java | 65 ++++- .../internal/reference/SynchronizedObject.java | 305 ++++++++++++++++----- .../reference/ModifiableObjectReference.java | 18 +- .../common/utility/reference/ObjectReference.java | 33 ++- 6 files changed, 438 insertions(+), 82 deletions(-) create mode 100644 common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/AbstractModifiableObjectReference.java diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/AbstractModifiableObjectReference.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/AbstractModifiableObjectReference.java new file mode 100644 index 0000000000..22af0639b6 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/AbstractModifiableObjectReference.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright (c) 2013, 2015 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.common.utility.internal.reference; + +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.reference.ModifiableObjectReference; + +/** + * Implement some of the methods in {@link ModifiableObjectReference} that can + * be defined in terms of the other methods. + * Subclasses need only implement
true
- * if the specified value was achieved; return false
+ * if the value became a member of the set; return false
* if a time-out occurred.
- * If the value is already the specified value, return true immediately.
+ * If the value already belongs to the set, return true
immediately.
* If the time-out is zero, wait indefinitely.
*/
- public boolean waitUntilValueIs(V v, long timeout) throws InterruptedException {
+ public boolean waitUntil(Predicate super V> predicate, long timeout) throws InterruptedException {
synchronized (this.mutex) {
- return this.waitUntilValueIs_(v, timeout);
+ return this.waitUntil_(predicate, timeout);
}
}
/**
* Pre-condition: synchronized
*/
- private boolean waitUntilValueIs_(V v, long timeout) throws InterruptedException {
+ private boolean waitUntil_(Predicate super V> predicate, long timeout) throws InterruptedException {
if (timeout == 0L) {
- this.waitUntilValueIs_(v); // wait indefinitely until notified
+ this.waitUntil_(predicate); // wait indefinitely until notified
return true; // if it ever comes back, the condition was met
}
long stop = System.currentTimeMillis() + timeout;
long remaining = timeout;
- while (ObjectTools.notEquals(this.value, v) && (remaining > 0L)) {
+ while (( ! predicate.evaluate(this.value)) && (remaining > 0L)) {
this.mutex.wait(remaining);
remaining = stop - System.currentTimeMillis();
}
- return ObjectTools.equals(this.value, v);
+ return predicate.evaluate(this.value);
}
/**
- * Suspend the current thread until the value changes to something
- * other than the specified value or the specified time-out occurs.
+ * Suspend the current thread until the value changes
+ * to no longer belong to the set specified by the specified predicate
+ * or the specified time-out occurs.
* The time-out is specified in milliseconds. Return true
- * if the specified value was removed; return false
if a
- * time-out occurred. If the value is already not the specified
- * value, return true
immediately.
+ * if the value moved outside of the set; return false
+ * if a time-out occurred.
+ * If the value is already outside the set, return true
immediately.
* If the time-out is zero, wait indefinitely.
*/
- public boolean waitUntilValueIsNot(V v, long timeout) throws InterruptedException {
- synchronized (this.mutex) {
- return this.waitUntilValueIsNot_(v, timeout);
- }
+ public boolean waitUntilNot(Predicate super V> predicate, long timeout) throws InterruptedException {
+ return this.waitUntil(PredicateTools.not(predicate), timeout);
}
/**
- * Pre-condition: synchronized
+ * Suspend the current thread until the value changes
+ * to be {@link Object#equals(Object) equal} to the specified object
+ * or the specified time-out occurs.
+ * The time-out is specified in milliseconds. Return true
+ * if the specified value was achieved; return false
if a
+ * time-out occurred. If the value is already equal to the specified
+ * object, return true
immediately.
+ * If the time-out is zero, wait indefinitely.
*/
- private boolean waitUntilValueIsNot_(V v, long timeout) throws InterruptedException {
- if (timeout == 0L) {
- this.waitUntilValueIsNot_(v); // wait indefinitely until notified
- return true; // if it ever comes back, the condition was met
- }
+ public boolean waitUntilValueEquals(V object, long timeout) throws InterruptedException {
+ return this.waitUntil(PredicateTools.isEqual(object), timeout);
+ }
- long stop = System.currentTimeMillis() + timeout;
- long remaining = timeout;
- while (ObjectTools.equals(this.value, v) && (remaining > 0L)) {
- this.mutex.wait(remaining);
- remaining = stop - System.currentTimeMillis();
- }
- return ObjectTools.notEquals(this.value, v);
+ /**
+ * Suspend the current thread until the value changes
+ * to be not {@link Object#equals(Object) equal} to the specified object
+ * or the specified time-out occurs.
+ * The time-out is specified in milliseconds. Return true
+ * if the value changed to be unequal; return false
if a
+ * time-out occurred. If the value is already unequal to the specified
+ * object, return true
immediately.
+ * If the time-out is zero, wait indefinitely.
+ */
+ public boolean waitUntilValueNotEqual(V object, long timeout) throws InterruptedException {
+ return this.waitUntil(PredicateTools.isNotEqual(object), timeout);
+ }
+
+ /**
+ * Suspend the current thread until the value changes
+ * to be the specified object or the specified time-out occurs.
+ * The time-out is specified in milliseconds. Return true
+ * if the specified value was achieved; return false
if a
+ * time-out occurred. If the value is already the specified
+ * object, return true
immediately.
+ * If the time-out is zero, wait indefinitely.
+ */
+ public boolean waitUntilValueIs(V object, long timeout) throws InterruptedException {
+ return this.waitUntil(PredicateTools.isIdentical(object), timeout);
+ }
+
+ /**
+ * Suspend the current thread until the value changes
+ * to be not {@link Object#equals(Object) equal} to the specified object
+ * or the specified time-out occurs.
+ * The time-out is specified in milliseconds. Return true
+ * if the value changed to be unequal; return false
if a
+ * time-out occurred. If the value is already unequal to the specified
+ * value, return true
immediately.
+ * If the time-out is zero, wait indefinitely.
+ */
+ public boolean waitUntilValueIsNot(V object, long timeout) throws InterruptedException {
+ return this.waitUntil(PredicateTools.isNotIdentical(object), timeout);
}
/**
* Suspend the current thread until the value changes
* to null
or the specified time-out occurs.
* The time-out is specified in milliseconds. Return true
- * if the specified value was achieved; return false
+ * if the value changes to null
; return false
* if a time-out occurred. If the value is already null
,
* return true
immediately.
* If the time-out is zero, wait indefinitely.
@@ -351,7 +507,7 @@ public class SynchronizedObjectnull
or the specified time-out occurs.
* The time-out is specified in milliseconds. Return true
- * if the specified value was achieved; return false
+ * if the value changes to something other than null
; return false
* if a time-out occurred. If the value is already not
* null
, return true
immediately.
* If the time-out is zero, wait indefinitely.
@@ -362,21 +518,21 @@ public class SynchronizedObjecttrue
* if the value was set to the specified value; return false
* if a time-out occurred.
- * If the value is already something other than the specified value, set
+ * If the value is already something unequal to the specified value, set
* the value immediately and return true
.
* If the time-out is zero, wait indefinitely.
*/
public boolean waitToSetValue(V v, long timeout) throws InterruptedException {
synchronized (this.mutex) {
- boolean success = this.waitUntilValueIsNot_(v, timeout);
+ boolean success = this.waitUntil_(PredicateTools.isNotEqual(v), timeout);
if (success) {
this.setChangedValue_(v);
}
@@ -403,7 +559,8 @@ public class SynchronizedObject
@@ -33,4 +32,19 @@ public interface ModifiableObjectReference==
) to the specified object.
+ */
+ boolean is(Object object);
+
+ /**
+ * Return whether the current value is not
+ * identical (==
) to the specified object.
+ */
+ boolean isNot(Object object);
+
/**
* Return whether the current value is null
.
*/
@@ -63,4 +78,16 @@ public interface ObjectReferencenull
.
*/
boolean isNotNull();
+
+ /**
+ * Return whether the current value is a
+ * member of the set specified by the specified predicate.
+ */
+ boolean isMemberOf(Predicate super V> predicate);
+
+ /**
+ * Return whether the current value is not a
+ * member of the set specified by the specified predicate.
+ */
+ boolean isNotMemberOf(Predicate super V> predicate);
}
--
cgit v1.2.3