Package uk.ipfreely
Class Address<A extends Address<A>>
java.lang.Object
uk.ipfreely.Address<A>
- Type Parameters:
A
- the address type
- All Implemented Interfaces:
Comparable<A>
Abstract IP address type implemented by V4
and V6
.
Use Family
to create instances.
Inheritance outside the package is not supported. Future implementations may become sealed. Future implementations may become value objects.
Usage Hints
Feature | Address |
InetAddress |
---|---|---|
IP address | YES | YES |
Network I/O | YES | |
DNS host names | YES | |
Numeric type | YES | |
Generic type | YES | |
Canonical string form (RFC5952) | YES | |
Comparable |
YES | |
Serializable |
YES |
For InetAddress
see the following libraries for overlapping utility methods:
Apache Commons Net;
Google Guava.
Conversion to/from InetAddress
can be performed using:
-
Method Summary
Modifier and TypeMethodDescriptionabstract A
Addition with overflow.abstract A
Bitwise AND.abstract A
Division.abstract double
LikeNumber.doubleValue()
.abstract boolean
Object equality.family()
Internet protocol family - V4 or V6.abstract int
hashCode()
Hash code.abstract long
highBits()
Useful for efficient conversion.abstract int
Similar toInteger.numberOfLeadingZeros(int)
.abstract long
lowBits()
Useful for efficient conversion.abstract A
Modulus.abstract A
Multiplication with overflow.next()
Returns the IP address incremented by one, with overflow.abstract A
not()
Bitwise NOT.abstract A
Bitwise OR.prev()
Returns the IP address decremented by one, with underflow.abstract A
shift
(int bits) Bitwise shift.abstract A
Subtraction with underflow.abstract BigInteger
The address as aBigInteger
.abstract byte[]
toBytes()
The address as bytes of lengthFamily.width()
/Byte.SIZE
.abstract String
toString()
String form amenable to parsers.abstract int
Similar toInteger.numberOfTrailingZeros(int)
.abstract A
Bitwise exclusive OR.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Comparable
compareTo
-
Method Details
-
family
Internet protocol family - V4 or V6.- Returns:
- address family
-
equals
Object equality. -
hashCode
public abstract int hashCode()Hash code. -
toString
String form amenable to parsers.
-
toBigInteger
The address as aBigInteger
.- Returns:
- the address as a positive integer
-
toBytes
public abstract byte[] toBytes()The address as bytes of lengthFamily.width()
/Byte.SIZE
.- Returns:
- the address as a byte sequence, most significant bits first
- See Also:
-
highBits
public abstract long highBits()Useful for efficient conversion.- Returns:
- the high part of the value
-
lowBits
public abstract long lowBits()Useful for efficient conversion.- Returns:
- the low part of the value
-
leadingZeros
public abstract int leadingZeros()Similar toInteger.numberOfLeadingZeros(int)
.- Returns:
- number of zero bits preceding the highest-order ("leftmost") one-bit
-
trailingZeros
public abstract int trailingZeros()Similar toInteger.numberOfTrailingZeros(int)
.- Returns:
- number of zero bits following the lowest-order ("rightmost") one-bit
-
doubleValue
public abstract double doubleValue()LikeNumber.doubleValue()
.- Returns:
- approximate floating point value
-
add
Addition with overflow.- Parameters:
addend
- the summand- Returns:
- sum
-
subtract
Subtraction with underflow.- Parameters:
subtrahend
- number to subtract from this- Returns:
- difference
-
multiply
Multiplication with overflow.- Parameters:
multiplicand
- the factor- Returns:
- product
-
divide
Division.- Parameters:
denominator
- the divisor- Returns:
- quotient
- Throws:
ArithmeticException
- on divide-by-zero
-
mod
Modulus.- Parameters:
denominator
- the divisor- Returns:
- remainder
- Throws:
ArithmeticException
- on divide-by-zero
-
next
Returns the IP address incremented by one, with overflow.- Returns:
- the next IP address
-
prev
Returns the IP address decremented by one, with underflow.- Returns:
- the previous IP address
-
and
Bitwise AND.// EXAMPLE // LinkLocal is fe80::/10 private static V6 LL = Family.v6().parse("fe80::"); private static V6 MASK = Family.v6().subnets().masks().get(10); public static boolean isLinkLocal(V6 candidate) { return MASK.and(candidate).equals(LL); }
- Parameters:
operand
- the mask address- Returns:
- the AND'd address
-
or
Bitwise OR.- Parameters:
operand
- the mask address- Returns:
- the OR'd address
-
xor
Bitwise exclusive OR.- Parameters:
operand
- the mask address- Returns:
- the XOR'd address
-
not
Bitwise NOT.// EXAMPLE // Define 192.168.0.0/24 int maskBits = 24; V4 networkAddress = Family.v4().parse("192.168.0.0"); // 255.255.255.0 V4 mask = Family.v4().subnets().masks().get(maskBits); // 0.0.0.255 V4 inverseMask = mask.not() // 192.168.0.255 V4 lastAddress = inverseMask.or(networkAddress);
- Returns:
- the complement
-
shift
Bitwise shift. Negative operands shift left. Positive operands shift right. Values exceedingFamily.width()
overflow as described in The Java Language Specification Java SE 21 Edition 15.19. Shift Operators.// EXAMPLE // 0.0.0.1 V4 one = Family.v4().parse(1); // 0.0.0.2 V4 two = one.shift(-1);
- Parameters:
bits
- number of bits to shift- Returns:
- shifted value
-