Class StringConverter<T>
java.lang.Object
org.apache.sis.internal.converter.ClassPair<S,T>
org.apache.sis.internal.converter.SystemConverter<String,T>
org.apache.sis.internal.converter.StringConverter<T>
- All Implemented Interfaces:
Serializable
,Function<String,
,T> ObjectConverter<String,
T>
- Direct Known Subclasses:
StringConverter.Angle
,StringConverter.BigDecimal
,StringConverter.BigInteger
,StringConverter.Boolean
,StringConverter.Byte
,StringConverter.Charset
,StringConverter.CodeList
,StringConverter.Double
,StringConverter.Enum
,StringConverter.File
,StringConverter.Float
,StringConverter.Integer
,StringConverter.InternationalString
,StringConverter.Locale
,StringConverter.Long
,StringConverter.Number
,StringConverter.Path
,StringConverter.Short
,StringConverter.Unit
,StringConverter.URI
,StringConverter.URL
Handles conversions between
String
and various kinds of objects.
Each inner class in this StringConverter
class defines both the
forward and the inverse converters.
Most converters are pretty close to bijective functions, but not exactly.
For example, conversions from String
to File
is not
completely bijective because various path separators ('/'
and '\'
)
produce the same File
object.
Special cases
Conversion table fromString
to Boolean
:
source | target |
---|---|
"true" | Boolean.TRUE |
"false" | Boolean.FALSE |
"yes" | Boolean.TRUE |
"no" | Boolean.FALSE |
"on" | Boolean.TRUE |
"off" | Boolean.FALSE |
"1" | Boolean.TRUE |
"0" | Boolean.FALSE |
Immutability and thread safety
This base class and all inner classes are immutable, and thus inherently thread-safe.- Since:
- 0.3
- Version:
- 0.5
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
static final class
static final class
static final class
static final class
static final class
(package private) static final class
StringConverter.CodeList<T extends org.opengis.util.CodeList<T>>
Converter fromString
toCodeList
.static final class
(package private) static final class
StringConverter.Enum<T extends Enum<T>>
static final class
static final class
static final class
static final class
static final class
static final class
static final class
static final class
static final class
static final class
static final class
static final class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ObjectConverter<T,
String> The inverse converter from the target to the source class.private static final long
For cross-version compatibility.Fields inherited from class org.apache.sis.internal.converter.ClassPair
sourceClass, targetClass
-
Constructor Summary
ConstructorsConstructorDescriptionStringConverter
(Class<T> targetClass) Creates a new converter for the given target class. -
Method Summary
Modifier and TypeMethodDescriptionfinal T
Converts the given string to the target type of this converter.(package private) ObjectConverter<T,
String> Invoked by the constructor for creating the inverse converter.(package private) abstract T
Invoked byapply(String)
for converting the given string to the target type of this converter.final ObjectConverter<T,
String> inverse()
Returns the inverse converter.While this is not a general rule for surjective functions, all converters defined in this class are invertibles.Methods inherited from class org.apache.sis.internal.converter.SystemConverter
bijective, equals, formatErrorMessage, getSourceClass, getTargetClass, readResolve, unique
Methods inherited from class org.apache.sis.internal.converter.ClassPair
cast, hashCode, parentSource, toString
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDFor cross-version compatibility.- See Also:
-
inverse
The inverse converter from the target to the source class.
-
-
Constructor Details
-
StringConverter
Creates a new converter for the given target class.- Parameters:
targetClass
- the target class.
-
-
Method Details
-
createInverse
ObjectConverter<T,String> createInverse()Invoked by the constructor for creating the inverse converter. To be overridden by classes which need a specialized instance. -
properties
While this is not a general rule for surjective functions, all converters defined in this class are invertibles.- Returns:
- the manners in which source values are mapped to target values. May be an empty set, but never null.
-
inverse
Returns the inverse converter.- Specified by:
inverse
in interfaceObjectConverter<String,
T> - Overrides:
inverse
in classSystemConverter<String,
T> - Returns:
- a converter for converting instances of T back to instances of S.
- See Also:
-
apply
Converts the given string to the target type of this converter. This method verifies that the given string is non-null and non-empty, then delegates todoConvert(String)
.- Parameters:
source
- the string to convert, ornull
.- Returns:
- the converted value, or
null
if the given string was null or empty. - Throws:
UnconvertibleObjectException
- if an error occurred during the conversion.
-
doConvert
Invoked byapply(String)
for converting the given string to the target type of this converter.- Parameters:
source
- the string to convert, guaranteed to be non-null and non-empty.- Returns:
- the converted value.
- Throws:
Exception
- if an error occurred during the conversion.
-