-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
405 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/com/tngtech/configbuilder/annotation/typetransformer/TypeTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,61 @@ | ||
package com.tngtech.configbuilder.annotation.typetransformer; | ||
|
||
import com.tngtech.configbuilder.configuration.ErrorMessageSetup; | ||
import com.tngtech.configbuilder.util.GenericsAndCastingHelper; | ||
import com.tngtech.configbuilder.util.ConfigBuilderFactory; | ||
import com.tngtech.configbuilder.util.FieldValueTransformer; | ||
|
||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.ArrayList; | ||
|
||
/** | ||
* Implementations of this interface transform an object into a different type of object | ||
* @param <SourceClass> the type of the parameter before the transformation | ||
* @param <TargetClass> return type | ||
*/ | ||
public abstract class TypeTransformer<SourceClass, TargetClass> { | ||
|
||
protected Type targetType; | ||
protected FieldValueTransformer fieldValueTransformer; | ||
protected GenericsAndCastingHelper genericsAndCastingHelper; | ||
protected ErrorMessageSetup errorMessageSetup; | ||
protected ArrayList<Class> availableTransformers; | ||
|
||
public abstract TargetClass transform(SourceClass argument); | ||
|
||
public void initialize(FieldValueTransformer fieldValueTransformer, ConfigBuilderFactory configBuilderFactory, Type targetType, ArrayList<Class> availableTransformers) { | ||
|
||
this.targetType = targetType; | ||
this.fieldValueTransformer = fieldValueTransformer; | ||
this.genericsAndCastingHelper = configBuilderFactory.getInstance(GenericsAndCastingHelper.class); | ||
this.errorMessageSetup = configBuilderFactory.getInstance(ErrorMessageSetup.class); | ||
this.availableTransformers = availableTransformers; | ||
} | ||
|
||
public boolean isMatching(Class<?> sourceClass, Class<?> targetClass) { | ||
Class<?> transformerSourceClass = getTransformerSourceClass(); | ||
Class<?> transformerTargetClass = getTransformerTargetClass(); | ||
if(transformerSourceClass.isAssignableFrom(sourceClass) && targetClass.isAssignableFrom(transformerTargetClass)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
protected Class<?> getTransformerSourceClass() { | ||
Type typeOfInterface = this.getClass().getGenericSuperclass(); | ||
Type[] genericTypes = ((ParameterizedType) typeOfInterface).getActualTypeArguments(); | ||
return genericsAndCastingHelper.castTypeToClass(genericTypes[0]); | ||
} | ||
|
||
protected Class<?> getTransformerTargetClass() { | ||
Type typeOfInterface = this.getClass().getGenericSuperclass(); | ||
Type[] genericTypes = ((ParameterizedType) typeOfInterface).getActualTypeArguments(); | ||
return genericsAndCastingHelper.castTypeToClass(genericTypes[1]); | ||
} | ||
|
||
|
||
public void setGenericsAndCastingHelper(GenericsAndCastingHelper genericsAndCastingHelper) { | ||
this.genericsAndCastingHelper = genericsAndCastingHelper; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 43 additions & 2 deletions
45
.../java/com/tngtech/configbuilder/annotation/typetransformer/CollectionTransformerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 1,62 @@ | ||
package com.tngtech.configbuilder.annotation.typetransformer; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.google.common.collect.Sets; | ||
import com.tngtech.configbuilder.configuration.ErrorMessageSetup; | ||
import com.tngtech.configbuilder.util.ConfigBuilderFactory; | ||
import com.tngtech.configbuilder.util.FieldValueTransformer; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Set; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class CollectionTransformerTest { | ||
|
||
private CollectionTransformer collectionTransformer; | ||
|
||
@Mock | ||
private ParameterizedType type; | ||
@Mock | ||
private FieldValueTransformer fieldValueTransformer; | ||
@Mock | ||
private ConfigBuilderFactory configBuilderFactory; | ||
@Mock | ||
private ErrorMessageSetup errorMessageSetup; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
when(configBuilderFactory.getInstance(ErrorMessageSetup.class)).thenReturn(errorMessageSetup); | ||
|
||
collectionTransformer = new CollectionTransformer(); | ||
collectionTransformer.initialize(fieldValueTransformer, configBuilderFactory, type, Lists.newArrayList(new Class[]{StringOrPrimitiveToPrimitiveTransformer.class})); | ||
} | ||
|
||
@Test | ||
public void testTransform() throws Exception { | ||
|
||
Set<Integer> input = Sets.newHashSet(1,2,3); | ||
when(type.getActualTypeArguments()).thenReturn(new Class[]{Double.class}); | ||
when(fieldValueTransformer.performNecessaryTransformations(1, Double.class, Lists.newArrayList(new Class[]{StringOrPrimitiveToPrimitiveTransformer.class}))).thenReturn(1.0); | ||
when(fieldValueTransformer.performNecessaryTransformations(2, Double.class, Lists.newArrayList(new Class[]{StringOrPrimitiveToPrimitiveTransformer.class}))).thenReturn(2.0); | ||
when(fieldValueTransformer.performNecessaryTransformations(3, Double.class, Lists.newArrayList(new Class[]{StringOrPrimitiveToPrimitiveTransformer.class}))).thenReturn(3.0); | ||
assertEquals(Lists.newArrayList(1.0,2.0,3.0), collectionTransformer.transform(input)); | ||
} | ||
|
||
@Test | ||
public void testIsMatching() throws Exception { | ||
|
||
assertTrue(collectionTransformer.isMatching(Collection.class, ArrayList.class)); | ||
assertFalse(collectionTransformer.isMatching(Collection.class, Double.class)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.