com.github.staslev.concurrent.AtomicMapOperationsForLongValues Maven / Gradle / Ivy
package com.github.staslev.concurrent;
import java.util.concurrent.ConcurrentMap;
/**
*
* Provides atomic non blocking operations on maps with Long values, this is typically the case when values are
* counters of some sort.
*
*/
public class AtomicMapOperationsForLongValues {
AtomicMapOperationsForLongValues() {
}
NonBlockingOperations.Aggregator increaseByAggregator = new NonBlockingOperations.Aggregator() {
@Override
public Long aggregate(final Long input, final Long previousValue) {
return previousValue + input;
}
};
NonBlockingOperations.Aggregator decreaseByAggregator = new NonBlockingOperations.Aggregator() {
@Override
public Long aggregate(final Long input, final Long previousValue) {
return previousValue - input;
}
};
final NonBlockingOperations.Transformer increaseByOneTransformer = new NonBlockingOperations.Transformer() {
@Override
public Long transform(final Long previousValue) {
return previousValue == null ? 1L : previousValue + 1;
}
};
final NonBlockingOperations.Transformer decreaseByOneTransformer = new NonBlockingOperations.Transformer() {
@Override
public Long transform(final Long previousValue) {
return previousValue == null ? -1L : previousValue - 1;
}
};
/**
* Atomically increases the value assigned to key
by 1. If key
is not present in the map, its value is set to 1.
*
* @param map The map to perform the operation on.
* @param key The key whose value is to be put or transformed.
* @param The type of the keys.
*/
public void increase(final ConcurrentMap map, final K key) {
NonBlockingOperations.forMap.withImmutableValues().putOrTransform(map, key, increaseByOneTransformer);
}
/**
* Atomically increases the value assigned to key
by increaseBy
. If key
is not present in the map, its value is set to increaseBy
.
*
* @param map The map to perform the operation on.
* @param key The key whose value is to be put or transformed.
* @param The type of the keys.
*/
public void increase(final ConcurrentMap map, final K key, final long increaseBy) {
NonBlockingOperations.forMap.withImmutableValues().putOrAggregate(map, key, increaseByAggregator, increaseBy);
}
/**
* Atomically decreases the value of key
by 1, if the given key is not present in the map, its value is set to -1.
*
* @param map The map to perform the operation on.
* @param key The key whose value is to be put or transformed.
* @param The type of the keys.
*/
public void decrease(final ConcurrentMap map, final K key) {
NonBlockingOperations.forMap.withImmutableValues().putOrTransform(map, key, decreaseByOneTransformer);
}
/**
* Atomically decreases the value of key
by decreaseBy
, if key
is not present in the map, its value is set to (-1 * decreaseBy)
.
*
* @param map The map to perform the operation on.
* @param key The key whose value is to be put or transformed.
* @param The type of the keys.
*/
public void decrease(final ConcurrentMap map, final K key, final long decreaseBy) {
NonBlockingOperations.forMap.withImmutableValues().putOrAggregate(map, key, decreaseByAggregator, decreaseBy);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy