com.beans.observables.properties.atomic.AtomicObservableLongProperty Maven / Gradle / Ivy
package com.beans.observables.properties.atomic;
import com.beans.observables.binding.AtomicPropertyBindingController;
import com.beans.observables.binding.PropertyBindingController;
import com.beans.observables.listeners.ObservableEventController;
import com.beans.observables.properties.ObservableLongProperty;
import com.beans.observables.properties.ObservableLongPropertyBase;
import com.notifier.EventController;
import java.util.concurrent.atomic.AtomicLong;
/**
*
* A thread-safe implementation of {@link ObservableLongProperty}, holding a
* variable which is accessed for writing or reading through {@link #setAsLong(long)}
* and {@link #getAsLong()}.
*
*
* This implementation uses the java.util.concurrent.atomic package, to provide
* a lock-free, atomic read and write operations.
*
*
* In some cases, {@link AtomicLong} may old a lock, specifically, if the operating
* system does not support 64-bit operations.
*
*
* Depending on the {@link ObservableEventController} used, it is possible
* that changes from multiple threads won't dispatch in the correct order.
*
*
* @since JavaBeans 1.0
*/
public class AtomicObservableLongProperty extends ObservableLongPropertyBase {
private final AtomicLong mValue;
public AtomicObservableLongProperty(ObservableEventController eventController,
PropertyBindingController bindingController,
long initialValue) {
super(eventController, bindingController);
mValue = new AtomicLong(initialValue);
}
public AtomicObservableLongProperty(EventController eventController,
PropertyBindingController bindingController,
long initialValue) {
super(eventController, bindingController);
mValue = new AtomicLong(initialValue);
}
public AtomicObservableLongProperty(ObservableEventController eventController,
PropertyBindingController bindingController) {
this(eventController, bindingController, 0);
}
public AtomicObservableLongProperty(EventController eventController, long initialValue) {
this(eventController, new AtomicPropertyBindingController<>(), initialValue);
}
public AtomicObservableLongProperty(EventController eventController) {
this(eventController, 0);
}
@Override
protected void setInternalDirect(Long value) {
mValue.set(value);
}
@Override
protected void setInternal(long value) {
long oldValue = mValue.getAndSet(value);
if (oldValue != value) {
fireValueChangedEvent(oldValue, value);
}
}
@Override
protected long getInternal() {
return mValue.get();
}
}