Coverage Report - com.jcabi.dynamo.AttributeUpdates
 
Classes in this File Line Coverage Branch Coverage Complexity
AttributeUpdates
77%
28/36
20%
4/20
1.421
AttributeUpdates$AjcClosure1
100%
1/1
N/A
1.421
AttributeUpdates$AjcClosure11
100%
1/1
N/A
1.421
AttributeUpdates$AjcClosure13
100%
1/1
N/A
1.421
AttributeUpdates$AjcClosure15
100%
1/1
N/A
1.421
AttributeUpdates$AjcClosure17
100%
1/1
N/A
1.421
AttributeUpdates$AjcClosure19
100%
1/1
N/A
1.421
AttributeUpdates$AjcClosure21
100%
1/1
N/A
1.421
AttributeUpdates$AjcClosure23
100%
1/1
N/A
1.421
AttributeUpdates$AjcClosure25
100%
1/1
N/A
1.421
AttributeUpdates$AjcClosure27
100%
1/1
N/A
1.421
AttributeUpdates$AjcClosure29
100%
1/1
N/A
1.421
AttributeUpdates$AjcClosure3
100%
1/1
N/A
1.421
AttributeUpdates$AjcClosure31
100%
1/1
N/A
1.421
AttributeUpdates$AjcClosure5
100%
1/1
N/A
1.421
AttributeUpdates$AjcClosure7
0%
0/1
N/A
1.421
AttributeUpdates$AjcClosure9
100%
1/1
N/A
1.421
 
 1  0
 /**
 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.AttributeAction;
 33  
 import com.amazonaws.services.dynamodbv2.model.AttributeValue;
 34  
 import com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate;
 35  
 import com.google.common.base.Joiner;
 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.Locale;
 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 attribute updates.
 50  
  *
 51  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 52  
  * @version $Id: ec123ce33d05a718dcf52c126ef8c06baca2b81e $
 53  
  * @since 0.12
 54  
  */
 55  0
 @Immutable
 56  
 @Loggable(Loggable.DEBUG)
 57  0
 @EqualsAndHashCode(of = "attrs")
 58  
 @SuppressWarnings
 59  
     (
 60  
         {
 61  
             "PMD.TooManyMethods",
 62  
             "PMD.AvoidInstantiatingObjectsInLoops"
 63  
         }
 64  
     )
 65  
 public final class AttributeUpdates
 66  
     implements Map<String, AttributeValueUpdate> {
 67  
 
 68  
     /**
 69  
      * Encapsulated AttributeUpdates.
 70  
      */
 71  
     private final transient ArrayMap<String, AttributeValueUpdate> attrs;
 72  
 
 73  
     /**
 74  
      * Private ctor.
 75  
      */
 76  
     public AttributeUpdates() {
 77  20
         this(new ArrayMap<String, AttributeValueUpdate>());
 78  20
     }
 79  
 
 80  
     /**
 81  
      * Private ctor.
 82  
      * @param map Map of them
 83  
      */
 84  43
     public AttributeUpdates(final Map<String, AttributeValueUpdate> map) {
 85  43
         this.attrs = new ArrayMap<String, AttributeValueUpdate>(map);
 86  43
     }
 87  
 
 88  
     /**
 89  
      * With this attribute.
 90  
      * @param name Attribute name
 91  
      * @param value The value
 92  
      * @return AttributeUpdates
 93  
      */
 94  
     public AttributeUpdates with(final String name,
 95  
         final AttributeValueUpdate value) {
 96  42
         return new AttributeUpdates(
 97  
             this.attrs.with(String.format(Locale.ENGLISH, name), value)
 98  
         );
 99  
     }
 100  
 
 101  
     /**
 102  
      * With this attribute.
 103  
      * @param name Attribute name
 104  
      * @param value The value
 105  
      * @return AttributeUpdates
 106  
      * @since 0.14.3
 107  
      */
 108  
     public AttributeUpdates with(final String name,
 109  
         final AttributeValue value) {
 110  38
         return this.with(
 111  
             name,
 112  
             new AttributeValueUpdate(value, AttributeAction.PUT)
 113  
         );
 114  
     }
 115  
 
 116  
     /**
 117  
      * With this attribute.
 118  
      * @param name Attribute name
 119  
      * @param value The value
 120  
      * @return AttributeUpdates
 121  
      * @since 0.14.3
 122  
      */
 123  
     public AttributeUpdates with(final String name, final Object value) {
 124  
         final AttributeValue attr;
 125  36
         if (value instanceof Long || value instanceof Integer) {
 126  0
             attr = new AttributeValue().withN(value.toString());
 127  
         } else {
 128  18
             attr = new AttributeValue(value.toString());
 129  
         }
 130  18
         return this.with(name, attr);
 131  
     }
 132  
 
 133  
     /**
 134  
      * With these AttributeUpdates.
 135  
      * @param map AttributeUpdates to add
 136  
      * @return AttributeUpdates
 137  
      */
 138  
     public AttributeUpdates with(final Map<String, AttributeValueUpdate> map) {
 139  0
         final ConcurrentMap<String, AttributeValueUpdate> attribs =
 140  
             new ConcurrentHashMap<String, AttributeValueUpdate>(map.size());
 141  
         for (final Map.Entry<String, AttributeValueUpdate> entry
 142  0
             : map.entrySet()) {
 143  0
             attribs.put(
 144  
                 String.format(Locale.ENGLISH, entry.getKey()),
 145  
                 entry.getValue()
 146  
             );
 147  0
         }
 148  0
         return new AttributeUpdates(this.attrs.with(attribs));
 149  
     }
 150  
 
 151  
     @Override
 152  
     public String toString() {
 153  66
         final Collection<String> terms =
 154  
             new ArrayList<String>(this.attrs.size());
 155  
         for (final Map.Entry<String, AttributeValueUpdate> attr
 156  66
             : this.attrs.entrySet()) {
 157  84
             terms.add(
 158  
                 String.format(
 159  
                     "%s=%s",
 160  
                     attr.getKey(),
 161  
                     attr.getValue()
 162  
                 )
 163  
             );
 164  84
         }
 165  66
         return Joiner.on("; ").join(terms);
 166  
     }
 167  
 
 168  
     @Override
 169  
     public int size() {
 170  16
         return this.attrs.size();
 171  
     }
 172  
 
 173  
     @Override
 174  
     public boolean isEmpty() {
 175  4
         return this.attrs.isEmpty();
 176  
     }
 177  
 
 178  
     @Override
 179  
     public boolean containsKey(final Object key) {
 180  2
         return this.attrs.containsKey(
 181  
             String.format(Locale.ENGLISH, key.toString())
 182  
         );
 183  
     }
 184  
 
 185  
     @Override
 186  
     public boolean containsValue(final Object value) {
 187  2
         return this.attrs.containsValue(value);
 188  
     }
 189  
 
 190  
     @Override
 191  
     public AttributeValueUpdate get(final Object key) {
 192  2
         return this.attrs.get(
 193  
             String.format(Locale.ENGLISH, key.toString())
 194  
         );
 195  
     }
 196  
 
 197  
     @Override
 198  
     public Set<String> keySet() {
 199  12
         return this.attrs.keySet();
 200  
     }
 201  
 
 202  
     @Override
 203  
     public Collection<AttributeValueUpdate> values() {
 204  12
         return this.attrs.values();
 205  
     }
 206  
 
 207  
     @Override
 208  
     public Set<Map.Entry<String, AttributeValueUpdate>> entrySet() {
 209  2
         return this.attrs.entrySet();
 210  
     }
 211  
 
 212  
     @Override
 213  
     public AttributeValueUpdate put(final String key,
 214  
         final AttributeValueUpdate value) {
 215  2
         throw new UnsupportedOperationException(
 216  
             "AttributeUpdates class is immutable, can't do #put()"
 217  
         );
 218  
     }
 219  
 
 220  
     @Override
 221  
     public AttributeValueUpdate remove(final Object key) {
 222  2
         throw new UnsupportedOperationException(
 223  
             "AttributeUpdates class is immutable, can't do #remove()"
 224  
         );
 225  
     }
 226  
 
 227  
     @Override
 228  
     public void putAll(
 229  
         final Map<? extends String, ? extends AttributeValueUpdate> map) {
 230  2
         throw new UnsupportedOperationException(
 231  
             "AttributeUpdates class is immutable, can't do #putAll()"
 232  
         );
 233  
     }
 234  
 
 235  
     @Override
 236  
     public void clear() {
 237  2
         throw new UnsupportedOperationException(
 238  
             "AttributeUpdates class is immutable, can't do #clear()"
 239  
         );
 240  
     }
 241  
 }