com.beans.observables.properties.atomic.AtomicObservableIntProperty 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.ObservableIntProperty;
import com.beans.observables.properties.ObservableIntPropertyBase;
import com.notifier.EventController;
import java.util.concurrent.atomic.AtomicInteger;
/**
*
* A thread-safe implementation of {@link ObservableIntProperty}, holding a
* variable which is accessed for writing or reading through {@link #setAsInt(int)}
* and {@link #getAsInt()}.
*
*
* This implementation uses the java.util.concurrent.atomic package, to provide
* a lock-free, atomic read and write 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 AtomicObservableIntProperty extends ObservableIntPropertyBase {
private final AtomicInteger mValue;
public AtomicObservableIntProperty(ObservableEventController eventController,
PropertyBindingController bindingController,
int initialValue) {
super(eventController, bindingController);
mValue = new AtomicInteger(initialValue);
}
public AtomicObservableIntProperty(EventController eventController,
PropertyBindingController bindingController,
int initialValue) {
super(eventController, bindingController);
mValue = new AtomicInteger(initialValue);
}
public AtomicObservableIntProperty(ObservableEventController eventController,
PropertyBindingController bindingController) {
this(eventController, bindingController,0);
}
public AtomicObservableIntProperty(EventController eventController, int initialValue) {
this(eventController, new AtomicPropertyBindingController<>(), initialValue);
}
public AtomicObservableIntProperty(EventController eventController) {
this(eventController, 0);
}
@Override
protected void setInternalDirect(Integer value) {
mValue.set(value);
}
@Override
protected void setInternal(int value) {
int oldValue = mValue.getAndSet(value);
if (oldValue != value) {
fireValueChangedEvent(oldValue, value);
}
}
@Override
protected int getInternal() {
return mValue.get();
}
}