Package com.inductiveautomation.rm.base
Class RMKey
java.lang.Object
com.inductiveautomation.rm.base.RMKey
This class provides an optimized convenience for getting named values from arbitrary objects.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceThis is interface is implemented by objects that can get key values themselves.static interfaceThis is interface is implemented by objects that can get/set key value themselves.static classKeyAccessor - enclosed class for actually getting/setting values for a given object (class) and key.static interfacestatic classNoSetMethodException. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic RMKey.KeyAccessorgetAccessor(Object anObj, String aKey) Returns the accessor object for a given object (class) and key.static intgetIntValue(Object anObj, String aKey) ResolvesaKeyonanObjviagetValue(java.lang.Object, java.lang.String)and coerces the result to anintviaRMUtils.intValue(java.lang.Object).static Collection<String>static @NonNull StringgetSanitizedKey(@Nullable String aKey) Returns the given string with all invalid key characters stripped out.static StringgetStandard(@NonNull String aKey) Returns the key in a standard format (strip is/get prefix and start with capital letter).static StringgetStringValue(Object anObj, String aKey) ResolvesaKeyonanObjviagetValue(java.lang.Object, java.lang.String)and coerces the result to aStringviaRMUtils.stringValue(java.lang.Object).static ObjectReturns a value for the given object and key.static <T> TResolvesaKeyonanObjviagetValue(Object, String)and returns the result only if it is an instance ofaClass(viaRMClassUtils.getInstance(java.lang.Object, java.lang.Class<T>)); otherwisenull.static ObjectgetValueImpl(Object anObj, String aKey) Implementation method for getValue() when the object is not a Get instance.static booleanReturns whether given object has an accessor for given key.static booleanReturns whether given string is a valid key (starts with letter or underscore and only contains letters, digits, white space and underscores).static voidSets a value for the given object, key, and value.static voidsetValueSafe(Object anObj, String aKey, Object aValue) Sets the value but only prints a warning if it fails.static voidsetValueSilent(Object anObj, String aKey, Object aValue) Tries to set value in given object, ignoring failure exceptions.
-
Constructor Details
-
RMKey
public RMKey()
-
-
Method Details
-
isKey
Returns whether given string is a valid key (starts with letter or underscore and only contains letters, digits, white space and underscores). -
getSanitizedKey
Returns the given string with all invalid key characters stripped out.Keys that are blank, as determined by StringUtils#isBlank(CharSequence) blank}, will yield an empty string, which is invalid, but will be returned regardless. The result may also end up a duplicate of another key; there's only so much we can do for the user.
- Since:
- 7.8.0
-
getKeys
-
getValue
Returns a value for the given object and key.Special keys:
"this": returns the object itself"idHashCode": returns System.identityHashCode(object)
-
getValueImpl
Implementation method for getValue() when the object is not a Get instance. -
setValue
Sets a value for the given object, key, and value. For a POJO target the value is coerced to the resolved setter's parameter type; seeRMKey.KeyAccessor.set(Object, Object)for the coercion rules.- Throws:
Exception- See Also:
-
setValueSafe
Sets the value but only prints a warning if it fails. -
setValueSilent
Tries to set value in given object, ignoring failure exceptions. -
hasKey
Returns whether given object has an accessor for given key. -
getIntValue
ResolvesaKeyonanObjviagetValue(java.lang.Object, java.lang.String)and coerces the result to anintviaRMUtils.intValue(java.lang.Object). This is a convenience wrapper that never throws: a missing key, anullobject, or a non-numeric value all yield0.Coercion follows
RMUtils.intValue(java.lang.Object)/RMUtils.longValue(java.lang.Object):- a
Numberis truncated toward zero, then narrowed toint— so a value outsideintrange wraps silently (e.g.12.99 -> 12, and alongof3_000_000_000 -> -1294967296) - a
Stringyields its first signed-integer run, parsed then narrowed (e.g."12.99" -> 12,"1,234" -> 1,"-5" -> -5); a string with no digit run yields0 nullor any other type yields0
- Parameters:
anObj- the object to read from (may benull)aKey- the key to resolve- Returns:
- the resolved value coerced to
int, or0if it is absent or not coercible - See Also:
- a
-
getStringValue
ResolvesaKeyonanObjviagetValue(java.lang.Object, java.lang.String)and coerces the result to aStringviaRMUtils.stringValue(java.lang.Object). This is a convenience wrapper that never throws. UnlikegetIntValue(java.lang.Object, java.lang.String)(which defaults to0), a missing key, anullobject, or anullvalue all yieldnull.Coercion follows
RMUtils.stringValue(java.lang.Object):- a
Stringis returned as-is - a
Numberis string-formatted (e.g.42 -> "42") - a
Dateasyyyy-MM-dd HH:mm:ss, aFileas its absolute path, abyte[]as base64 nullyieldsnull; any other type yields itsObject.toString()
- Parameters:
anObj- the object to read from (may benull)aKey- the key to resolve- Returns:
- the resolved value coerced to a String, or
nullif it is absent or itself null - See Also:
- a
-
getValue
ResolvesaKeyonanObjviagetValue(Object, String)and returns the result only if it is an instance ofaClass(viaRMClassUtils.getInstance(java.lang.Object, java.lang.Class<T>)); otherwisenull. This filters by type, it does not convert: anIntegervalue requested asString.classyieldsnull, not"42". Anullor absent value also yieldsnull.- Type Parameters:
T- the requested type- Parameters:
anObj- the object to read from (may benull)aKey- the key to resolveaClass- the type the resolved value must be an instance of- Returns:
- the resolved value if it is an instance of
aClass, otherwisenull - See Also:
-
getAccessor
Returns the accessor object for a given object (class) and key. -
getStandard
Returns the key in a standard format (strip is/get prefix and start with capital letter).
-