Coverage Report - com.jcabi.simpledb.AwsItem
 
Classes in this File Line Coverage Branch Coverage Complexity
AwsItem
35%
20/57
14%
5/34
1.375
AwsItem$AjcClosure1
0%
0/1
N/A
1.375
AwsItem$AjcClosure11
100%
1/1
N/A
1.375
AwsItem$AjcClosure13
0%
0/1
N/A
1.375
AwsItem$AjcClosure15
0%
0/1
N/A
1.375
AwsItem$AjcClosure17
0%
0/1
N/A
1.375
AwsItem$AjcClosure19
0%
0/1
N/A
1.375
AwsItem$AjcClosure21
0%
0/1
N/A
1.375
AwsItem$AjcClosure23
0%
0/1
N/A
1.375
AwsItem$AjcClosure25
100%
1/1
N/A
1.375
AwsItem$AjcClosure3
0%
0/1
N/A
1.375
AwsItem$AjcClosure5
0%
0/1
N/A
1.375
AwsItem$AjcClosure7
0%
0/1
N/A
1.375
AwsItem$AjcClosure9
0%
0/1
N/A
1.375
 
 1  0
 /**
 2  
  * Copyright (c) 2012-2014, 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.simpledb;
 31  
 
 32  
 import com.amazonaws.services.simpledb.model.Attribute;
 33  
 import com.amazonaws.services.simpledb.model.DeleteAttributesRequest;
 34  
 import com.amazonaws.services.simpledb.model.GetAttributesRequest;
 35  
 import com.amazonaws.services.simpledb.model.GetAttributesResult;
 36  
 import com.amazonaws.services.simpledb.model.PutAttributesRequest;
 37  
 import com.amazonaws.services.simpledb.model.ReplaceableAttribute;
 38  
 import com.jcabi.aspects.Immutable;
 39  
 import com.jcabi.aspects.Loggable;
 40  
 import java.util.AbstractMap;
 41  
 import java.util.ArrayList;
 42  
 import java.util.Collection;
 43  
 import java.util.HashSet;
 44  
 import java.util.Map;
 45  
 import java.util.Set;
 46  
 import java.util.concurrent.ConcurrentHashMap;
 47  
 import java.util.concurrent.ConcurrentMap;
 48  
 import lombok.EqualsAndHashCode;
 49  
 
 50  
 /**
 51  
  * Single item/row in a SimpleDB table.
 52  
  *
 53  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 54  
  * @version $Id$
 55  
  * @since 0.1
 56  
  * @checkstyle ClassDataAbstractionCoupling (500 lines)
 57  
  */
 58  1
 @Immutable
 59  
 @Loggable(Loggable.DEBUG)
 60  0
 @EqualsAndHashCode(of = { "credentials", "table", "label" })
 61  
 @SuppressWarnings("PMD.TooManyMethods")
 62  
 final class AwsItem implements Item {
 63  
 
 64  
     /**
 65  
      * AWS credentials.
 66  
      */
 67  
     private final transient Credentials credentials;
 68  
 
 69  
     /**
 70  
      * Table name.
 71  
      */
 72  
     private final transient String table;
 73  
 
 74  
     /**
 75  
      * Table name.
 76  
      */
 77  
     private final transient String label;
 78  
 
 79  
     /**
 80  
      * Public ctor.
 81  
      * @param creds Credentials
 82  
      * @param tbl Table name
 83  
      * @param item Item name
 84  
      */
 85  
     protected AwsItem(final Credentials creds, final String tbl,
 86  2
         final String item) {
 87  2
         this.credentials = creds;
 88  2
         this.table = tbl;
 89  2
         this.label = item;
 90  2
     }
 91  
 
 92  
     /**
 93  
      * Public ctor.
 94  
      * @param creds Credentials
 95  
      * @param tbl Table name
 96  
      * @param item Item name
 97  
      */
 98  
     protected AwsItem(final Credentials creds, final String tbl,
 99  
         final com.amazonaws.services.simpledb.model.Item item) {
 100  0
         this(creds, tbl, item.getName());
 101  0
     }
 102  
 
 103  
     /**
 104  
      * {@inheritDoc}
 105  
      */
 106  
     @Override
 107  
     public String toString() {
 108  1
         return String.format("%s in %s", this.label, this.table);
 109  
     }
 110  
 
 111  
     /**
 112  
      * {@inheritDoc}
 113  
      */
 114  
     @Override
 115  
     public String name() {
 116  0
         return this.label;
 117  
     }
 118  
 
 119  
     /**
 120  
      * {@inheritDoc}
 121  
      */
 122  
     @Override
 123  
     public int size() {
 124  0
         return this.entrySet().size();
 125  
     }
 126  
 
 127  
     /**
 128  
      * {@inheritDoc}
 129  
      */
 130  
     @Override
 131  
     public boolean isEmpty() {
 132  0
         return this.entrySet().isEmpty();
 133  
     }
 134  
 
 135  
     /**
 136  
      * {@inheritDoc}
 137  
      */
 138  
     @Override
 139  
     public boolean containsKey(final Object key) {
 140  0
         return this.keySet().contains(key);
 141  
     }
 142  
 
 143  
     /**
 144  
      * {@inheritDoc}
 145  
      */
 146  
     @Override
 147  
     public boolean containsValue(final Object value) {
 148  0
         return this.values().contains(value);
 149  
     }
 150  
 
 151  
     /**
 152  
      * {@inheritDoc}
 153  
      */
 154  
     @Override
 155  
     public String get(final Object key) {
 156  2
         final Set<Entry<String, String>> entries = this.entrySet();
 157  1
         String value = null;
 158  1
         for (final Entry<String, String> entry : entries) {
 159  1
             if (entry.getKey().equals(key)) {
 160  0
                 value = entry.getValue();
 161  
             }
 162  1
         }
 163  1
         return value;
 164  
     }
 165  
 
 166  
     /**
 167  
      * {@inheritDoc}
 168  
      */
 169  
     @Override
 170  
     public String put(final String key, final String value) {
 171  0
         final String before = this.get(key);
 172  0
         final ConcurrentMap<String, String> map =
 173  
             new ConcurrentHashMap<String, String>(0);
 174  0
         map.put(key, value);
 175  0
         this.putAll(map);
 176  0
         return before;
 177  
     }
 178  
 
 179  
     /**
 180  
      * {@inheritDoc}
 181  
      */
 182  
     @Override
 183  
     public String remove(final Object key) {
 184  0
         final String before = this.get(key);
 185  0
         this.credentials.aws().deleteAttributes(
 186  
             new DeleteAttributesRequest()
 187  
                 .withDomainName(this.table)
 188  
                 .withItemName(this.label)
 189  
                 .withAttributes(new Attribute().withName(key.toString()))
 190  
         );
 191  0
         return before;
 192  
     }
 193  
 
 194  
     /**
 195  
      * {@inheritDoc}
 196  
      */
 197  
     @Override
 198  
     @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
 199  
     public void putAll(final Map<? extends String, ? extends String> map) {
 200  0
         final Collection<ReplaceableAttribute> attrs =
 201  
             new ArrayList<ReplaceableAttribute>(map.size());
 202  0
         for (final Map.Entry<?, ?> entry : map.entrySet()) {
 203  0
             attrs.add(
 204  
                 new ReplaceableAttribute()
 205  
                     .withName(entry.getKey().toString())
 206  
                     .withValue(entry.getValue().toString())
 207  
                     .withReplace(true)
 208  
             );
 209  0
         }
 210  0
         this.credentials.aws().putAttributes(
 211  
             new PutAttributesRequest()
 212  
                 .withDomainName(this.table)
 213  
                 .withItemName(this.label)
 214  
                 .withAttributes(attrs)
 215  
         );
 216  0
     }
 217  
 
 218  
     /**
 219  
      * {@inheritDoc}
 220  
      */
 221  
     @Override
 222  
     public void clear() {
 223  0
         this.credentials.aws().deleteAttributes(
 224  
             new DeleteAttributesRequest()
 225  
                 .withDomainName(this.table)
 226  
                 .withItemName(this.label)
 227  
         );
 228  0
     }
 229  
 
 230  
     /**
 231  
      * {@inheritDoc}
 232  
      */
 233  
     @Override
 234  
     public Set<String> keySet() {
 235  0
         final Set<Entry<String, String>> entries = this.entrySet();
 236  0
         final Set<String> keys = new HashSet<String>(entries.size());
 237  0
         for (final Entry<String, String> entry : entries) {
 238  0
             keys.add(entry.getValue());
 239  0
         }
 240  0
         return keys;
 241  
     }
 242  
 
 243  
     /**
 244  
      * {@inheritDoc}
 245  
      */
 246  
     @Override
 247  
     public Collection<String> values() {
 248  0
         final Set<Entry<String, String>> entries = this.entrySet();
 249  0
         final Collection<String> values = new ArrayList<String>(entries.size());
 250  0
         for (final Entry<String, String> entry : entries) {
 251  0
             values.add(entry.getValue());
 252  0
         }
 253  0
         return values;
 254  
     }
 255  
 
 256  
     /**
 257  
      * {@inheritDoc}
 258  
      */
 259  
     @Override
 260  
     @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
 261  
     public Set<Entry<String, String>> entrySet() {
 262  2
         final GetAttributesResult result = this.credentials.aws().getAttributes(
 263  
             new GetAttributesRequest()
 264  
                 .withConsistentRead(true)
 265  
                 .withDomainName(this.table)
 266  
                 .withItemName(this.label)
 267  
         );
 268  1
         final Set<Entry<String, String>> entries =
 269  
             new HashSet<Entry<String, String>>(0);
 270  1
         for (final Attribute attr : result.getAttributes()) {
 271  1
             entries.add(
 272  
                 new AbstractMap.SimpleImmutableEntry<String, String>(
 273  
                     attr.getName(), attr.getValue()
 274  
                 )
 275  
             );
 276  1
         }
 277  1
         return entries;
 278  
     }
 279  
 
 280  
 }