- java.lang.Object
-
- inet.ipaddr.format.AddressDivisionBase
-
- inet.ipaddr.format.standard.AddressDivision
-
- inet.ipaddr.format.standard.IPAddressDivision
-
- inet.ipaddr.IPAddressSegment
-
- inet.ipaddr.ipv4.IPv4AddressSegment
-
- All Implemented Interfaces:
AddressComponent
,AddressSegment
,AddressComponentRange
,AddressGenericDivision
,AddressItem
,AddressItemRange
,IPAddressGenericDivision
,AddressStringDivision
,IPAddressStringDivision
,java.io.Serializable
,java.lang.Comparable<AddressItem>
,java.lang.Iterable<IPv4AddressSegment>
public class IPv4AddressSegment extends IPAddressSegment implements java.lang.Iterable<IPv4AddressSegment>
This represents a segment of an IPv4 address. For IPv4, segments are 1 byte. For IPv6, they are two bytes. Like String and Integer and various others basic objects, segments are immutable, which also makes them thread-safe.- Author:
- sfoley
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class inet.ipaddr.format.standard.AddressDivision
AddressDivision.BitwiseOrResult, AddressDivision.MaskResult
-
-
Field Summary
Fields Modifier and Type Field Description static int
MAX_CHARS
When printed with the default radix of 10, the max number of characters per segment
-
Constructor Summary
Constructors Constructor Description IPv4AddressSegment(int value)
Constructs a segment of an IPv4 address with the given value.IPv4AddressSegment(int lower, int upper, java.lang.Integer segmentPrefixLength)
Constructs a segment of an IPv4 address that represents a range of values.IPv4AddressSegment(int value, java.lang.Integer segmentPrefixLength)
Constructs a segment of an IPv4 address.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description boolean
contains(AddressSegment other)
boolean
equals(java.lang.Object other)
Two divisions are equal if they: - they match type/version (ipv4, ipv6, mac, or a specific division class) - match bit counts - match values Prefix lengths, for those divisions that have them, are ignored.int
getBitCount()
Provides the number of bits comprising this address itemint
getByteCount()
Provides the number of bytes required for this address item, rounding up if the bit count is not a multiple of 8int
getDefaultTextualRadix()
IPAddress.IPVersion
getIPVersion()
java.lang.Iterable<IPv4AddressSegment>
getIterable()
Useful for using an instance in a "for-each loop".IPv4AddressSegment
getLower()
If this segment represents a range of values, returns a segment representing just the lowest value in the range, otherwise returns this.int
getMaxDigitCount()
int
getMaxSegmentValue()
Gets the maximum possible value for this type of segment (for the highest range value of this particular segment, useAddressSegment.getUpper()
long
getMaxValue()
IPv4AddressNetwork
getNetwork()
Returns the network object for components of the same version (eg IPv4, IPv6 and MAC each have their own network object)IPv4AddressNetwork.IPv4AddressCreator
getSegmentCreator()
IPv4AddressSegment
getUpper()
If this segment represents a range of values, returns a segment representing just the highest value in the range, otherwise returns this.boolean
isIPv4()
java.util.Iterator<IPv4AddressSegment>
iterator()
Iterates through the individual address components.IPv6AddressSegment
join(IPv6AddressNetwork.IPv6AddressCreator creator, IPv4AddressSegment low)
Joins with another IPv4 segment to produce a IPv6 segment.java.util.Iterator<IPv4AddressSegment>
prefixBlockIterator()
Iterates through the individual prefix blocks.java.util.Iterator<IPv4AddressSegment>
prefixBlockIterator(int prefixLength)
Iterates through the individual prefix blocks according to the given segment prefix length.AddressComponentSpliterator<IPv4AddressSegment>
prefixBlockSpliterator()
Partitions and traverses through the individual prefix blocks of this segment for its prefix length.AddressComponentSpliterator<IPv4AddressSegment>
prefixBlockSpliterator(int segPrefLength)
Partitions and traverses through the individual prefix blocks for the given prefix length.java.util.stream.Stream<IPv4AddressSegment>
prefixBlockStream()
Returns a sequential stream of the individual prefix blocks of this segment.java.util.stream.Stream<IPv4AddressSegment>
prefixBlockStream(int segPrefLength)
Returns a sequential stream of the individual prefix blocks for the given prefix length.boolean
prefixContains(IPAddressSegment other, int segmentPrefixLength)
Returns whether the given prefix bit value ranges contain the same bits of the given segment.boolean
prefixEquals(AddressSegment other, int segmentPrefixLength)
Returns whether the given prefix bits match the same bits of the given segment.java.util.Iterator<IPv4AddressSegment>
prefixIterator()
Iterates through the individual prefixes.AddressComponentSpliterator<IPv4AddressSegment>
prefixSpliterator()
Partitions and traverses through the individual prefixes of this segment for its prefix length.java.util.stream.Stream<IPv4AddressSegment>
prefixStream()
Returns a sequential stream of the individual prefixes of this segment.IPv4AddressSegment
removePrefixLength()
Deprecated.IPv4AddressSegment
removePrefixLength(boolean zeroed)
Deprecated.IPv4AddressSegment
reverseBits()
IPv4AddressSegment
reverseBits(boolean perByte)
Returns a new AddressComponent with the bits reversed.IPv4AddressSegment
reverseBytes()
Returns an AddressComponent with the bytes reversed.AddressComponentSpliterator<IPv4AddressSegment>
spliterator()
Partitions and traverses through the individual address components.java.util.stream.Stream<IPv4AddressSegment>
stream()
Returns a sequential stream of the individual address components.IPv4AddressSegment
toHostSegment(java.lang.Integer bits)
used by getHostSection, seeIPAddressSegmentSeries.getHostSection(int)
IPv4AddressSegment
toNetworkSegment(java.lang.Integer segmentPrefixLength)
used by constructors of IPAddressSection, seeIPAddressSegmentSeries.getNetworkSection(int, boolean)
IPv4AddressSegment
toNetworkSegment(java.lang.Integer segmentPrefixLength, boolean withPrefixLength)
used by getNetworkSection and by constructors of IPAddressSection, seeIPAddressSegmentSeries.getNetworkSection(int, boolean)
IPv4AddressSegment
toZeroHost()
Returns a segment with the same network bits as this segment, but with the host bits changed to 0.IPv4AddressSegment
withoutPrefixLength()
Returns a segment with the same values but without a prefix length.-
Methods inherited from class inet.ipaddr.IPAddressSegment
bitwiseOrRange, getBitCount, getByteCount, getDefaultTextualRadix, getDivisionValue, getDivisionValueCount, getMaxSegmentValue, getMinPrefixLengthForBlock, getPrefixValueCount, getSegmentPrefixLength, getSegmentValue, getUpperDivisionValue, getUpperSegmentValue, getValueCount, hashCode, includesMax, includesZero, isBoundedBy, isIPv6, isMultiple, isPrefixBlock, maskRange, matches, matchesWithMask, matchesWithMask, matchesWithPrefixMask, prefixContains, prefixEquals, toHexString, toNormalizedString, toNormalizedString
-
Methods inherited from class inet.ipaddr.format.standard.IPAddressDivision
containsPrefixBlock, containsSinglePrefixBlock, getBlockMaskPrefixLength, getDivisionPrefixLength, getLeadingBitCount, getPrefixAdjustedRangeString, getString, getTrailingBitCount, getWildcardString, isPrefixed, isSinglePrefixBlock, matchesWithPrefixMask
-
Methods inherited from class inet.ipaddr.format.standard.AddressDivision
bitwiseOrRange, getCount, getDigitCount, getDivisionPrefixCount, getMaxDigitCount, getPrefixCount, getPrefixLengthForSingleBlock, getUpperValue, getValue, hasUppercaseVariations, isBitwiseOrCompatibleWithRange, isMaskCompatibleWithRange, isMax, isZero, maskRange, matches, matchesWithMask, matchesWithMask
-
Methods inherited from class inet.ipaddr.format.AddressDivisionBase
getBytes, getBytes, getBytes, getDigitCount, getLowerStandardString, getStandardString, getUpperBytes, getUpperBytes, getUpperBytes, toString
-
Methods inherited from interface inet.ipaddr.format.AddressItem
compareTo, containsPrefixBlock, containsSinglePrefixBlock, getBytes, getBytes, getBytes, getCount, getPrefixCount, getPrefixLengthForSingleBlock, getUpperBytes, getUpperBytes, getUpperBytes, getUpperValue, getValue, isMax, isZero
-
Methods inherited from interface inet.ipaddr.AddressSegment
isOneBit, testBit
-
Methods inherited from interface inet.ipaddr.format.string.AddressStringDivision
getDigitCount, getLowerStandardString, getMaxDigitCount, getStandardString
-
Methods inherited from interface inet.ipaddr.format.IPAddressGenericDivision
isFullRange
-
-
-
-
Field Detail
-
MAX_CHARS
public static final int MAX_CHARS
When printed with the default radix of 10, the max number of characters per segment- See Also:
- Constant Field Values
-
-
Constructor Detail
-
IPv4AddressSegment
public IPv4AddressSegment(int value) throws AddressValueException
Constructs a segment of an IPv4 address with the given value.- Parameters:
value
- the value of the segment- Throws:
AddressValueException
- if value is negative or too large
-
IPv4AddressSegment
public IPv4AddressSegment(int value, java.lang.Integer segmentPrefixLength) throws AddressValueException
Constructs a segment of an IPv4 address.- Parameters:
value
- the value of the segment. If the segmentPrefixLength is non-null, the network prefix of the value is used, and the segment represents all segment values with the same network prefix.segmentPrefixLength
- the segment prefix, which can be null- Throws:
AddressValueException
- if value or prefix length is negative or too large
-
IPv4AddressSegment
public IPv4AddressSegment(int lower, int upper, java.lang.Integer segmentPrefixLength) throws AddressValueException
Constructs a segment of an IPv4 address that represents a range of values.- Parameters:
segmentPrefixLength
- the segment prefix length, which can be null. If segmentPrefixLength is non-null, this segment represents a range of segment values with the given network prefix length.lower
- the lower value of the range of values represented by the segment. If segmentPrefixLength is non-null, the lower value becomes the smallest value with the same network prefix.upper
- the upper value of the range of values represented by the segment. If segmentPrefixLength is non-null, the upper value becomes the largest value with the same network prefix.- Throws:
AddressValueException
- if either lower or upper value or prefix length is negative or too large
-
-
Method Detail
-
getMaxValue
public long getMaxValue()
- Overrides:
getMaxValue
in classIPAddressSegment
-
isIPv4
public boolean isIPv4()
- Overrides:
isIPv4
in classIPAddressSegment
-
getIPVersion
public IPAddress.IPVersion getIPVersion()
- Specified by:
getIPVersion
in classIPAddressSegment
-
getMaxSegmentValue
public int getMaxSegmentValue()
Description copied from interface:AddressSegment
Gets the maximum possible value for this type of segment (for the highest range value of this particular segment, useAddressSegment.getUpper()
- Specified by:
getMaxSegmentValue
in interfaceAddressSegment
- Returns:
-
toNetworkSegment
public IPv4AddressSegment toNetworkSegment(java.lang.Integer segmentPrefixLength)
Description copied from class:IPAddressSegment
used by constructors of IPAddressSection, seeIPAddressSegmentSeries.getNetworkSection(int, boolean)
- Overrides:
toNetworkSegment
in classIPAddressSegment
-
toNetworkSegment
public IPv4AddressSegment toNetworkSegment(java.lang.Integer segmentPrefixLength, boolean withPrefixLength)
Description copied from class:IPAddressSegment
used by getNetworkSection and by constructors of IPAddressSection, seeIPAddressSegmentSeries.getNetworkSection(int, boolean)
- Specified by:
toNetworkSegment
in classIPAddressSegment
-
toHostSegment
public IPv4AddressSegment toHostSegment(java.lang.Integer bits)
Description copied from class:IPAddressSegment
used by getHostSection, seeIPAddressSegmentSeries.getHostSection(int)
- Specified by:
toHostSegment
in classIPAddressSegment
-
getLower
public IPv4AddressSegment getLower()
Description copied from class:IPAddressSegment
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
- Specified by:
getLower
in interfaceAddressSegment
- Specified by:
getLower
in classIPAddressSegment
- Returns:
-
getUpper
public IPv4AddressSegment getUpper()
Description copied from class:IPAddressSegment
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
- Specified by:
getUpper
in interfaceAddressSegment
- Specified by:
getUpper
in classIPAddressSegment
- Returns:
-
getNetwork
public IPv4AddressNetwork getNetwork()
Description copied from interface:AddressComponent
Returns the network object for components of the same version (eg IPv4, IPv6 and MAC each have their own network object)- Specified by:
getNetwork
in interfaceAddressComponent
- Specified by:
getNetwork
in classIPAddressSegment
- Returns:
-
getSegmentCreator
public IPv4AddressNetwork.IPv4AddressCreator getSegmentCreator()
-
getIterable
public java.lang.Iterable<IPv4AddressSegment> 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
- Specified by:
getIterable
in interfaceAddressSegment
- Specified by:
getIterable
in classIPAddressSegment
- Returns:
-
iterator
public java.util.Iterator<IPv4AddressSegment> 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
- Specified by:
iterator
in interfaceAddressSegment
- Specified by:
iterator
in interfacejava.lang.Iterable<IPv4AddressSegment>
- Specified by:
iterator
in classIPAddressSegment
- Returns:
-
spliterator
public AddressComponentSpliterator<IPv4AddressSegment> spliterator()
Description copied from interface:AddressComponentRange
Partitions and traverses through the individual address components.- Specified by:
spliterator
in interfaceAddressComponent
- Specified by:
spliterator
in interfaceAddressComponentRange
- Specified by:
spliterator
in interfaceAddressSegment
- Specified by:
spliterator
in interfacejava.lang.Iterable<IPv4AddressSegment>
- Specified by:
spliterator
in classIPAddressSegment
- Returns:
-
stream
public java.util.stream.Stream<IPv4AddressSegment> 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
- Specified by:
stream
in interfaceAddressSegment
- Specified by:
stream
in classIPAddressSegment
- Returns:
-
prefixBlockIterator
public java.util.Iterator<IPv4AddressSegment> prefixBlockIterator()
Description copied from class:IPAddressSegment
Iterates through the individual prefix blocks.If the series has no prefix length, then this is equivalent to
IPAddressSegment.iterator()
- Specified by:
prefixBlockIterator
in classIPAddressSegment
-
prefixBlockSpliterator
public AddressComponentSpliterator<IPv4AddressSegment> prefixBlockSpliterator()
Description copied from class:IPAddressSegment
Partitions and traverses through the individual prefix blocks of this segment for its prefix length.- Specified by:
prefixBlockSpliterator
in classIPAddressSegment
- Returns:
-
prefixBlockStream
public java.util.stream.Stream<IPv4AddressSegment> prefixBlockStream()
Description copied from class:IPAddressSegment
Returns a sequential stream of the individual prefix blocks of this segment. For a parallel stream, callBaseStream.parallel()
on the returned stream.- Specified by:
prefixBlockStream
in classIPAddressSegment
- Returns:
-
prefixIterator
public java.util.Iterator<IPv4AddressSegment> prefixIterator()
Description copied from class:IPAddressSegment
Iterates through the individual prefixes.If the series has no prefix length, then this is equivalent to
IPAddressSegment.iterator()
- Specified by:
prefixIterator
in classIPAddressSegment
-
prefixSpliterator
public AddressComponentSpliterator<IPv4AddressSegment> prefixSpliterator()
Description copied from class:IPAddressSegment
Partitions and traverses through the individual prefixes of this segment for its prefix length.- Specified by:
prefixSpliterator
in classIPAddressSegment
- Returns:
-
prefixStream
public java.util.stream.Stream<IPv4AddressSegment> prefixStream()
Description copied from class:IPAddressSegment
Returns a sequential stream of the individual prefixes of this segment. For a parallel stream, callBaseStream.parallel()
on the returned stream.- Specified by:
prefixStream
in classIPAddressSegment
- Returns:
-
prefixBlockIterator
public java.util.Iterator<IPv4AddressSegment> prefixBlockIterator(int prefixLength)
Description copied from class:IPAddressSegment
Iterates through the individual prefix blocks according to the given segment prefix length. Any existing prefix length is disregarded.- Specified by:
prefixBlockIterator
in classIPAddressSegment
-
prefixBlockSpliterator
public AddressComponentSpliterator<IPv4AddressSegment> prefixBlockSpliterator(int segPrefLength)
Description copied from class:IPAddressSegment
Partitions and traverses through the individual prefix blocks for the given prefix length.- Specified by:
prefixBlockSpliterator
in classIPAddressSegment
- Returns:
-
prefixBlockStream
public java.util.stream.Stream<IPv4AddressSegment> prefixBlockStream(int segPrefLength)
Description copied from class:IPAddressSegment
Returns a sequential stream of the individual prefix blocks for the given prefix length. For a parallel stream, callBaseStream.parallel()
on the returned stream.- Specified by:
prefixBlockStream
in classIPAddressSegment
- Returns:
-
getBitCount
public int getBitCount()
Description copied from interface:AddressItem
Provides the number of bits comprising this address item- Specified by:
getBitCount
in interfaceAddressItem
- Specified by:
getBitCount
in interfaceIPAddressStringDivision
- Returns:
- the number of bits
-
getByteCount
public int getByteCount()
Description copied from interface:AddressItem
Provides the number of bytes required for this address item, rounding up if the bit count is not a multiple of 8- Specified by:
getByteCount
in interfaceAddressItem
- Returns:
- the number of bytes
-
getDefaultTextualRadix
public int getDefaultTextualRadix()
-
getMaxDigitCount
public int getMaxDigitCount()
-
reverseBits
public IPv4AddressSegment reverseBits(boolean perByte)
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
- Specified by:
reverseBits
in interfaceAddressSegment
- Specified by:
reverseBits
in classIPAddressSegment
- Parameters:
perByte
- if true, only the bits in each byte are reversed, if false, then all bits in the component are reversed- Returns:
-
reverseBits
public IPv4AddressSegment reverseBits()
-
reverseBytes
public IPv4AddressSegment 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
- Specified by:
reverseBytes
in interfaceAddressSegment
- Specified by:
reverseBytes
in classIPAddressSegment
- Returns:
-
toZeroHost
public IPv4AddressSegment toZeroHost()
Description copied from class:IPAddressSegment
Returns a segment with the same network bits as this segment, but with the host bits changed to 0.If there is no prefix length associated with this segment, returns an all-zero segment.
This is nearly equivalent to doing the mask (see
IPAddressSegment.maskRange(int)
) of this segment with the network mask for the given prefix length, but when applying a mask to a range of values you can have a non-sequential result.With this method, if the resulting series has a range of values, then the resulting series range boundaries will have host values of 0, but not necessarily all the intervening values.
For instance, the 1-byte segment range 4-7 with prefix length 6, when masked with 252 (the network mask) results in just the single value 4, matching the result of this method. The 1-byte segment range 4-8 with prefix length 6, when masked with 252 results in the two non-sequential values, 4 and 8, but the result of this method with prefix length 6 results in the range 4-8, the same as the original segment.
The default behaviour is that the resultant series will have the same prefix length. The resultant series will not have a prefix length if
AddressNetwork.getPrefixConfiguration()
isAddressNetwork.PrefixConfiguration.ALL_PREFIXED_ADDRESSES_ARE_SUBNETS
.- Specified by:
toZeroHost
in classIPAddressSegment
- Returns:
-
removePrefixLength
@Deprecated public IPv4AddressSegment removePrefixLength(boolean zeroed)
Deprecated.- Specified by:
removePrefixLength
in classIPAddressSegment
- Returns:
-
removePrefixLength
@Deprecated public IPv4AddressSegment removePrefixLength()
Deprecated.- Specified by:
removePrefixLength
in classIPAddressSegment
- Returns:
-
withoutPrefixLength
public IPv4AddressSegment withoutPrefixLength()
Description copied from class:IPAddressSegment
Returns a segment with the same values but without a prefix length.- Specified by:
withoutPrefixLength
in classIPAddressSegment
- Returns:
-
prefixContains
public boolean prefixContains(IPAddressSegment other, int segmentPrefixLength)
Description copied from class:IPAddressSegment
Returns whether the given prefix bit value ranges contain the same bits of the given segment.- Overrides:
prefixContains
in classIPAddressSegment
- Returns:
-
prefixEquals
public boolean prefixEquals(AddressSegment other, int segmentPrefixLength)
Description copied from interface:AddressSegment
Returns whether the given prefix bits match the same bits of the given segment.- Specified by:
prefixEquals
in interfaceAddressSegment
- Overrides:
prefixEquals
in classIPAddressSegment
- Returns:
-
contains
public boolean contains(AddressSegment other)
- Specified by:
contains
in interfaceAddressSegment
-
equals
public boolean equals(java.lang.Object other)
Description copied from class:AddressDivisionBase
Two divisions are equal if they: - they match type/version (ipv4, ipv6, mac, or a specific division class) - match bit counts - match values Prefix lengths, for those divisions that have them, are ignored.- Specified by:
equals
in interfaceAddressSegment
- Overrides:
equals
in classAddressDivision
-
join
public IPv6AddressSegment join(IPv6AddressNetwork.IPv6AddressCreator creator, IPv4AddressSegment low) throws IncompatibleAddressException
Joins with another IPv4 segment to produce a IPv6 segment.- Parameters:
creator
-low
-- Returns:
- Throws:
IncompatibleAddressException
-
-