Coverage Report - com.jcabi.dynamo.Attributes
 
Classes in this File Line Coverage Branch Coverage Complexity
Attributes
86%
43/50
67%
19/28
1.6
Attributes$AjcClosure1
100%
1/1
N/A
1.6
Attributes$AjcClosure11
100%
1/1
N/A
1.6
Attributes$AjcClosure13
0%
0/1
N/A
1.6
Attributes$AjcClosure15
100%
1/1
N/A
1.6
Attributes$AjcClosure17
0%
0/1
N/A
1.6
Attributes$AjcClosure19
100%
1/1
N/A
1.6
Attributes$AjcClosure21
100%
1/1
N/A
1.6
Attributes$AjcClosure23
100%
1/1
N/A
1.6
Attributes$AjcClosure25
100%
1/1
N/A
1.6
Attributes$AjcClosure27
0%
0/1
N/A
1.6
Attributes$AjcClosure29
0%
0/1
N/A
1.6
Attributes$AjcClosure3
100%
1/1
N/A
1.6
Attributes$AjcClosure31
0%
0/1
N/A
1.6
Attributes$AjcClosure33
0%
0/1
N/A
1.6
Attributes$AjcClosure5
100%
1/1
N/A
1.6
Attributes$AjcClosure7
100%
1/1
N/A
1.6
Attributes$AjcClosure9
100%
1/1
N/A
1.6
 
 1  16
 /**
 2  
  * Copyright (c) 2012-2016, jcabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.dynamo;
 31  
 
 32  
 import com.amazonaws.services.dynamodbv2.model.AttributeValue;
 33  
 import com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue;
 34  
 import com.google.common.base.Joiner;
 35  
 import com.google.common.collect.ImmutableMap;
 36  
 import com.jcabi.aspects.Immutable;
 37  
 import com.jcabi.aspects.Loggable;
 38  
 import com.jcabi.immutable.ArrayMap;
 39  
 import java.util.ArrayList;
 40  
 import java.util.Collection;
 41  
 import java.util.HashSet;
 42  
 import java.util.Map;
 43  
 import java.util.Set;
 44  
 import java.util.concurrent.ConcurrentHashMap;
 45  
 import java.util.concurrent.ConcurrentMap;
 46  
 import lombok.EqualsAndHashCode;
 47  
 
 48  
 /**
 49  
  * DynamoDB item attributes.
 50  
  *
 51  
  * <p>It's a convenient immutable builder of a map of attribute values for
 52  
  * DynamoDB put operation. Use it like this:
 53  
  *
 54  
  * <pre>Map&lt;String, AttributeValue&gt; attributes = new Attributes()
 55  
  *   .with("hash", "some value")
 56  
  *   .with("range", 12345);
 57  
  * </pre>
 58  
  *
 59  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 60  
  * @version $Id: 26ba78e0b53155a9093056ee342f2d341bd7bff0 $
 61  
  * @since 0.1
 62  
  */
 63  0
 @Immutable
 64  
 @Loggable(Loggable.DEBUG)
 65  1
 @EqualsAndHashCode(of = "attrs")
 66  
 @SuppressWarnings
 67  
     (
 68  
         {
 69  
             "PMD.TooManyMethods",
 70  
             "PMD.AvoidInstantiatingObjectsInLoops"
 71  
         }
 72  
     )
 73  
 public final class Attributes implements Map<String, AttributeValue> {
 74  
 
 75  
     /**
 76  
      * Encapsulated attributes.
 77  
      */
 78  
     private final transient ArrayMap<String, AttributeValue> attrs;
 79  
 
 80  
     /**
 81  
      * Private ctor.
 82  
      */
 83  
     public Attributes() {
 84  33
         this(new ArrayMap<String, AttributeValue>());
 85  33
     }
 86  
 
 87  
     /**
 88  
      * Private ctor.
 89  
      * @param map Map of them
 90  
      */
 91  99
     public Attributes(final Map<String, AttributeValue> map) {
 92  99
         this.attrs = new ArrayMap<String, AttributeValue>(map);
 93  99
     }
 94  
 
 95  
     /**
 96  
      * With this attribute.
 97  
      * @param name Attribute name
 98  
      * @param value The value
 99  
      * @return Attributes
 100  
      */
 101  
     public Attributes with(final String name, final AttributeValue value) {
 102  98
         return new Attributes(this.attrs.with(name, value));
 103  
     }
 104  
 
 105  
     /**
 106  
      * With these attributes.
 107  
      * @param map Attributes to add
 108  
      * @return Attributes
 109  
      */
 110  
     public Attributes with(final Map<String, AttributeValue> map) {
 111  2
         final ConcurrentMap<String, AttributeValue> attribs =
 112  
             new ConcurrentHashMap<String, AttributeValue>(map.size());
 113  1
         for (final Map.Entry<String, AttributeValue> entry : map.entrySet()) {
 114  2
             attribs.put(entry.getKey(), entry.getValue());
 115  2
         }
 116  1
         return new Attributes(this.attrs.with(attribs));
 117  
     }
 118  
 
 119  
     /**
 120  
      * Convert them to a map of expected values.
 121  
      * @return Expected values
 122  
      */
 123  
     public Map<String, ExpectedAttributeValue> asKeys() {
 124  2
         final ImmutableMap.Builder<String, ExpectedAttributeValue> map =
 125  
             new ImmutableMap.Builder<String, ExpectedAttributeValue>();
 126  
         for (final Map.Entry<String, AttributeValue> attr
 127  1
             : this.attrs.entrySet()) {
 128  1
             map.put(
 129  
                 attr.getKey(),
 130  
                 new ExpectedAttributeValue(attr.getValue())
 131  
             );
 132  1
         }
 133  1
         return map.build();
 134  
     }
 135  
 
 136  
     /**
 137  
      * With this attribute.
 138  
      * @param name Attribute name
 139  
      * @param value The value
 140  
      * @return Attributes
 141  
      * @
 142  
      */
 143  
     public Attributes with(final String name, final Object value) {
 144  
         final AttributeValue attr;
 145  44
         if (value instanceof Long || value instanceof Integer) {
 146  4
             attr = new AttributeValue().withN(value.toString());
 147  
         } else {
 148  18
             attr = new AttributeValue(value.toString());
 149  
         }
 150  22
         return this.with(name, attr);
 151  
     }
 152  
 
 153  
     /**
 154  
      * Filter out all keys except provided ones.
 155  
      * @param keys Keys to leave in the map
 156  
      * @return Attributes
 157  
      */
 158  
     public Attributes only(final Iterable<String> keys) {
 159  16
         final ImmutableMap.Builder<String, AttributeValue> map =
 160  
             new ImmutableMap.Builder<String, AttributeValue>();
 161  8
         final Collection<String> hash = new HashSet<String>(0);
 162  8
         for (final String key : keys) {
 163  8
             hash.add(key);
 164  8
         }
 165  8
         for (final Map.Entry<String, AttributeValue> entry : this.entrySet()) {
 166  18
             if (hash.contains(entry.getKey())) {
 167  6
                 map.put(
 168  
                     entry.getKey(),
 169  
                     entry.getValue()
 170  
                 );
 171  
             }
 172  18
         }
 173  8
         return new Attributes(map.build());
 174  
     }
 175  
 
 176  
     @Override
 177  
     public String toString() {
 178  129
         final Collection<String> terms =
 179  
             new ArrayList<String>(this.attrs.size());
 180  
         for (final Map.Entry<String, AttributeValue> attr
 181  129
             : this.attrs.entrySet()) {
 182  199
             terms.add(
 183  
                 String.format(
 184  
                     "%s=%s",
 185  
                     attr.getKey(),
 186  
                     attr.getValue()
 187  
                 )
 188  
             );
 189  199
         }
 190  129
         return Joiner.on("; ").join(terms);
 191  
     }
 192  
 
 193  
     @Override
 194  
     public int size() {
 195  22
         return this.attrs.size();
 196  
     }
 197  
 
 198  
     @Override
 199  
     public boolean isEmpty() {
 200  0
         return this.attrs.isEmpty();
 201  
     }
 202  
 
 203  
     @Override
 204  
     public boolean containsKey(final Object key) {
 205  2
         return this.attrs.containsKey(key.toString());
 206  
     }
 207  
 
 208  
     @Override
 209  
     public boolean containsValue(final Object value) {
 210  0
         return this.attrs.containsValue(value);
 211  
     }
 212  
 
 213  
     @Override
 214  
     public AttributeValue get(final Object key) {
 215  18
         return this.attrs.get(key.toString());
 216  
     }
 217  
 
 218  
     @Override
 219  
     public Set<String> keySet() {
 220  28
         return this.attrs.keySet();
 221  
     }
 222  
 
 223  
     @Override
 224  
     public Collection<AttributeValue> values() {
 225  22
         return this.attrs.values();
 226  
     }
 227  
 
 228  
     @Override
 229  
     public Set<Map.Entry<String, AttributeValue>> entrySet() {
 230  60
         return this.attrs.entrySet();
 231  
     }
 232  
 
 233  
     @Override
 234  
     public AttributeValue put(final String key, final AttributeValue value) {
 235  0
         throw new UnsupportedOperationException(
 236  
             "Attributes class is immutable, can't do #put()"
 237  
         );
 238  
     }
 239  
 
 240  
     @Override
 241  
     public AttributeValue remove(final Object key) {
 242  0
         throw new UnsupportedOperationException(
 243  
             "Attributes class is immutable, can't do #remove()"
 244  
         );
 245  
     }
 246  
 
 247  
     @Override
 248  
     public void putAll(
 249  
         final Map<? extends String, ? extends AttributeValue> map) {
 250  0
         throw new UnsupportedOperationException(
 251  
             "Attributes class is immutable, can't do #putAll()"
 252  
         );
 253  
     }
 254  
 
 255  
     @Override
 256  
     public void clear() {
 257  0
         throw new UnsupportedOperationException(
 258  
             "Attributes class is immutable, can't do #clear()"
 259  
         );
 260  
     }
 261  
 }