| 1 | 2 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 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 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 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 | |
|
| 76 | |
|
| 77 | |
private final transient ArrayMap<String, Condition> conds; |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
public Conditions() { |
| 83 | 19 | this(new ArrayMap<String, Condition>()); |
| 84 | 19 | } |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | 28 | public Conditions(final Map<String, Condition> map) { |
| 91 | 28 | this.conds = Conditions.array(map); |
| 92 | 28 | } |
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 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 | |
|
| 111 | |
|
| 112 | |
|
| 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 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 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 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 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 | |
|
| 150 | |
|
| 151 | |
|
| 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 | |
|
| 167 | |
|
| 168 | |
|
| 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 | |
|
| 261 | |
|
| 262 | |
|
| 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 | |
} |