From a5efba94514c5466bcb1c74891f624aa0482d43b Mon Sep 17 00:00:00 2001 From: Miles Ziemer Date: Wed, 24 May 2023 11:39:17 -0400 Subject: [PATCH] Use array to return required members --- .../smithy/model/shapes/CollectionShape.java | 4 +-- .../amazon/smithy/model/shapes/MapShape.java | 6 ++-- .../amazon/smithy/model/shapes/Shape.java | 33 +++++++++---------- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/shapes/CollectionShape.java b/smithy-model/src/main/java/software/amazon/smithy/model/shapes/CollectionShape.java index 06d248ce981..5d88c25573b 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/shapes/CollectionShape.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/shapes/CollectionShape.java @@ -36,8 +36,8 @@ public abstract class CollectionShape extends Shape { CollectionShape(Builder builder) { super(builder, false); - Map members = getRequiredMembers(builder, "member"); - member = members.get("member"); + MemberShape[] members = getRequiredMembers(builder, "member"); + member = members[0]; validateMemberShapeIds(); } diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/shapes/MapShape.java b/smithy-model/src/main/java/software/amazon/smithy/model/shapes/MapShape.java index ad975d6dd36..cab5ed6a800 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/shapes/MapShape.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/shapes/MapShape.java @@ -43,9 +43,9 @@ public final class MapShape extends Shape implements ToSmithyBuilder { private MapShape(Builder builder) { super(builder, false); - Map members = getRequiredMembers(builder, "key", "value"); - key = members.get("key"); - value = members.get("value"); + MemberShape[] members = getRequiredMembers(builder, "key", "value"); + key = members[0]; + value = members[1]; validateMemberShapeIds(); } diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/shapes/Shape.java b/smithy-model/src/main/java/software/amazon/smithy/model/shapes/Shape.java index 60ced14f2cc..847501cb888 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/shapes/Shape.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/shapes/Shape.java @@ -109,18 +109,18 @@ protected void validateMixins(Map mixins, Map in } } - protected Map getRequiredMembers( - AbstractShapeBuilder builder, - String... requiredMembersNames - ) { + protected MemberShape[] getRequiredMembers(AbstractShapeBuilder builder, String... requiredMembersNames) { // Only lists/sets and maps have required members, the maximum number being 2. List missingMembersNames = new ArrayList<>(2); - Map members = new HashMap<>(2); - for (String requiredMemberName : requiredMembersNames) { - Optional member = getRequiredMixinMember(builder, requiredMemberName); - if (member.isPresent()) { - members.put(requiredMemberName, member.get()); + // Caller knows the order of provided member names, so we don't need a dynamic data structure. + MemberShape[] members = new MemberShape[requiredMembersNames.length]; + + for (int memberNameIndex = 0; memberNameIndex < requiredMembersNames.length; memberNameIndex++) { + String requiredMemberName = requiredMembersNames[memberNameIndex]; + MemberShape member = getRequiredMixinMember(builder, requiredMemberName); + if (member != null) { + members[memberNameIndex] = member; } else { missingMembersNames.add(requiredMemberName); } @@ -133,13 +133,10 @@ protected Map getRequiredMembers( return members; } - protected Optional getRequiredMixinMember( - AbstractShapeBuilder builder, - String requiredMemberName - ) { + private MemberShape getRequiredMixinMember(AbstractShapeBuilder builder, String requiredMemberName) { Optional memberOnBuilder = builder.getMember(requiredMemberName); if (memberOnBuilder.isPresent()) { - return memberOnBuilder; + return memberOnBuilder.get(); } // Get the most recently introduced mixin member with the given name. @@ -155,18 +152,18 @@ protected Optional getRequiredMixinMember( } if (mostRecentMember == null) { - return Optional.empty(); + return null; } - return Optional.of(MemberShape.builder() + return MemberShape.builder() .id(getId().withMember(requiredMemberName)) .target(mostRecentMember.getTarget()) .source(getSourceLocation()) .addMixin(mostRecentMember) - .build()); + .build(); } - protected SourceException missingRequiredMembersException(List missingMembersNames) { + private SourceException missingRequiredMembersException(List missingMembersNames) { String missingRequired = missingMembersNames.size() > 1 ? "members" : "member"; String missingMembers = String.join(", ", missingMembersNames); String message = String.format("Missing required %s of shape `%s`: %s",