Last active
August 29, 2015 14:24
-
-
Save fbencosme/5b516a110dbb60be632e to your computer and use it in GitHub Desktop.
using java 8 options and streams vs list and null checking v2
This file contains hidden or 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
public String cookie(String name) { | |
Cookie[] cookies = raw.getCookies(); | |
if (cookies != null) { | |
for (Cookie cookie : cookies) { | |
if (cookie.getName().equals(name)) { | |
return cookie.getValue(); | |
} | |
} | |
} | |
return null; | |
} | |
public <T> T bindParams(Class<T> clazz) { | |
T returnObject = null; | |
try { | |
Method[] methods = clazz.getDeclaredMethods(); | |
if (Modifier.isStatic(clazz.getModifiers())) { | |
try { | |
Constructor<T> ctr = clazz.getDeclaredConstructor(); | |
ctr.setAccessible(true); | |
returnObject = (T) ctr.newInstance(); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
return null; | |
} | |
} else { | |
returnObject = (T) clazz.newInstance(); | |
} | |
for (Method m : methods) { | |
if (m.getName().startsWith("set")) { | |
String prop = m.getName().substring(3); | |
char[] stringArray = prop.trim().toCharArray(); | |
stringArray[0] = Character.toLowerCase(stringArray[0]); | |
prop = new String(stringArray); | |
String value = raw.getParameter(prop); | |
if (value != null) { | |
Class<?> t = m.getParameterTypes()[0]; | |
if (t.isAssignableFrom(boolean.class)) { | |
m.invoke(returnObject, Boolean.valueOf(value)); | |
} else if (t.isAssignableFrom(byte.class)) { | |
m.invoke(returnObject, Byte.valueOf(value)); | |
} else if (t.isAssignableFrom(char.class)) { | |
m.invoke(returnObject, value.charAt(0)); | |
} else if (t.isAssignableFrom(short.class)) { | |
m.invoke(returnObject, Short.valueOf(value)); | |
} else if (t.isAssignableFrom(int.class)) { | |
m.invoke(returnObject, Integer.valueOf(value)); | |
} else if (t.isAssignableFrom(long.class)) { | |
m.invoke(returnObject, Long.valueOf(value)); | |
} else if (t.isAssignableFrom(float.class)) { | |
m.invoke(returnObject, Float.valueOf(value)); | |
} else if (t.isAssignableFrom(double.class)) { | |
m.invoke(returnObject, Double.valueOf(value)); | |
} else if (t.isAssignableFrom(BigInteger.class)) { | |
m.invoke(returnObject, new BigInteger(value)); | |
} else if (t.isAssignableFrom(BigDecimal.class)) { | |
m.invoke(returnObject, new BigDecimal(value)); | |
} else if (t.isAssignableFrom(String.class)) { | |
m.invoke(returnObject, value); | |
} | |
} | |
} | |
} | |
} catch (InstantiationException | IllegalAccessException | |
| IllegalArgumentException | InvocationTargetException e) { | |
e.printStackTrace(); | |
} | |
return returnObject; | |
} | |
/////////////////// | |
//VERSUS | |
//////////////////// | |
public Optional<String> cookie(String name) { | |
final Optional<Cookie[]> cookies = Optional.ofNullable(raw.getCookies()); | |
if (cookies.isPresent()) { | |
return Arrays.stream(cookies.get()) | |
.filter(cookie -> cookie.getName().equals(name)) | |
.map(cookie -> cookie.getValue()) | |
.findFirst(); | |
} | |
return Optional.empty(); | |
} | |
public <T> Optional<T> bindParams(final Class<T> clazz) { | |
final Optional<T> bindObject = bindInstance(clazz); | |
bindObject.ifPresent(bo -> { | |
Arrays.stream(clazz.getDeclaredMethods()) | |
.filter (m -> m.getName().startsWith("set")) | |
.forEach(m -> { | |
String prop = m.getName().substring(2).toLowerCase(); | |
Optional<String> value = Optional.ofNullable(raw.getParameter(prop)); | |
if (value.isPresent()) | |
assignables | |
.stream() | |
.parallel() | |
.filter(c -> m.getParameterTypes()[0].isAssignableFrom(c)) | |
.findFirst() | |
.ifPresent(c -> { | |
try { m.invoke(bo, c.cast(value));} | |
catch (Exception e) { e.printStackTrace(); } | |
}); | |
}); | |
}); | |
return bindObject; | |
} | |
<T> Optional<T> bindInstance(final Class<T> clazz) { | |
T newInstance = null; | |
try { | |
if (Modifier.isStatic(clazz.getModifiers())) { | |
Constructor<T> ctr = clazz.getDeclaredConstructor(); | |
ctr.setAccessible(true); | |
newInstance = (T) ctr.newInstance(); | |
} else { | |
newInstance = (T) clazz.newInstance(); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return Optional.ofNullable(newInstance); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment