|
The following example shows how to find whether some named entry exists in the parameters object, and at the same time obtain the ``proxy'' object referring to that entry:
Parameters.Entry e = params.find("first_name");
If the params
object is filled according to previous examples, then there is an entry named ``first_name'' and the Parameter.Entry
proxy refers to it:
if (e != null) { System.out.println(e.getString()); }
If the content is being read in a truly exploratory way and the code has to be resistant not only to missing entries, but also to entries that have unexpected type, more elaborate check can be performed instead:
if (e != null && e.type() == Parameters.EntryType.STRING) { System.out.println(e.getString()); }
In the above example the type
function returns the type name of the given entry.
All possible type names are defined as a nested enumeration in the Parameters
class:
public class Parameters { public enum EntryType { BOOLEAN, INTEGER, LONG, DOUBLE, STRING, BINARY, BOOLEAN_ARRAY, INTEGER_ARRAY, LONG_ARRAY, DOUBLE_ARRAY, STRING_ARRAY, BINARY_ARRAY, NESTED_PARAMETERS, NESTED_PARAMETERS_ARRAY } // ... }
Some applications might have a need for custom serialization or completely general data handling schemes - then a switch
statement with separate branches for all possible type names is a natural approach. In fact, a conceptually similar code is part of the YAMI4 library itself.
The type of the entry can be explored directly as well, which might be handy if it is already known that such entry exists:
Parameters.EntryType ageType = params.getType("age");
The Parameter.Entry
type offers all reading operations and they are used in exactly the same way as the reading operations of the parameters object itself, with the exception that there is no need for the entry name parameter - the given entry is already being referred to, so there is no need to look for it by name.
The entry name can be also extracted from the Parameters.Entry
object:
String name = e.name();