Last active
July 6, 2021 07:15
-
-
Save reikop/282ad29b01ffc993d50acdcf3c19b2d7 to your computer and use it in GitHub Desktop.
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 java.lang.reflect.InvocationTargetException; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class QuerydslUtils { | |
/** | |
* Tuple을 map으로 변환 | |
* @param keys | |
* @param tuples | |
* @return | |
*/ | |
public static List<Map<String,Object>> toMap(String[] keys, List<Tuple> tuples) { | |
List<Map<String,Object>> result = new ArrayList<Map<String, Object>>(); | |
for (Tuple t : tuples) { | |
HashMap<String,Object> hashMap = new HashMap<String, Object>(); | |
Object[] data = t.toArray(); | |
extract(keys, result, hashMap, data); | |
} | |
return result; | |
} | |
/** | |
* List<Object[]>를 map으로 변환 | |
* @param keys | |
* @param result | |
* @return | |
*/ | |
public static List<Map<String, Object>> toMapFromObject(String[] keys, List<Object[]> result) { | |
List<Map<String,Object>> list = new ArrayList<Map<String, Object>>(); | |
for (Object[] data : result) { | |
HashMap<String,Object> hashMap = new HashMap<String, Object>(); | |
extract(keys, list, hashMap, data); | |
} | |
return list; | |
} | |
public static <T> List<T> convertFromObject(Class<T> clazz, String[] keys, List<Object[]> objects) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, InvocationTargetException { | |
List<T> result = new ArrayList<>(); | |
for (Object[] data : objects) { | |
T instance = clazz.newInstance(); | |
for (int i = 0; i < keys.length; i++) { | |
String key = keys[i].trim(); | |
Object value = data[i]; | |
PropertyUtils.setNestedProperty(instance, key, value); | |
} | |
result.add(instance); | |
} | |
return result; | |
} | |
public static <T> List<T> convertFromTuple(Class<T> clazz, String[] keys, List<Tuple> tuples) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { | |
List<T> result = new ArrayList<>(); | |
for (Tuple t : tuples) { | |
T instance = clazz.newInstance(); | |
Object[] data = t.toArray(); | |
for (int i = 0; i < keys.length; i++) { | |
String key = keys[i].trim(); | |
Object value = data[i]; | |
PropertyUtils.setNestedProperty(instance, key, value); | |
} | |
result.add(instance); | |
} | |
return result; | |
} | |
private static void extract(String[] keys, List<Map<String, Object>> result, HashMap<String, Object> hashMap, Object[] data) { | |
for (int i = 0; i < keys.length; i++) { | |
String key = keys[i].trim(); | |
if (i < data.length) { | |
hashMap.put(key, data[i]); | |
} else { | |
hashMap.put(key, null); | |
} | |
} | |
result.add(hashMap); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment