Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/AbstractModifiableObjectReference.java')
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/reference/AbstractModifiableObjectReference.java57
1 files changed, 57 insertions, 0 deletions
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<ul>
+ * <li>{@link #getValue()}
+ * <li>{@link #setValue(Object)}
+ * </ul>
+ *
+ * @param <V> the type of the reference's value
+ */
+public abstract class AbstractModifiableObjectReference<V>
+ extends AbstractObjectReference<V>
+ implements ModifiableObjectReference<V>
+{
+ protected AbstractModifiableObjectReference() {
+ super();
+ }
+
+ public V setNull() {
+ return this.setValue(null);
+ }
+
+ public boolean commit(V newValue, V expectedValue) {
+ if (ObjectTools.equals(this.getValue(), expectedValue)) {
+ this.setValue(newValue);
+ return true;
+ }
+ return false;
+ }
+
+ public V swap(ModifiableObjectReference<V> other) {
+ if (other == this) {
+ return this.getValue();
+ }
+ V otherValue = other.getValue();
+ if (ObjectTools.equals(this.getValue(), otherValue)) {
+ return this.getValue();
+ }
+ other.setValue(this.getValue());
+ this.setValue(otherValue);
+ return otherValue;
+ }
+}

Back to the top