/** * @return A supplier that atomically increments the value returned on each call to * {@link Supplier#get get}, starting from zero. */ public static Supplier<Integer> incrementing() { return incrementing(0); }
@BeforeMethod(alwaysRun=true) public void setUp() throws Exception { closedVals = new CopyOnWriteArrayList<Integer>(); supplier = Suppliers.incrementing(); closer = new Function<Integer,Void>() { @Override public Void apply(@Nullable Integer input) { closedVals.add(input); return null; } }; executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); }
@Test public void testIncrementingFrom() throws Exception { Supplier<Integer> supplier = Suppliers.incrementing(9876); assertEquals(supplier.get(), Integer.valueOf(9876)); assertEquals(supplier.get(), Integer.valueOf(9877)); assertEquals(supplier.get(), Integer.valueOf(9878)); }
@Test public void testIncrementing() throws Exception { Supplier<Integer> supplier = Suppliers.incrementing(); assertEquals(supplier.get(), Integer.valueOf(0)); assertEquals(supplier.get(), Integer.valueOf(1)); assertEquals(supplier.get(), Integer.valueOf(2)); }
private void initialiseMemberId() { synchronized (mutex) { if (sensors().get(NEXT_CLUSTER_MEMBER_ID) == null) { sensors().set(NEXT_CLUSTER_MEMBER_ID, Suppliers.incrementing()); } } }
@Override protected void onEntityAdded(Entity member) { if (member.config().get(ZooKeeperNode.MY_ID) == null) { Supplier<Integer> id; synchronized (mutex) { // Entities may not have been created with NODE_ID_SUPPLIER, so create it if // it's not there. We can't provide any good guarantees about what number to // start with, but then again the previous version of the entity gave no // guarantee either. id = entity.config().get(ZooKeeperEnsemble.NODE_ID_SUPPLIER); if (id == null) { id = Suppliers.incrementing(); entity.config().set(ZooKeeperEnsemble.NODE_ID_SUPPLIER, id); } } member.config().set(ZooKeeperNode.MY_ID, id.get()); } } }