1 | 0 | |
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.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 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
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 | |
|
70 | |
|
71 | |
private final transient ArrayMap<String, AttributeValueUpdate> attrs; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public AttributeUpdates() { |
77 | 20 | this(new ArrayMap<String, AttributeValueUpdate>()); |
78 | 20 | } |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | 43 | public AttributeUpdates(final Map<String, AttributeValueUpdate> map) { |
85 | 43 | this.attrs = new ArrayMap<String, AttributeValueUpdate>(map); |
86 | 43 | } |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
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 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
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 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
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 | |
|
135 | |
|
136 | |
|
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 | |
} |