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.int
compareTo
(InetAddressPrefix other) Ordered by address, prefix.boolean
contains
(InetAddress other) Checks if the given address is in this prefix.boolean
contains
(InetAddressPrefix other) Checks if the given address prefix is in this prefix.boolean
Equal when has equal address and prefix.getDto()
getFrom()
Gets the first address in the network range represented by this address and prefix.int
getTo()
Gets the last address in the network range represented by this address and prefix.int
hashCode()
Normalizes this address prefix, where all bits not inprefix
are zeroed.toString()
static ValidationResult
validate
(InetAddress address, int prefix) Checks if the address and prefix are valid.static InetAddressPrefix
valueOf
(InetAddress address, int prefix) Gets an IPv6 address prefix from an address and prefix.static InetAddressPrefix
Parses 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,normalize
each address prefix. -
hashCode
public int hashCode() -
toString
-
compareTo
Ordered by address, prefix.- Specified by:
compareTo
in interfaceComparable<InetAddressPrefix>
- See Also:
-
getAddress
-
getPrefix
public int getPrefix() -
getDto
- Specified by:
getDto
in 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 inprefix
are 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 anormalized
network 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.
null
when they cannot be combined.
- If different address prefixes are non-overlapping and non-adjacent along prefix boundaries, returns
-