Class Model
- java.lang.Object
-
- software.amazon.smithy.model.Model
-
- All Implemented Interfaces:
ToSmithyBuilder<Model>
public final class Model extends java.lang.Object implements ToSmithyBuilder<Model>
A Smithy model that contains shapes, traits, metadata, and various computed information used to interpret the model.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classModel.BuilderBuilder used to create a Model.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringMODEL_VERSIONSpecifies the highest supported version of the IDL.
-
Method Summary
Modifier and Type Method Description static ModelAssemblerassembler()Assembles and validates a Smithy model from files, nodes, and other disparate sources.static ModelAssemblerassembler(java.lang.ClassLoader classLoader)Creates aModelAssemblerthat is configured to discover traits, validators, and built-in validators using the givenClassLoader.static Model.Builderbuilder()Builds an explicitly configured Smithy model.booleanequals(java.lang.Object other)ShapeexpectShape(ShapeId id)<T extends Shape>
TexpectShape(ShapeId id, java.lang.Class<T> type)java.util.Set<ShapeId>getAppliedTraits()Gets a set of every trait shape ID that is used in the model.<T extends KnowledgeIndex>
TgetKnowledge(java.lang.Class<T> type)Gets a computed knowledge index of a specific type for the model.java.util.Map<java.lang.String,Node>getMetadata()java.util.Optional<Node>getMetadataProperty(java.lang.String name)Gets a metadata property by namespace and name.java.util.Optional<Shape>getShape(ShapeId id)java.util.Set<Shape>getShapesWithTrait(java.lang.Class<? extends Trait> trait)Gets a set of shapes in the model marked with a specific trait.java.util.Set<Shape>getShapesWithTrait(ToShapeId trait)Gets a set of shapes in the model marked with a specific trait.java.util.Optional<TraitDefinition>getTraitDefinition(ToShapeId traitId)Gets the trait definition of a specific trait shape ID.inthashCode()booleanisTraitApplied(java.lang.Class<? extends Trait> trait)Returns true if the given trait shape ID was used in the model.java.util.stream.Stream<Shape>shapes()Gets a stream ofShapes in the index.<T extends Shape>
java.util.stream.Stream<T>shapes(java.lang.Class<T> shapeType)Gets a stream of shapes in the index of a specific typeT.Model.BuildertoBuilder()Take this object and create a builder that contains all of the current property values of this object.java.util.Set<Shape>toSet()Converts the model to an immutable Set of shapes.
-
-
-
Field Detail
-
MODEL_VERSION
public static final java.lang.String MODEL_VERSION
Specifies the highest supported version of the IDL.- See Also:
- Constant Field Values
-
-
Method Detail
-
builder
public static Model.Builder builder()
Builds an explicitly configured Smithy model.Note that the builder does not validate the correctness of the model. Use the
assembler()method to build and validate a model.- Returns:
- Returns a model builder.
- See Also:
assembler()
-
assembler
public static ModelAssembler assembler()
Assembles and validates a Smithy model from files, nodes, and other disparate sources.- Returns:
- Returns a model assembler.
-
assembler
public static ModelAssembler assembler(java.lang.ClassLoader classLoader)
Creates aModelAssemblerthat is configured to discover traits, validators, and built-in validators using the givenClassLoader.- Parameters:
classLoader- Class loader used to discover services.- Returns:
- Returns a model assembler.
-
getMetadataProperty
public java.util.Optional<Node> getMetadataProperty(java.lang.String name)
Gets a metadata property by namespace and name.- Parameters:
name- Name of the property to retrieve.- Returns:
- Returns the optional property.
-
getMetadata
public java.util.Map<java.lang.String,Node> getMetadata()
- Returns:
- Gets the unmodifiable metadata of the model across all namespaces.
-
getTraitDefinition
public java.util.Optional<TraitDefinition> getTraitDefinition(ToShapeId traitId)
Gets the trait definition of a specific trait shape ID.- Parameters:
traitId- ID of the shape to get the trait definition of.- Returns:
- Returns the optionally found trait definition.
-
getShapesWithTrait
public java.util.Set<Shape> getShapesWithTrait(ToShapeId trait)
Gets a set of shapes in the model marked with a specific trait.- Parameters:
trait- Trait shape ID to look for on shapes.- Returns:
- Returns the immutable set of matching shapes.
-
getShapesWithTrait
public java.util.Set<Shape> getShapesWithTrait(java.lang.Class<? extends Trait> trait)
Gets a set of shapes in the model marked with a specific trait.The result is an exact match on trait classes and does not utilize any kind of polymorphic instance of checks.
- Parameters:
trait- Trait class to look for on shapes.- Returns:
- Returns the immutable set of matching shapes.
-
getAppliedTraits
public java.util.Set<ShapeId> getAppliedTraits()
Gets a set of every trait shape ID that is used in the model.- Returns:
- Returns the shape IDs of traits used in the model.
-
isTraitApplied
public boolean isTraitApplied(java.lang.Class<? extends Trait> trait)
Returns true if the given trait shape ID was used in the model.- Parameters:
trait- The trait class to check.- Returns:
- Returns true if the trait was used in the model.
-
getShape
public java.util.Optional<Shape> getShape(ShapeId id)
- Parameters:
id- Shape to retrieve by ID.- Returns:
- Returns the optional shape.
-
expectShape
public Shape expectShape(ShapeId id)
- Parameters:
id- Shape to retrieve by ID.- Returns:
- Returns the shape.
- Throws:
ExpectationNotMetException- if the shape is not found.
-
expectShape
public <T extends Shape> T expectShape(ShapeId id, java.lang.Class<T> type)
Attempts to retrieve aShapebyShapeIdand throws if not found or if the shape is not of the expected type.- Type Parameters:
T- Expected shape type.- Parameters:
id- Shape to retrieve by ID.type- Shape type to expect and convert to.- Returns:
- Returns the shape.
- Throws:
ExpectationNotMetException- if the shape is not found or is not the expected type.
-
shapes
public java.util.stream.Stream<Shape> shapes()
Gets a stream ofShapes in the index.- Returns:
- Returns a stream of shapes.
-
shapes
public <T extends Shape> java.util.stream.Stream<T> shapes(java.lang.Class<T> shapeType)
Gets a stream of shapes in the index of a specific typeT.The provided shapeType class must exactly match the class of a shape in the model in order to be returned from this method; that is, the provided class must be a concrete subclass of
Shapeand not an abstract class likeNumberShape.- Type Parameters:
T- Shape type to stream from the index.- Parameters:
shapeType- Shape typeTto retrieve.- Returns:
- A stream of shapes of
TmatchingshapeType.
-
toSet
public java.util.Set<Shape> toSet()
Converts the model to an immutable Set of shapes.- Returns:
- Returns an unmodifiable set of shapes.
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toBuilder
public Model.Builder toBuilder()
Description copied from interface:ToSmithyBuilderTake this object and create a builder that contains all of the current property values of this object.- Specified by:
toBuilderin interfaceToSmithyBuilder<Model>- Returns:
- a builder for type T
-
getKnowledge
public <T extends KnowledgeIndex> T getKnowledge(java.lang.Class<T> type)
Gets a computed knowledge index of a specific type for the model.If a
KnowledgeIndexof the given type has not yet been computed, one will be created using a constructor of the given type that accepts aModel. Computed knowledge indexes are cached and returned on subsequent retrievals.Using this method is preferred over directly instantiating instances of a KnowledgeIndex if the KnowledgeIndex required in various unrelated code paths where passing around an instance of a KnowledgeIndex is not practical or impossible.
- Type Parameters:
T- The type of knowledge index to retrieve.- Parameters:
type- Type of knowledge index to retrieve.- Returns:
- Returns the computed knowledge index.
-
-