-
Notifications
You must be signed in to change notification settings - Fork 10
/
MultiInstantiator.java
32 lines (25 loc) · 995 Bytes
/
MultiInstantiator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package br.com.caelum.iogi;
import java.util.Collection;
import br.com.caelum.iogi.exceptions.InvalidTypeException;
import br.com.caelum.iogi.parameters.Parameters;
import br.com.caelum.iogi.reflection.Target;
public class MultiInstantiator implements Instantiator<Object> {
private final Collection<Instantiator<?>> instantiators;
public MultiInstantiator(final Collection<Instantiator<?>> instantiators) {
this.instantiators = instantiators;
}
public Object instantiate(final Target<?> target, final Parameters parameters) {
for (final Instantiator<?> instantiator : instantiators) {
if (instantiator.isAbleToInstantiate(target))
return instantiator.instantiate(target, parameters);
}
throw new InvalidTypeException("Cannot instantiate " + target);
}
public boolean isAbleToInstantiate(final Target<?> target) {
for (final Instantiator<?> instantiator : instantiators) {
if (instantiator.isAbleToInstantiate(target))
return true;
}
return false;
}
}