Package ca.spatial.util
Class SPSDecoder
java.lang.Object
ca.spatial.util.SPSDecoder
This class keeps tracks of decoders that convert objects into
constructor scripts. Class register their decoder information
with this class using the
setDecoder(ca.spatial.util.DecodeTemplate)
methods. The
decodeValue(java.lang.Object)
method or the DecodeTemplate.decode(java.lang.Object, java.lang.String, java.lang.StringBuilder, java.util.Map<java.lang.Object, java.lang.String>)
methods may be used to generate the Java source code representation
of the objects.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
addDecoder(ArgumentDecoder decoder)
Add an ArgumentDecoder to the front of the liststatic void
addDefaultMapping(Object o, String name)
Add a mapping for a well known object that will be used for all subsequent decode operations.static String
capitalize(String name)
Capitalize the first letter of a stringstatic String
Get the name of a class, but not the packagestatic String
decapitalize(String name)
Decapitalize the first letter of a stringstatic String
decodeValue(Object arg)
Decode an object if a decoder is available, otherwise return "null".static String
decodeValue(Object arg, String name, StringBuilder sb, Map<Object,String> resolved)
Decode an object if a decoder is available, otherwise return "null".static String
decodeValue(Object arg, String name, StringBuilder sb, Map<Object,String> resolved, Class asType)
Decode an object if a decoder is available, otherwise return "null".static String
Decode an object and interpolate the results in to the specified format string.static String
Decode an object and interpolate the results in to the specified format string.static String
Decode an object and interpolate the results in to the specified format string.static String
formatArgs(Object[] o, String formatString)
Decode an array of objects and interpolate the results in to the specified format string.static DecodeTemplate
getDecoder(Class c)
Get an encoder for the specified class or superclassstatic ArrayList
getDefaultMappingsFor(Class clz)
Return a list of all of the default mappings that match to the specified class.static Object[]
makeSpecificArray(Object[] inVals)
If possible make the array in to sometihng more specific than just Object[].static DecodeTemplate
setDecoder(DecodeTemplate et)
Add a default decoder that uses reflection to automatically figure out the constructor.static DecodeTemplate
setDecoder(Class c, String[] args)
Add a decoder for the specified class that uses reflection to automatically figure out the constructor.static DecodeTemplate
setDecoder(Class c, String[] args, String documentation)
Add a decoder for the specified class that uses reflection to automatically figure out the constructor.
-
Constructor Details
-
SPSDecoder
public SPSDecoder()
-
-
Method Details
-
addDecoder
Add an ArgumentDecoder to the front of the list- Parameters:
decoder
- the ArgumentDecoder to add
-
setDecoder
Add a default decoder that uses reflection to automatically figure out the constructor.- Parameters:
et
- the DecodeTemplate to add. This will be applied to object of the class specified in the template.
-
setDecoder
Add a decoder for the specified class that uses reflection to automatically figure out the constructor.- Parameters:
c
- The class to defineargs
- The base names of the getters for the arguments. For each argument 'arg' there must exist a getter method with a name of 'getArg()'. See the DecodeTemplate class documentation for more information.
-
setDecoder
Add a decoder for the specified class that uses reflection to automatically figure out the constructor.- Parameters:
c
- The class to defineargs
- The base names of the getters for the arguments. For each argument 'arg' there must exist a getter method with a name of 'getArg()'. See the DecodeTemplate class documentation for more information. * @param documentation optional documentation that will be output prior to the object definition. This is only written if the object is not output using inline formatting.
-
getDecoder
Get an encoder for the specified class or superclass- Parameters:
c
- the class to retrieve the encoder for. This methods will search up through the inheritance chain looking for a matching decoder.- Returns:
- an appropriate decoder or a
-
addDefaultMapping
Add a mapping for a well known object that will be used for all subsequent decode operations. These objects are assumed to be known to the environment where the scripts will be run, and therefor do not need to be fully decoded.- Parameters:
o
- the object to add to the mapname
- the symbolic name to be used to refer to the object.
-
getDefaultMappingsFor
Return a list of all of the default mappings that match to the specified class. -
format
Decode an object and interpolate the results in to the specified format string. Attempt to format the object in 'inline' mode (suitable for string, numbers and simple objects).- Parameters:
o
- the object to decodeformatString
- the format string to embed the object in- Returns:
- the interpolated string with the decoded object
-
format
Decode an object and interpolate the results in to the specified format string.- Parameters:
o
- the object to decodeformatString
- the format string to embed the object invarName
- the variable name to assign the object to- Returns:
- the interpolated string with the decoded object
-
format
public static String format(Object o, String formatString, String varName, Map<Object,String> resolved)Decode an object and interpolate the results in to the specified format string.- Parameters:
o
- the object to decodeformatString
- the format string to embed the object invarName
- the variable name to assign the object toresolved
- a hashmap containing previously resolved objects and and the symbolic names that they have been assigned to. This parameter may be null, in which case the default resolved list will be used.- Returns:
- the interpolated string with the decoded object
-
formatArgs
Decode an array of objects and interpolate the results in to the specified format string.- Parameters:
o
- the array of objects to decodeformatString
- theMessageFormat
format string to embed the objects in. In this format string argument {0} will be populated with any additional not-inlined object content, and {1} to {n} with the individual arguments. Note this means object o[0] is in position {1}, o[1] in {2}, etc.- Returns:
- the interpolated string with the decoded objects
- See Also:
MessageFormat
-
decodeValue
Decode an object if a decoder is available, otherwise return "null". This method will search for the most appropriate decoder for the object. This decoder only works for simple objects that do not contain additional dependents. An unchecked exception will be thrown if the argument is not a simple expression.This will make a canonical representations of the simple object.
- Parameters:
arg
- The object to decode- Returns:
- the object definition.
-
decodeValue
public static String decodeValue(Object arg, String name, StringBuilder sb, Map<Object,String> resolved)Decode an object if a decoder is available, otherwise return "null". This method will search for the most appropriate decoder for the object, and continue to decode all parameters.- Parameters:
arg
- The object to decodename
- A name to be used to refer to the object in the resolve map, which can be reused in subsequent decodings to refer to the same object.sb
- a StringBuilder object that the decode methods will use to to contain the Java code that it creates.resolved
- A Map of object references and symbolic names. The decoder will use heuristics to decide when to inline references or to give object symbolic names. The objects that are given sybolic names are added to the resolved map so that they may be defined once and referenced multiple times. You may also pre-populate the resolved map with references to well known objects. These objects will be replaced with the symbolic names rather than being decoded. Be careful if reusing a resolved map from one invocation of the decode mthod to another: in some cases different object may be given the same symbolic name resulting in incorrect references.- Returns:
- if a name has been provided, the name is return and the object definition (plus all subordinate object definitions) are contained in the StringBuilder object. If the name parameter had a null value, then the full object definition is returned.
-
decodeValue
public static String decodeValue(Object arg, String name, StringBuilder sb, Map<Object,String> resolved, Class asType)Decode an object if a decoder is available, otherwise return "null". This method will search for the most appropriate decoder for the object, and continue to decode all parameters.- Parameters:
arg
- The object to decodename
- A name to be used to refer to the object in the resolve map, which can be reused in subsequent decodings to refer to the same object.sb
- a StringBuilder object that the decode methods will use to to contain the Java code that it creates.resolved
- A Map of object references and symbolic names.asType
- the type of object that is expected to be converted. This is mostly ignored, but will make a difference when primitives are boxed within reference types. The decoder will use heuristics to decide when to inline references or to give object symbolic names. The objects that are given sybolic names are added to the resolved map so that they may be defined once and referenced multiple times. You may also pre-populate the resolved map with references to well known objects. These objects will be replaced with the symbolic names rather than being decoded. Be careful if reusing a resolved map from one invocation of the decode mthod to another: in some cases different object may be given the same symbolic name resulting in incorrect references.- Returns:
- if a name has been provided, the name is return and the object definition (plus all subordinate object definitions) are contained in the StringBuilder object. If the name parameter had a null value, then the full object definition is returned.
-
makeSpecificArray
If possible make the array in to sometihng more specific than just Object[]. Also filter out any null values. -
capitalize
Capitalize the first letter of a string -
className
Get the name of a class, but not the package -
decapitalize
Decapitalize the first letter of a string
-