Package com.datastax.driver.core
Class ParseUtils
- java.lang.Object
-
- com.datastax.driver.core.ParseUtils
-
public abstract class ParseUtils extends Object
Simple utility class used to help parsing CQL values (mainly UDT and collection ones).
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringdoubleQuote(String value)Double quote the given string; double quotes are escaped.static StringformatTime(long value)Format the given long value as a CQL time literal, using the following time pattern:hh:mm:ss[.fffffffff].static booleanisBlank(int c)Returntrueif the given character is a valid whitespace character in CQL, that is, if it is a regular space, a tabulation sign, or a new line sign.static booleanisDoubleQuoted(String value)Returntrueif the given string is surrounded by double quotes, andfalseotherwise.static booleanisIdentifierChar(int c)Returntrueif the given character is allowed in a CQL identifier, that is, if it is in the range:[0..9a..zA..Z-+._&].static booleanisLongLiteral(String str)Check whether the given string corresponds to a valid CQL long literal.static booleanisQuoted(String value)Returntrueif the given string is surrounded by single quotes, andfalseotherwise.static DateparseDate(String str)Parse the given string as a date, using one of the accepted ISO-8601 date patterns.static DateparseDate(String str, String pattern)Parse the given string as a date, using the supplied date pattern.static longparseTime(String str)Parse the given string as a time, using the following time pattern:hh:mm:ss[.fffffffff].static Stringquote(String value)Quote the given string; single quotes are escaped.static intskipCQLId(String toParse, int idx)Assuming that idx points to the beginning of a CQL identifier in toParse, returns the index of the first character after this identifier.static intskipCQLValue(String toParse, int idx)Assuming that idx points to the beginning of a CQL value in toParse, returns the index of the first character after this value.static intskipSpaces(String toParse, int idx)Returns the index of the first character in toParse from idx that is not a "space".static StringunDoubleQuote(String value)Unquote the given string if it is double quoted; double quotes are unescaped.static Stringunquote(String value)Unquote the given string if it is quoted; single quotes are unescaped.
-
-
-
Method Detail
-
skipSpaces
public static int skipSpaces(String toParse, int idx)
Returns the index of the first character in toParse from idx that is not a "space".- Parameters:
toParse- the string to skip space on.idx- the index to start skipping space from.- Returns:
- the index of the first character in toParse from idx that is not a "space.
-
skipCQLValue
public static int skipCQLValue(String toParse, int idx)
Assuming that idx points to the beginning of a CQL value in toParse, returns the index of the first character after this value.- Parameters:
toParse- the string to skip a value form.idx- the index to start parsing a value from.- Returns:
- the index ending the CQL value starting at
idx. - Throws:
IllegalArgumentException- if idx doesn't point to the start of a valid CQL value.
-
skipCQLId
public static int skipCQLId(String toParse, int idx)
Assuming that idx points to the beginning of a CQL identifier in toParse, returns the index of the first character after this identifier.- Parameters:
toParse- the string to skip an identifier from.idx- the index to start parsing an identifier from.- Returns:
- the index ending the CQL identifier starting at
idx. - Throws:
IllegalArgumentException- if idx doesn't point to the start of a valid CQL identifier.
-
isIdentifierChar
public static boolean isIdentifierChar(int c)
Returntrueif the given character is allowed in a CQL identifier, that is, if it is in the range:[0..9a..zA..Z-+._&].- Parameters:
c- The character to inspect.- Returns:
trueif the given character is allowed in a CQL identifier,falseotherwise.
-
isBlank
public static boolean isBlank(int c)
Returntrueif the given character is a valid whitespace character in CQL, that is, if it is a regular space, a tabulation sign, or a new line sign.- Parameters:
c- The character to inspect.- Returns:
trueif the given character is a valid whitespace character,falseotherwise.
-
isLongLiteral
public static boolean isLongLiteral(String str)
Check whether the given string corresponds to a valid CQL long literal. Long literals are composed solely by digits, but can have an optional leading minus sign.- Parameters:
str- The string to inspect.- Returns:
trueif the given string corresponds to a valid CQL integer literal,falseotherwise.
-
isQuoted
public static boolean isQuoted(String value)
Returntrueif the given string is surrounded by single quotes, andfalseotherwise.- Parameters:
value- The string to inspect.- Returns:
trueif the given string is surrounded by single quotes, andfalseotherwise.
-
quote
public static String quote(String value)
Quote the given string; single quotes are escaped. If the given string is null, this method returns a quoted empty string ('').- Parameters:
value- The value to quote.- Returns:
- The quoted string.
-
unquote
public static String unquote(String value)
Unquote the given string if it is quoted; single quotes are unescaped. If the given string is not quoted, it is returned without any modification.- Parameters:
value- The string to unquote.- Returns:
- The unquoted string.
-
isDoubleQuoted
public static boolean isDoubleQuoted(String value)
Returntrueif the given string is surrounded by double quotes, andfalseotherwise.- Parameters:
value- The string to inspect.- Returns:
trueif the given string is surrounded by double quotes, andfalseotherwise.
-
doubleQuote
public static String doubleQuote(String value)
Double quote the given string; double quotes are escaped. If the given string is null, this method returns a quoted empty string ("").- Parameters:
value- The value to double quote.- Returns:
- The double quoted string.
-
unDoubleQuote
public static String unDoubleQuote(String value)
Unquote the given string if it is double quoted; double quotes are unescaped. If the given string is not double quoted, it is returned without any modification.- Parameters:
value- The string to un-double quote.- Returns:
- The un-double quoted string.
-
parseDate
public static Date parseDate(String str) throws ParseException
Parse the given string as a date, using one of the accepted ISO-8601 date patterns.This method is adapted from Apache Commons
DateUtils.parseStrictly()method (that is used Cassandra side to parse date strings)..- Throws:
ParseException- If the given string is not a valid ISO-8601 date.- See Also:
- 'Working with timestamps' section of CQL specification
-
parseDate
public static Date parseDate(String str, String pattern) throws ParseException
Parse the given string as a date, using the supplied date pattern.This method is adapted from Apache Commons
DateUtils.parseStrictly()method (that is used Cassandra side to parse date strings)..- Throws:
ParseException- If the given string cannot be parsed with the given pattern.- See Also:
- 'Working with timestamps' section of CQL specification
-
parseTime
public static long parseTime(String str) throws ParseException
Parse the given string as a time, using the following time pattern:hh:mm:ss[.fffffffff].This method is loosely based on
java.sql.Timestamp.- Parameters:
str- The string to parse.- Returns:
- A long value representing the number of nanoseconds since midnight.
- Throws:
ParseException- if the string cannot be parsed.- See Also:
- 'Working with time' section of CQL specification
-
formatTime
public static String formatTime(long value)
Format the given long value as a CQL time literal, using the following time pattern:hh:mm:ss[.fffffffff].- Parameters:
value- A long value representing the number of nanoseconds since midnight.- Returns:
- The formatted value.
- See Also:
- 'Working with time' section of CQL specification
-
-