/** Retrieves a component by name and ensures the returned component is * initialized. * @return the initialized component, never returns null. * @param name is the name of the component. * @throws RuntimeException if the type parameter is null. * @throws RuntimeException if the component can not be found in the brewery */ public synchronized Object get(String name) { Object component = getOpt(name); if (component!=null) { return component; } throw new RuntimeException("Unknown component name: '"+name+"' \n"+toString()); }
/** Retrieves an optional component based on the given class and if it's found, * the brewery ensures the returned component is initialized. * If the component is not found, null is returned. * Lookup by type should only be done for classes and interfaces * of which you know there will only be one in the whole brewery. * Use lookup by name otherwise. * @return the initialized component or null if it is not found. * @param type is the class or interface of the requested component. Subclasses * of the given class or implementations of the given class will * be found as well. * @throws RuntimeException if the type parameter is null. */ @SuppressWarnings("unchecked") public <T> T getOpt(Class<T> type) { Exceptions.checkNotNullParameter(type, "type"); return (T) getOpt(type.getName()); }
public WorkflowParser(Configuration configuration) { this.configuration = configuration; this.path = new LinkedList<>(); this.contextStack = new Stack<>(); this.issues = new ParseIssues(); // this cast is necessary to get the workflow parse listener optional // because the brewery.getOpt method is not available on the configuration itself if (configuration instanceof DefaultConfiguration) { DefaultConfiguration defaultConfiguration = (DefaultConfiguration)configuration; Brewery brewery = defaultConfiguration.getBrewery(); this.workflowParseListener = brewery.getOpt(WorkflowParseListener.class); } }
@Test public void testOptionalUnavailable() { Brewery brewery = new Brewery(); assertNull(brewery.getOpt("unavailable")); } }