ConnectorService implements IConnectorService { @SuppressWarnings("unchecked") @Override public <T extends Connector> T getConnector(Class<T> type, String id) { if (id == null) { throw new LmRuntimeException("Null connector id"); } Connector connector = connectors.computeIfAbsent(id, i -> { IConnectorFactory<?> factory = factories.get(type.getName()); if (factory == null) { throw new IllegalStateException("No factory mapped for Connector type of '" + type.getName() + "'"); } return factory.createConnector(id); }); if (!type.isAssignableFrom(connector.getClass())) { throw new LmRuntimeException("Connector for id '" + id + "' is not a " + type.getName() + ". The actual type is " + connector.getClass().getName()); } return (T) connector; } ConnectorService(
            @Inject Map<String, IConnectorFactory> factories,
            @Inject Map<String, Connector> connectors); @SuppressWarnings("unchecked") @Override T getConnector(Class<T> type, String id);  }
@Test public void testGetConnector() { assertSame(connectorA, connectorService.getConnector(JdbcConnector.class, "a")); assertSame(connectorB, connectorService.getConnector(JdbcConnector.class, "b")); assertSame(connectorA, connectorService.getConnector(JdbcConnector.class, "a")); assertSame(connectorB, connectorService.getConnector(JdbcConnector.class, "b")); }