public abstract class StringConvert<T>
extends java.lang.Object
T
, to and from a serialized String format.
This abstract class should usually be made concrete by a single-purpose class (not the type T itself).
Also includes a static registry of types as CharSequence arrays (including the classes of generic type parameters in
array elements after the first) to the corresponding StringConvert objects that have been constructed for those
types, although the registry must store the StringConvert objects without any further types (you can cast the
StringConvert to a StringConvert with the desired generic type, or call restore(String)
on the
un-parametrized type and get back an Object that can be cast to the correct type, but we aren't able to store the
actual type). The static method lookup(CharSequence[])
can be used to find the StringConvert registered for
a type name combination. The static method get(CharSequence)
can be used to find a StringConvert by its
name. The static utility method asArray(CharSequence[])
can be used to reduce the amount of arrays produced
by varargs, especially when you have a bunch of Class items and need Strings, but the array it returns must not be
edited once used to construct a StringConvert.Modifier and Type | Field and Description |
---|---|
boolean |
isArray |
java.lang.CharSequence |
name |
static K2<StringConvert,java.lang.CharSequence[]> |
registry |
static CrossHash.IHasher |
spaceIgnoringArrayHasher |
static CrossHash.IHasher |
spaceIgnoringHasher |
java.lang.String |
specificName |
java.lang.CharSequence[] |
typeNames |
Constructor and Description |
---|
StringConvert(boolean isArray,
java.lang.CharSequence... types)
Constructs a StringConvert using a vararg or array of CharSequence objects, such as Strings, as well as a boolean
flag to determine if the StringConvert works on an array instead of a normal object.
|
StringConvert(java.lang.CharSequence... types)
Constructs a StringConvert using a vararg or array of CharSequence objects, such as Strings.
|
Modifier and Type | Method and Description |
---|---|
static java.lang.CharSequence[] |
asArray(java.lang.CharSequence... types)
Simply takes a vararg of Class and returns the simple names of the Classes as a String array.
|
static StringConvert<?> |
get(java.lang.CharSequence name)
Gets the registered StringConvert for the given type name, if there is one, or returns null otherwise.
|
java.lang.CharSequence |
getName() |
static StringConvert<?> |
lookup(java.lang.CharSequence... types)
Looks up the StringConvert for a given vararg of Class instances (if an array of Classes is used other than a
vararg, it must not be altered in the future, nor reused in a way that modifies its elements).
|
abstract T |
restore(java.lang.String text) |
<T2> T2 |
restore(java.lang.String text,
java.lang.Class<T2> type)
Attempts to restore a specific type of value from the given text.
|
abstract java.lang.String |
stringify(T item) |
public final java.lang.CharSequence name
public final java.lang.CharSequence[] typeNames
public final java.lang.String specificName
public final boolean isArray
public static final CrossHash.IHasher spaceIgnoringHasher
public static final CrossHash.IHasher spaceIgnoringArrayHasher
public static final K2<StringConvert,java.lang.CharSequence[]> registry
public StringConvert(boolean isArray, java.lang.CharSequence... types)
Class.getSimpleName()
(note that this produces an empty string for anonymous classes), and
typeNames will again have that simple name as its only value. Otherwise, this considers items after the first to
be the names of generic type arguments of the first, using normal Java syntax of "Outer<A,B>"
if given
the Strings for types "Outer", "A", "B"
. No spaces will be present in the name, but thanks to some
customization of the registry, you can give a String with spaces in it to get(CharSequence)
and still
find the correct one). You can give type names with generic components as the names of generic type arguments,
such as new StringConvert("OrderedMap", "String", "OrderedSet<String>")
for a mapping of String keys to
values that are themselves sets of Strings. After constructing a StringConvert, it is automatically registered
so it can be looked up by name with get(CharSequence)
or by component generic types with
lookup(CharSequence...)
; both of these will not return a StringConvert with type info for what it
takes and returns beyond "Object", but the result can be cast to a StringConvert with the correct type.isArray
- true if this should convert an array type as opposed to a normal object or primitive typetypes
- a vararg of CharSequence objects representing the type this can convert, including generic type
parameters of the first element, if there are any, at positions after the firstpublic StringConvert(java.lang.CharSequence... types)
Class.getSimpleName()
(note that this produces an empty string for anonymous classes), and
typeNames will again have that simple name as its only value. Otherwise, this considers items after the first to
be the names of generic type arguments of the first, using normal Java syntax of "Outer<A,B>"
if given
the Strings for types "Outer", "A", "B"
. No spaces will be present in the name, but thanks to some
customization of the registry, you can give a String with spaces in it to get(CharSequence)
and still
find the correct one). You can give type names with generic components as the names of generic type arguments,
such as new StringConvert("OrderedMap", "String", "OrderedSet<String>")
for a mapping of String keys to
values that are themselves sets of Strings. After constructing a StringConvert, it is automatically registered
so it can be looked up by name with get(CharSequence)
or by component generic types with
lookup(CharSequence...)
; both of these will not return a StringConvert with type info for what it
takes and returns beyond "Object", but the result can be cast to a StringConvert with the correct type.types
- a vararg of CharSequence objects representing the type this can convert, including generic type
parameters of the first element, if there are any, at positions after the firstpublic java.lang.CharSequence getName()
public abstract java.lang.String stringify(T item)
public abstract T restore(java.lang.String text)
public <T2> T2 restore(java.lang.String text, java.lang.Class<T2> type)
StringConvert<?>
), and you know the correct type externally.
May throw a ClassCastException if type is not compatible with the type this deserializes to (that is, T).T2
- you must be able to cast from a T (the type described by this class' specificName
) to a T2text
- the text to try to read as serialized data describing a T2 objecttype
- the Class of the data to try to produce, which should be as specific as possiblepublic static StringConvert<?> get(java.lang.CharSequence name)
Converters.convertOrderedMap(Converters.convertString, Converters.convertArrayList(Converters.convertString))
to produce and register a StringConvert for the aforementioned generic type.name
- the name of the type to find a registered StringConvert, such as "ArrayList<String>" or "char[]"public static StringConvert<?> lookup(java.lang.CharSequence... types)
types
- the vararg of types to look uppublic static java.lang.CharSequence[] asArray(java.lang.CharSequence... types)
types
- a vararg of ClassCopyright © Eben Howard 2012–2022. All rights reserved.