Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
    *
    * 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://aws.amazon.com/apache2.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.
   */
  package com.amazonaws.services.dynamodbv2.document;
  import static com.amazonaws.services.dynamodbv2.document.internal.InternalUtils.checkInvalidAttrName;
  import static com.amazonaws.services.dynamodbv2.document.internal.InternalUtils.checkInvalidAttribute;
  import static com.amazonaws.services.dynamodbv2.document.internal.InternalUtils.rejectNullInput;
  import static com.amazonaws.services.dynamodbv2.document.internal.InternalUtils.rejectNullOrEmptyInput;
  import static com.amazonaws.services.dynamodbv2.document.internal.InternalUtils.rejectNullValue;
  import static com.amazonaws.services.dynamodbv2.document.internal.InternalUtils.valToString;
  import static com.amazonaws.util.BinaryUtils.copyAllBytesFrom;
  import static com.amazonaws.util.BinaryUtils.copyBytesFrom;
  
  import java.util.Arrays;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  
An item in DynamoDB. An item is a collection of attributes. Each attribute has a name and a value. An attribute value can be one of the followings:
  • String
  • Set<String>
  • Number (including any subtypes and primitive types)
  • Set<Number>
  • byte[]
  • Set<byte[]>
  • ByteBuffer
  • Set<ByteBuffer>
  • Boolean or boolean
  • null
  • Map<String,T>, where T can be any type on this list but must not induce any circular reference
  • List<T>, where T can be any type on this list but must not induce any circular reference
