- 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 intMAX_CHARSWhen printed with the default radix of 10, the max number of characters per segment-
Fields inherited from class inet.ipaddr.format.AddressDivisionBase
EXTENDED_DIGITS
-
-
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 booleancontains(AddressSegment other)booleanequals(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.intgetBitCount()Provides the number of bits comprising this address itemintgetByteCount()Provides the number of bytes required for this address item, rounding up if the bit count is not a multiple of 8intgetDefaultTextualRadix()IPAddress.IPVersiongetIPVersion()java.lang.Iterable<IPv4AddressSegment>getIterable()Useful for using an instance in a "for-each loop".IPv4AddressSegmentgetLower()If this segment represents a range of values, returns a segment representing just the lowest value in the range, otherwise returns this.intgetMaxDigitCount()intgetMaxSegmentValue()Gets the maximum possible value for this type of segment (for the highest range value of this particular segment, useAddressSegment.getUpper()longgetMaxValue()IPv4AddressNetworkgetNetwork()Returns the network object for components of the same version (eg IPv4, IPv6 and MAC each have their own network object)IPv4AddressNetwork.IPv4AddressCreatorgetSegmentCreator()IPv4AddressSegmentgetUpper()If this segment represents a range of values, returns a segment representing just the highest value in the range, otherwise returns this.booleanisIPv4()java.util.Iterator<IPv4AddressSegment>iterator()Iterates through the individual address components.IPv6AddressSegmentjoin(IPv6AddressNetwork.IPv6AddressCreator creator, IPv4AddressSegment low)Joins with another IPv4 segment to produce a IPv6 segment.booleanoverlaps(AddressSegment other)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.booleanprefixContains(IPAddressSegment other, int segmentPrefixLength)Returns whether the given prefix bit value ranges contain the same bits of the given segment.booleanprefixEquals(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.IPv4AddressSegmentremovePrefixLength()Deprecated.IPv4AddressSegmentremovePrefixLength(boolean zeroed)Deprecated.IPv4AddressSegmentreverseBits()IPv4AddressSegmentreverseBits(boolean perByte)Returns a new AddressComponent with the bits reversed.IPv4AddressSegmentreverseBytes()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.IPv4AddressSegmenttoHostSegment(java.lang.Integer bits)used by getHostSection, seeIPAddressSegmentSeries.getHostSection(int)IPv4AddressSegmenttoNetworkSegment(java.lang.Integer segmentPrefixLength)used by constructors of IPAddressSection, seeIPAddressSegmentSeries.getNetworkSection(int, boolean)IPv4AddressSegmenttoNetworkSegment(java.lang.Integer segmentPrefixLength, boolean withPrefixLength)used by getNetworkSection and by constructors of IPAddressSection, seeIPAddressSegmentSeries.getNetworkSection(int, boolean)IPv4AddressSegmenttoZeroHost()Returns a segment with the same network bits as this segment, but with the host bits changed to 0.IPv4AddressSegmentwithoutPrefixLength()Returns a segment with the same values but without a prefix length.-
Methods inherited from class inet.ipaddr.IPAddressSegment
bitwiseOrRange, getBitCount, getByteCount, getCount, getDefaultTextualRadix, getDivisionValue, getMaxSegmentValue, getMinPrefixLengthForBlock, getPrefixCount, getPrefixValueCount, 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, getDigitCount, getMaxDigitCount, getPrefixLengthForSingleBlock, getUpperValue, getValue, hasUppercaseVariations, isBitwiseOrCompatibleWithRange, isMaskCompatibleWithRange, isMax, isZero, maskRange, matches, matchesWithMask, matchesWithMask
-
Methods inherited from class inet.ipaddr.format.AddressDivisionBase
getBytes, getBytes, getBytes, getDigitCount, getDigitCount, getLowerStandardString, getStandardString, getUpperBytes, getUpperBytes, getUpperBytes, toString
-
Methods inherited from interface inet.ipaddr.format.AddressItem
compareTo, containsPrefixBlock, containsSinglePrefixBlock, getBytes, getBytes, getBytes, 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 AddressValueExceptionConstructs 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 AddressValueExceptionConstructs 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 AddressValueExceptionConstructs 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:
getMaxValuein classIPAddressSegment
-
isIPv4
public boolean isIPv4()
- Overrides:
isIPv4in classIPAddressSegment
-
getIPVersion
public IPAddress.IPVersion getIPVersion()
- Specified by:
getIPVersionin classIPAddressSegment
-
getMaxSegmentValue
public int getMaxSegmentValue()
Description copied from interface:AddressSegmentGets the maximum possible value for this type of segment (for the highest range value of this particular segment, useAddressSegment.getUpper()- Specified by:
getMaxSegmentValuein interfaceAddressSegment- Returns:
-
toNetworkSegment
public IPv4AddressSegment toNetworkSegment(java.lang.Integer segmentPrefixLength)
Description copied from class:IPAddressSegmentused by constructors of IPAddressSection, seeIPAddressSegmentSeries.getNetworkSection(int, boolean)- Overrides:
toNetworkSegmentin classIPAddressSegment
-
toNetworkSegment
public IPv4AddressSegment toNetworkSegment(java.lang.Integer segmentPrefixLength, boolean withPrefixLength)
Description copied from class:IPAddressSegmentused by getNetworkSection and by constructors of IPAddressSection, seeIPAddressSegmentSeries.getNetworkSection(int, boolean)- Specified by:
toNetworkSegmentin classIPAddressSegment
-
toHostSegment
public IPv4AddressSegment toHostSegment(java.lang.Integer bits)
Description copied from class:IPAddressSegmentused by getHostSection, seeIPAddressSegmentSeries.getHostSection(int)- Specified by:
toHostSegmentin classIPAddressSegment
-
getLower
public IPv4AddressSegment getLower()
Description copied from class:IPAddressSegmentIf this segment represents a range of values, returns a segment representing just the lowest value in the range, otherwise returns this.- Specified by:
getLowerin interfaceAddressComponentRange- Specified by:
getLowerin interfaceAddressSegment- Specified by:
getLowerin classIPAddressSegment- Returns:
-
getUpper
public IPv4AddressSegment getUpper()
Description copied from class:IPAddressSegmentIf this segment represents a range of values, returns a segment representing just the highest value in the range, otherwise returns this.- Specified by:
getUpperin interfaceAddressComponentRange- Specified by:
getUpperin interfaceAddressSegment- Specified by:
getUpperin classIPAddressSegment- Returns:
-
getNetwork
public IPv4AddressNetwork getNetwork()
Description copied from interface:AddressComponentReturns the network object for components of the same version (eg IPv4, IPv6 and MAC each have their own network object)- Specified by:
getNetworkin interfaceAddressComponent- Specified by:
getNetworkin classIPAddressSegment- Returns:
-
getSegmentCreator
public IPv4AddressNetwork.IPv4AddressCreator getSegmentCreator()
-
getIterable
public java.lang.Iterable<IPv4AddressSegment> getIterable()
Description copied from interface:AddressComponentRangeUseful for using an instance in a "for-each loop". Otherwise just callAddressComponentRange.iterator()directly.- Specified by:
getIterablein interfaceAddressComponentRange- Specified by:
getIterablein interfaceAddressSegment- Specified by:
getIterablein classIPAddressSegment- Returns:
-
iterator
public java.util.Iterator<IPv4AddressSegment> iterator()
Description copied from interface:AddressComponentRangeIterates 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:
iteratorin interfaceAddressComponentRange- Specified by:
iteratorin interfaceAddressSegment- Specified by:
iteratorin interfacejava.lang.Iterable<IPv4AddressSegment>- Specified by:
iteratorin classIPAddressSegment- Returns:
-
spliterator
public AddressComponentSpliterator<IPv4AddressSegment> spliterator()
Description copied from interface:AddressComponentRangePartitions and traverses through the individual address components.- Specified by:
spliteratorin interfaceAddressComponent- Specified by:
spliteratorin interfaceAddressComponentRange- Specified by:
spliteratorin interfaceAddressSegment- Specified by:
spliteratorin interfacejava.lang.Iterable<IPv4AddressSegment>- Specified by:
spliteratorin classIPAddressSegment- Returns:
-
stream
public java.util.stream.Stream<IPv4AddressSegment> stream()
Description copied from interface:AddressComponentRangeReturns a sequential stream of the individual address components. For a parallel stream, callBaseStream.parallel()on the returned stream.- Specified by:
streamin interfaceAddressComponentRange- Specified by:
streamin interfaceAddressSegment- Specified by:
streamin classIPAddressSegment- Returns:
-
prefixBlockIterator
public java.util.Iterator<IPv4AddressSegment> prefixBlockIterator()
Description copied from class:IPAddressSegmentIterates through the individual prefix blocks.If the series has no prefix length, then this is equivalent to
IPAddressSegment.iterator()- Specified by:
prefixBlockIteratorin classIPAddressSegment
-
prefixBlockSpliterator
public AddressComponentSpliterator<IPv4AddressSegment> prefixBlockSpliterator()
Description copied from class:IPAddressSegmentPartitions and traverses through the individual prefix blocks of this segment for its prefix length.- Specified by:
prefixBlockSpliteratorin classIPAddressSegment- Returns:
-
prefixBlockStream
public java.util.stream.Stream<IPv4AddressSegment> prefixBlockStream()
Description copied from class:IPAddressSegmentReturns a sequential stream of the individual prefix blocks of this segment. For a parallel stream, callBaseStream.parallel()on the returned stream.- Specified by:
prefixBlockStreamin classIPAddressSegment- Returns:
-
prefixIterator
public java.util.Iterator<IPv4AddressSegment> prefixIterator()
Description copied from class:IPAddressSegmentIterates through the individual prefixes.If the series has no prefix length, then this is equivalent to
IPAddressSegment.iterator()- Specified by:
prefixIteratorin classIPAddressSegment
-
prefixSpliterator
public AddressComponentSpliterator<IPv4AddressSegment> prefixSpliterator()
Description copied from class:IPAddressSegmentPartitions and traverses through the individual prefixes of this segment for its prefix length.- Specified by:
prefixSpliteratorin classIPAddressSegment- Returns:
-
prefixStream
public java.util.stream.Stream<IPv4AddressSegment> prefixStream()
Description copied from class:IPAddressSegmentReturns a sequential stream of the individual prefixes of this segment. For a parallel stream, callBaseStream.parallel()on the returned stream.- Specified by:
prefixStreamin classIPAddressSegment- Returns:
-
prefixBlockIterator
public java.util.Iterator<IPv4AddressSegment> prefixBlockIterator(int prefixLength)
Description copied from class:IPAddressSegmentIterates through the individual prefix blocks according to the given segment prefix length. Any existing prefix length is disregarded.- Specified by:
prefixBlockIteratorin classIPAddressSegment
-
prefixBlockSpliterator
public AddressComponentSpliterator<IPv4AddressSegment> prefixBlockSpliterator(int segPrefLength)
Description copied from class:IPAddressSegmentPartitions and traverses through the individual prefix blocks for the given prefix length.- Specified by:
prefixBlockSpliteratorin classIPAddressSegment- Returns:
-
prefixBlockStream
public java.util.stream.Stream<IPv4AddressSegment> prefixBlockStream(int segPrefLength)
Description copied from class:IPAddressSegmentReturns 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:
prefixBlockStreamin classIPAddressSegment- Returns:
-
getBitCount
public int getBitCount()
Description copied from interface:AddressItemProvides the number of bits comprising this address item- Specified by:
getBitCountin interfaceAddressItem- Specified by:
getBitCountin interfaceIPAddressStringDivision- Returns:
- the number of bits
-
getByteCount
public int getByteCount()
Description copied from interface:AddressItemProvides the number of bytes required for this address item, rounding up if the bit count is not a multiple of 8- Specified by:
getByteCountin interfaceAddressItem- Returns:
- the number of bytes
-
getDefaultTextualRadix
public int getDefaultTextualRadix()
-
getMaxDigitCount
public int getMaxDigitCount()
-
reverseBits
public IPv4AddressSegment reverseBits(boolean perByte)
Description copied from interface:AddressComponentReturns 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:
reverseBitsin interfaceAddressComponent- Specified by:
reverseBitsin interfaceAddressSegment- Specified by:
reverseBitsin 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:AddressComponentReturns 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:
reverseBytesin interfaceAddressComponent- Specified by:
reverseBytesin interfaceAddressSegment- Specified by:
reverseBytesin classIPAddressSegment- Returns:
-
toZeroHost
public IPv4AddressSegment toZeroHost()
Description copied from class:IPAddressSegmentReturns 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:
toZeroHostin classIPAddressSegment- Returns:
-
removePrefixLength
@Deprecated public IPv4AddressSegment removePrefixLength(boolean zeroed)
Deprecated.- Specified by:
removePrefixLengthin classIPAddressSegment- Returns:
-
removePrefixLength
@Deprecated public IPv4AddressSegment removePrefixLength()
Deprecated.- Specified by:
removePrefixLengthin classIPAddressSegment- Returns:
-
withoutPrefixLength
public IPv4AddressSegment withoutPrefixLength()
Description copied from class:IPAddressSegmentReturns a segment with the same values but without a prefix length.- Specified by:
withoutPrefixLengthin classIPAddressSegment- Returns:
-
prefixContains
public boolean prefixContains(IPAddressSegment other, int segmentPrefixLength)
Description copied from class:IPAddressSegmentReturns whether the given prefix bit value ranges contain the same bits of the given segment.- Overrides:
prefixContainsin classIPAddressSegment- Returns:
-
prefixEquals
public boolean prefixEquals(AddressSegment other, int segmentPrefixLength)
Description copied from interface:AddressSegmentReturns whether the given prefix bits match the same bits of the given segment.- Specified by:
prefixEqualsin interfaceAddressSegment- Overrides:
prefixEqualsin classIPAddressSegment- Returns:
-
overlaps
public boolean overlaps(AddressSegment other)
- Specified by:
overlapsin interfaceAddressSegment
-
contains
public boolean contains(AddressSegment other)
- Specified by:
containsin interfaceAddressSegment
-
equals
public boolean equals(java.lang.Object other)
Description copied from class:AddressDivisionBaseTwo 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:
equalsin interfaceAddressSegment- Overrides:
equalsin 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
-
-