software.amazon.smithy.model.shapes.IntEnumShape Maven / Gradle / Ivy
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package software.amazon.smithy.model.shapes;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import software.amazon.smithy.model.SourceException;
import software.amazon.smithy.model.traits.EnumValueTrait;
import software.amazon.smithy.model.traits.UnitTypeTrait;
import software.amazon.smithy.utils.BuilderRef;
public final class IntEnumShape extends IntegerShape {
private final Map members;
private volatile Map enumValues;
private IntEnumShape(Builder builder) {
super(builder);
members = NamedMemberUtils.computeMixinMembers(
builder.getMixins(), builder.members, getId(), getSourceLocation());
validateMemberShapeIds();
if (members.size() < 1) {
throw new SourceException("intEnum shapes must have at least one member", getSourceLocation());
}
}
/**
* Gets a map of enum member names to their corresponding values.
*
* @return A map of member names to enum values.
*/
public Map getEnumValues() {
if (enumValues == null) {
Map values = new LinkedHashMap<>(members.size());
for (MemberShape member : members()) {
values.put(member.getMemberName(), member.expectTrait(EnumValueTrait.class).expectIntValue());
}
enumValues = Collections.unmodifiableMap(values);
}
return enumValues;
}
/**
* Gets the members of the shape, including mixin members.
*
* @return Returns the immutable member map.
*/
@Override
public Map getAllMembers() {
return members;
}
public static Builder builder() {
return new Builder();
}
@Override
public Builder toBuilder() {
return (Builder) updateBuilder(builder());
}
@Override
public R accept(ShapeVisitor cases) {
return cases.intEnumShape(this);
}
@Override
public Optional asIntEnumShape() {
return Optional.of(this);
}
@Override
public ShapeType getType() {
return ShapeType.INT_ENUM;
}
/**
* Builder used to create a {@link IntegerShape}.
*/
public static class Builder extends IntegerShape.Builder {
private final BuilderRef