AddressUtils { public static SocketAddress updatePort(@Nullable Supplier<? extends SocketAddress> address, int port) { if (address == null) { return createUnresolved(NetUtil.LOCALHOST.getHostAddress(), port); } SocketAddress socketAddress = address.get(); if (socketAddress instanceof DomainSocketAddress) { throw new IllegalArgumentException("Cannot update DomainSocketAddress with post number [" + port + "]."); } if(!(address.get() instanceof InetSocketAddress)) { return createUnresolved(NetUtil.LOCALHOST.getHostAddress(), port); } InetSocketAddress inet = (InetSocketAddress) address.get(); InetAddress addr = inet.getAddress(); String host = addr == null ? inet.getHostName() : addr.getHostAddress(); return createUnresolved(host, port); } private  AddressUtils(); static InetSocketAddress createInetSocketAddress(String hostname, int port, boolean resolve); static InetSocketAddress createResolved(String hostname, int port); static InetSocketAddress createUnresolved(String hostname, int port); static InetSocketAddress parseAddress(String address, int defaultPort); static InetSocketAddress replaceUnresolvedNumericIp(InetSocketAddress inetSocketAddress); static InetSocketAddress replaceWithResolved(InetSocketAddress inetSocketAddress); static SocketAddress updateHost(@Nullable Supplier<? extends SocketAddress> address, String host); static SocketAddress updatePort(@Nullable Supplier<? extends SocketAddress> address, int port);  }
@Test public void updatePortBadValues() { assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy(() -> AddressUtils.updatePort(null, -1)) .withMessage("port out of range:-1"); }