001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.utils.concurrent;
020
021/**
022 * An {@code Object} value on which a set of operations can be performed atomically.
023 * @param <T> thr type of the refered object.
024 * @author Laurent Cohen
025 */
026public class SynchronizedReference<T> {
027  /**
028   * The actual value.
029   */
030  private T value;
031
032  /**
033   * Initialize with the value ste to {@code false}.
034   */
035  public SynchronizedReference() {
036    value = null;
037  }
038
039  /**
040   * Iinitialize witht he specified value.
041   * @param value the initial value.
042   */
043  public SynchronizedReference(final T value) {
044    this.value = value;
045  }
046
047  /**
048   * @return the value of this boolan.
049   */
050  public synchronized T get() {
051    return value;
052  }
053
054  /**
055   * Set a new value.
056   * @param newValue the value to set.
057   */
058  public synchronized void set(final T newValue) {
059    value = newValue;
060  }
061
062  /**
063   * Set a new value if it is different from the current value.
064   * @param newValue the value to set.
065   * @return {@code true} if the value was changed, {@code false} otherwise.
066   */
067  public synchronized boolean setIfDifferent(final T newValue) {
068    if (newValue == value) return false;
069    value = newValue;
070    return true;
071  }
072
073  /**
074   * Compare the value with the expected value, and set it to the update value if they are equal.
075   * @param expected the expected value.
076   * @param update the new value to set to if the comparison succeeds.
077   * @return {@code true} if the update was performed, {@code false} otherwise.
078   */
079  public synchronized boolean compareAndSet(final T expected, final T update) {
080    if (value == expected) {
081      value = update;
082      return true;
083    }
084    return false;
085  }
086
087  /**
088   * Set a new value and return the old value.
089   * @param newValue the value to set.
090   * @return the value before the set.
091   */
092  public synchronized Object getAndSet(final T newValue) {
093    final Object oldValue = value;
094    value = newValue;
095    return oldValue;
096  }
097
098  /**
099   * Run an action if the value is the expected one.
100   * @param expected the expected value.
101   * @param action the aciton to run.
102   * @return {@code true} if the update was performed, {@code false} otherwise.
103   */
104  public synchronized boolean compareAndRun(final T expected, final Runnable action) {
105    if (value == expected) {
106      if (action != null) action.run();
107      return true;
108    }
109    return false;
110  }
111}