/** * Determine whether the given object is identical to this object. * * @param o An object to be compared. * @return {@code true} if identical. Otherwise {@code false}. */ @Override public boolean equals(Object o) { if (o == this) { return true; } if (o == null || !Ip4Network.class.equals(o.getClass())) { return false; } Ip4Network ip4 = (Ip4Network)o; return (address == ip4.address && getPrefixLength() == ip4.getPrefixLength()); }
/** * Construct a new instance. * * @param addr An integer value which represents an IPv4 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 IllegalArgumentException * The given prefix length is invalid. */ public Ip4Network(int addr, int prefix) { super(prefix); int mask = getNetMask(getPrefixLength()); address = addr & mask; netMask = mask; }
/** * {@inheritDoc} */ @Override public IpPrefix getIpPrefix() { IpPrefix ip = ipPrefix; if (ip == null) { StringBuilder builder = new StringBuilder(); builder.append(getHostAddress()).append(CIDR_SEPARATOR). append(getPrefixLength()); ip = new IpPrefix(new Ipv4Prefix(builder.toString())); ipPrefix = ip; } return ip; }
/** * Construct a new instance. * * @param bytes A byte array which represents an IPv4 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 bytes} is {@code null}. * @throws IllegalArgumentException * The given prefix length is invalid. * @throws IllegalArgumentException * The given byte address does not represent an IPv4 address. */ public Ip4Network(byte[] bytes, int prefix) { super(prefix); int addr = NumberUtils.toInteger(bytes); int plen = getPrefixLength(); int mask = getNetMask(plen); netMask = mask; if (plen == Integer.SIZE) { address = addr; byteAddress = bytes.clone(); } else { address = addr & mask; } }