/** * Determine whether this instance represents an IP address or not. * * @return {@code true} if this instance represents an IP address. * {@code false} if this instance represents an IP network * specified by a pair of network address and prefix length. */ public final boolean isAddress() { return (prefixLength == getMaxPrefix()); }
/** * Construct a new instance. * * @param prefix Prefix length that specifies network range. * Note that zero means "no mask". So zero is treated as if * the maximum prefix length is specified. */ IpNetwork(int prefix) { prefixLength = (prefix == 0) ? getMaxPrefix() : prefix; }
/** * Construct a new instance. * * @param iaddr An {@link InetAddress} instance which representse the * network address. * @param prefix Prefix length that specifies network range. * Note that zero means "no mask". So zero is treated as if * the maximum prefix length is specified. * @throws NullPointerException * {@code iaddr} is {@code null}. * @throws IllegalArgumentException * The given network address or prefix length is invalid. */ IpNetwork(InetAddress iaddr, int prefix) { int plen = (prefix == 0) ? getMaxPrefix() : prefix; inetAddress = init(iaddr, plen); prefixLength = plen; }
/** * Return a string representation of IP network. * * <p> * If this instance represents a specific IP host, an IP address of the * host without prefix length is returned. Otherwise a string which * represents the IP network in CIDR notation is returned. * </p> * * @return A string representation of IP network. */ public final String getText() { int max = getMaxPrefix(); return (prefixLength == max) ? getHostAddress() : getCidrText(); }
/** * Set the IP network address by a string representation of network address * in CIDR notation. * * @param cidr A string representation of this network address. * @throws NullPointerException * {@code cidr} is {@code null}. * @throws IllegalArgumentException * The given network address or prefix length is invalid. */ private void setCidrValue(String cidr) { InetAddressPrefix ipfx = new InetAddressPrefix(cidr); int prefix = ipfx.getPrefix(); if (prefix == 0) { prefix = getMaxPrefix(); } inetAddress = init(ipfx.getAddress(), prefix); prefixLength = prefix; }