private Factory.Query<RestxConfig> restxConfigQuery = Factory.Query.byClass(RestxConfig.class).mandatory();
@Override
protected FactoryMachine doNewComponent(SatisfiedBOM satisfiedBOM) {
final RestxConfig config = satisfiedBOM.getOneAsComponent(restxConfigQuery).get();
return new FactoryMachine() {
@Override
public boolean canBuild(Name<?> name) {
Optional<ConfigElement> configElement = config.getElement(name.getName());
return configElement.isPresent()
&& (name.getClazz() == ConfigElement.class
|| (name.getClazz() == String.class
&& !Strings.isNullOrEmpty(configElement.get().getValue()))
);
}
@Override
public <T> MachineEngine<T> getEngine(final Name<T> name) {
return new NoDepsMachineEngine<T>(name, BoundlessComponentBox.FACTORY) {
@Override
@SuppressWarnings("unchecked")
protected T doNewComponent(SatisfiedBOM satisfiedBOM) {
if (name.getClazz() == String.class) {
return (T) config.getString(name.getName()).get();
}
if (name.getClazz() == ConfigElement.class) {
return (T) config.getElement(name.getName()).get();
}
throw new IllegalArgumentException("can't satisfy name " + name);
}
};
}
@Override
@SuppressWarnings("unchecked")
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
if (String.class == componentClass) {
return (Set) Sets.newHashSet(Iterables.transform(config.elements(),
new Function<ConfigElement, Name<String>>() {
@Override
public Name<String> apply(ConfigElement input) {
return Name.of(String.class, input.getKey());
}
}));
} else if (ConfigElement.class == componentClass) {
return (Set) Sets.newHashSet(Iterables.transform(config.elements(),
new Function<ConfigElement, Name<ConfigElement>>() {
@Override
public Name<ConfigElement> apply(ConfigElement input) {
return Name.of(ConfigElement.class, input.getKey());
}