Skip to content

Commit

Permalink
breaking: FactoryMachine.nameBuildableComponents use Type
Browse files Browse the repository at this point in the history
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 Jun 9, 2015
1 parent 460715b commit ceb7a5b
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 21 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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 {
Expand Down Expand Up @@ -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>>() {
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -37,10 +39,10 @@ public <T> MachineEngine<T> getEngine(Name<T> 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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion restx-factory/src/main/java/restx/factory/Factory.java
Expand Up @@ -1274,7 +1274,7 @@ private <T> BillOfMaterials getBillOfMaterialsFor(Name<T> name) {

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);
}
Expand Down
@@ -1,5 +1,6 @@
package restx.factory;

import java.lang.reflect.Type;
import java.util.Set;

/**
Expand All @@ -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();
}
Expand Up @@ -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;
Expand Down Expand Up @@ -108,8 +109,9 @@ public <T> MachineEngine<T> getEngine(Name<T> name) {
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
Expand Down
@@ -1,5 +1,6 @@
package restx.factory;

import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Set;

Expand All @@ -23,7 +24,7 @@ public <T> MachineEngine<T> getEngine(Name<T> name) {
}

@Override
public <T> Set<Name<T>> nameBuildableComponents(Class<T> componentClass) {
public <T> Set<Name<T>> nameBuildableComponents(Type componentType) {
return Collections.emptySet();
}

Expand Down
@@ -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
Expand Down Expand Up @@ -34,8 +36,8 @@ public <T> MachineEngine<T> getEngine(Name<T> 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();
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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<>();
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -95,11 +97,11 @@ private <T> Optional<NamedComponent<T>> findComponent(Name<T> 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 = 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);
}
}
Expand Down
Expand Up @@ -3,6 +3,7 @@
import org.jongo.Jongo;
import restx.factory.*;

import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Set;

Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit ceb7a5b

Please sign in to comment.