T
- The type that is parsed.@PublicEvolving public abstract class FieldParser<T> extends Object
The parsers do not throw exceptions in general, but set an error state. That way, they can be used in functions that ignore invalid lines, rather than failing on them.
Modifier and Type | Class and Description |
---|---|
static class |
FieldParser.ParseErrorState
An enumeration of different types of errors that may occur.
|
Constructor and Description |
---|
FieldParser() |
Modifier and Type | Method and Description |
---|---|
abstract T |
createValue()
Returns an instance of the parsed value type.
|
static boolean |
delimiterNext(byte[] bytes,
int startPos,
byte[] delim)
Checks if the delimiter starts at the given start position of the byte array.
|
static boolean |
endsWithDelimiter(byte[] bytes,
int endPos,
byte[] delim)
Checks if the given bytes ends with the delimiter at the given end position.
|
Charset |
getCharset()
Gets the character set used for this parser.
|
FieldParser.ParseErrorState |
getErrorState()
Gets the error state of the parser, as a value of the enumeration
FieldParser.ParseErrorState . |
abstract T |
getLastResult()
Gets the parsed field.
|
static <T> Class<FieldParser<T>> |
getParserForType(Class<T> type)
Gets the parser for the type specified by the given class.
|
protected int |
nextStringEndPos(byte[] bytes,
int startPos,
int limit,
byte[] delimiter)
Returns the end position of a string.
|
protected static int |
nextStringLength(byte[] bytes,
int startPos,
int length,
char delimiter)
Returns the length of a string.
|
protected abstract int |
parseField(byte[] bytes,
int startPos,
int limit,
byte[] delim,
T reuse)
Each parser's logic should be implemented inside this method
|
int |
resetErrorStateAndParse(byte[] bytes,
int startPos,
int limit,
byte[] delim,
T reuse)
Parses the value of a field from the byte array, taking care of properly reset the state of
this parser.
|
protected void |
resetParserState()
Reset the state of the parser.
|
void |
setCharset(Charset charset)
Sets the character set used for this parser.
|
protected void |
setErrorState(FieldParser.ParseErrorState error)
Sets the error state of the parser.
|
public int resetErrorStateAndParse(byte[] bytes, int startPos, int limit, byte[] delim, T reuse)
bytes
- The byte array that holds the value.startPos
- The index where the field startslimit
- The limit unto which the byte contents is valid for the parser. The limit is the
position one after the last valid byte.delim
- The field delimiter characterreuse
- An optional reusable field to hold the valueprotected abstract int parseField(byte[] bytes, int startPos, int limit, byte[] delim, T reuse)
protected void resetParserState()
resetErrorStateAndParse(byte[], int, int, byte[], Object)
, by default it just
reset its error state.public abstract T getLastResult()
parseField(byte[], int, int, byte[], Object)
. It objects are mutable and reused,
it will return the object instance that was passed the parse function.public abstract T createValue()
public static final boolean delimiterNext(byte[] bytes, int startPos, byte[] delim)
Attention: This method assumes that enough characters follow the start position for the delimiter check!
bytes
- The byte array that holds the value.startPos
- The index of the byte array where the check for the delimiter starts.delim
- The delimiter to check for.public static final boolean endsWithDelimiter(byte[] bytes, int endPos, byte[] delim)
bytes
- The byte array that holds the value.endPos
- The index of the byte array where the check for the delimiter ends.delim
- The delimiter to check for.protected void setErrorState(FieldParser.ParseErrorState error)
error
- The error state to set.public FieldParser.ParseErrorState getErrorState()
FieldParser.ParseErrorState
. If
no error occurred, the error state will be FieldParser.ParseErrorState.NONE
.protected final int nextStringEndPos(byte[] bytes, int startPos, int limit, byte[] delimiter)
protected static final int nextStringLength(byte[] bytes, int startPos, int length, char delimiter)
public Charset getCharset()
public void setCharset(Charset charset)
charset
- charset used for this parser.public static <T> Class<FieldParser<T>> getParserForType(Class<T> type)
type
- The class of the type to get the parser for.Copyright © 2014–2024 The Apache Software Foundation. All rights reserved.