Created
April 1, 2016 13:03
-
-
Save aweiland/28ad71a48097421ebfd9cfc4600c3340 to your computer and use it in GitHub Desktop.
RethinkDB helper
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
import com.fasterxml.jackson.databind.DeserializationFeature; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.rethinkdb.net.Cursor; | |
import org.apache.commons.beanutils.BeanMap; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.util.*; | |
import java.util.stream.Collectors; | |
public final class RethinkHelper { | |
private static final Logger LOGGER = LoggerFactory.getLogger(RethinkHelper.class); | |
private static final ObjectMapper objectMapper; | |
static { | |
objectMapper = new ObjectMapper(); | |
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | |
// Object mapper stuff here | |
} | |
private RethinkHelper() {} | |
/** | |
* Turn a cursor into a collection of models | |
* @param cursor | |
* @param target | |
* @param <U> | |
* @return | |
*/ | |
public static <U> List<U> cursorToCollection(Cursor<HashMap> cursor, Class<U> target) { | |
List<U> models = new ArrayList<>(); | |
while (cursor.hasNext()) { | |
Optional<U> maybe = mapToObject(cursor.next(), target); | |
maybe.ifPresent(models::add); | |
} | |
return models; | |
} | |
/** | |
* Helper for Rethink hashmap to Object. Need to test nested objects. | |
* @param source | |
* @param target | |
* @param <U> | |
* @return | |
*/ | |
public static <U> Optional<U> mapToObject(Map<String, Object> source, Class<U> target) { | |
try { | |
return Optional.of(objectMapper.convertValue(source, target)); | |
} catch (IllegalArgumentException e) { | |
LOGGER.error("Failed Map to Object [{}]", target.getSimpleName()); | |
LOGGER.error("Details", e); | |
return Optional.empty(); | |
} | |
} | |
/** | |
* Helper for Rethink. Object to hash. | |
* @param source | |
* @return | |
*/ | |
public static Optional<Map<String, Object>> objectToHash(Object source) { | |
try { | |
return Optional.of(objectMapper.convertValue(source, Map.class)); | |
} catch (IllegalArgumentException e) { | |
LOGGER.error("Details", e); | |
return Optional.empty(); | |
} | |
} | |
/** | |
* Helper for Rethink. Object to hash. | |
* @param source | |
* @return | |
*/ | |
public static Optional<Map<String, Object>> objectToHash2(Object source) { | |
return Optional.of(objectToHashRecur(source)); | |
} | |
private static Map<String, Object> objectToHashRecur(Object source) { | |
Map<Object, Object> start = new BeanMap(source); | |
// First fix the keys to be strings | |
Map<String, Object> map = start.entrySet().stream() | |
.filter(objectObjectEntry -> !objectObjectEntry.getKey().toString().equals("class")) | |
.collect(HashMap::new, | |
(m, i) -> m.put(i.getKey().toString(), i.getValue()), | |
HashMap::putAll); | |
// Deal with collections within the map | |
return map.entrySet().stream() | |
.map(o -> { | |
if (o.getValue() instanceof Collection<?>) { | |
o.setValue(((Collection<?>)o.getValue()) | |
.stream() | |
.map(o1 -> objectToHashRecur(o1)).collect(Collectors.toList())); | |
} | |
return o; | |
}) | |
.collect(HashMap::new, | |
(m, i) -> m.put(i.getKey(), i.getValue()), | |
HashMap::putAll); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment