@Test public void overflow() throws Exception { List<Server> servers = new ArrayList<>(); servers.add(new Server("foo", 7001, false)); servers.add(new Server("bar", 7001, false)); ServerEntry entry = new ServerEntry(servers, 0L); Field f = ServerEntry.class.getDeclaredField("nextPos"); f.setAccessible(true); ((AtomicInteger) f.get(entry)).set(Integer.MAX_VALUE - 2); List<Server> results = entry.next(5); Assert.assertEquals(5, results.size()); }
List<Server> next(int n) { if (servers.isEmpty()) { return Collections.emptyList(); } final List<Server> out = new ArrayList<>(n); final int size = servers.size(); final int start = Integer.remainderUnsigned(nextPos.getAndIncrement(), size); for (int i = 0, j = start; i < n; ++i, ++j) { out.add(servers.get(j % size)); } return out; }
ServerEntry { List<Server> next(int n) { if (servers.isEmpty()) { return Collections.emptyList(); } final List<Server> out = new ArrayList<>(n); final int size = servers.size(); final int start = Integer.remainderUnsigned(nextPos.getAndIncrement(), size); for (int i = 0, j = start; i < n; ++i, ++j) { out.add(servers.get(j % size)); } return out; } }
ServerEntry { List<Server> next(int n) { if (servers.isEmpty()) { return Collections.emptyList(); } final List<Server> out = new ArrayList<>(n); final int size = servers.size(); final int start = Integer.remainderUnsigned(nextPos.getAndIncrement(), size); for (int i = 0, j = start; i < n; ++i, ++j) { out.add(servers.get(j % size)); } return out; } ServerEntry(List<Server> servers, long ctime); }
ServerEntry { List<Server> next(int n) { if (servers.isEmpty()) { return Collections.emptyList(); } final List<Server> out = new ArrayList<>(n); final int size = servers.size(); final int start = Integer.remainderUnsigned(nextPos.getAndIncrement(), size); for (int i = 0, j = start; i < n; ++i, ++j) { out.add(servers.get(j % size)); } return out; } ServerEntry(List<Server> servers, long ctime);  }
ServerEntry { List<Server> next(int n) { if (servers.isEmpty()) { return Collections.emptyList(); } final List<Server> out = new ArrayList<>(n); final int size = servers.size(); final int start = Integer.remainderUnsigned(nextPos.getAndIncrement(), size); for (int i = 0, j = start; i < n; ++i, ++j) { out.add(servers.get(j % size)); } return out; } ServerEntry(List<Server> servers, long ctime);   }