private static Object mapValue(Object v) { if ((v == null) || v instanceof Number || v instanceof Boolean || v instanceof Character || v instanceof String || v instanceof DTO) { return v; } if (v instanceof Map) { Map<?, ?> m = (Map<?, ?>) v; Map<Object, Object> map = newMap(m.size()); for (Map.Entry<?, ?> e : m.entrySet()) { map.put(mapValue(e.getKey()), mapValue(e.getValue())); } return map; } if (v instanceof List) { List<?> c = (List<?>) v; List<Object> list = newList(c.size()); for (Object o : c) { list.add(mapValue(o)); } return list; } if (v instanceof Set) { Set<?> c = (Set<?>) v; Set<Object> set = newSet(c.size()); for (Object o : c) { set.add(mapValue(o)); } return set; } if (v.getClass().isArray()) { final int length = Array.getLength(v); final Class<?> componentType = mapComponentType(v.getClass().getComponentType()); Object array = Array.newInstance(componentType, length); for (int i = 0; i < length; i++) { Array.set(array, i, mapValue(Array.get(v, i))); } return array; } return String.valueOf(v); }
/** * Assumes the input map always has String keys and the values are of types: String, Version, * Long, Double or List of the previous types. Lists are copied and Version objects are converted * to String objects. */ private static Map<String, Object> newAttributesMapDTO(Map<String, Object> map) { Map<String, Object> dto = new HashMap<String, Object>(map); /* Lists are copied and Version objects are converted to String objects. */ for (Map.Entry<String, Object> entry : dto.entrySet()) { Object value = entry.getValue(); if (value instanceof Version) { entry.setValue(String.valueOf(value)); continue; } if (value instanceof List) { List<Object> newList = new ArrayList<Object>((List<?>) value); for (ListIterator<Object> iter = newList.listIterator(); iter.hasNext(); ) { Object element = iter.next(); if (element instanceof Version) { iter.set(String.valueOf(element)); } } entry.setValue(newList); continue; } } return dto; }