All Downloads are FREE. Search and download functionalities are using the official Maven repository.

netflix.ocelli.util.AtomicDouble Maven / Gradle / Ivy

There is a newer version: 0.1.0-rc.2
Show newest version
package netflix.ocelli.util;

import java.util.concurrent.atomic.AtomicLong;

/**
 * Utility class to track an atomic double for use in non blocking algorithms (@see ExponentialAverage)
 * 
 * @author elandau
 */
public class AtomicDouble {

    private AtomicLong bits;

    public AtomicDouble() {
        this(0f);
    }

    public AtomicDouble(double initialValue) {
        bits = new AtomicLong(Double.doubleToLongBits(initialValue));
    }

    public final boolean compareAndSet(double expect, double update) {
        return bits.compareAndSet(Double.doubleToLongBits(expect),
                                  Double.doubleToLongBits(update));
    }

    public final void set(double newValue) {
        bits.set(Double.doubleToLongBits(newValue));
    }

    public final double get() {
        return Double.longBitsToDouble(bits.get());
    }

    public final double getAndSet(double newValue) {
        return Double.longBitsToDouble(bits.getAndSet(Double.doubleToLongBits(newValue)));
    }

    public final boolean weakCompareAndSet(double expect, double update) {
        return bits.weakCompareAndSet(Double.doubleToLongBits(expect),
                                      Double.doubleToLongBits(update));
    }

    public double doubleValue() { return (double) get(); }
    public int intValue()       { return (int) get();           }
    public long longValue()     { return (long) get();          }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy