Coverage Report - com.jcabi.dynamo.Conditions
 
Classes in this File Line Coverage Branch Coverage Complexity
Conditions
72%
31/43
36%
8/22
1.409
Conditions$AjcClosure1
100%
1/1
N/A
1.409
Conditions$AjcClosure11
0%
0/1
N/A
1.409
Conditions$AjcClosure13
100%
1/1
N/A
1.409
Conditions$AjcClosure15
100%
1/1
N/A
1.409
Conditions$AjcClosure17
0%
0/1
N/A
1.409
Conditions$AjcClosure19
0%
0/1
N/A
1.409
Conditions$AjcClosure21
0%
0/1
N/A
1.409
Conditions$AjcClosure23
100%
1/1
N/A
1.409
Conditions$AjcClosure25
100%
1/1
N/A
1.409
Conditions$AjcClosure27
100%
1/1
N/A
1.409
Conditions$AjcClosure29
0%
0/1
N/A
1.409
Conditions$AjcClosure3
100%
1/1
N/A
1.409
Conditions$AjcClosure31
0%
0/1
N/A
1.409
Conditions$AjcClosure33
0%
0/1
N/A
1.409
Conditions$AjcClosure35
0%
0/1
N/A
1.409
Conditions$AjcClosure5
100%
1/1
N/A
1.409
Conditions$AjcClosure7
0%
0/1
N/A
1.409
Conditions$AjcClosure9
100%
1/1
N/A
1.409
 
 1  2
 /**
 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.ComparisonOperator;
 34  
 import com.amazonaws.services.dynamodbv2.model.Condition;
 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.Map;
 42  
 import java.util.Set;
 43  
 import java.util.concurrent.ConcurrentHashMap;
 44  
 import java.util.concurrent.ConcurrentMap;
 45  
 import lombok.EqualsAndHashCode;
 46  
 
 47  
 /**
 48  
  * DynamoDB query conditions.
 49  
  *
 50  
  * <p>It's a convenient immutable builder of a map of conditions for
 51  
  * DynamoDB query/scan operations. Use it like this:
 52  
  *
 53  
  * <pre>Map&lt;String, Condition&gt; conditions = new Conditions()
 54  
  *   .with("hash", Conditions.equalTo("some value"))
 55  
  *   .with("range", Conditions.equalTo(12345));
 56  
  * </pre>
 57  
  *
 58  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 59  
  * @version $Id: 74a696df8176f872b7cda9ab44f9e5c8985261d6 $
 60  
  * @since 0.1
 61  
  */
 62  0
 @Immutable
 63  
 @Loggable(Loggable.DEBUG)
 64  0
 @EqualsAndHashCode(of = "conds")
 65  
 @SuppressWarnings
 66  
     (
 67  
         {
 68  
             "PMD.TooManyMethods",
 69  
             "PMD.AvoidInstantiatingObjectsInLoops"
 70  
         }
 71  
     )
 72  
 public final class Conditions implements Map<String, Condition> {
 73  
 
 74  
     /**
 75  
      * Pairs.
 76  
      */
 77  
     private final transient ArrayMap<String, Condition> conds;
 78  
 
 79  
     /**
 80  
      * Public ctor.
 81  
      */
 82  
     public Conditions() {
 83  19
         this(new ArrayMap<String, Condition>());
 84  19
     }
 85  
 
 86  
     /**
 87  
      * Public ctor.
 88  
      * @param map Map of them
 89  
      */
 90  28
     public Conditions(final Map<String, Condition> map) {
 91  28
         this.conds = Conditions.array(map);
 92  28
     }
 93  
 
 94  
     /**
 95  
      * Equal to static condition builder (factory method).
 96  
      * @param value The value to equal to
 97  
      * @return The condition just created
 98  
      */
 99  
     public static Condition equalTo(final Object value) {
 100  
         final AttributeValue attr;
 101  4
         if (value instanceof Long || value instanceof Integer) {
 102  2
             attr = new AttributeValue().withN(value.toString());
 103  
         } else {
 104  0
             attr = new AttributeValue().withS(value.toString());
 105  
         }
 106  2
         return Conditions.equalTo(attr);
 107  
     }
 108  
 
 109  
     /**
 110  
      * Equal to static condition builder (factory method).
 111  
      * @param value The value to equal to
 112  
      * @return The condition just created
 113  
      */
 114  
     public static Condition equalTo(final AttributeValue value) {
 115  14
         return new Condition()
 116  
             .withComparisonOperator(ComparisonOperator.EQ)
 117  
             .withAttributeValueList(value);
 118  
     }
 119  
 
 120  
     /**
 121  
      * With this condition.
 122  
      * @param name Attribute name
 123  
      * @param value The condition
 124  
      * @return New map of conditions
 125  
      */
 126  
     public Conditions with(final String name, final Condition value) {
 127  6
         return new Conditions(
 128  
             this.conds.with(name, value)
 129  
         );
 130  
     }
 131  
 
 132  
     /**
 133  
      * With this condition.
 134  
      * @param name Attribute name
 135  
      * @param value The condition
 136  
      * @return New map of conditions
 137  
      * @since 0.18
 138  
      */
 139  
     public Conditions with(final String name, final Object value) {
 140  0
         return new Conditions(
 141  
             this.conds.with(
 142  
                 name,
 143  
                 Conditions.equalTo(value)
 144  
             )
 145  
         );
 146  
     }
 147  
 
 148  
     /**
 149  
      * With these conditions.
 150  
      * @param map The conditions
 151  
      * @return New map of conditions
 152  
      */
 153  
     public Conditions withAttributes(final Map<String, AttributeValue> map) {
 154  10
         final ConcurrentMap<String, Condition> cnds =
 155  
             new ConcurrentHashMap<String, Condition>(map.size());
 156  5
         for (final Map.Entry<String, AttributeValue> entry : map.entrySet()) {
 157  5
             cnds.put(
 158  
                 entry.getKey(),
 159  
                 Conditions.equalTo(entry.getValue())
 160  
             );
 161  5
         }
 162  5
         return new Conditions(this.conds.with(cnds));
 163  
     }
 164  
 
 165  
     /**
 166  
      * With these conditions.
 167  
      * @param map The conditions
 168  
      * @return New map of conditions
 169  
      */
 170  
     public Conditions with(final Map<String, Condition> map) {
 171  0
         return new Conditions(this.conds.with(map));
 172  
     }
 173  
 
 174  
     @Override
 175  
     public String toString() {
 176  30
         final Collection<String> terms =
 177  
             new ArrayList<String>(this.conds.size());
 178  30
         for (final Map.Entry<String, Condition> cond : this.conds.entrySet()) {
 179  15
             terms.add(
 180  
                 String.format(
 181  
                     "%s %s %s",
 182  
                     cond.getKey(),
 183  
                     cond.getValue().getComparisonOperator(),
 184  
                     cond.getValue().getAttributeValueList()
 185  
                 )
 186  
             );
 187  15
         }
 188  30
         return Joiner.on(" AND ").join(terms);
 189  
     }
 190  
 
 191  
     @Override
 192  
     public int size() {
 193  2
         return this.conds.size();
 194  
     }
 195  
 
 196  
     @Override
 197  
     public boolean isEmpty() {
 198  20
         return this.conds.isEmpty();
 199  
     }
 200  
 
 201  
     @Override
 202  
     public boolean containsKey(final Object key) {
 203  0
         return this.conds.containsKey(key.toString());
 204  
     }
 205  
 
 206  
     @Override
 207  
     public boolean containsValue(final Object value) {
 208  0
         return this.conds.containsValue(value);
 209  
     }
 210  
 
 211  
     @Override
 212  
     public Condition get(final Object key) {
 213  0
         return this.conds.get(key.toString());
 214  
     }
 215  
 
 216  
     @Override
 217  
     public Set<String> keySet() {
 218  16
         return this.conds.keySet();
 219  
     }
 220  
 
 221  
     @Override
 222  
     public Collection<Condition> values() {
 223  20
         return this.conds.values();
 224  
     }
 225  
 
 226  
     @Override
 227  
     public Set<Map.Entry<String, Condition>> entrySet() {
 228  6
         return this.conds.entrySet();
 229  
     }
 230  
 
 231  
     @Override
 232  
     public Condition put(final String key, final Condition value) {
 233  0
         throw new UnsupportedOperationException(
 234  
             "Conditions class is immutable, can't do #put()"
 235  
         );
 236  
     }
 237  
 
 238  
     @Override
 239  
     public Condition remove(final Object key) {
 240  0
         throw new UnsupportedOperationException(
 241  
             "Conditions class is immutable, can't do #remove()"
 242  
         );
 243  
     }
 244  
 
 245  
     @Override
 246  
     public void putAll(final Map<? extends String, ? extends Condition> map) {
 247  0
         throw new UnsupportedOperationException(
 248  
             "Conditions class is immutable, can't do #putAll()"
 249  
         );
 250  
     }
 251  
 
 252  
     @Override
 253  
     public void clear() {
 254  0
         throw new UnsupportedOperationException(
 255  
             "Conditions class is immutable, can't do #clear()"
 256  
         );
 257  
     }
 258  
 
 259  
     /**
 260  
      * Convert map to ArrayMap.
 261  
      * @param map Map of them
 262  
      * @return Array map
 263  
      */
 264  
     private static ArrayMap<String, Condition> array(
 265  
         final Map<String, Condition> map) {
 266  28
         final ConcurrentMap<String, Condition> cnds =
 267  
             new ConcurrentHashMap<String, Condition>(map.size());
 268  28
         for (final Map.Entry<String, Condition> entry : map.entrySet()) {
 269  9
             cnds.put(
 270  
                 entry.getKey(),
 271  
                 entry.getValue()
 272  
             );
 273  9
         }
 274  28
         return new ArrayMap<String, Condition>(cnds);
 275  
     }
 276  
 
 277  
 }