1 | 16 | |
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.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 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
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 | |
|
77 | |
|
78 | |
private final transient ArrayMap<String, AttributeValue> attrs; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public Attributes() { |
84 | 33 | this(new ArrayMap<String, AttributeValue>()); |
85 | 33 | } |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | 99 | public Attributes(final Map<String, AttributeValue> map) { |
92 | 99 | this.attrs = new ArrayMap<String, AttributeValue>(map); |
93 | 99 | } |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
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 | |
|
107 | |
|
108 | |
|
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 | |
|
121 | |
|
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 | |
|
138 | |
|
139 | |
|
140 | |
|
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 | |
|
155 | |
|
156 | |
|
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 | |
} |