@Override public long getValue() { return syncCounter.getValue(); }
/** * @see StrongCounter#incrementAndGet() */ default long incrementAndGet() { return addAndGet(1); }
/** * @see StrongCounter#compareAndSet(long, long) */ default boolean compareAndSet(long expect, long update) { return compareAndSwap(expect, update) == expect; }
public void testCompareAndSwapBounds(Method method) { CounterManager counterManager = counterManager(0); counterManager.defineCounter(method.getName(), CounterConfiguration.builder(CounterType.BOUNDED_STRONG).lowerBound(-2).upperBound(2).build()); SyncStrongCounter counter = counterManager.getStrongCounter(method.getName()).sync(); assertEquals(0, counter.compareAndSwap(0, 2)); assertEquals(2, counter.getValue()); assertOutOfBoundCompareAndSwap(counter, 2, 3); counter.reset(); assertEquals(0, counter.compareAndSwap(0, -2)); assertEquals(-2, counter.getValue()); assertOutOfBoundCompareAndSwap(counter, -2, -3); counter.reset(); assertEquals(0, counter.compareAndSwap(1, 3)); assertEquals(0, counter.compareAndSwap(1, -3)); }
public void testCompareAndSetBounds(Method method) { CounterManager counterManager = counterManager(0); counterManager.defineCounter(method.getName(), CounterConfiguration.builder(CounterType.BOUNDED_STRONG).lowerBound(-2).upperBound(2).build()); SyncStrongCounter counter = counterManager.getStrongCounter(method.getName()).sync(); assertTrue(counter.compareAndSet(0, 2)); assertEquals(2, counter.getValue()); assertOutOfBoundCompareAndSet(counter, 2, 3); counter.reset(); assertTrue(counter.compareAndSet(0, -2)); assertEquals(-2, counter.getValue()); assertOutOfBoundCompareAndSet(counter, -2, -3); counter.reset(); assertFalse(counter.compareAndSet(1, 3)); assertFalse(counter.compareAndSet(1, -3)); }
private void assertOutOfBoundCompareAndSwap(SyncStrongCounter counter, long expect, long value) { try { counter.compareAndSwap(expect, value); fail("Threshold should have been reached!"); } catch (CounterOutOfBoundsException e) { log.debug("Expected exception", e); } assertEquals("Wrong return value of counter.getNewValue()", expect, counter.getValue()); } }
private void assertOutOfBoundCompareAndSet(SyncStrongCounter counter, long expect, long value) { try { counter.compareAndSet(expect, value); fail("Threshold should have been reached!"); } catch (CounterOutOfBoundsException e) { log.debug("Expected exception", e); } assertEquals("Wrong return value of counter.getNewValue()", expect, counter.getValue()); }
@Override public void increment() { syncCounter.incrementAndGet(); }
@Override public void decrement() { syncCounter.decrementAndGet(); }
@Override public void reset() { syncCounter.reset(); }
@Override public String getName() { return syncCounter.getName(); }
public boolean compareAndSet(long expect, long value) { return syncCounter.compareAndSet(expect, value); }
@Override public CounterConfiguration getConfiguration() { return syncCounter.getConfiguration(); }
@Override protected ModelNode invoke(CounterManager counterManager, ModelNode operation) { final String counterName = counterName(operation); final String counterType = counterType(operation); if (counterManager.isDefined(counterName)) { boolean isStrongCounter = ModelKeys.STRONG_COUNTER.equals(counterType); if (isStrongCounter) { StrongCounter strongCounter = counterManager.getStrongCounter(counterName); strongCounter.sync().incrementAndGet(); } else { WeakCounter weakCounter = counterManager.getWeakCounter(counterName); weakCounter.sync().increment(); } } return new ModelNode(); } }
@Override protected ModelNode invoke(CounterManager counterManager, ModelNode operation) { final String counterName = counterName(operation); final String counterType = counterType(operation); if (counterManager.isDefined(counterName)) { boolean isStrongCounter = ModelKeys.STRONG_COUNTER.equals(counterType); if (isStrongCounter) { StrongCounter strongCounter = counterManager.getStrongCounter(counterName); strongCounter.sync().decrementAndGet(); } else { WeakCounter weakCounter = counterManager.getWeakCounter(counterName); weakCounter.sync().decrement(); } } return new ModelNode(); } }
@Override protected ModelNode invoke(CounterManager counterManager, ModelNode operation) { final String counterName = counterName(operation); final String counterType = counterType(operation); if (counterManager.isDefined(counterName)) { boolean isStrongCounter = ModelKeys.STRONG_COUNTER.equals(counterType); if (isStrongCounter) { StrongCounter strongCounter = counterManager.getStrongCounter(counterName); strongCounter.sync().reset(); } else { WeakCounter weakCounter = counterManager.getWeakCounter(counterName); weakCounter.sync().reset(); } } return new ModelNode(); } }
private long getValue(CounterManager counterManager) { switch (type) { case WEAK: return counterManager.getWeakCounter(name).getValue(); case BOUNDED_STRONG: case UNBOUNDED_STRONG: return counterManager.getStrongCounter(name).sync().getValue(); } throw new IllegalStateException(); } }
/** * @see StrongCounter#decrementAndGet() */ default long decrementAndGet() { return addAndGet(-1); }
private void incrementCounter(CounterManager counterManager) { switch (type) { case UNBOUNDED_STRONG: case BOUNDED_STRONG: counterManager.getStrongCounter(name).sync().incrementAndGet(); break; case WEAK: counterManager.getWeakCounter(name).sync().increment(); break; } }
public long compareAndSwap(long expect, long value) { return syncCounter.compareAndSwap(expect, value); } }