- All Superinterfaces:
AddressComponent
,AddressComponentRange
,AddressGenericDivision
,AddressItem
,AddressItemRange
,AddressStringDivision
,Comparable<AddressItem>
,Serializable
- All Known Implementing Classes:
IPAddressSegment
,IPv4AddressSegment
,IPv6AddressSegment
,MACAddressSegment
The current implementations of this class are the most common representations of IPv4, IPv6 and MAC; segments are 1 byte for Ipv4, they are two bytes for Ipv6, and they are 1 byte for MAC addresses.
There are alternative forms of dividing addresses into segments, such as dotted representation for MAC like 1111.2222.3333, embedded IPv4 representation for IPv6 like f:f:f:f:f:f:1.2.3.4, inet_aton formats like 1.2 for IPv4, and so on.
If those alternative representations were to follow the general rules for segment representation, then you could reuse this class for thos alternative representations.
The general rules are that segments have a whole number of bytes, and in a given address all segments have the same length.
When alternatives forms do not follow the general rules for segments,
you can use the AddressDivision
interface instead.
Divisions do not have the restriction that divisions of an address are equal length and a whole number of bytes.
Divisions can be grouped using AddressDivisionGrouping
.
AddressSegment objects are immutable and thus also thread-safe.
- Author:
- sfoley
-
Method Summary
Modifier and TypeMethodDescriptionboolean
contains
(AddressSegment other) boolean
Iterable
<? extends AddressSegment> Useful for using an instance in a "for-each loop".getLower()
If this segment represents a range of values, returns a segment representing just the lowest value in the range, otherwise returns this.int
Gets the maximum possible value for this type of segment (for the highest range value of this particular segment, usegetUpper()
int
returns the lower valuegetUpper()
If this segment represents a range of values, returns a segment representing just the highest value in the range, otherwise returns this.int
returns the upper valueint
default boolean
isOneBit
(int segmentBitIndex) Returns true if the bit in the lower value of this segment at the given index is 1, where index 0 is the most significant bit.Iterator
<? extends AddressSegment> iterator()
Iterates through the individual address components.boolean
matches
(int value) boolean
matchesWithMask
(int value, int mask) boolean
matchesWithMask
(int lowerValue, int upperValue, int mask) boolean
prefixEquals
(AddressSegment other, int prefixLength) Returns whether the given prefix bits match the same bits of the given segment.reverseBits
(boolean perByte) Returns a new AddressComponent with the bits reversed.Returns an AddressComponent with the bytes reversed.AddressComponentSpliterator
<? extends AddressSegment> Partitions and traverses through the individual address components.Stream
<? extends AddressSegment> stream()
Returns a sequential stream of the individual address components.default boolean
testBit
(int n) Analogous toBigInteger.testBit(int)
, Computes (this & (1 << n)) != 0)Methods inherited from interface inet.ipaddr.AddressComponent
getNetwork, toHexString, toNormalizedString
Methods inherited from interface inet.ipaddr.format.AddressItem
compareTo, containsPrefixBlock, containsSinglePrefixBlock, getBitCount, getByteCount, getBytes, getBytes, getBytes, getCount, getMinPrefixLengthForBlock, getPrefixCount, getPrefixLengthForSingleBlock, getUpperBytes, getUpperBytes, getUpperBytes, getUpperValue, getValue, includesMax, includesZero, isFullRange, isMax, isMultiple, isZero
Methods inherited from interface inet.ipaddr.format.string.AddressStringDivision
getDigitCount, getLowerStandardString, getMaxDigitCount, getStandardString, isBoundedBy
-
Method Details
-
getValueCount
int getValueCount()- Returns:
- the same value as
AddressItem.getCount()
as an integer
-
getSegmentValue
int getSegmentValue()returns the lower value -
getUpperSegmentValue
int getUpperSegmentValue()returns the upper value -
getLower
AddressSegment getLower()If this segment represents a range of values, returns a segment representing just the lowest value in the range, otherwise returns this.- Specified by:
getLower
in interfaceAddressComponentRange
- Returns:
-
getUpper
AddressSegment getUpper()If this segment represents a range of values, returns a segment representing just the highest value in the range, otherwise returns this.- Specified by:
getUpper
in interfaceAddressComponentRange
- Returns:
-
reverseBits
Description copied from interface:AddressComponent
Returns a new AddressComponent with the bits reversed. If this component represents a range of values that cannot be reversed, then this throwsIncompatibleAddressException
. In a range the most significant bits stay constant while the least significant bits range over different values, so reversing that scenario results in a series of non-consecutive values, in most cases, which cannot be represented with a single AddressComponent object.In such cases where isMultiple() is true, call iterator(), getLower(), getUpper() or some other methods to break the series down into a series representing a single value.
- Specified by:
reverseBits
in interfaceAddressComponent
- Parameters:
perByte
- if true, only the bits in each byte are reversed, if false, then all bits in the component are reversed- Returns:
-
reverseBytes
AddressSegment reverseBytes()Description copied from interface:AddressComponent
Returns an AddressComponent with the bytes reversed. If this component represents a range of values that cannot be reversed, then this throwsIncompatibleAddressException
. In a range the most significant bits stay constant while the least significant bits range over different values, so reversing that scenario results in a series of non-consecutive values, in most cases, which cannot be represented with a single AddressComponent object.In such cases where isMultiple() is true, call iterator(), getLower(), getUpper() or some other methods to break the series down into a series representing a single value.
- Specified by:
reverseBytes
in interfaceAddressComponent
- Returns:
-
getIterable
Iterable<? extends AddressSegment> getIterable()Description copied from interface:AddressComponentRange
Useful for using an instance in a "for-each loop". Otherwise just callAddressComponentRange.iterator()
directly.- Specified by:
getIterable
in interfaceAddressComponentRange
- Returns:
-
iterator
Iterator<? extends AddressSegment> iterator()Description copied from interface:AddressComponentRange
Iterates through the individual address components.An address component can represent an individual segment, address, or section, or it can represent multiple, typically a subnet of addresses or a range of segment or section values.
Call
AddressItem.isMultiple()
to determine if this instance represents multiple, orAddressItem.getCount()
for the count.- Specified by:
iterator
in interfaceAddressComponentRange
- Returns:
-
spliterator
AddressComponentSpliterator<? extends AddressSegment> spliterator()Description copied from interface:AddressComponentRange
Partitions and traverses through the individual address components.- Specified by:
spliterator
in interfaceAddressComponent
- Specified by:
spliterator
in interfaceAddressComponentRange
- Returns:
-
stream
Stream<? extends AddressSegment> stream()Description copied from interface:AddressComponentRange
Returns a sequential stream of the individual address components. For a parallel stream, callBaseStream.parallel()
on the returned stream.- Specified by:
stream
in interfaceAddressComponentRange
- Returns:
-
matches
boolean matches(int value) -
matchesWithMask
boolean matchesWithMask(int value, int mask) -
matchesWithMask
boolean matchesWithMask(int lowerValue, int upperValue, int mask) -
contains
-
equals
-
prefixEquals
Returns whether the given prefix bits match the same bits of the given segment.- Parameters:
other
-prefixLength
-- Returns:
-
testBit
default boolean testBit(int n) Analogous toBigInteger.testBit(int)
, Computes (this & (1 << n)) != 0)- Parameters:
n
-- Returns:
- Throws:
IndexOutOfBoundsException
- if the index is negative or as large as the bit count- See Also:
-
isOneBit
default boolean isOneBit(int segmentBitIndex) Returns true if the bit in the lower value of this segment at the given index is 1, where index 0 is the most significant bit.- Parameters:
segmentBitIndex
-- Returns:
- Throws:
IndexOutOfBoundsException
- if the index is negative or as large as the bit count- See Also:
-
getMaxSegmentValue
int getMaxSegmentValue()Gets the maximum possible value for this type of segment (for the highest range value of this particular segment, usegetUpper()
- Returns:
-