com.graphaware.reco.generic.util.AtomicFloat Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of recommendation-engine Show documentation
Show all versions of recommendation-engine Show documentation
Skeleton for a Recommendation Engine
The newest version!
/*
* Copyright (c) 2013-2016 GraphAware
*
* This file is part of the GraphAware Framework.
*
* GraphAware Framework is free software: you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of
* the GNU General Public License along with this program. If not, see
* .
*/
package com.graphaware.reco.generic.util;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import static java.lang.Float.*;
/**
* A {@code float} value that may be updated atomically.
*/
public class AtomicFloat extends Number {
private transient volatile int value;
private static final AtomicIntegerFieldUpdater updater = AtomicIntegerFieldUpdater.newUpdater(AtomicFloat.class, "value");
/**
* Creates a new {@code AtomicFloat} with the given initial value.
*
* @param initialValue the initial value
*/
public AtomicFloat(float initialValue) {
value = floatToRawIntBits(initialValue);
}
/**
* Gets the current value.
*
* @return the current value
*/
public final float get() {
return intBitsToFloat(value);
}
/**
* Sets to the given value.
*
* @param newValue the new value
*/
public final void set(float newValue) {
value = floatToRawIntBits(newValue);
}
/**
* Atomically sets to the given value and returns the old value.
*
* @param newValue the new value
* @return the previous value
*/
public final float getAndSet(float newValue) {
int next = floatToIntBits(newValue);
return intBitsToFloat(updater.getAndSet(this, next));
}
/**
* Atomically sets the value to the given updated value
* if the current value is bitwise equal
* to the expected value.
*
* @param expect the expected value
* @param update the new value
* @return {@code true} if successful. False return indicates that
* the actual value was not bitwise equal to the expected value.
*/
public final boolean compareAndSet(float expect, float update) {
return updater.compareAndSet(this,
floatToRawIntBits(expect),
floatToRawIntBits(update));
}
/**
* Atomically adds the given value to the current value.
*
* @param delta the value to add
* @return the updated value
*/
public final float addAndGet(float delta) {
while (true) {
int current = value;
float currentVal = intBitsToFloat(current);
float nextVal = currentVal + delta;
int next = floatToRawIntBits(nextVal);
if (updater.compareAndSet(this, current, next)) {
return nextVal;
}
}
}
/**
* Returns the String representation of the current value.
*
* @return the String representation of the current value
*/
public String toString() {
return Float.toString(get());
}
/**
* Returns the value of this {@code AtomicDouble} as an {@code int}
* after a narrowing primitive conversion.
*/
public int intValue() {
return (int) get();
}
/**
* Returns the value of this {@code AtomicDouble} as a {@code long}
* after a narrowing primitive conversion.
*/
public long longValue() {
return (long) get();
}
/**
* Returns the value of this {@code AtomicDouble} as a {@code float}
* after a narrowing primitive conversion.
*/
public float floatValue() {
return get();
}
/**
* Returns the value of this {@code AtomicDouble} as a {@code double}.
*/
public double doubleValue() {
return get();
}
}