Created
July 14, 2016 09:06
-
-
Save rkdgusrnrlrl/98c988cc702a31aeee274ca5e6162e38 to your computer and use it in GitHub Desktop.
VO 빌더
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
public class Builder<T> { | |
private T t; | |
private Map<String, String> map; | |
private MappingRule rule; | |
private static final String SET = "set"; | |
public Builder(T clz) { | |
map = new HashMap<String, String>(); | |
this.t = clz; | |
this.rule = new DefultMappingRule(); | |
} | |
public Builder(T clz, MappingRule rule) { | |
map = new HashMap<String, String>(); | |
this.t = clz; | |
this.rule = rule; | |
} | |
public Builder<T> set(String key, String value) { | |
map.put(key, value); | |
return this; | |
} | |
public T build() { | |
setFieldFromMap(map, t, rule); | |
return t; | |
} | |
/** | |
* 맵으로 받은 파리미터 값을 VO 맴머 필드에 set 해줌 | |
* @param paramMap | |
* @param vo | |
* @throws IllegalAccessException | |
* @throws InvocationTargetException | |
*/ | |
public void setFieldFromMap(Map<String, String> paramMap, Object vo, MappingRule func) { | |
Class<?> c = vo.getClass(); | |
Field[] fields = c.getDeclaredFields(); | |
Method[] methods = c.getMethods(); | |
for (Method method:methods) { | |
String name = method.getName(); | |
if (name.startsWith(SET)) { | |
String key = func.getKeyFromMethodName(SET, name); | |
//assertEquals("", key); | |
try { | |
method.invoke(vo, paramMap.get(key)); | |
} catch (IllegalAccessException e) { | |
} catch (IllegalArgumentException e) { | |
} catch (InvocationTargetException e) { | |
} | |
} | |
} | |
} | |
} |
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
import static com.google.common.base.CaseFormat.*; | |
//guava 사용함 | |
public interface MappingRule { | |
String getKeyFromMethodName(String setOrGet, String name); | |
} | |
class FormUpperCamelToLowerUnder implements MappingRule{ | |
@Override | |
public String getKeyFromMethodName(String setOrGet, String name) { | |
String fieldName = name.replaceAll(setOrGet, ""); | |
String key = UPPER_CAMEL.to(LOWER_UNDERSCORE, fieldName); | |
return key; | |
} | |
} | |
class DefultMappingRule implements MappingRule{ | |
@Override | |
public String getKeyFromMethodName(String setOrGet, String methodName) { | |
String fieldName = methodName.replaceAll(setOrGet, ""); | |
String key = UPPER_CAMEL.to(LOWER_CAMEL, fieldName); | |
return key; | |
} | |
} |
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
public class VO { | |
public static <T> Builder<T> builder(T t){ | |
return new Builder<T>(t); | |
} | |
public static <T> Builder<T> builder(T t, MappingRule rule){ | |
return new Builder<T>(t, rule); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment