All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kiwiproject.net.SimpleHostAndPort Maven / Gradle / Ivy

Go to download

Kiwi is a utility library. We really like Google's Guava, and also use Apache Commons. But if they don't have something we need, and we think it is useful, this is where we put it.

There is a newer version: 4.4.0
Show newest version
package org.kiwiproject.net;

import static com.google.common.base.Preconditions.checkState;
import static org.apache.commons.lang3.StringUtils.isBlank;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;

/**
 * Inspired by Guava's {@link com.google.common.net.HostAndPort} but much simpler in implementation.
 * (Just go look at the code in {@link com.google.common.net.HostAndPort#fromString(String)} if you don't believe me.)
 * Because it is much simpler, it also only handles a very specific host/port format, which is {@code host:port}.
 * 

* It also does not attempt to validate anything about the host or port, e.g. it will happily accept a negative port * value or a one-character long host name. */ @EqualsAndHashCode @AllArgsConstructor(access = AccessLevel.PRIVATE) @SuppressWarnings("UnstableApiUsage") // Because Guava's InetAddresses is marked @Beta (but has been there a long time) public class SimpleHostAndPort { @Getter private final String host; @Getter private final int port; /** * Parse {@code hostPortString} assuming format {@code host:port}; if it is blank, use the specified * {@code defaultHost} and {@code defaultPort} values to create a link {@link SimpleHostAndPort}. *

* No validation is performed on the default values. * * @param hostPortString a string containing host and port, e.g. acme.com:8001 * @param defaultHost a default host if the host/port string is blank * @param defaultPort a default port if the host/port string is blank * @return a new SimpleHostAndPort instance */ public static SimpleHostAndPort from(String hostPortString, String defaultHost, int defaultPort) { if (isBlank(hostPortString)) { return new SimpleHostAndPort(defaultHost, defaultPort); } return from(hostPortString); } /** * Parse {@code hostPortString} assuming format {@code host:port} * * @param hostPortString a string containing host and port, e.g. foo.com:9000 * @return a new SimpleHostAndPort instance * @throws NullPointerException if {@code hostPortString} is null * @throws IllegalStateException if not in the expected format * @throws NumberFormatException if port is not a valid number */ public static SimpleHostAndPort from(String hostPortString) { var split = hostPortString.split(":"); checkState(split.length == 2, "%s is not in format host:port", hostPortString); return new SimpleHostAndPort(split[0], Integer.parseInt(split[1], 10)); } /** * Return a string in {@code host:port} format. */ @Override public String toString() { return host + ":" + port; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy