public final class CodecUtils extends Object
Although it would be preferable to hide the implementation details of how string arrays (and lists) are encoded, we have determined cases where it is important for the consumer to be able to make assumptions about the delimiters used. Therefore, a brief description of the algorithm is given here:
The method encodeStringArray
(and its counterpart,
encodeStringList
which simply delegates to this method),
examines the strings in the array and attempts to find a delimiter which does not
occur in the data. If one can be found, that one is used, avoiding the need to
escape occurrences of the delimiter in the data.
If all considered delimiters are found in the data, then a deeper examination
is made and the one that occurs the least number of times is used, and
escaped as appropriate.
If the caller knows that the string data does not contain the
default delimiter
, then the
assumption can be made that this will be the delimiter used.
Modifier and Type | Method and Description |
---|---|
static Object |
decodePrimitive(String encoded)
This method accepts an encoded string (previously generated from
encodePrimitive ) and returns the original
value (as the original type). |
static String[] |
decodeStringArray(String encoded)
This method takes a string generated by
encodeStringArray
and regenerates the contents of the original array. |
static List<String> |
decodeStringList(String encoded)
This method takes a string generated by
encodeStringList
and regenerates the contents of the original list. |
static String |
encodePrimitive(Object p)
This method accepts a primitive and produces a string encoding of it.
|
static String |
encodeStringArray(String[] strings)
This method takes an array of strings and returns a single string that encodes
that string array.
|
static String |
encodeStringList(List<String> strings)
This method takes a list of strings and returns a single string that encodes
that list.
|
static String |
getDefaultDelimiter()
Returns the default delimiter used when encoding string arrays and lists.
|
static String |
getMagic()
Returns the magic value used to tag encoded string arrays and lists.
|
public static String getDefaultDelimiter()
public static String encodeStringList(List<String> strings)
encodeStringArray
.
Note that any null
elements in the list will be silently converted to
empty strings.
strings
- a list of stringsNullPointerException
- if the parameter is nullpublic static String encodeStringArray(String[] strings)
decodeStringArray
.
Note that any null
elements in the array will be silently converted to
empty strings.
strings
- an array of stringsNullPointerException
- if the parameter is nullpublic static List<String> decodeStringList(String encoded)
encodeStringList
and regenerates the contents of the original list. This method
calls decodeStringArray
then converts that array
back to a list before returning it.encoded
- the encoded stringNullPointerException
- if the parameter is nullIllegalArgumentException
- if the format of the string is not recognizedpublic static String[] decodeStringArray(String encoded)
encodeStringArray
and regenerates the contents of the original array.encoded
- the encoded stringNullPointerException
- if the parameter is nullIllegalArgumentException
- if the format of the string is not recognizedpublic static String getMagic()
String[] myStrings = ... String encArray = CodecUtils.encodeStringArray(myStrings); assert encArray.startsWith(CodecUtils.getMagic());
public static String encodePrimitive(Object p)
decodePrimitive
to restore
the original value (as the original type).
As a convenience, a String
is also considered a "primitive" and
can be "encoded" by this method.
p
- the primitive valueNullPointerException
- if the parameter is nullIllegalArgumentException
- if the parameter is not a
String, Boolean, Character, Byte, Short, Integer, Long, Float, or Doublepublic static Object decodePrimitive(String encoded)
encodePrimitive
) and returns the original
value (as the original type).encoded
- the encoded stringNullPointerException
- if the parameter is nullIllegalArgumentException
- if the encoding is not recognizedCopyright © 2015. All Rights Reserved.