Class RMKey

java.lang.Object
com.inductiveautomation.rm.base.RMKey

public class RMKey extends Object
This class provides an optimized convenience for getting named values from arbitrary objects.
  • Constructor Details

    • RMKey

      public RMKey()
  • Method Details

    • isKey

      public static boolean isKey(@Nullable String aString)
      Returns whether given string is a valid key (starts with letter or underscore and only contains letters, digits, white space and underscores).
    • getSanitizedKey

      public static @NonNull String getSanitizedKey(@Nullable String aKey)
      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

      public static Collection<String> getKeys(Object obj)
    • getValue

      public static Object getValue(Object anObj, String aKey)
      Returns a value for the given object and key.

      Special keys:

      • "this": returns the object itself
      • "idHashCode": returns System.identityHashCode(object)
    • getValueImpl

      public static Object getValueImpl(Object anObj, String aKey)
      Implementation method for getValue() when the object is not a Get instance.
    • setValue

      public static void setValue(Object anObj, String aKey, Object aValue) throws Exception
      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; see RMKey.KeyAccessor.set(Object, Object) for the coercion rules.
      Throws:
      Exception
      See Also:
    • setValueSafe

      public static void setValueSafe(Object anObj, String aKey, Object aValue)
      Sets the value but only prints a warning if it fails.
    • setValueSilent

      public static void setValueSilent(Object anObj, String aKey, Object aValue)
      Tries to set value in given object, ignoring failure exceptions.
    • hasKey

      public static boolean hasKey(Object anObj, String aKey)
      Returns whether given object has an accessor for given key.
    • getIntValue

      public static int getIntValue(Object anObj, String aKey)
      Resolves aKey on anObj via getValue(java.lang.Object, java.lang.String) and coerces the result to an int via RMUtils.intValue(java.lang.Object). This is a convenience wrapper that never throws: a missing key, a null object, or a non-numeric value all yield 0.

      Coercion follows RMUtils.intValue(java.lang.Object) / RMUtils.longValue(java.lang.Object):

      • a Number is truncated toward zero, then narrowed to int — so a value outside int range wraps silently (e.g. 12.99 -> 12, and a long of 3_000_000_000 -> -1294967296)
      • a String yields its first signed-integer run, parsed then narrowed (e.g. "12.99" -> 12, "1,234" -> 1, "-5" -> -5); a string with no digit run yields 0
      • null or any other type yields 0
      Parameters:
      anObj - the object to read from (may be null)
      aKey - the key to resolve
      Returns:
      the resolved value coerced to int, or 0 if it is absent or not coercible
      See Also:
    • getStringValue

      public static String getStringValue(Object anObj, String aKey)
      Resolves aKey on anObj via getValue(java.lang.Object, java.lang.String) and coerces the result to a String via RMUtils.stringValue(java.lang.Object). This is a convenience wrapper that never throws. Unlike getIntValue(java.lang.Object, java.lang.String) (which defaults to 0), a missing key, a null object, or a null value all yield null.

      Coercion follows RMUtils.stringValue(java.lang.Object):

      • a String is returned as-is
      • a Number is string-formatted (e.g. 42 -> "42")
      • a Date as yyyy-MM-dd HH:mm:ss, a File as its absolute path, a byte[] as base64
      • null yields null; any other type yields its Object.toString()
      Parameters:
      anObj - the object to read from (may be null)
      aKey - the key to resolve
      Returns:
      the resolved value coerced to a String, or null if it is absent or itself null
      See Also:
    • getValue

      public static <T> T getValue(Object anObj, String aKey, Class<T> aClass)
      Resolves aKey on anObj via getValue(Object, String) and returns the result only if it is an instance of aClass (via RMClassUtils.getInstance(java.lang.Object, java.lang.Class<T>)); otherwise null. This filters by type, it does not convert: an Integer value requested as String.class yields null, not "42". A null or absent value also yields null.
      Type Parameters:
      T - the requested type
      Parameters:
      anObj - the object to read from (may be null)
      aKey - the key to resolve
      aClass - the type the resolved value must be an instance of
      Returns:
      the resolved value if it is an instance of aClass, otherwise null
      See Also:
    • getAccessor

      public static RMKey.KeyAccessor getAccessor(Object anObj, String aKey)
      Returns the accessor object for a given object (class) and key.
    • getStandard

      public static String getStandard(@NonNull String aKey)
      Returns the key in a standard format (strip is/get prefix and start with capital letter).