any
Values of the
any type can hold a value of a concrete EPL type (that is, a type other than the
any type). Different values of
any variables may have different types. This is useful when mapping to generic data formats such as JSON, or where the data in an event or being processed by an action may be one of a number of different types. An
any value may also be empty and not contain a value (similar to the concept of
null in other languages). See also
Handling the any type.
If a variable is potentially empty but of a known type, then the
optional type may be a better representation; see
optional for further information.
Usage
An any is either empty or contains a single value which is of some other type. All types other than the any type are referred to as concrete types. A concrete type may be assigned to an any type, or passed to an action or method that takes an any type, or returned from an action that includes a returns any clause.
The
switch statement is the preferred way of handling
any values unless the type is known or not important. See
Handling any values of different types with the switch
statement for details on the
switch statement.
EPL supports casting of the
any type to a concrete target type and vice versa. See
Handling the any type for information on possible cast operations.
any types can be compared, provided that the value's types can be compared (see also
Comparable types). Values of different types are never equal and will sort according to the type's full name, with empty values sorting before any non-empty values.
Note that while primitive types (integer, float, boolean and decimal) can be used freely with the any type, using primitive values in an any object is more expensive than using just primitives. The values are automatically wrapped in an object ("boxed") which has a single field called "value" and unwrapped when converted back to primitive values. This all happens automatically, but there is a higher runtime cost for primitives in an any variable than in normal primitive variables.
Methods
The methods available to the any type are:
clone() returns any —
Returns a new any that is an exact copy of the any that this method is called on. The contained value (if it exists) is cloned into the new
any, and if that value is a reference type, its contents are cloned as well.
getAction(string name) returns any —
Gets the action or method by name. Throws
IllegalArgumentException if the action name is not found.
getActionNames() returns sequence<string> —
Gets the names of the actions or methods within this any. For events, returns the name of all actions, in declaration order.
getActionParameterNames() returns sequence<string> —
Gets the parameter names of the action contained in this any. Returns a sequence containing the string parameter names, in the order of the signature of the action this any contains. Throws
IllegalArgumentException if not called on the
action type.
getActionParameters() returns dictionary<string,string> —
Gets a dictionary whose keys are the parameter names of the action contained in this any, and whose values are the types of each parameter. Throws
IllegalArgumentException if not called on the
action type.
getActionReturnTypeName() returns string —
Gets the name of the return type of the action contained in this any. If an action does not return anything, then an empty string is returned. Throws
IllegalArgumentException if not called on the
action type.
getConstant(string) returns any —
Gets the value of a constant by name. Throws
IllegalArgumentException if not called on the
action type.
getConstantNames() returns sequence<string> —
Gets the names of the constants within this any. For events, returns the name of all constants, in declaration order.
getEntries() returns sequence<any> —
Gets a sequence of all of the entries (empty if getKeys() returns empty). Will never throw an exception.
getEntry(any key) returns any —
Gets the entry. For events, retrieves the field value whose name is specified in
key (
key must be
string), an entry for sequence (
key must be
integer) or a dictionary entry (
key must be of the correct type for the dictionary). Throws an exception if no entry by that name or index exists.
getGenericAction() returns action<sequence<any> > returns any —
Gets the "generic" form of the action. A generic form allows calling the action via the
getGenericAction even if the signature type is not known at compile time. The sequence must be of the same size as the number of arguments to the action. If the action returns a value, then that is wrapped in an
any, else the action returns an empty
any. Throws
IllegalArgumentException if not called on the
action type.
getKeys() returns sequence<any> —
Returns the names of the fields of the value's types for structured values, that is, events, boxed primitive objects (which have a single "value" field), dictionaries and sequences. Returns the key values for dictionaries. Returns a sequence of indices for sequences. Otherwise returns empty sequence (even for empty values).
getTypeName() returns string —
Returns the fully-qualified name of the value's type (or the word "empty"). For primitive types, this will be the primitive type name (
integer,
float,
decimal or
boolean).
getTypes() returns sequence<string> —
Similar to getKeys(), but returns the full type names of the keys. setEntry(any key, any value) —
Sets the named entry. Throws an exception if the
key does not exist, or if the
key or
value is of the wrong type. This can add new entries to dictionaries, but not to sequences or events.
static canParse(string s) returns boolean —
Returns true if the parsing of the string succeeds. For more information about the parseable type property, see the table in
Type properties summary.
static newInstance(string typeName) returns any —
Creates a new (default) instance if typeName names a type. This method will throw an exception if the type is not found or has been deleted.
static parse(string s) returns any —
Returns the any object represented by the string argument. Example:
any av := any.parse("any(sequence<integer>,[1,2,3])");
If the correlator is unable to parse the string, the method will throw an exception. For more information about the parseable type property, see the table in
Type properties summary.
static parseType(string typeName, string stringForm) returns any —
Creates a new instance if typeName names a type, and parses the stringForm into it. This method will throw an exception if the type is not found or has been deleted. Example:
any av := any.parseType("sequence<integer>","[1,2,3]");
Note: The correlator cannot parse a type which it has not seen so far. For example, you cannot parse a sequence<sequence<float> > unless it is explicitly used in EPL. This applies for parse(), parseType() and also for newInstance().
toString() returns string —
Creates a string representation of the any. The string form is the word "any", followed by parentheses. If the
any has a value, then the full name of the type of the value follows, then a comma, and then the string form of that value. For example:
print (new any).toString(); // prints "any()"
sequence<integer> si:=[1,2,3];
any av:=si;
print av.toString(); // prints "any(sequence<integer>,[1,2,3])"
When an
any is a potentially cyclic type, the behavior of the
toString() method is different. See
Potentially
cyclic types.
valueToString() returns string —
Returns the result of calling .toString() on the contained value directly, or the word "empty" if there is no value. For strings, the contents of the string will be returned, without extra quoting.
When handling event fields (or dictionaries with a string key), the following methods may be used instead. These methods take and return strings for the field names, but are otherwise equivalent to getKeys, getTypes, getEntry, getEntries and setEntry above. These methods throw an exception if the any type does not contain an event or dictionary with a string key (unless stated otherwise).
getFieldNames() returns seq<string> — Similar to
getKeys(), but returns the string form of the keys.
getFieldString(string fieldName) returns string — Returns a given field converted
toString(), else throws an exception.
getField(string fieldName) returns any — Returns a given field. Throws an exception if the field is not found.
setField(string fieldName, any value) — Sets the named field. Throws an exception if the field is not found.
getFieldTypes() returns sequence<string> — Synonym of
getTypes().
getFieldValues() returns sequence<string> — Returns the string form of all fields (empty if
getKeys() returns empty). Will never throw an exception.
getFields() returns sequence<any> — Synonym of
getEntries().