Skip to content

Commit 189179a

Browse files
committed
introduce DeactivationFactoryMachine to easily deactivate components
1 parent c0bb9e6 commit 189179a

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package restx.factory;
2+
3+
import org.junit.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
public class DeactivationFactoryMachineTest {
8+
@Test
9+
public void should_deactivate_components() throws Exception {
10+
Name<Integer> one = Name.of(Integer.class, "one");
11+
Name<Integer> two = Name.of(Integer.class, "two");
12+
Factory factory = Factory.builder()
13+
.addMachine(new SingletonFactoryMachine<>(0, new NamedComponent(one, 1)))
14+
.addMachine(new SingletonFactoryMachine<>(0, new NamedComponent(two, 2)))
15+
.addMachine(DeactivationFactoryMachine.forNames(one))
16+
.build();
17+
18+
assertThat(factory.getComponents(Integer.class)).containsExactly(2);
19+
}
20+
}

0 commit comments

Comments
 (0)