Package io.smallrye.common.version
Class AbstractVersionIterator
- java.lang.Object
-
- io.smallrye.common.version.AbstractVersionIterator
-
- All Implemented Interfaces:
VersionIterator
public abstract class AbstractVersionIterator extends Object implements VersionIterator
A version scheme iterator base class that provides useful utilities and mechanisms. The iterator does not support version strings which are greater than 4095 characters in length.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classAbstractVersionIterator.TokenTypeThe current state of iteration.
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractVersionIterator(String string)Construct a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringBuilderappendAlphaPartTo(StringBuilder target)Append the current alphabetical part to the given string builder.StringBuilderappendNumberPartTo(StringBuilder target)Get the current numeric part, appending it to the given builder.StringBuilderappendPartTo(StringBuilder target)Append this version part to the given string builder.protected voidchangeType(AbstractVersionIterator.TokenType newType)Change the current token type.intcompareAlphaPart(VersionIterator other, boolean ignoreCase)Compare two alphabetical parts lexicographically.intcompareAlphaPart(String str, int offs, int len, boolean ignoreCase)Compare two alphabetical parts lexicographically.intcompareNumberPart(int value)Compare two numerical parts (using an unsigned comparison).intcompareNumberPart(long value)Compare two numerical parts (using an unsigned comparison).intcompareNumberPart(VersionIterator other)Compare two numerical parts (using an unsigned comparison).intcompareNumberPart(String value, int offs, int len)Compare two numerical parts (using an unsigned comparison).protected AbstractVersionIterator.TokenTypecurrentType()Get the current iteration token type.StringgetAlphaPart()Get the current alphabetical part.protected intgetEndIndex()Get the end index (exclusive) of the current token.protected intgetExtraBits()Get the extra implementation-specific state bits.intgetNumberPartAsInt()Get the current numeric part, as anint.longgetNumberPartAsLong()Get the current numeric part, as along.StringgetNumberPartAsString()Get the current numeric part, as aString.intgetSeparatorCodePoint()Get the code point of the current separator.protected intgetStartIndex()Get the start index of the current token.booleanhasNext()A basic implementation ofVersionIterator.hasNext()which returnstrueif there are more characters after the current token.protected voidinsertEmptyAlpha()Insert an empty part into the current position.protected voidinsertEmptyNumber()Insert an empty (zero) number into the current position.protected voidinsertEmptySeparator()Insert an empty separator into the current position.protected booleanisAlphaCodePoint(int cp)Determine whether the given code point is a valid alpha code point according to the current scheme.booleanisAlphaPart()Determine if the current token is an alphabetical part.booleanisEmptySeparator()Determine if the current token is an empty (or zero-length alphabetical-to-numeric or numeric-to-alphabetical) separator.booleanisNonEmptySeparator()Determine if the current token is a non-empty separator.booleanisNumberPart()Determine if the current token is a numeric part.booleanisPart()Determine if the current token is some kind of part (alphabetical or numeric).booleanisSeparator()Determine if the current token is some kind of separator (a character or a zero-length alphabetical-to-numeric or numeric-to-alphabetical transition).protected booleanisSeparatorCodePoint(int cp)Determine whether the given code point is a valid separator according to the current scheme.intlength()Get the length of the current token.protected longmark()Mark the current iteration position and state.voidnext()A basic implementation ofVersionIterator.next()which uses the methods on this class to establish the next iteration token.protected intnumericCodePointValue(int cp)Determine whether the given code point is a valid numeric code point according to the current scheme, and if so, what the digit's numerical value is.protected voidreset(long mark)Restore the iteration position to a previousmark()state.protected voidsetCurrentToken(AbstractVersionIterator.TokenType tokenType, int startIndex, int endIndex, int extraBits)Set the current token.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.smallrye.common.version.VersionIterator
alphaPartEquals, alphaPartEquals, compareAlphaPart, compareNumberPart, getNumberPartAsBigInteger, numberPartEquals, numberPartEquals
-
-
-
-
Field Detail
-
string
protected final String string
-
-
Constructor Detail
-
AbstractVersionIterator
protected AbstractVersionIterator(String string)
Construct a new instance.- Parameters:
string- the version string (must not benull)
-
-
Method Detail
-
changeType
protected void changeType(AbstractVersionIterator.TokenType newType)
Change the current token type.- Parameters:
newType- the new type (must not benull)
-
insertEmptyAlpha
protected void insertEmptyAlpha()
Insert an empty part into the current position.
-
insertEmptySeparator
protected void insertEmptySeparator()
Insert an empty separator into the current position.
-
insertEmptyNumber
protected void insertEmptyNumber()
Insert an empty (zero) number into the current position.
-
setCurrentToken
protected void setCurrentToken(AbstractVersionIterator.TokenType tokenType, int startIndex, int endIndex, int extraBits)
Set the current token.- Parameters:
tokenType- the token type (must not benull)startIndex- the start indexendIndex- the end indexextraBits- extra implementation-specific state bits
-
mark
protected long mark()
Mark the current iteration position and state.- Returns:
- the current iteration cookie
-
reset
protected void reset(long mark)
Restore the iteration position to a previousmark()state.- Parameters:
mark- the mark to restore to
-
currentType
protected AbstractVersionIterator.TokenType currentType()
Get the current iteration token type.- Returns:
- the iteration state
-
getStartIndex
protected int getStartIndex()
Get the start index of the current token.- Returns:
- the index into the version string
-
getEndIndex
protected int getEndIndex()
Get the end index (exclusive) of the current token. The length- Returns:
- the index into the version string
-
getExtraBits
protected int getExtraBits()
Get the extra implementation-specific state bits.- Returns:
- the extra bits
-
compareAlphaPart
public int compareAlphaPart(String str, int offs, int len, boolean ignoreCase) throws IllegalStateException, StringIndexOutOfBoundsException
Description copied from interface:VersionIteratorCompare two alphabetical parts lexicographically. This iterator must be positioned at an alphabetical part or an exception is thrown.- Specified by:
compareAlphaPartin interfaceVersionIterator- Parameters:
str- the string to compare against (must not benull)offs- the offset into the string to comparelen- the length to compareignoreCase-trueto perform a case-insensitive comparison, orfalseto perform a case-sensitive comparison- Returns:
-1,0, or1if this segment is less than, equal to, or greater than the given string- Throws:
IllegalStateException- if this iterator is not positioned on an alphabetical partStringIndexOutOfBoundsException- if the given offset or length fall outside of the string
-
compareAlphaPart
public int compareAlphaPart(VersionIterator other, boolean ignoreCase) throws IllegalStateException
Description copied from interface:VersionIteratorCompare two alphabetical parts lexicographically. Both this and the other iterator must be positioned at alphabetical parts or an exception is thrown.- Specified by:
compareAlphaPartin interfaceVersionIterator- Parameters:
other- the other iterator (must not benull)ignoreCase- a boolean to ignore case in the compare- Returns:
-1,0, or1if this segment is less than, equal to, or greater than the other iterator's part- Throws:
IllegalStateException- if this or the other iterator are not positioned on an alphabetical part
-
length
public int length()
Description copied from interface:VersionIteratorGet the length of the current token. If there is no current token, zero is returned.- Specified by:
lengthin interfaceVersionIterator- Returns:
- the length of the current token
-
getNumberPartAsString
public String getNumberPartAsString() throws IllegalStateException
Description copied from interface:VersionIteratorGet the current numeric part, as aString. If the iterator is not positioned on a numeric part (i.e.VersionIterator.isNumberPart()returnsfalse), then an exception is thrown. Any redundant leading zeros are removed.- Specified by:
getNumberPartAsStringin interfaceVersionIterator- Returns:
- the current numeric part as a
String - Throws:
IllegalStateException- if the current token is not a numeric part
-
appendNumberPartTo
public StringBuilder appendNumberPartTo(StringBuilder target) throws IllegalStateException
Description copied from interface:VersionIteratorGet the current numeric part, appending it to the given builder. If the iterator is not positioned on a numeric part (i.e.VersionIterator.isNumberPart()returnsfalse), then an exception is thrown. Any redundant leading zeros are removed.- Specified by:
appendNumberPartToin interfaceVersionIterator- Parameters:
target- the StringBuilder to append- Returns:
- the current numeric part as a
String - Throws:
IllegalStateException- if the current token is not a numeric part
-
getNumberPartAsInt
public int getNumberPartAsInt()
Description copied from interface:VersionIteratorGet the current numeric part, as anint. If the iterator is not positioned on a numeric part (i.e.VersionIterator.isNumberPart()returnsfalse), then an exception is thrown. If the value overflows the maximum value for anint, then only the low-order 32 bits of the version number value are returned.- Specified by:
getNumberPartAsIntin interfaceVersionIterator- Returns:
- the current numeric part as an
int
-
getNumberPartAsLong
public long getNumberPartAsLong()
Description copied from interface:VersionIteratorGet the current numeric part, as along. If the iterator is not positioned on a numeric part (i.e.VersionIterator.isNumberPart()returnsfalse), then an exception is thrown. If the value overflows the maximum value for along, then only the low-order 64 bits of the version number value are returned.- Specified by:
getNumberPartAsLongin interfaceVersionIterator- Returns:
- the current numeric part as a
long
-
compareNumberPart
public int compareNumberPart(int value) throws IllegalStateExceptionDescription copied from interface:VersionIteratorCompare two numerical parts (using an unsigned comparison). This iterator must be positioned at a numerical part or an exception is thrown.- Specified by:
compareNumberPartin interfaceVersionIterator- Parameters:
value- the number to compare against- Returns:
-1,0, or1if this segment is less than, equal to, or greater than the given value- Throws:
IllegalStateException- if this iterator is not positioned on a numeric part
-
compareNumberPart
public int compareNumberPart(long value) throws IllegalStateExceptionDescription copied from interface:VersionIteratorCompare two numerical parts (using an unsigned comparison). This iterator must be positioned at a numerical part or an exception is thrown.- Specified by:
compareNumberPartin interfaceVersionIterator- Parameters:
value- the number to compare against- Returns:
-1,0, or1if this segment is less than, equal to, or greater than the given value- Throws:
IllegalStateException- if this iterator is not positioned on a numeric part
-
compareNumberPart
public int compareNumberPart(String value, int offs, int len) throws IllegalStateException
Description copied from interface:VersionIteratorCompare two numerical parts (using an unsigned comparison). This iterator must be positioned at a numerical part or an exception is thrown. The given string must be numeric according to the rules of this iterator or an exception is thrown.- Specified by:
compareNumberPartin interfaceVersionIterator- Parameters:
value- the number to compare againstoffs- the offset into the string to comparelen- the length to compare- Returns:
-1,0, or1if this segment is less than, equal to, or greater than the given value- Throws:
IllegalStateException- if this iterator is not positioned on a numeric part
-
compareNumberPart
public int compareNumberPart(VersionIterator other)
Description copied from interface:VersionIteratorCompare two numerical parts (using an unsigned comparison). Both iterators must be positioned at a numerical part or an exception is thrown.- Specified by:
compareNumberPartin interfaceVersionIterator- Parameters:
other- the other iterator (must not benull)- Returns:
-1,0, or1if this segment is less than, equal to, or greater than the other iterator's part
-
isPart
public boolean isPart()
Description copied from interface:VersionIteratorDetermine if the current token is some kind of part (alphabetical or numeric).- Specified by:
isPartin interfaceVersionIterator- Returns:
trueif the token is a part,falseotherwise
-
isSeparator
public boolean isSeparator()
Description copied from interface:VersionIteratorDetermine if the current token is some kind of separator (a character or a zero-length alphabetical-to-numeric or numeric-to-alphabetical transition).- Specified by:
isSeparatorin interfaceVersionIterator- Returns:
trueif the token is a separator,falseotherwise
-
isEmptySeparator
public boolean isEmptySeparator()
Description copied from interface:VersionIteratorDetermine if the current token is an empty (or zero-length alphabetical-to-numeric or numeric-to-alphabetical) separator. Note that some version schemes do not have empty separators.- Specified by:
isEmptySeparatorin interfaceVersionIterator- Returns:
trueif the token is an empty separator,falseotherwise
-
isNonEmptySeparator
public boolean isNonEmptySeparator()
Description copied from interface:VersionIteratorDetermine if the current token is a non-empty separator.- Specified by:
isNonEmptySeparatorin interfaceVersionIterator- Returns:
trueif the token is a non-empty separator,falseotherwise
-
getSeparatorCodePoint
public int getSeparatorCodePoint()
Description copied from interface:VersionIteratorGet the code point of the current separator. If the iterator is not positioned on a non-empty separator (i.e.VersionIterator.isNonEmptySeparator()returnsfalse), then an exception is thrown.- Specified by:
getSeparatorCodePointin interfaceVersionIterator- Returns:
- the code point of the current separator
-
isAlphaPart
public boolean isAlphaPart()
Description copied from interface:VersionIteratorDetermine if the current token is an alphabetical part.- Specified by:
isAlphaPartin interfaceVersionIterator- Returns:
trueif the token is an alphabetical part,falseotherwise
-
isNumberPart
public boolean isNumberPart()
Description copied from interface:VersionIteratorDetermine if the current token is a numeric part.- Specified by:
isNumberPartin interfaceVersionIterator- Returns:
trueif the token is a numeric part,falseotherwise
-
getAlphaPart
public String getAlphaPart() throws IllegalStateException
Description copied from interface:VersionIteratorGet the current alphabetical part. If the iterator is not positioned on an alphabetical part (i.e.VersionIterator.isAlphaPart()returnsfalse), then an exception is thrown.- Specified by:
getAlphaPartin interfaceVersionIterator- Returns:
- the current alphabetical part
- Throws:
IllegalStateException- if the current token is not an alphabetical part
-
appendAlphaPartTo
public StringBuilder appendAlphaPartTo(StringBuilder target) throws IllegalStateException
Description copied from interface:VersionIteratorAppend the current alphabetical part to the given string builder. If the iterator is not positioned on an alphabetical part (i.e.VersionIterator.isAlphaPart()returnsfalse), then an exception is thrown.- Specified by:
appendAlphaPartToin interfaceVersionIterator- Parameters:
target- the StringBuilder to append- Returns:
- the current alphabetical part
- Throws:
IllegalStateException- if the current token is not an alphabetical part
-
appendPartTo
public StringBuilder appendPartTo(StringBuilder target)
Description copied from interface:VersionIteratorAppend this version part to the given string builder. This is used to produce a canonical representation of an input string.- Specified by:
appendPartToin interfaceVersionIterator- Parameters:
target- the string builder (must not benull)- Returns:
- the same string builder (not
null)
-
hasNext
public boolean hasNext()
A basic implementation ofVersionIterator.hasNext()which returnstrueif there are more characters after the current token. Subclasses should generally override this basic behavior but may delegate to this implementation for convenience.- Specified by:
hasNextin interfaceVersionIterator- Returns:
trueif there are more characters after the current token,falseotherwise
-
isSeparatorCodePoint
protected boolean isSeparatorCodePoint(int cp)
Determine whether the given code point is a valid separator according to the current scheme. This basic implementation returnstruefor'.','-','+', and'_'. Subclasses may override this method to recognize a different set of separators.- Parameters:
cp- the code point to test- Returns:
trueif the code point is a separator, orfalseotherwise
-
numericCodePointValue
protected int numericCodePointValue(int cp)
Determine whether the given code point is a valid numeric code point according to the current scheme, and if so, what the digit's numerical value is. This basic implementation delegates toCharacter.digit(int, int)with aradixvalue of 10. Subclasses may override this method to recognize a different set of numeric code points.- Parameters:
cp- the code point to test- Returns:
- the numerical value of the given code point, or
-1if the code point is not a valid numerical digit for this version scheme
-
isAlphaCodePoint
protected boolean isAlphaCodePoint(int cp)
Determine whether the given code point is a valid alpha code point according to the current scheme. This basic implementation returnstruefor any character whose general category is in the set recognized byCharacter.isLetter(int). Subclasses may override this method to recognize a different set of alpha code points.- Parameters:
cp- the code point to test- Returns:
trueif the code point is a valid letter, orfalseotherwise
-
next
public void next() throws NoSuchElementException, VersionSyntaxExceptionA basic implementation ofVersionIterator.next()which uses the methods on this class to establish the next iteration token. This implementation consumes leading zeros from numeric parts but otherwise does not perform any transformations.- Specified by:
nextin interfaceVersionIterator- Throws:
NoSuchElementException- if there are no tokens left to iterateVersionSyntaxException- if the version string is unparseable due to syntax error
-
-