class SocketAddressParser {

    public static <T extends SocketAddress> T deriveSocketAddress(
            String settingName, String settingValue, String defaultHostname, int defaultPort,
            BiFunction<String,Integer,T> constructor )
    {
        if ( settingValue == null )
        {
            return constructor.apply( defaultHostname, defaultPort );
        }

        settingValue = settingValue.trim();

        T socketAddress;
        if ( (socketAddress = matchHostnamePort( settingValue, constructor )) != null )
        {
            return socketAddress;
        }

        if ( (socketAddress = matchPort( settingValue, defaultHostname, constructor )) != null )
        {
            return socketAddress;
        }

        throw new IllegalArgumentException( format(
                "Setting \"%s\" must be in the format " +
                "\"hostname:port\" or \":port\". \"%s\" does not conform to these formats",
                settingName, settingValue ) );
    }

}