|
| 1 | +package restx.factory; |
| 2 | + |
| 3 | +import com.google.common.base.Function; |
| 4 | +import com.google.common.collect.ImmutableSet; |
| 5 | +import com.google.common.collect.Iterables; |
| 6 | +import com.google.common.collect.Lists; |
| 7 | +import com.google.common.collect.Sets; |
| 8 | + |
| 9 | +import java.util.Set; |
| 10 | + |
| 11 | +public class DeactivationFactoryMachine implements FactoryMachine { |
| 12 | + |
| 13 | + public static DeactivationFactoryMachine forNames(Iterable<Name<?>> names) { |
| 14 | + return new DeactivationFactoryMachine(names); |
| 15 | + } |
| 16 | + |
| 17 | + public static DeactivationFactoryMachine forNames(Name<?>... names) { |
| 18 | + return new DeactivationFactoryMachine(Lists.newArrayList(names)); |
| 19 | + } |
| 20 | + |
| 21 | + private final ImmutableSet<String> keys; |
| 22 | + |
| 23 | + public DeactivationFactoryMachine(Iterable<Name<?>> keys) { |
| 24 | + this.keys = ImmutableSet.copyOf(Iterables.transform(keys, new Function<Name<?>, String>() { |
| 25 | + @Override |
| 26 | + public String apply(Name<?> input) { |
| 27 | + return Factory.activationKey(input.getClazz(), input.getName()); |
| 28 | + } |
| 29 | + })); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public boolean canBuild(Name<?> name) { |
| 34 | + return name.getClazz() == String.class && keys.contains(name.getName()); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public <T> MachineEngine<T> getEngine(Name<T> name) { |
| 39 | + return new NoDepsMachineEngine<T>(name, priority(), BoundlessComponentBox.FACTORY) { |
| 40 | + @Override |
| 41 | + protected T doNewComponent(SatisfiedBOM satisfiedBOM) { |
| 42 | + return (T) "false"; |
| 43 | + } |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) { |
| 49 | + if (componentClass != String.class) { |
| 50 | + return ImmutableSet.of(); |
| 51 | + } |
| 52 | + return Sets.newLinkedHashSet(Iterables.transform(keys, new Function<String, Name<T>>() { |
| 53 | + @Override |
| 54 | + public Name<T> apply(String input) { |
| 55 | + return (Name<T>) Name.of(String.class, input); |
| 56 | + } |
| 57 | + })); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public int priority() { |
| 62 | + return -10000; |
| 63 | + } |
| 64 | +} |
0 commit comments