Skip to content

Commit a6385fe

Browse files
Craigacpjhalexand
authored andcommitted
Adding a lookupAllMap method which returns the configurables and their names.
1 parent 9b73a5c commit a6385fe

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

olcut-core/src/main/java/com/oracle/labs/mlrg/olcut/config/ConfigurationManager.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,50 @@ public <T extends Configurable> T lookup(Class<T> c, ComponentListener<T> cl) {
14221422
return comps.get(0);
14231423
}
14241424

1425+
/**
1426+
* Looks up all the components of a given type, returning a map of them.
1427+
* <p>
1428+
* If the class is an interface, it returns all the configurables which implement that interface,
1429+
* if it's a concrete class then it returns only those configurables which are exactly that class.
1430+
* @param c The class of component to lookup.
1431+
* @param <T> The type of the component.
1432+
* @return A map containing all instances of the desired class this configuration manager knows about.
1433+
*/
1434+
@SuppressWarnings("unchecked") // Casts to T are implicitly checked as we use Class<T> to find the names.
1435+
public <T extends Configurable> Map<String, T> lookupAllMap(Class<T> c) {
1436+
Map<String, T> ret = new HashMap<>();
1437+
1438+
//
1439+
// If the class isn't an interface, then lookup each of the names
1440+
// in the raw property data with the given class
1441+
// name, ignoring those things marked as importable.
1442+
if(!c.isInterface()) {
1443+
String className = c.getName();
1444+
for (Map.Entry<String, ConfigurationData> e : configurationDataMap.entrySet()) {
1445+
if (e.getValue().getClassName().equals(className) &&
1446+
!e.getValue().isImportable()) {
1447+
ret.put(e.getKey(),(T)lookup(e.getKey()));
1448+
}
1449+
}
1450+
} else {
1451+
//
1452+
// If we have an interface and no registry, lookup all the
1453+
// implementing classes and return them.
1454+
for (Map.Entry<String, ConfigurationData> e : configurationDataMap.entrySet()) {
1455+
try {
1456+
Class clazz = Class.forName(e.getValue().getClassName());
1457+
if (!e.getValue().isImportable() && c.isAssignableFrom(clazz) && !clazz.isInterface()) {
1458+
ret.put(e.getKey(),(T)innerLookup(e.getKey(),null,true));
1459+
}
1460+
} catch (ClassNotFoundException ex) {
1461+
throw new PropertyException(ex,e.getKey(),"Class not found for component " + e.getKey());
1462+
}
1463+
}
1464+
}
1465+
1466+
return ret;
1467+
}
1468+
14251469
/**
14261470
* Looks up all the components of a given type, returning a list of them.
14271471
* @param c The class of component to lookup.

olcut-core/src/test/java/com/oracle/labs/mlrg/olcut/config/GenericConfigTest.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2004-2020, Oracle and/or its affiliates.
2+
* Copyright (c) 2004-2021, Oracle and/or its affiliates.
33
*
44
* Licensed under the 2-clause BSD license.
55
*
@@ -30,9 +30,15 @@
3030

3131
import java.io.IOException;
3232
import java.util.List;
33+
import java.util.Map;
3334

3435
import com.oracle.labs.mlrg.olcut.config.test.Ape;
36+
import com.oracle.labs.mlrg.olcut.config.test.Barbary;
37+
import com.oracle.labs.mlrg.olcut.config.test.Chimp;
38+
import com.oracle.labs.mlrg.olcut.config.test.Gorilla;
3539
import com.oracle.labs.mlrg.olcut.config.test.Monkey;
40+
import com.oracle.labs.mlrg.olcut.config.test.Orangutan;
41+
import com.oracle.labs.mlrg.olcut.config.test.Rhesus;
3642
import org.junit.jupiter.api.Test;
3743

3844
import static org.junit.jupiter.api.Assertions.*;
@@ -58,6 +64,41 @@ public void lookupAllTest() throws ClassNotFoundException {
5864
assertEquals(3, apes.size(), "Didn't find all the Ape classes");
5965
}
6066

67+
@SuppressWarnings("unchecked")//Looking up a specific class via it's full name
68+
@Test
69+
public void lookupAllMapTest() throws ClassNotFoundException {
70+
ConfigurationManager cm = new ConfigurationManager("genericConfig.xml");
71+
72+
Class<Monkey> clazz = (Class<Monkey>) Class.forName("com.oracle.labs.mlrg.olcut.config.test.Monkey");
73+
Map<String,Monkey> monkeys = cm.lookupAllMap(clazz);
74+
75+
assertEquals(5, monkeys.size(), "Didn't find all the MonkeyConfigurable classes");
76+
assertTrue(monkeys.get("monkey-one") instanceof Gorilla);
77+
assertTrue(monkeys.get("monkey-two") instanceof Chimp);
78+
assertTrue(monkeys.get("monkey-three") instanceof Orangutan);
79+
assertTrue(monkeys.get("monkey-four") instanceof Rhesus);
80+
assertTrue(monkeys.get("monkey-five") instanceof Barbary);
81+
82+
Class<Ape> apeClazz = (Class<Ape>) Class.forName("com.oracle.labs.mlrg.olcut.config.test.Ape");
83+
Map<String,Ape> apes = cm.lookupAllMap(apeClazz);
84+
85+
assertEquals(3, apes.size(), "Didn't find all the Ape classes");
86+
assertTrue(apes.get("monkey-one") instanceof Gorilla);
87+
assertTrue(apes.get("monkey-two") instanceof Chimp);
88+
assertTrue(apes.get("monkey-three") instanceof Orangutan);
89+
90+
Class<Gorilla> gorillaClazz = (Class<Gorilla>) Class.forName("com.oracle.labs.mlrg.olcut.config.test.Gorilla");
91+
Map<String,Gorilla> gorillas = cm.lookupAllMap(gorillaClazz);
92+
93+
assertEquals(1,gorillas.size(),"Didn't find the Gorilla instance");
94+
assertTrue(gorillas.get("monkey-one") instanceof Gorilla);
95+
96+
Class<ArrayStringConfigurable> arrStrConfClazz = (Class<ArrayStringConfigurable>) Class.forName("com.oracle.labs.mlrg.olcut.config.ArrayStringConfigurable");
97+
Map<String,ArrayStringConfigurable> arrays = cm.lookupAllMap(arrStrConfClazz);
98+
99+
assertTrue(arrays.isEmpty());
100+
}
101+
61102
@Test
62103
public void correctListConfig() throws IOException {
63104
ConfigurationManager cm = new ConfigurationManager("genericConfig.xml");

0 commit comments

Comments
 (0)