Java Convert via ByteBuffer toAttributeValue(Object value)

Here you can find the source of toAttributeValue(Object value)

Description

Copied from DynamoDB Document SDK InternalUtils.java Converts a simple value into the low-level representation.

License

Open Source License

Parameter

Parameter Description
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>

Exception

Parameter Description
UnsupportedOperationException if the input object type is not supported

Return

a non-null low level representation of the input object value

Declaration

public static AttributeValue toAttributeValue(Object value) 

Method Source Code

//package com.java2s;
/*//from www  .  jav a2 s .  c om
 * Copyright 2016 Classmethod, Inc. or its affiliates. All Rights Reserved.
 * Portions copyright Amazon.com, Inc. or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.util.ArrayList;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.amazonaws.services.dynamodbv2.model.AttributeValue;

public class Main {
    /**
     * Copied from DynamoDB Document SDK InternalUtils.java
     *
     * Converts a simple value into the low-level <code><AttributeValue/code>
     * representation.
     *
     * @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;
    }

    /**
     * Copied from DynamoDB Document SDK InternalUtils.java
     *
     * converts a number to a bigdecimal
     */
    private static BigDecimal toBigDecimal(Number n) {
        if (n instanceof BigDecimal) {
            return (BigDecimal) n;
        }
        return new BigDecimal(n.toString());
    }
}

Related

  1. toArray(IntBuffer src, int[] dst, int offset)
  2. toArray(long length)
  3. toASCII(String str)
  4. toAsciiBytes(String s)
  5. toASCIIString(URI u)
  6. toBigDecimal(byte[] bytes, int scale)
  7. toBigInteger(byte[] bytes)
  8. toBinaryFloatingPoint(double source)
  9. toBinaryString(byte[] b)