For an Item to be successfully persisted in DynamoDB, at a minimum the respective attributes for the primary key must be specified.
  
  public class Item {
      private static final String DUPLICATE_VALUES_FOUND_IN_INPUT = "Duplicate values found in input";
      private final Map<StringObjectattributes = new LinkedHashMap<StringObject>();
      private static final ItemValueConformer valueConformer = new ItemValueConformer();

    
Returns true if the specified attribute exists with a null value; false otherwise.
  
      public boolean isNull(String attrName) {
          return .containsKey(attrName)
                  && .get(attrName) == null;
      }

    
Returns true if this item contains the specified attribute; false otherwise.
  
      public boolean isPresent(String attrName) {
          return .containsKey(attrName);
      }

    
Returns the value of the specified attribute in the current item as a string; or null if the attribute either doesn't exist or the attribute value is null.

See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
  
      public String getString(String attrName) {
          Object val = .get(attrName);
         return valToString(val);
     }

    
Sets the value of the specified attribute in the current item to the given string value.
 
     public Item withString(String attrNameString val) {
         checkInvalidAttribute(attrNameval);
         .put(attrNameval);
         return this;
     }

    
Returns the value of the specified attribute in the current item as a BigDecimal; or null if the attribute either doesn't exist or the attribute value is null.

Throws:
java.lang.NumberFormatException if the attribute value is not a valid representation of a BigDecimal.
See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
 
     public BigDecimal getNumber(String attrName) {
         Object val = .get(attrName);
         return toBigDecimal(val);
     }
 
     private BigDecimal toBigDecimal(Object val) {
         if (val == null)
             return null;
         return val instanceof BigDecimal 
              ?  (BigDecimalval 
              : new BigDecimal(val.toString())
              ;
     }

    
Returns the value of the specified attribute in the current item as an BigInteger; or null if the attribute doesn't exist.

Throws:
java.lang.NumberFormatException if the attribute value is null or not a valid representation of a BigDecimal.
See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
 
     public BigInteger getBigInteger(String attrName) {
         BigDecimal bd = getNumber(attrName);
         return bd == null ? null : bd.toBigInteger();
     }

    
Returns the value of the specified attribute in the current item as a short.

Throws:
java.lang.NumberFormatException if the attribute value is null or not a valid representation of a BigDecimal.
See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
 
     public short getShort(String attrName) {
         BigDecimal bd = getNumber(attrName);
         if (bd == null)
             throw new NumberFormatException
                 ("value of " + attrName + " is null");
         return bd.shortValue();
     }

    
Returns the value of the specified attribute in the current item as an int.

Throws:
java.lang.NumberFormatException if the attribute value is null or not a valid representation of a BigDecimal.
See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
 
     public int getInt(String attrName) {
         BigDecimal bd = getNumber(attrName);
         if (bd == null)
             throw new NumberFormatException
                 ("value of " + attrName + " is null");
         return bd.intValue();
     }

    
Returns the value of the specified attribute in the current item as an long.

Throws:
java.lang.NumberFormatException if the attribute value is null or not a valid representation of a BigDecimal.
See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
 
     public long getLong(String attrName) {
         BigDecimal bd = getNumber(attrName);
         if (bd == null)
             throw new NumberFormatException
                 ("value of " + attrName + " is null");
         return bd.longValue();
     }

    
Returns the value of the specified attribute in the current item as a float.

Throws:
java.lang.NumberFormatException if the attribute value is null or not a valid representation of a BigDecimal.
See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
 
     public float getFloat(String attrName) {
         BigDecimal bd = getNumber(attrName);
         if (bd == null)
             throw new NumberFormatException
                 ("value of " + attrName + " is null");
         return bd.floatValue();
     }

    
Returns the value of the specified attribute in the current item as a double.

Throws:
java.lang.NumberFormatException if the attribute value is null or not a valid representation of a BigDecimal.
See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
 
     public double getDouble(String attrName) {
         BigDecimal bd = getNumber(attrName);
         if (bd == null)
             throw new NumberFormatException
                 ("value of " + attrName + " is null");
         return bd.doubleValue();
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withNumber(String attrNameBigDecimal val) {
         checkInvalidAttribute(attrNameval);
         .put(attrNameval);
         return this;
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withNumber(String attrNameNumber val) {
         checkInvalidAttribute(attrNameval);
         .put(attrNametoBigDecimal(val));
         return this;
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withInt(String attrNameint val) {
         checkInvalidAttrName(attrName);
         return withNumber(attrName, Integer.valueOf(val));
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withBigInteger(String attrNameBigInteger val) {
         checkInvalidAttrName(attrName);
         return withNumber(attrNameval);
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withShort(String attrNameshort val) {
         checkInvalidAttrName(attrName);
         return withNumber(attrName, Short.valueOf(val));
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withFloat(String attrNamefloat val) {
         checkInvalidAttrName(attrName);
         return withNumber(attrName, Float.valueOf(val));
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withDouble(String attrNamedouble val) {
         checkInvalidAttrName(attrName);
         return withNumber(attrName, Double.valueOf(val));
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withLong(String attrNamelong val) {
         checkInvalidAttrName(attrName);
         return withNumber(attrName, Long.valueOf(val));
     }

    
Returns the value of the specified attribute in the current item as a byte array; or null if the attribute either doesn't exist or the attribute value is null.

Throws:
java.lang.UnsupportedOperationException If the attribute value involves a byte buffer which is not backed by an accessible array
IncompatibleTypeException if the attribute value cannot be converted into a byte array
See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
 
     public byte[] getBinary(String attrName) {
         Object val = .get(attrName);
         return toByteArray(val);
     }

    
Returns the value of the specified attribute in the current item as a ByteBuffer; or null if the attribute either doesn't exist or the attribute value is null.

Throws:
IncompatibleTypeException if the attribute value cannot be converted into a byte array
See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
 
     public ByteBuffer getByteBuffer(String attrName) {
         Object val = .get(attrName);
         return toByteBuffer(val);
     }

    
This method is assumed to be only called from a getter method, but NOT from a setter method.
 
     private byte[] toByteArray(Object val) {
         if (val == null)
             return null;
         if (val instanceof byte[])
             return (byte[]) val;
         if (val instanceof ByteBuffer) {
             // Defensive code but execution should never get here. The internal
             // representation of binary should always be
             // byte[], not ByteBuffer. This allows Item to be converted into
             // a JSON string via Jackson without causing trouble.
             return copyAllBytesFrom((ByteBuffer)val);
         }
         throw new IncompatibleTypeException(val.getClass()
                 + " cannot be converted into a byte array");
     }
 
     private ByteBuffer toByteBuffer(Object val) {
         if (val == null)
             return null;
         if (val instanceof byte[])
             return ByteBuffer.wrap((byte[])val);
         if (val instanceof ByteBuffer) {
             // Defensive code but execution should never get here. The internal
             // representation of binary should always be
             // byte[], not ByteBuffer. This allows Item to be converted into
             // a JSON string via Jackson without causing trouble.
             return (ByteBuffer)val;
         }
         throw new IncompatibleTypeException(val.getClass()
                 + " cannot be converted into a ByteBuffer");
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withBinary(String attrNamebyte[] val) {
         checkInvalidAttribute(attrNameval);
         .put(attrNameval);
         return this;
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withBinary(String attrNameByteBuffer val) {
         checkInvalidAttribute(attrNameval);
         // convert ByteBuffer to bytes to keep Jackson happy
         .put(attrNamecopyBytesFrom(val));
         return this;
     }

    
Returns the value of the specified attribute in the current item as a set of strings; or null if the attribute either doesn't exist or the attribute value is null.

Throws:
IncompatibleTypeException if the attribute value cannot be converted into a set of strings because of duplicate elements
See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
 
     public Set<StringgetStringSet(String attrName) {
         Object val = .get(attrName);
         if (val == null)
             return null;
         Set<StringstringSet = new LinkedHashSet<String>();
         if (val instanceof Collection) {
             Collection<?> col = (Collection<?>) val;
             if (col.size() == 0)
                 return stringSet;
             for (Object elementcol) {
                 String s = element == null ? null : valToString(element);
                 if (!stringSet.add(s))
                     throw new IncompatibleTypeException(val.getClass()
                         + " cannot be converted into a set of strings because of duplicate elements");
             }
             return stringSet;
         }
         stringSet.add(valToString(val));
         return stringSet;
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withStringSet(String attrNameSet<Stringval) {
         checkInvalidAttribute(attrNameval);
         .put(attrNameval);
         return this;
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withStringSet(String attrNameString ...val) {
         checkInvalidAttribute(attrNameval);
         Set<StringstrSet = new LinkedHashSet<String>(Arrays.asList(val));
         if (strSet.size() != val.length)
             throw new IllegalArgumentException();
         .put(attrNamestrSet);
         return this;
     }

    
Returns the value of the specified attribute in the current item as a set of BigDecimal's; or null if the attribute either doesn't exist or the attribute value is null.

Throws:
java.lang.NumberFormatException if the attribute involves a value that is not a valid representation of a BigDecimal.
IncompatibleTypeException if the attribute value cannot be converted into a set of BigDecimal's because of duplicate elements
See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
 
     public Set<BigDecimalgetNumberSet(String attrName) {
         Object val = .get(attrName);
         if (val == null)
             return null;
         Set<BigDecimalnumSet = new LinkedHashSet<BigDecimal>();
         if (val instanceof Collection) {
             Collection<?> col = (Collection<?>) val;
             if (col.size() == 0)
                 return numSet;
             for (Object elementcol) {
                 BigDecimal bd = toBigDecimal(element);
                 if (!numSet.add(bd))
                     throw new IncompatibleTypeException(val.getClass()
                         + " cannot be converted into a set of BigDecimal's because of duplicate elements");
             }
             return numSet;
         } else if (val instanceof BigDecimal) {
             numSet.add((BigDecimal)val);
             return numSet;
         } else {
             numSet.add(new BigDecimal(val.toString()));
             return numSet;
         }
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withBigDecimalSet(String attrNameSet<BigDecimalval) {
         checkInvalidAttribute(attrNameval);
         .put(attrNameval);
         return this;
     }
    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withBigDecimalSet(String attrNameBigDecimal ... vals) {
         checkInvalidAttribute(attrNamevals);
         Set<BigDecimalset = new LinkedHashSet<BigDecimal>(Arrays.asList(vals));
         if (set.size() != vals.length)
             throw new IllegalArgumentException();
         .put(attrNameset);
         return this;
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withNumberSet(String attrNameNumber ... vals) {
         checkInvalidAttribute(attrNamevals);
         Set<BigDecimalset = InternalUtils.toBigDecimalSet(vals);
         if (set.size() != vals.length)
             throw new IllegalArgumentException();
         return withBigDecimalSet(attrNameset);
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withNumberSet(String attrNameSet<Numbervals) {
         checkInvalidAttribute(attrNamevals);
         Set<BigDecimalset = InternalUtils.toBigDecimalSet(vals);
         if (set.size() != vals.size())
             throw new IllegalArgumentException();
         return withBigDecimalSet(attrNameset);
     }

    
Returns the value of the specified attribute in the current item as a set of byte arrays; or null if the attribute either doesn't exist or the attribute value is null.

Throws:
IncompatibleTypeException if the attribute value cannot be converted into a set of byte arrays
See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
 
     public Set<byte[]> getBinarySet(String attrName) {
         Object val = .get(attrName);
         if (val == null)
             return null;
         Set<byte[]> binarySet = new LinkedHashSet<byte[]>();
         if (val instanceof Collection) {
             Collection<?> col = (Collection<?>) val;
             if (col.size() == 0)
                 return binarySet;
             for (Object elementcol) {
                 byte[] ba = toByteArray(element);
                 if (!binarySet.add(ba))
                     throw new IncompatibleTypeException(val.getClass()
                         + " cannot be converted into a set of byte arrays because of duplicate elements");
             }
             return binarySet;
         } else if (val instanceof byte[]) {
             binarySet.add((byte[])val);
             return binarySet;
         } else if (val instanceof ByteBuffer) {
             // Defensive code but execution should never get here. The internal
             // representation of binary should always be
             // byte[], not ByteBuffer. This allows Item to be converted into
             // a JSON string via Jackson without causing trouble.
             ByteBuffer bb = (ByteBufferval;
             binarySet.add(copyAllBytesFrom(bb));
             return binarySet;
         }
         throw new IncompatibleTypeException(val.getClass()
                 + " cannot be converted into a set of byte arrays");
     }

    
Returns the value of the specified attribute in the current item as a set of ByteBuffer; or null if the attribute either doesn't exist or the attribute value is null.

Throws:
IncompatibleTypeException if the attribute value cannot be converted into a set of ByteBuffer
See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
 
     public Set<ByteBuffergetByteBufferSet(String attrName) {
         Object val = .get(attrName);
         if (val == null)
             return null;
         Set<ByteBufferbinarySet = new LinkedHashSet<ByteBuffer>();
         if (val instanceof Collection) {
             Collection<?> col = (Collection<?>) val;
             if (col.size() == 0)
                 return binarySet;
             for (Object elementcol) {
                 ByteBuffer ba = toByteBuffer(element);
                 if (!binarySet.add(ba))
                     throw new IncompatibleTypeException(val.getClass()
                         + " cannot be converted into a set of ByteBuffer because of duplicate elements");
             }
             return binarySet;
         } else if (val instanceof ByteBuffer) {
             // Defensive code but execution should never get here. The internal
             // representation of binary should always be
             // byte[], not ByteBuffer. This allows Item to be converted into
             // a JSON string via Jackson without causing trouble.
             binarySet.add((ByteBuffer)val);
             return binarySet;
         } else if (val instanceof byte[]) {
             binarySet.add(ByteBuffer.wrap((byte[])val));
             return binarySet;
         }
         throw new IncompatibleTypeException(val.getClass()
                 + " cannot be converted into a set of ByteBuffer");
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withBinarySet(String attrNameSet<byte[]> val) {
         checkInvalidAttribute(attrNameval);
         .put(attrNameval);
         return this;
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withByteBufferSet(String attrNameSet<ByteBufferval) {
         checkInvalidAttribute(attrNameval);
         // convert ByteBuffer to bytes to keep Jackson happy
         Set<byte[]> set = new LinkedHashSet<byte[]>(val.size());
         for (ByteBuffer bbval)
             set.add(copyBytesFrom(bb));
         .put(attrNameset);
         return this;
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withBinarySet(String attrNamebyte[] ... vals) {
         checkInvalidAttribute(attrNamevals);
         Set<byte[]> set = new LinkedHashSet<byte[]>(Arrays.asList(vals));
         if (set.size() != vals.length)
             throw new IllegalArgumentException();
         .put(attrNameset);
         return this;
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withBinarySet(String attrNameByteBuffer ... vals) {
         checkInvalidAttribute(attrNamevals);
         // convert ByteBuffer to bytes to keep Jackson happy
         Set<byte[]> set = new LinkedHashSet<byte[]>(vals.length);
         for (ByteBuffer bbvals)
             set.add(copyBytesFrom(bb));
         if (set.size() != vals.length)
             throw new IllegalArgumentException();
         .put(attrNameset);
         return this;
     }

    
Returns the value of the specified attribute in the current item as a set of T's.; or null if the attribute either doesn't exist or the attribute value is null.

Throws:
java.lang.ClassCastException if the attribute involves a value that cannot be casted to T
See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
 
     public <T> List<T> getList(String attrName) {
         Object val = .get(attrName);
         if (val == null)
             return null;
         if (val instanceof List) {
             @SuppressWarnings("unchecked")
             List<T> ret = (List<T>)val;
             return ret;
         }
         List<T> list = new ArrayList<T>();
         if (val instanceof Collection) {
             Collection<?> col = (Collection<?>)val;
             for (Object elementcol) {
                 @SuppressWarnings("unchecked")
                 T t = (T)element;
                 list.add(t);
             }
             return list;
         }
         @SuppressWarnings("unchecked")
         T t = (T)val;
         list.add(t);
         return list;
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withList(String attrNameList<?> val) {
         checkInvalidAttribute(attrNameval);
         .put(attrName.transform(val));
         return this;
     }

    
Sets the value of the specified attribute in the current item to the given values as a list.
 
     public Item withList(String attrNameObject ... vals) {
         checkInvalidAttribute(attrNamevals);
         List<Objectlist_in = Arrays.asList(vals);
         .put(attrName.transform(list_in));
         return this;
     }

    
Returns the value of the specified attribute in the current item as a map of string-to-T's; or null if the attribute either doesn't exist or the attribute value is null. Note that any numeric type of a map is always canonicalized into BigDecimal, and therefore if T referred to a Number type, it would need to be BigDecimal to avoid a class cast exception.

Throws:
java.lang.ClassCastException if the attribute is not a map of string to T
See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
 
     @SuppressWarnings("unchecked")
     public <T> Map<String, T> getMap(String attrName) {
         return (Map<String, T>).get(attrName);
     }

    
Convenient method to return the specified attribute in the current item as a (copy of) map of string-to-T's where T must be a subclass of Number; or null if the attribute doesn't exist.

Parameters:
attrName the attribute name
valueType the specific number type of the value to be returned. Currently, only
  • Short
  • Integer
  • Long
  • Float
  • Double
  • Number
  • BigDecimal
  • BigInteger
are supported.
Throws:
java.lang.UnsupportedOperationException if the value type is not supported
java.lang.ClassCastException if the attribute is not a map of string to numbers
 
     @SuppressWarnings("unchecked")
     public <T extends NumberMap<String, T> getMapOfNumbers(String attrName,
             Class<T> valueType) {
         if (valueType == Short.class
         ||  valueType == Integer.class
         ||  valueType == Long.class
         ||  valueType == Float.class
         ||  valueType == Double.class
         ||  valueType == Number.class
         ||  valueType == BigDecimal.class
         ||  valueType == BigInteger.class) {
             final Map<StringBigDecimalsrc =
                 (Map<StringBigDecimal>).get(attrName);
             if (src == null)
                 return null;
             final Map<String, T> dst = new LinkedHashMap<String, T>(src.size()); 
             for (Map.Entry<String,BigDecimalesrc.entrySet()) {
                 final String key = e.getKey();
                 final BigDecimal val = e.getValue();
                 if (val == null) {
                     dst.put(keynull);
                 } else if (valueType == Short.class) {
                     dst.put(key, (T)Short.valueOf(val.shortValue()));
                 } else if (valueType == Integer.class) {
                     dst.put(key, (T)Integer.valueOf(val.intValue()));
                 } else if (valueType == Long.class) {
                     dst.put(key, (T)Long.valueOf(val.longValue()));
                 } else if (valueType == Float.class) {
                     dst.put(key, (T)Float.valueOf(val.floatValue()));
                 } else if (valueType == Double.class) {
                     dst.put(key, (T)Double.valueOf(val.doubleValue()));
                 } else if (valueType == BigDecimal.class || valueType == Number.class) {
                     dst.put(key, (T)val);
                 } else if (valueType == BigInteger.class) {
                     dst.put(key, (T)val.toBigInteger());
                 }
             }
             return dst;
         } else {
             throw new UnsupportedOperationException("Value type " + valueType
                     + " is not currently supported");
         }
     }

    
Convenient method to return the value of the specified attribute in the current item as a map of string-to-Object's; or null if the attribute either doesn't exist or the attribute value is null. Note that any numeric type of the map will be returned as BigDecimal.

Throws:
java.lang.ClassCastException if the attribute is not a map
See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
 
     @SuppressWarnings("unchecked")
     public Map<StringObjectgetRawMap(String attrName) {
         return (Map<StringObject>).get(attrName);
     }

    
Sets the value of the specified attribute in the current item to the given value.
 
     public Item withMap(String attrNameMap<String, ?> val) {
         checkInvalidAttribute(attrNameval);
         .put(attrName.transform(val));
         return this;
     }

    
Sets the value of the specified attribute in the current item to the given JSON document in the form of a string.
 
     public Item withJSON(String attrNameString json) {
         checkInvalidAttribute(attrNamejson);
         .put(attrName
             .transform(Jackson.fromJsonString(jsonMap.class)));
         return this;
     }

    
Returns the value of the specified attribute in the current item as a JSON string; or null if the attribute either doesn't exist or the attribute value is null.

See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
 
     public String getJSON(String attrName) {
         checkInvalidAttrName(attrName);
         Object val = .get(attrName);
         return val == null ? null : Jackson.toJsonString(val);
     }

    
Returns the value of the specified attribute in the current item as a JSON string with pretty indentation; or null if the attribute either doesn't exist or the attribute value is null.

See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
 
     public String getJSONPretty(String attrName) {
         checkInvalidAttrName(attrName);
         Object val = .get(attrName);
         return val == null ? null : Jackson.toJsonPrettyString(val);
     }

    
Returns the value of the specified attribute in the current item as a non-null Boolean.

Throws:
IncompatibleTypeException if either the attribute doesn't exist or if the attribute value cannot be converted into a non-null Boolean value
See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
 
     public Boolean getBOOL(String attrName) {
         final Object val = .get(attrName);
         if (val instanceof Boolean)
             return (Boolean)val;
         if (val instanceof String) {
             if ("1".equals(val))
                 return true;
             if ("0".equals(val))
                 return false;
             return Boolean.valueOf((String)val);
         }
         throw new IncompatibleTypeException("Value of attribute " + attrName
                 + " of type " + getTypeOf(attrName)
                 + " cannot be converted into a boolean value");
     }

    
Returns the value of the specified attribute in the current item as a primitive boolean.

Throws:
IncompatibleTypeException if either the attribute doesn't exist or if the attribute value cannot be converted into a boolean value
 
     public boolean getBoolean(String attrName) {
         final Boolean b = getBOOL(attrName);
         return b.booleanValue();
     }

    
Sets the value of the specified attribute in the current item to the boolean value.
 
     public Item withBoolean(String attrNameboolean val) {
         checkInvalidAttrName(attrName);
         .put(attrName, Boolean.valueOf(val));
         return this;
     }

    
Sets the value of the specified attribute to null.
    public Item withNull(String attrName) {
        checkInvalidAttrName(attrName);
        .put(attrNamenull);
        return this;
    }
    
Sets the value of the specified attribute to the given value. An attribute value can be a
  • Number
  • String
  • binary (ie byte array or byte buffer)
  • boolean
  • null
  • list (of any of the types on this list)
  • map (with string key to value of any of the types on this list)
  • set (of any of the types on this list)
    public Item with(String attrNameObject val) {
        if (val == null)
            return withNull(attrName);
        if (val instanceof String)
            return withString(attrName, (String)val);
        if (val instanceof Number)
            return withNumber(attrName, (Number)val);
        if (val instanceof byte[])
            return withBinary(attrName, (byte[])val);
        if (val instanceof ByteBuffer)
            return withBinary(attrName, (ByteBuffer)val);
        if (val instanceof Boolean)
            return withBoolean(attrName, (Boolean)val);
        if (val instanceof List)
            return withList(attrName, (List<?>)val);
        if (val instanceof Map) {
            @SuppressWarnings("unchecked")
            Map<String,?> map = (Map<String,?>)val;
            return withMap(attrNamemap);
        }
        if (val instanceof Set) {
            Set<?> set = (Set<?>)val;
            // Treat an empty set as a set of String
            if (set.size() == 0) {
                @SuppressWarnings("unchecked")
                Set<Stringss = (Set<String>)val;
                return withStringSet(attrNamess);
            }
            // Try to locate the first non-null element and use that as the
            // representative type
            Object representative = null;
            for (Object oset) {
                if (o != null)
                    representative = o;
            }
            // If all elements are null, treat the element type as String
            if (representative == null || representative instanceof String) {
                @SuppressWarnings("unchecked")
                Set<Stringss = (Set<String>)val;
                return withStringSet(attrNamess);
            }
            if (representative instanceof Number) {
                @SuppressWarnings("unchecked")
                Set<Numberns = (Set<Number>)val;
                return withNumberSet(attrNamens);
            }
            if (representative instanceof byte[]) {
                @SuppressWarnings("unchecked")
                Set<byte[]> bs = (Set<byte[]>)val;
                return withBinarySet(attrNamebs);
            }
            if (representative instanceof ByteBuffer) {
                @SuppressWarnings("unchecked")
                Set<ByteBufferbs = (Set<ByteBuffer>)val;
                return withByteBufferSet(attrNamebs);
            }
            throw new UnsupportedOperationException("Set of "
                    + representative.getClass() + " is not currently supported");
        }
        throw new UnsupportedOperationException("Input type "
                + val.getClass() + " is not currently supported");
    }

    
Convenient methods - sets the attributes of this item from the given key attributes.
    public Item withPrimaryKey(PrimaryKey primaryKey) {
        rejectNullValue(primaryKey);
        if (primaryKey.getComponents().size() == 0)
            throw new IllegalArgumentException("primary key must not be empty");
        for (KeyAttribute kaprimaryKey.getComponents())
            this.with(ka.getName(), ka.getValue());
        return this;
    }

    
Convenient method to set the attributes of this item from the given hash-only primary key name and value.
    public Item withPrimaryKey(String hashKeyNameObject hashKeyValue) {
        return withKeyComponent(hashKeyNamehashKeyValue);
    }

    
Convenient method to set the attributes of this item from the given hash and range primary key.
    public Item withPrimaryKey(String hashKeyNameObject hashKeyValue,
            String rangeKeyNameObject rangeKeyValue) {
        return withKeyComponent(hashKeyNamehashKeyValue)
              .withKeyComponent(rangeKeyNamerangeKeyValue);
    }

    
Convenient methods - sets the attributes of this item from the specified key components.
    public Item withKeyComponents(KeyAttribute ...components) {
        rejectNullOrEmptyInput(components);
        for (KeyAttribute kacomponents) {
            rejectNullValue(ka);
            this.with(ka.getName(), ka.getValue());
        }
        return this;
    }

    
Convenient methods - sets an attribute of this item for the specified key attribute name and value.
    public Item withKeyComponent(String keyAttrNameObject keyAttrValue) {
        return with(keyAttrNamekeyAttrValue);
    }

    
Returns the value of the specified attribute in the current item as an object; or null if the attribute either doesn't exist or the attribute value is null.

An attribute value can be a

  • Number
  • String
  • binary (ie byte array or byte buffer)
  • boolean
  • null
  • list (of any of the types on this list)
  • map (with string key to value of any of the types on this list)
  • set (of any of the types on this list)

See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
    public Object get(String attrName) {
        return .get(attrName);
    }

    
Returns the type of the specified attribute in the current item; or null if the attribute either doesn't exist or the attribute value is null.

See also:
isNull(java.lang.String) .isNull(String) to check if the attribute value is null.
isPresent(java.lang.String) .isPresent(String) to check if the attribute value is present.
    public Class<?> getTypeOf(String attrName) {
        Object val = .get(attrName);
        return val == null ? null : val.getClass();
    }

    
Removes the specified attribute from the current item.
    public Item removeAttribute(String attrName) {
        checkInvalidAttrName(attrName);
        .remove(attrName);
        return this;
    }

    
Returns all attributes of the current item.
    public Iterable<Entry<StringObject>> attributes() {
        return new LinkedHashMap<StringObject>().entrySet();
    }

    
Returns all attributes of the current item as a map.
    public Map<StringObjectasMap() {
        return new LinkedHashMap<String,Object>();
    }

    
Returns the number of attributes of this item.
    public int numberOfAttributes() {
        return .size();
    }

    
Convenient factory method - instantiates an Item from the given map.

Parameters:
attributes simple Java types; not the DyanmoDB types
    public static Item fromMap(Map<StringObjectattributes) {
        if (attributes == null)
            return null;
        Item item = new Item();
        for (Map.Entry<StringObjecte : attributes.entrySet())
            item.with(e.getKey(), e.getValue());
        return item;
    }

    
Convenient factory method - instantiates an Item from the given JSON string.

Returns:
an Item initialized from the given JSON document; or null if the input is null.
    public static Item fromJSON(String json) {
        if (json == null)
            return null;
        @SuppressWarnings("unchecked")
        Map<StringObjectmap = (Map<StringObject>)
            .transform(Jackson.fromJsonString(jsonMap.class));
        return fromMap(map);
    }

    
Returns this item as a JSON string. Note all binary data will become base-64 encoded in the resultant string.
    public String toJSON() {
        return Jackson.toJsonString(this.);
    }

    
Utility method to decode the designated binary attributes from base-64 encoding; converting binary lists into binary sets.

Parameters:
binaryAttrNames names of binary attributes or binary set attributes currently base-64 encoded (typically when converted from a JSON string.)
See also:
fromJSON(java.lang.String)
    public Item base64Decode(String ... binaryAttrNames) {
        rejectNullInput(binaryAttrNames);
        // Verify all attributes are good
        for (String attrNamebinaryAttrNames) {
            checkInvalidAttrName(attrName);
            if (String.class == getTypeOf(attrName)) {
                String b64 = getString(attrName);
                Base64.decode(b64);
            } else {
                Set<Stringb64s = getStringSet(attrName);
                for (String b64b64s)
                    Base64.decode(b64);
            }
        }
        // Decodes b64 into binary 
        for (String attrNamebinaryAttrNames) {
            if (String.class == getTypeOf(attrName)) {
                String b64 = getString(attrName);
                byte[] bytes = Base64.decode(b64);
                withBinary(attrNamebytes);
            } else {
                Set<Stringb64s = getStringSet(attrName);
                Set<byte[]> binarySet = new LinkedHashSet<byte[]>(b64s.size());
                for (String b64b64s)
                    binarySet.add(Base64.decode(b64));
                withBinarySet(attrNamebinarySet);
            }
        }
        return this;
    }

    
Utility method to converts the designated attributes from List into Set, throwing IllegalArgumentException should there be duplicate elements.

Parameters:
listAttrNames names of attributes to be converted.
See also:
fromJSON(java.lang.String)
    public Item convertListsToSets(String ... listAttrNames) {
        rejectNullInput(listAttrNames);
        // Verify all attributes are good
        for (String attrNamelistAttrNames) {
            checkInvalidAttrName(attrName);
            if (List.class.isAssignableFrom(getTypeOf(attrName))) {
                List<?> list = getList(attrName);
                if (list != null) {
                    for (Object elist) {
                        if (e instanceof String) {
                            Set<Stringss = getStringSet(attrName);
                            if (list.size() != ss.size())
                                throw new IllegalArgumentException("List cannot be converted to Set due to duplicate elements");
                        } else if (e instanceof Number) {
                            Set<BigDecimalss = getNumberSet(attrName);
                            if (list.size() != ss.size())
                                throw new IllegalArgumentException("List cannot be converted to Set due to duplicate elements");
                        } else if (e instanceof byte[]) {
                            Set<byte[]> ss = getBinarySet(attrName);
                            if (list.size() != ss.size())
                                throw new IllegalArgumentException("List cannot be converted to Set due to duplicate elements");
                        }
                    }
                }
            } else {
                throw new IllegalArgumentException("Attribute " + attrName + " is not a list");
            }
        }
        // Do the conversion
        for (String attrNamelistAttrNames) {
            checkInvalidAttrName(attrName);
            List<?> list = getList(attrName);
            if (list != null) {
                boolean converted = false;
                for (Object elist) {
                    if (e instanceof String) {
                        Set<Stringset = getStringSet(attrName);
                        withStringSet(attrNameset);
                        converted = true;
                        break;
                    } else if (e instanceof Number) {
                        Set<BigDecimalset = getNumberSet(attrName);
                        withBigDecimalSet(attrNameset);
                        converted = true;
                        break;
                    } else if (e instanceof byte[]) {
                        Set<byte[]> set = getBinarySet(attrName);
                        withBinarySet(attrNameset);
                        converted = true;
                        break;
                    }
                }
                if (!converted) {
                    // All elements are null.  So treat it as a String set.
                    Set<Stringset = getStringSet(attrName);
                    withStringSet(attrNameset);
                }
            }
        }
        return this;
    }

    
Returns this item as a pretty JSON string. Note all binary data will become base-64 encoded in the resultant string.
    public String toJSONPretty() {
        return Jackson.toJsonPrettyString(this.);
    }
    @Override
    public String toString() {
        return "{ Item: " + .toString() + " }";
    }
 // hchar: let's not define the equals method until we figure out a more full
 // proof solution to define it's meaning and the corresponding hashCode method.
New to GrepCode? Check out our FAQ X