AtomicRangeInteger extends Number implements Serializable { public final int getAndIncrement() { int next; do { next = this.values.incrementAndGet(VALUE_OFFSET); if (next > endValue && this.values.compareAndSet(VALUE_OFFSET, next, startValue)) { return endValue; } } while (next > endValue); return next - 1; } AtomicRangeInteger(int startValue, int maxValue); final int getAndIncrement(); final int get(); int intValue(); long longValue(); float floatValue(); double doubleValue();  }
@Test public void testGetAndIncrement() { AtomicRangeInteger atomicI = new AtomicRangeInteger(0, 10); for (int i = 0; i < 10; i++) { Assert.assertEquals(i, atomicI.getAndIncrement()); } Assert.assertEquals(0, atomicI.getAndIncrement()); Assert.assertEquals(1, atomicI.get()); Assert.assertEquals(1, atomicI.intValue()); Assert.assertEquals(1, atomicI.longValue()); Assert.assertEquals(1, (int) atomicI.floatValue()); Assert.assertEquals(1, (int) atomicI.doubleValue()); }
@Test @Benchmark public void testGetAndIncrementV1Performance() { ATOMIC_V1.getAndIncrement(); }
@Test @Benchmark public void testGetAndIncrementV2Performance() { ATOMIC_V2.getAndIncrement(); }
@Test @Benchmark public void testGetAndIncrementV3Performance() { ATOMIC_V3.getAndIncrement(); }