java.lang.Object
uk.ipfreely.Addr<A>
- Type Parameters:
A- the address type
- All Implemented Interfaces:
Comparable<A>
public abstract sealed class Addr<A extends Addr<A>>
extends Object
implements Comparable<A>
permits V4, V6
Abstract IP address type implemented by V4 and V6.
Use Family to create instances.
Future implementations may become value objects.
Usage Hints
| Feature | Addr |
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.
-
Method Summary
Modifier and TypeMethodDescriptionabstract AAddition with overflow.abstract ABitwise AND.abstract ADivision.abstract doubleLikeNumber.doubleValue().abstract booleanObject equality.family()Internet protocol family - V4 or V6.abstract inthashCode()Hash code.abstract longhighBits()Useful for efficient conversion.abstract intSimilar toInteger.numberOfLeadingZeros(int).abstract longlowBits()Useful for efficient conversion.abstract AModulus.abstract AMultiplication with overflow.next()Returns the IP address incremented by one, with overflow.abstract Anot()Bitwise NOT.abstract ABitwise OR.prev()Returns the IP address decremented by one, with underflow.abstract Ashift(int bits) Bitwise shift.abstract ASubtraction with underflow.abstract BigIntegerThe address as aBigInteger.abstract byte[]toBytes()The address as bytes of lengthFamily.width()/Byte.SIZE.abstract StringtoString()String form amenable to parsers.abstract intSimilar toInteger.numberOfTrailingZeros(int).abstract ABitwise exclusive OR.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods 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
-