|
17 | 17 |
|
18 | 18 | import java.util.ArrayList; |
19 | 19 | import java.util.Collections; |
| 20 | +import java.util.HashMap; |
20 | 21 | import java.util.List; |
21 | 22 | import java.util.Map; |
| 23 | +import java.util.StringJoiner; |
22 | 24 | import java.util.stream.Collectors; |
23 | 25 | import java.util.stream.Stream; |
24 | 26 | import software.amazon.awssdk.annotations.SdkInternalApi; |
@@ -113,53 +115,89 @@ public static List<String> findAttributeNames(UpdateExpression updateExpression) |
113 | 115 | private static List<String> groupExpressions(UpdateExpression expression) { |
114 | 116 | List<String> groupExpressions = new ArrayList<>(); |
115 | 117 | if (!expression.setActions().isEmpty()) { |
116 | | - groupExpressions.add(SET + expression.setActions().stream() |
117 | | - .map(a -> String.format("%s = %s", a.path(), a.value())) |
118 | | - .collect(Collectors.joining(ACTION_SEPARATOR))); |
| 118 | + StringJoiner joiner = new StringJoiner(ACTION_SEPARATOR, SET, ""); |
| 119 | + expression.setActions().forEach(a -> joiner.add(a.path() + " = " + a.value())); |
| 120 | + groupExpressions.add(joiner.toString()); |
119 | 121 | } |
120 | 122 | if (!expression.removeActions().isEmpty()) { |
121 | | - groupExpressions.add(REMOVE + expression.removeActions().stream() |
122 | | - .map(RemoveAction::path) |
123 | | - .collect(Collectors.joining(ACTION_SEPARATOR))); |
| 123 | + StringJoiner joiner = new StringJoiner(ACTION_SEPARATOR, REMOVE, ""); |
| 124 | + expression.removeActions().forEach(a -> joiner.add(a.path())); |
| 125 | + groupExpressions.add(joiner.toString()); |
124 | 126 | } |
125 | 127 | if (!expression.deleteActions().isEmpty()) { |
126 | | - groupExpressions.add(DELETE + expression.deleteActions().stream() |
127 | | - .map(a -> String.format("%s %s", a.path(), a.value())) |
128 | | - .collect(Collectors.joining(ACTION_SEPARATOR))); |
| 128 | + StringJoiner joiner = new StringJoiner(ACTION_SEPARATOR, DELETE, ""); |
| 129 | + expression.deleteActions().forEach(a -> joiner.add(a.path() + " " + a.value())); |
| 130 | + groupExpressions.add(joiner.toString()); |
129 | 131 | } |
130 | 132 | if (!expression.addActions().isEmpty()) { |
131 | | - groupExpressions.add(ADD + expression.addActions().stream() |
132 | | - .map(a -> String.format("%s %s", a.path(), a.value())) |
133 | | - .collect(Collectors.joining(ACTION_SEPARATOR))); |
| 133 | + StringJoiner joiner = new StringJoiner(ACTION_SEPARATOR, ADD, ""); |
| 134 | + expression.addActions().forEach(a -> joiner.add(a.path() + " " + a.value())); |
| 135 | + groupExpressions.add(joiner.toString()); |
134 | 136 | } |
135 | 137 | return groupExpressions; |
136 | 138 | } |
137 | 139 |
|
138 | | - private static Stream<Map<String, String>> streamOfExpressionNames(UpdateExpression expression) { |
139 | | - return Stream.concat(expression.setActions().stream().map(SetAction::expressionNames), |
140 | | - Stream.concat(expression.removeActions().stream().map(RemoveAction::expressionNames), |
141 | | - Stream.concat(expression.deleteActions().stream() |
142 | | - .map(DeleteAction::expressionNames), |
143 | | - expression.addActions().stream() |
144 | | - .map(AddAction::expressionNames)))); |
| 140 | + private static Map<String, AttributeValue> mergeExpressionValues(UpdateExpression expression) { |
| 141 | + Map<String, AttributeValue> merged = new HashMap<>(); |
| 142 | + |
| 143 | + for (SetAction action : expression.setActions()) { |
| 144 | + mergeValuesInto(merged, action.expressionValues()); |
| 145 | + } |
| 146 | + for (DeleteAction action : expression.deleteActions()) { |
| 147 | + mergeValuesInto(merged, action.expressionValues()); |
| 148 | + } |
| 149 | + for (AddAction action : expression.addActions()) { |
| 150 | + mergeValuesInto(merged, action.expressionValues()); |
| 151 | + } |
| 152 | + |
| 153 | + return merged.isEmpty() ? Collections.emptyMap() : Collections.unmodifiableMap(merged); |
145 | 154 | } |
146 | 155 |
|
147 | | - private static Map<String, AttributeValue> mergeExpressionValues(UpdateExpression expression) { |
148 | | - return streamOfExpressionValues(expression) |
149 | | - .reduce(Expression::joinValues) |
150 | | - .orElseGet(Collections::emptyMap); |
| 156 | + private static Map<String, String> mergeExpressionNames(UpdateExpression expression) { |
| 157 | + Map<String, String> merged = new HashMap<>(); |
| 158 | + |
| 159 | + for (SetAction action : expression.setActions()) { |
| 160 | + mergeNamesInto(merged, action.expressionNames()); |
| 161 | + } |
| 162 | + for (RemoveAction action : expression.removeActions()) { |
| 163 | + mergeNamesInto(merged, action.expressionNames()); |
| 164 | + } |
| 165 | + for (DeleteAction action : expression.deleteActions()) { |
| 166 | + mergeNamesInto(merged, action.expressionNames()); |
| 167 | + } |
| 168 | + for (AddAction action : expression.addActions()) { |
| 169 | + mergeNamesInto(merged, action.expressionNames()); |
| 170 | + } |
| 171 | + |
| 172 | + return merged.isEmpty() ? Collections.emptyMap() : Collections.unmodifiableMap(merged); |
151 | 173 | } |
152 | 174 |
|
153 | | - private static Stream<Map<String, AttributeValue>> streamOfExpressionValues(UpdateExpression expression) { |
154 | | - return Stream.concat(expression.setActions().stream().map(SetAction::expressionValues), |
155 | | - Stream.concat(expression.deleteActions().stream().map(DeleteAction::expressionValues), |
156 | | - expression.addActions().stream().map(AddAction::expressionValues))); |
| 175 | + private static void mergeNamesInto(Map<String, String> target, Map<String, String> source) { |
| 176 | + if (source == null || source.isEmpty()) { |
| 177 | + return; |
| 178 | + } |
| 179 | + source.forEach((key, value) -> { |
| 180 | + String oldValue = target.put(key, value); |
| 181 | + if (oldValue != null && !oldValue.equals(value)) { |
| 182 | + throw new IllegalArgumentException( |
| 183 | + String.format("Attempt to coalesce two expressions with conflicting expression names. " |
| 184 | + + "Expression name key = '%s'", key)); |
| 185 | + } |
| 186 | + }); |
157 | 187 | } |
158 | 188 |
|
159 | | - private static Map<String, String> mergeExpressionNames(UpdateExpression expression) { |
160 | | - return streamOfExpressionNames(expression) |
161 | | - .reduce(Expression::joinNames) |
162 | | - .orElseGet(Collections::emptyMap); |
| 189 | + private static void mergeValuesInto(Map<String, AttributeValue> target, Map<String, AttributeValue> source) { |
| 190 | + if (source == null || source.isEmpty()) { |
| 191 | + return; |
| 192 | + } |
| 193 | + source.forEach((key, value) -> { |
| 194 | + AttributeValue oldValue = target.put(key, value); |
| 195 | + if (oldValue != null && !oldValue.equals(value)) { |
| 196 | + throw new IllegalArgumentException( |
| 197 | + String.format("Attempt to coalesce two expressions with conflicting expression values. " |
| 198 | + + "Expression value key = '%s'", key)); |
| 199 | + } |
| 200 | + }); |
163 | 201 | } |
164 | 202 |
|
165 | 203 | private static List<String> listPathsWithoutTokens(UpdateExpression expression) { |
|
0 commit comments