Skip to main content
Skip table of contents

extractMapValueByExistingKey

Versionshinweise

25.6

Code

GROOVY

Object extractMapValueByExistingKey(Map<?, ?> dataMap, Object... key);

Beschreibung

Die Methode liest einen Wert aus der gegebenen Map, indem anhand der Keys tiefer in die Map navigiert wird.
Jeder Key muss auf eine Map verweisen, außer dem letzen. Verweist einer der Keys (bis auf den letzten) nicht auf eine Map, so wird eine ScriptingException ausgelöst.
Der letzte Key kann auf ein beliebiges Objekt verweisen, dieses wird dann zurückgegeben.
Wird ein Key nicht an der entsprechenden Stelle gefunden, wird eine ScriptingException ausgelöst.

Parameter

  • dataMap - Die Map, aus der der Wert gelesen werden soll.

  • key - Die Keys, die den Weg zum gesuchten Wert in der Map beschreiben.

Rückgabe

Der über die Keys referenzierte Wert der dataMap.

Beispiele

GROOVY

GROOVY
Map<String, Object> m1 = new HashMap();
m1.put("k1Test1IntVal", 10);

Map<String, Object> m2 = new HashMap();
m2.put("k2Test1StringVal", "TestStringVal1");
m2.put("k2Test2StringVal", "TestStringVal2");
m2.put("k2Test3MapVal", m1);

Map<String, Object> m3 = new HashMap();
m3.put("k3Test1DoubleVal", 3.5d);
m3.put("k3Test2StringVal", "TestStringVal3");
m3.put("k3Test3MapVal", m2);
m3.put("k3Test3NullVal", null);

try
{
  LogicScriptUtils.extractMapValueByExistingKey(null, "k3Test2StringVal", "mich gibt es nicht");
  Assertions.fail("Test 0 ScriptingException expected.");
}
catch (ScriptingException e)
{
  // expected
}
Assertions.assertEquals(10, LogicScriptUtils.extractMapValueByExistingKey(m1, "k1Test1IntVal"), "Test 1 read key in simple Map");
Assertions.assertEquals(10, LogicScriptUtils.extractMapValueByExistingKey(m2, "k2Test3MapVal", "k1Test1IntVal"), "Test 2 read key in Map contained in Map");
Assertions.assertEquals(10, LogicScriptUtils.extractMapValueByExistingKey(m3, "k3Test3MapVal", "k2Test3MapVal", "k1Test1IntVal"), "Test 3 read key in Map contained in Map contained in Map");
Assertions.assertEquals("TestStringVal1", LogicScriptUtils.extractMapValueByExistingKey(m3, "k3Test3MapVal", "k2Test1StringVal"), "Test 4 read String value of key in Map contained in Map");
Assertions.assertEquals(m1, LogicScriptUtils.extractMapValueByExistingKey(m3, "k3Test3MapVal", "k2Test3MapVal"), "Test 5 read Map value of key in Map in Map");
Assertions.assertEquals("TestStringVal3", LogicScriptUtils.extractMapValueByExistingKey(m3, "k3Test2StringVal"), "Test 6 read String value of key in Map");
try
{
  LogicScriptUtils.extractMapValueByExistingKey(m3, "k3Test2StringVal", "mich gibt es nicht");
  Assertions.assertTrue(false, "Test 7 ScriptingException expected.");
}
catch (ScriptingException e)
{
  // expected try to read value of a key containing no Map
}
Assertions.assertNull(LogicScriptUtils.extractMapValueByExistingKey(m3, "k3Test3NullVal"), "Test 8 read null value of a key in Map");
try
{
  LogicScriptUtils.extractMapVal(m3, true, "k3Test3MapVal", "mich gibt es nicht");
  Assertions.assertTrue(false, "Test 9 ScriptingException expected.");
}
catch (ScriptingException e)
{
  // expected try to read value of a non-existing key in Map
}

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.