java.lang.Object
com.aoapps.net.InetAddressPrefix
- All Implemented Interfaces:
DtoFactory<InetAddressPrefix>,Serializable,Comparable<InetAddressPrefix>
public final class InetAddressPrefix
extends Object
implements Comparable<InetAddressPrefix>, Serializable, DtoFactory<InetAddressPrefix>
Combines an
InetAddress and an associated prefix.
See RFC 4291, Section 2.3. Text Representation of Address Prefixes.
- Author:
- AO Industries, Inc.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptioncoalesce(InetAddressPrefix other) Combines this address prefix with the given address prefix if possible.intcompareTo(InetAddressPrefix other) Ordered by address, prefix.booleancontains(InetAddress other) Checks if the given address is in this prefix.booleancontains(InetAddressPrefix other) Checks if the given address prefix is in this prefix.booleanEqual when has equal address and prefix.getDto()getFrom()Gets the first address in the network range represented by this address and prefix.intgetTo()Gets the last address in the network range represented by this address and prefix.inthashCode()Normalizes this address prefix, where all bits not inprefixare zeroed.toString()static ValidationResultvalidate(InetAddress address, int prefix) Checks if the address and prefix are valid.static InetAddressPrefixvalueOf(InetAddress address, int prefix) Gets an IPv6 address prefix from an address and prefix.static InetAddressPrefixParses an IP address with optional prefix.
-
Method Details
-
validate
Checks if the address and prefix are valid.- Parameters:
address- must be non-nullprefix- must be between zero andAddressFamily.getMaxPrefix(), inclusive
-
valueOf
Gets an IPv6 address prefix from an address and prefix.- Parameters:
address- If address is null, returns null.- Throws:
ValidationException- Seevalidate(com.aoapps.net.InetAddress, int)
-
valueOf
Parses an IP address with optional prefix.- Parameters:
address- The address and optional prefix asaddress[/prefix].- Throws:
ValidationException- See Also:
-
equals
Equal when has equal address and prefix. This means two prefixes that represent the same address range, but have different addresses, are not considered equal. To check if represent the same range,normalizeeach address prefix. -
hashCode
public int hashCode() -
toString
-
compareTo
Ordered by address, prefix.- Specified by:
compareToin interfaceComparable<InetAddressPrefix>- See Also:
-
getAddress
-
getPrefix
public int getPrefix() -
getDto
- Specified by:
getDtoin interfaceDtoFactory<InetAddressPrefix>
-
getFrom
Gets the first address in the network range represented by this address and prefix.- Returns:
getAddress()when from == address, otherwise new address.
-
getTo
Gets the last address in the network range represented by this address and prefix.- Returns:
getAddress()when to == address, otherwise new address.
-
normalize
Normalizes this address prefix, where all bits not inprefixare zeroed. This means the address will be the first address in the network range.- See Also:
-
contains
Checks if the given address is in this prefix. Must be of the sameaddress family; IPv4 addresses will never match IPv6. -
contains
Checks if the given address prefix is in this prefix. Must be of the sameaddress family; IPv4 addresses will never match IPv6. -
coalesce
Combines this address prefix with the given address prefix if possible. Will only combine address prefixes of the sameAddressFamily. Returns anormalizednetwork range.- If different address prefixes are non-overlapping and non-adjacent along prefix boundaries, returns
null. - If the combined address prefix equals
this, returnsthis. - If the combined address prefix equals
other, returnsother. - Otherwise, returns a new address prefix covering the full range.
- Returns:
- When the address prefix hae been combined, returns an address prefix spanning both.
nullwhen they cannot be combined.
- If different address prefixes are non-overlapping and non-adjacent along prefix boundaries, returns
-
