Skip to content

Commit dc4c045

Browse files
adwsinghrhernandez35
authored andcommitted
Make ImplicitErrorIndex immutable and deterministic
1 parent 04b4339 commit dc4c045

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

framework-errors/src/main/java/software/amazon/smithy/framework/knowledge/ImplicitErrorIndex.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
package software.amazon.smithy.framework.knowledge;
77

8+
import java.util.Collections;
89
import java.util.HashMap;
9-
import java.util.HashSet;
1010
import java.util.Map;
1111
import java.util.Set;
12+
import java.util.TreeSet;
1213
import software.amazon.smithy.framework.traits.ImplicitErrorsTrait;
1314
import software.amazon.smithy.model.Model;
1415
import software.amazon.smithy.model.knowledge.KnowledgeIndex;
@@ -21,10 +22,13 @@
2122
*/
2223
public final class ImplicitErrorIndex implements KnowledgeIndex {
2324

24-
private final Map<ShapeId, Set<ShapeId>> serviceImplicitErrorMap = new HashMap<>();
25-
private final Set<ShapeId> implicitErrors = new HashSet<>();
25+
private final Map<ShapeId, Set<ShapeId>> serviceImplicitErrorMap;
26+
private final Set<ShapeId> implicitErrors;
2627

2728
private ImplicitErrorIndex(Model model) {
29+
var serviceImplicitError = new HashMap<ShapeId, Set<ShapeId>>();
30+
var allImplicitErrors = new TreeSet<ShapeId>();
31+
2832
for (var service : model.getServiceShapes()) {
2933
for (var traitEntry : service.getAllTraits().entrySet()) {
3034
var traitShapeOptional = model.getShape(traitEntry.getKey());
@@ -33,25 +37,33 @@ private ImplicitErrorIndex(Model model) {
3337
// has --allow-unknown-traits set to true.
3438
continue;
3539
}
40+
3641
var traitShape = traitShapeOptional.get();
3742
if (traitShape.hasTrait(ImplicitErrorsTrait.class)) {
3843
var implicitErrorsTrait = traitShape.expectTrait(ImplicitErrorsTrait.class);
39-
var implicitErrorList = serviceImplicitErrorMap.computeIfAbsent(
44+
var implicitErrorList = serviceImplicitError.computeIfAbsent(
4045
service.toShapeId(),
41-
k -> new HashSet<>());
46+
k -> new TreeSet<>());
4247
implicitErrorList.addAll(implicitErrorsTrait.getValues());
43-
implicitErrors.addAll(implicitErrorsTrait.getValues());
48+
allImplicitErrors.addAll(implicitErrorsTrait.getValues());
4449
}
4550
}
4651
}
52+
53+
var immutableServiceMap = new HashMap<ShapeId, Set<ShapeId>>();
54+
for (var entry : serviceImplicitError.entrySet()) {
55+
immutableServiceMap.put(entry.getKey(), Collections.unmodifiableSet(entry.getValue()));
56+
}
57+
this.serviceImplicitErrorMap = Collections.unmodifiableMap(immutableServiceMap);
58+
this.implicitErrors = Collections.unmodifiableSet(allImplicitErrors);
4759
}
4860

4961
public static ImplicitErrorIndex of(Model model) {
5062
return model.getKnowledge(ImplicitErrorIndex.class, ImplicitErrorIndex::new);
5163
}
5264

5365
public Set<ShapeId> getImplicitErrorsForService(ToShapeId toShapeId) {
54-
return serviceImplicitErrorMap.computeIfAbsent(toShapeId.toShapeId(), k -> new HashSet<>());
66+
return serviceImplicitErrorMap.getOrDefault(toShapeId.toShapeId(), Collections.emptySet());
5567
}
5668

5769
public boolean isImplicitError(ShapeId shapeId) {

0 commit comments

Comments
 (0)