Source:
<code>public InetSocketAddress next(long spinDelay) { if (!connectedSinceNext) { InetSocketAddress curAddr = serverAddresses.get(currentIndex); if (!curAddr.getHostString().equals(curAddr.getAddress().getHostAddress())) { try { int thePort = curAddr.getPort(); InetAddress resolvedAddresses[] = InetAddress.getAllByName(getHostString(curAddr)); if (resolvedAddresses.length == 1) { serverAddresses.set(currentIndex, new InetSocketAddress(resolvedAddresses[0], thePort)); } else { serverAddresses.remove(currentIndex); for (InetAddress resolvedAddress : resolvedAddresses) { InetSocketAddress newAddr = new InetSocketAddress(resolvedAddress, thePort); if (!serverAddresses.contains(newAddr)) { serverAddresses.add(newAddr); } } } } catch (UnknownHostException e) { LOG.warn("Cannot re-resolve server: " + curAddr + " UnknownHostException: " + e); } } } ++currentIndex; connectedSinceNext = false; <START> if (currentIndex == serverAddresses.size()) { <END> currentIndex = 0; } if (currentIndex == lastIndex && spinDelay > 0) { try { Thread.sleep(spinDelay); } catch (InterruptedException e) { LOG.warn("Unexpected exception", e); } } else if (lastIndex == -1) { lastIndex = 0; } return serverAddresses.get(currentIndex); }</code><technical_language>serverAddresses.size() 0 (per constructor checking) this if condition line 137 rewritten as: currentIndex = currentIndex % serverAddresses.size(); currentIndex = ++currentIndex % serverAddresses.size(); Eliminating need of line 137 too. master, branch-3.4 branch-3.5. **Just a silly optimization, tough.**</technical_language>

Target:
public InetSocketAddress next(long spinDelay) { if (!connectedSinceNext) { InetSocketAddress curAddr = serverAddresses.get(currentIndex); String curHostString = getHostString(curAddr); if (!curHostString.equals(curAddr.getAddress().getHostAddress())) { LOG.info("Resolving again hostname: {}", getHostString(curAddr)); try { int thePort = curAddr.getPort(); InetAddress resolvedAddresses[] = InetAddress.getAllByName(curHostString); nextAdded = 0; nextRemoved = 0; if (resolvedAddresses.length == 1) { serverAddresses.set(currentIndex, new InetSocketAddress(resolvedAddresses[0], thePort)); nextAdded = nextRemoved = 1; LOG.debug("Newly resolved address: {}", resolvedAddresses[0]); } else { int i = 0; while (i < serverAddresses.size()) { if (getHostString(serverAddresses.get(i)).equals(curHostString) && serverAddresses.get(i).getPort() == curAddr.getPort()) { LOG.debug("Removing address: {}", serverAddresses.get(i)); serverAddresses.remove(i); nextRemoved++; } else { i++; } } for (InetAddress resolvedAddress : resolvedAddresses) { InetSocketAddress newAddr = new InetSocketAddress(resolvedAddress, thePort); if (!serverAddresses.contains(newAddr)) { LOG.debug("Adding address: {}", newAddr); serverAddresses.add(newAddr); nextAdded++; } } } } catch (UnknownHostException e) { LOG.warn("Cannot re-resolve server: {}", curAddr, e); } } } connectedSinceNext = false; currentIndex = ++currentIndex % serverAddresses.size(); if (currentIndex == lastIndex && spinDelay > 0) { try { Thread.sleep(spinDelay); } catch (InterruptedException e) { LOG.warn("Unexpected exception", e); } } else if (lastIndex == -1) { lastIndex = 0; } return serverAddresses.get(currentIndex); }