@Override
public Number nextValue(NextValueRequest request) {
final Cache<SerializableIdSourceKey> cache = datastoreProvider.getIdentifierCache();
SerializableIdSourceKey key = new SerializableIdSourceKey( request.getKey() );
Element previousValue = cache.get( key );
if ( previousValue == null ) {
previousValue = cache.putIfAbsent( new Element( key, request.getInitialValue() ) );
}
if ( previousValue != null ) {
while ( !cache.replace( previousValue,
new Element( key, ( (Integer) previousValue.getObjectValue() ) + request.getIncrement() ) ) ) {
previousValue = cache.get( key );
}
return ( (Integer) previousValue.getObjectValue() ) + request.getIncrement();
}
else {
return request.getInitialValue();
}
}