-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from scm-spain/fix/parameter_resolver
Formating param values error
- Loading branch information
Showing
10 changed files
with
120 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,4 @@ gradle-app.setting | |
|
||
#Ignore .gradle dir | ||
.gradle | ||
build_release.gradle |
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
Binary file not shown.
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,6 +1,6 @@ | ||
#Sun Sep 06 02:21:04 CEST 2015 | ||
#Thu Sep 10 20:42:37 CEST 2015 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.6-bin.zip |
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: 28 additions & 17 deletions
45
src/main/java/scmspain/karyon/restrouter/core/ParamTypeResolver.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,28 +1,39 @@ | ||
package scmspain.karyon.restrouter.core; | ||
|
||
import com.google.inject.Singleton; | ||
import scmspain.karyon.restrouter.exception.UnsupportedFormatException; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
@Singleton | ||
public class ParamTypeResolver<T> { | ||
|
||
private Map<Class, Function<String, T>> resolver = new HashMap<Class, Function<String, T>>() {{ | ||
put(int.class, (val) -> (T) Integer.valueOf(val)); | ||
put(double.class, (val) -> (T) Double.valueOf(val)); | ||
put(boolean.class, (val) -> (T) Boolean.valueOf(val)); | ||
put(long.class, (val) -> (T) Long.valueOf(val)); | ||
put(short.class, (val) -> (T) Short.valueOf(val)); | ||
put(float.class, (val) -> (T) Float.valueOf(val)); | ||
|
||
put(String.class, (val) -> (T) val); | ||
put(Integer.class, (val) -> (T) Integer.valueOf(val)); | ||
put(Double.class, (val) -> (T) Double.valueOf(val)); | ||
put(Boolean.class, (val) -> (T) Boolean.valueOf(val)); | ||
put(Long.class, (val) -> (T) Long.valueOf(val)); | ||
put(Short.class, (val) -> (T) Short.valueOf(val)); | ||
put(Float.class, (val) -> (T) Float.valueOf(val)); | ||
}}; | ||
|
||
public T resolveValueType(Class<T> classType, String value) throws UnsupportedFormatException { | ||
|
||
public T resolveValueType(Class<T> classType, String value){ | ||
if (classType.isAssignableFrom(String.class)) { | ||
return (T)value; | ||
} else if (classType.isAssignableFrom(int.class) || classType.isAssignableFrom(Integer.class)) { | ||
return (T)Integer.valueOf(value); | ||
} else if (classType.isAssignableFrom(double.class) || classType.isAssignableFrom(Double.class)) { | ||
return (T)Double.valueOf(value); | ||
} else if (classType.isAssignableFrom(boolean.class) || classType.isAssignableFrom(Boolean.class)) { | ||
return (T)Boolean.valueOf(value); | ||
} else if (classType.isAssignableFrom(long.class) || classType.isAssignableFrom(Long.class)) { | ||
return (T)Boolean.valueOf(value); | ||
} else if (classType.isAssignableFrom(short.class) || classType.isAssignableFrom(Short.class)) { | ||
return (T)Boolean.valueOf(value); | ||
} else if (classType.isAssignableFrom(float.class) || classType.isAssignableFrom(Float.class)) { | ||
return (T)Boolean.valueOf(value); | ||
} | ||
return null; | ||
return Optional | ||
.ofNullable(resolver.get(classType)) | ||
.map(casting -> casting.apply(value)) | ||
.orElseThrow(() -> new UnsupportedFormatException(classType.getName())); | ||
} | ||
|
||
} |
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
8 changes: 8 additions & 0 deletions
8
src/main/java/scmspain/karyon/restrouter/exception/UnsupportedFormatException.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,8 @@ | ||
package scmspain.karyon.restrouter.exception; | ||
|
||
|
||
public class UnsupportedFormatException extends Exception { | ||
public UnsupportedFormatException(String classTryingToFormat) { | ||
super(String.format("Class \"%s\" is not supported.", classTryingToFormat )); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/test/java/scmspain/karyon/restrouter/core/ParamTypeResolverTest.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 scmspain.karyon.restrouter.core; | ||
|
||
import com.tngtech.java.junit.dataprovider.DataProvider; | ||
import com.tngtech.java.junit.dataprovider.DataProviderRunner; | ||
import com.tngtech.java.junit.dataprovider.UseDataProvider; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import scmspain.karyon.restrouter.exception.UnsupportedFormatException; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
@RunWith(DataProviderRunner.class) | ||
public class ParamTypeResolverTest<T> { | ||
|
||
ParamTypeResolver<T> parameterResolver; | ||
|
||
@Before | ||
public void setUp() { | ||
parameterResolver = new ParamTypeResolver<>(); | ||
} | ||
|
||
@DataProvider | ||
public static Object[][] dataProviderAdd() { | ||
// @formatter:off | ||
return new Object[][] { | ||
{ String.class, "lorem ipsum", String.class }, | ||
{ Boolean.class, "true", Boolean.class }, | ||
{ Long.class, "27", Long.class }, | ||
{ Integer.class, "42", Integer.class }, | ||
{ Short.class, "2", Short.class }, | ||
{ Float.class, "27.42", Float.class }, | ||
{ Double.class, "27.42", Double.class }, | ||
|
||
{ boolean.class, "true", Boolean.class }, | ||
{ long.class, "27", Long.class }, | ||
{ int.class, "42", Integer.class }, | ||
{ short.class, "2", Short.class }, | ||
{ float.class, "27.42", Float.class }, | ||
{ double.class, "42.42", Double.class }, | ||
}; | ||
// @formatter:on | ||
} | ||
|
||
|
||
@Test | ||
@UseDataProvider("dataProviderAdd") | ||
public void itShouldResolveValueType( | ||
Class<T> type, | ||
String value, | ||
Class<T> expectedClass | ||
) throws Exception { | ||
assertEquals(expectedClass, parameterResolver.resolveValueType(type, value).getClass()); | ||
} | ||
|
||
@Test(expected = UnsupportedFormatException.class) | ||
public void itShouldReturnsErrorIfCanNotResolveValueType() throws Exception { | ||
Class anyUnsupportedClass = Byte.class; | ||
parameterResolver.resolveValueType(anyUnsupportedClass, "byte"); | ||
} | ||
} |