Permalink
Browse files

breaking: FactoryMachine.nameBuildableComponents use Type

This commit is preparing the field to be able to request the factory using types.
As names are from now on a couple (String, Type), factory machines might be requested
using types instead of classes.

Migration has to be done in all sub-classes of FactoryMachine which are overriding the
nameBuildableComponents method.
There is several cases:
- Either in the method you are using the `componentClass` parameter to
do some equality assertions with other classes like that:
```java
if (componentClass == SomeClass.class) { ... }
```
In that case the only thing to do is to change the type of the parameter from `Class<T>` to Type,
it should work directly as Type is a super type of Class (you might also refactor the parameter name
from componentClass to componentType, but this is up to you).
- Or in the method you are using the `componentClass` parameter to compare it to other classes using
`Class.isassignableFrom` method (in either way, `componentClass.isAssignableFrom(SomeClass.class)` or
`SomeClass.class.isAssignableFrom(componentClass)`). In that case you have to replace the use of
`Class.isAssignableFrom` to the static method `Types.isAssignableFrom`.
- Or last but not least, you are doing some comparisons using `isAssignableFrom` like in the second point,
but not on direct classes, but on classes of names, using `Name.getClazz()`. So you have to rely on names
types instead of names classes, and do a migration like this:
```java
componentClass.isAssignableFrom(aName.getClazz())
```
becomes
```java
Types.isAssignableFrom(componentType, aName.getType())
```
  • Loading branch information...
a-peyrard committed Apr 12, 2015
1 parent 460715b commit ceb7a5bfa3b2427574256c46ea1a5bd664e3ec7b
@@ -6,6 +6,7 @@
import com.google.common.base.Optional;
import com.google.common.collect.Iterables;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -98,8 +99,8 @@ public BillOfMaterials getBillOfMaterial() {
@Override
@SuppressWarnings("unchecked")
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
if (componentClass == RestxConfig.class) {
public <T> Set<Name<T>> nameBuildableComponents(Type componentType) {
if (componentType == RestxConfig.class) {
return Collections.singleton(Name.of((Class<T>) RestxConfig.class));
} else {
return Collections.emptySet();
@@ -6,6 +6,7 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Set;
import restx.common.ConfigElement;
@@ -69,16 +70,16 @@ protected T doNewComponent(SatisfiedBOM satisfiedBOM) {
@Override
@SuppressWarnings("unchecked")
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
if (String.class == componentClass) {
public <T> Set<Name<T>> nameBuildableComponents(Type componentType) {
if (String.class == componentType) {
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) {
} else if (ConfigElement.class == componentType) {
return (Set) Sets.newHashSet(Iterables.transform(config.elements(),
new Function<ConfigElement, Name<ConfigElement>>() {
@Override
@@ -6,6 +6,7 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import java.lang.reflect.Type;
import java.util.Set;
public class DeactivationFactoryMachine implements FactoryMachine {
@@ -45,8 +46,8 @@ protected T doNewComponent(SatisfiedBOM satisfiedBOM) {
}
@Override
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
if (componentClass != String.class) {
public <T> Set<Name<T>> nameBuildableComponents(Type componentType) {
if (componentType != String.class) {
return ImmutableSet.of();
}
return Sets.newLinkedHashSet(Iterables.transform(keys, new Function<String, Name<T>>() {
@@ -3,7 +3,9 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import java.lang.reflect.Type;
import java.util.Set;
import restx.common.Types;
/**
* User: xavierhanin
@@ -37,10 +39,10 @@ public boolean canBuild(Name<?> name) {
@Override
@SuppressWarnings("unchecked")
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
public <T> Set<Name<T>> nameBuildableComponents(Type componentType) {
Set<Name<T>> names = Sets.newHashSet();
for (Name<?> name : engines.keySet()) {
if (componentClass.isAssignableFrom(name.getClazz())) {
if (Types.isAssignableFrom(componentType, name.getType())) {
names.add((Name<T>) name);
}
}
@@ -1274,7 +1274,7 @@ private void satisfyBoms(Deque<BuildingBox<?>> dependencies) {
private <T> Set<Name<T>> nameBuildableComponents(FactoryMachine machine, Class<T> componentClass) {
Set<Name<T>> buildableComponents = new LinkedHashSet<>();
for (Name<T> tName : machine.nameBuildableComponents(componentClass)) {
for (Name<T> tName : machine.<T>nameBuildableComponents(componentClass)) {
if (checkActive(tName)) {
buildableComponents.add(tName);
}
@@ -1,5 +1,6 @@
package restx.factory;
import java.lang.reflect.Type;
import java.util.Set;
/**
@@ -10,6 +11,6 @@
public interface FactoryMachine {
boolean canBuild(Name<?> name);
<T> MachineEngine<T> getEngine(Name<T> name);
<T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass);
<T> Set<Name<T>> nameBuildableComponents(Type componentType);
int priority();
}
@@ -5,6 +5,7 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -108,8 +109,9 @@ public int priority() {
return original.getEngine(name);
}
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
return original.nameBuildableComponents(componentClass);
@Override
public <T> Set<Name<T>> nameBuildableComponents(Type componentType) {
return original.nameBuildableComponents(componentType);
}
@Override
@@ -1,5 +1,6 @@
package restx.factory;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Set;
@@ -23,7 +24,7 @@ public boolean canBuild(Name<?> name) {
}
@Override
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
public <T> Set<Name<T>> nameBuildableComponents(Type componentType) {
return Collections.emptySet();
}
@@ -1,7 +1,9 @@
package restx.factory;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Set;
import restx.common.Types;
/**
* User: xavierhanin
@@ -34,8 +36,8 @@ public boolean canBuild(Name<?> name) {
@Override
@SuppressWarnings("unchecked")
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
if (componentClass.isAssignableFrom(name.getClazz())) {
public <T> Set<Name<T>> nameBuildableComponents(Type componentType) {
if (Types.isAssignableFrom(componentType, name.getType())) {
return Collections.singleton((Name<T>) name);
} else {
return Collections.emptySet();
@@ -3,6 +3,7 @@
import com.google.common.base.Objects;
import com.google.common.base.Optional;
import java.lang.reflect.Type;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
@@ -30,8 +31,8 @@ protected T doNewComponent(SatisfiedBOM satisfiedBOM) {
@Override
@SuppressWarnings("unchecked")
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
if (componentClass != String.class) {
public <T> Set<Name<T>> nameBuildableComponents(Type componentType) {
if (componentType != String.class) {
return emptySet();
}
Set<Name<T>> names = new LinkedHashSet<>();
@@ -6,8 +6,10 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import java.lang.reflect.Type;
import java.util.LinkedHashSet;
import java.util.Set;
import restx.common.Types;
/**
* Date: 16/11/13
@@ -95,11 +97,11 @@ public String toString() {
@Override
@SuppressWarnings("unchecked")
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
public <T> Set<Name<T>> nameBuildableComponents(Type componentType) {
Set<Name<T>> names = new LinkedHashSet<>();
for (Warehouse provider : providers) {
for (Name<?> name : provider.listNames()) {
if (componentClass.isAssignableFrom(name.getClazz())) {
if (Types.isAssignableFrom(componentType, name.getType())) {
names.add((Name<T>) name);
}
}
@@ -3,6 +3,7 @@
import org.jongo.Jongo;
import restx.factory.*;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Set;
@@ -39,7 +40,7 @@ public BillOfMaterials getBillOfMaterial() {
}
@Override
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
public <T> Set<Name<T>> nameBuildableComponents(Type componentType) {
return Collections.emptySet();
}

0 comments on commit ceb7a5b

Please sign in to comment.