Package org.basex.util
Class TokenBuilder
- java.lang.Object
-
- org.basex.util.TokenBuilder
-
public final class TokenBuilder extends Object
This class serves as an efficient constructor forTokens
. It bears some resemblance to Java'sStringBuilder
.- Author:
- BaseX Team 2005-23, BSD License, Christian Gruen
-
-
Field Summary
Fields Modifier and Type Field Description static int
BOLD
Bold flag.static int
HLINE
Half new line.static int
MARK
Mark flag.static byte
NLINE
New line.static int
NORM
Standard flag.static int
PRIVATE_END
Unicode, private area (end).static int
PRIVATE_START
Unicode, private area (start).static int
ULINE
Underline flag.
-
Constructor Summary
Constructors Constructor Description TokenBuilder()
Empty constructor.TokenBuilder(byte[] token)
Constructor with initial token.TokenBuilder(long capacity)
Constructor with initial array capacity.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TokenBuilder
add(byte[] token)
Adds a token to the token.TokenBuilder
add(byte[] token, int start, int end)
Adds a subtoken.TokenBuilder
add(int cp)
Adds the specified character.TokenBuilder
add(Object object)
Adds an object to the token.TokenBuilder
add(String string)
Adds a string to the token.TokenBuilder
addAll(Object[] objects, String separator)
Adds multiple strings to the token, separated by the specified string.TokenBuilder
addByte(byte value)
Adds a single byte to the token.TokenBuilder
addExt(Object object, Object... extensions)
Adds the string representation of an object.TokenBuilder
addInt(int value)
Adds an integer value to the token.TokenBuilder
addLong(long value)
Adds a number to the token.TokenBuilder
bold()
Adds a bold flag.int
cl(int pos)
Returns the length of the codepoints stored at the specified position.int
cp(int pos)
Returns the codepoint stored at the specified position.TokenBuilder
delete(int start, int end)
Deletes bytes from the token.byte[]
finish()
Returns the token as byte array, and invalidates the internal array.byte
get(int pos)
Returns the byte stored at the specified position.TokenBuilder
hline()
Adds a half new line.boolean
isEmpty()
Tests if the token is empty.byte[]
next()
Returns the token as byte array and resets the token buffer.TokenBuilder
nline()
Adds a new line.TokenBuilder
norm()
Adds a norm flag.TokenBuilder
normalize()
Normalizes newlines.TokenBuilder
reset()
Resets the token buffer.void
set(int pos, byte value)
Sets a byte at the specified position.int
size()
Returns the number of bytes.void
size(int sz)
Sets the number of bytes.byte[]
toArray()
Returns the token as byte array.String
toString()
TokenBuilder
trim()
Trims leading and trailing whitespaces.TokenBuilder
uline()
Adds an underline toggle flag.
-
-
-
Field Detail
-
NLINE
public static final byte NLINE
New line.- See Also:
- Constant Field Values
-
PRIVATE_START
public static final int PRIVATE_START
Unicode, private area (start).- See Also:
- Constant Field Values
-
PRIVATE_END
public static final int PRIVATE_END
Unicode, private area (end).- See Also:
- Constant Field Values
-
HLINE
public static final int HLINE
Half new line.- See Also:
- Constant Field Values
-
BOLD
public static final int BOLD
Bold flag.- See Also:
- Constant Field Values
-
NORM
public static final int NORM
Standard flag.- See Also:
- Constant Field Values
-
MARK
public static final int MARK
Mark flag.- See Also:
- Constant Field Values
-
ULINE
public static final int ULINE
Underline flag.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
TokenBuilder
public TokenBuilder()
Empty constructor.
-
TokenBuilder
public TokenBuilder(long capacity)
Constructor with initial array capacity.- Parameters:
capacity
- array capacity
-
TokenBuilder
public TokenBuilder(byte[] token)
Constructor with initial token.- Parameters:
token
- initial token
-
-
Method Detail
-
size
public int size()
Returns the number of bytes.- Returns:
- number of bytes
-
size
public void size(int sz)
Sets the number of bytes. Note that no bound check is performed by this method.- Parameters:
sz
- number of bytes
-
isEmpty
public boolean isEmpty()
Tests if the token is empty.- Returns:
- result of check
-
reset
public TokenBuilder reset()
Resets the token buffer.- Returns:
- self reference
-
bold
public TokenBuilder bold()
Adds a bold flag. This method should only be called to control text rendering in the visual front end.- Returns:
- self reference
-
uline
public TokenBuilder uline()
Adds an underline toggle flag. This method should only be called to control text rendering in the visual front end.- Returns:
- self reference
-
norm
public TokenBuilder norm()
Adds a norm flag. This method should only be called to control text rendering in the visual front end.- Returns:
- self reference
-
hline
public TokenBuilder hline()
Adds a half new line. This method should only be called to control text rendering in the visual front end.- Returns:
- self reference
-
nline
public TokenBuilder nline()
Adds a new line. This method should only be called to control text rendering in the visual front end.- Returns:
- self reference
-
add
public TokenBuilder add(int cp)
Adds the specified character. CalladdInt(int)
to add the string value of an integer.- Parameters:
cp
- codepoint of the character- Returns:
- self reference
-
cp
public int cp(int pos)
Returns the codepoint stored at the specified position.- Parameters:
pos
- position- Returns:
- character
-
cl
public int cl(int pos)
Returns the length of the codepoints stored at the specified position.- Parameters:
pos
- position- Returns:
- character
-
get
public byte get(int pos)
Returns the byte stored at the specified position.- Parameters:
pos
- position- Returns:
- byte
-
set
public void set(int pos, byte value)
Sets a byte at the specified position.- Parameters:
value
- byte to be setpos
- position
-
delete
public TokenBuilder delete(int start, int end)
Deletes bytes from the token.- Parameters:
start
- start positionend
- end position- Returns:
- self reference
-
addByte
public TokenBuilder addByte(byte value)
Adds a single byte to the token.- Parameters:
value
- the byte to be added- Returns:
- self reference
-
addInt
public TokenBuilder addInt(int value)
Adds an integer value to the token.- Parameters:
value
- value to be added- Returns:
- self reference
-
addLong
public TokenBuilder addLong(long value)
Adds a number to the token.- Parameters:
value
- value to be added- Returns:
- self reference
-
add
public TokenBuilder add(byte[] token)
Adds a token to the token.- Parameters:
token
- the token to be added- Returns:
- self reference
-
add
public TokenBuilder add(byte[] token, int start, int end)
Adds a subtoken.- Parameters:
token
- the tokenstart
- start positionend
- end position- Returns:
- self reference
-
add
public TokenBuilder add(String string)
Adds a string to the token.- Parameters:
string
- the string to be added- Returns:
- self reference
-
addAll
public TokenBuilder addAll(Object[] objects, String separator)
Adds multiple strings to the token, separated by the specified string.- Parameters:
objects
- the object to be addedseparator
- separator string- Returns:
- self reference
-
add
public TokenBuilder add(Object object)
Adds an object to the token.- Parameters:
object
- the object to be added- Returns:
- self reference
-
addExt
public TokenBuilder addExt(Object object, Object... extensions)
Adds the string representation of an object. The specified string may contain%
characters as placeholders. All placeholders will be replaced by the specified extensions. If a digit is specified after the placeholder character, it will be interpreted as insertion position.- Parameters:
object
- object to be extendedextensions
- optional extension strings- Returns:
- self reference
-
trim
public TokenBuilder trim()
Trims leading and trailing whitespaces.- Returns:
- self reference
-
normalize
public TokenBuilder normalize()
Normalizes newlines.- Returns:
- self reference
-
toArray
public byte[] toArray()
Returns the token as byte array.- Returns:
- token
-
next
public byte[] next()
Returns the token as byte array and resets the token buffer. The call of this function is identical to callingtoArray()
andreset()
.- Returns:
- token
-
finish
public byte[] finish()
Returns the token as byte array, and invalidates the internal array. Warning: the function must only be called if the builder is discarded afterwards.- Returns:
- token
-
-