Example usage for com.amazonaws.services.dynamodbv2.model AttributeValue addMEntry

List of usage examples for com.amazonaws.services.dynamodbv2.model AttributeValue addMEntry

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.model AttributeValue addMEntry.

Prototype

public AttributeValue addMEntry(String key, AttributeValue value) 

Source Link

Usage

From source file:jp.classmethod.aws.dynamodb.DynamoDbInternalUtils.java

License:Open Source License

/**
 * Copied from DynamoDB Document SDK InternalUtils.java
 *
 * Converts a simple value into the low-level <code><AttributeValue/code>
 * representation.//from   ww  w.ja  v  a 2s. c o  m
 *
 * @param value
 *            the given value which can be one of the followings:
 * <ul>
 * <li>String</li>
 * <li>Set&lt;String></li>
 * <li>Number (including any subtypes and primitive types)</li>
 * <li>Set&lt;Number></li>
 * <li>byte[]</li>
 * <li>Set&lt;byte[]></li>
 * <li>ByteBuffer</li>
 * <li>Set&lt;ByteBuffer></li>
 * <li>Boolean or boolean</li>
 * <li>null</li>
 * <li>Map&lt;String,T>, where T can be any type on this list but must not
 * induce any circular reference</li>
 * <li>List&lt;T>, where T can be any type on this list but must not induce
 * any circular reference</li>
 * </ul>
 * @return a non-null low level representation of the input object value
 *
 * @throws UnsupportedOperationException
 *             if the input object type is not supported
 */
public static AttributeValue toAttributeValue(Object value) { //NOPMD
    AttributeValue result = new AttributeValue();
    if (value == null) {
        return result.withNULL(Boolean.TRUE);
    } else if (value instanceof Boolean) {
        return result.withBOOL((Boolean) value);
    } else if (value instanceof String) {
        return result.withS((String) value);
    } else if (value instanceof BigDecimal || value instanceof Number) {
        return result.withN(value instanceof Number ? value.toString() : ((BigDecimal) value).toPlainString());
    } else if (value instanceof byte[]) {
        return result.withB(ByteBuffer.wrap((byte[]) value));
    } else if (value instanceof ByteBuffer) {
        return result.withB((ByteBuffer) value);
    } else if (value instanceof Set) {
        // default to an empty string set if there is no element
        @SuppressWarnings("unchecked")
        Set<Object> set = (Set<Object>) value;
        if (set.isEmpty()) {
            result.setSS(new LinkedHashSet<>());
            return result;
        }
        Object element = set.iterator().next();
        if (element instanceof String) {
            @SuppressWarnings("unchecked")
            Set<String> ss = (Set<String>) value;
            result.setSS(new ArrayList<>(ss));
        } else if (element instanceof Number) {
            @SuppressWarnings("unchecked")
            Set<Number> in = (Set<Number>) value;
            List<String> out = new ArrayList<>(set.size());
            for (Number n : in) {
                BigDecimal bd = toBigDecimal(n);
                out.add(bd.toPlainString());
            }
            result.setNS(out);
        } else if (element instanceof byte[]) {
            @SuppressWarnings("unchecked")
            Set<byte[]> in = (Set<byte[]>) value;
            List<ByteBuffer> out = new ArrayList<>(set.size());
            for (byte[] buf : in) {
                out.add(ByteBuffer.wrap(buf));
            }
            result.setBS(out);
        } else if (element instanceof ByteBuffer) {
            @SuppressWarnings("unchecked")
            Set<ByteBuffer> bs = (Set<ByteBuffer>) value;
            result.setBS(bs);
        } else {
            throw new UnsupportedOperationException("element type: " + element.getClass());
        }
    } else if (value instanceof List) {
        @SuppressWarnings("unchecked")
        List<Object> in = (List<Object>) value;
        List<AttributeValue> out = new ArrayList<>();
        for (Object v : in) {
            out.add(toAttributeValue(v));
        }
        result.setL(out);
    } else if (value instanceof Map) {
        @SuppressWarnings("unchecked")
        Map<String, Object> in = (Map<String, Object>) value;
        if (false == in.isEmpty()) {
            for (Map.Entry<String, Object> e : in.entrySet()) {
                result.addMEntry(e.getKey(), toAttributeValue(e.getValue()));
            }
        } else { // empty map
            result.setM(new LinkedHashMap<>());
        }
    } else {
        throw new UnsupportedOperationException("value type: " + value.getClass());
    }
    return result;
}