Class AbstractTrait

java.lang.Object
software.amazon.smithy.model.traits.AbstractTrait
All Implemented Interfaces:
FromSourceLocation, ToNode, ToShapeId, Trait
Direct Known Subclasses:
ArnReferenceTrait, ArnTrait, AuthDefinitionTrait, AuthorizersTrait, AuthTrait, AwsProtocolTrait, AwsQueryErrorTrait, CfnResourceTrait, ClientContextParamsTrait, ClientDiscoveredEndpointTrait, ClientEndpointDiscoveryTrait, CognitoUserPoolsTrait, ContextParamTrait, CorsTrait, DefaultTrait, DefineConditionKeysTrait, DeprecatedTrait, DynamicTrait, EndpointRuleSetTrait, EndpointTestsTrait, EndpointTrait, EnumTrait, EnumValueTrait, ExamplesTrait, ExternalDocumentationTrait, HttpApiKeyAuthTrait, HttpChecksumTrait, HttpErrorTrait, HttpMalformedRequestTestsTrait, HttpRequestTestsTrait, HttpResponseTestsTrait, HttpTrait, IamResourceTrait, IdRefTrait, IntegrationTrait, LengthTrait, MixinTrait, MockIntegrationTrait, OriginalShapeIdTrait, PaginatedTrait, PropertyTrait, ProtocolDefinitionTrait, RangeTrait, RecommendedTrait, ReferencesTrait, RetryableTrait, ServiceTrait, SigV4Trait, StaticContextParamsTrait, StringListTrait, StringTrait, TagEnabledTrait, TaggableTrait, TraitDefinition, WaitableTrait, XmlNamespaceTrait

public abstract class AbstractTrait extends Object implements Trait
Base implementation of traits.

This implementation provides an equals(java.lang.Object) and hashCode() that should work for most traits that extend from this base class. Note that equality for traits that extend from this type are not based on the concrete class, but rather the trait name and the trait's ToNode representation.

The Node value of a trait can be provided when the trait is created using setNodeCache(Node). Note that when setting the node cache, the equality and hashcode of the trait are impacted because they are by default based on thetoNode() value of a trait. This typically isn't an issue until model transformations are performed that modify a trait. In these cases, the original node value of the trait might differ from the updated trait even if they are semantically the same value (for example, if the only change to the trait is modifying its source location, or if a property of the trait was explicitly set to false, but false is omitted when serializing the updated trait to a node value). If this use case needs to be accounted for, you must override equals and hashCode of the trait.

  • Constructor Details

    • AbstractTrait

      public AbstractTrait(ShapeId id, FromSourceLocation sourceLocation)
      Parameters:
      id - ID of the trait.
      sourceLocation - Where the trait was defined.
    • AbstractTrait

      public AbstractTrait(ShapeId id, Node nodeValue)
      Parameters:
      id - ID of the trait.
      nodeValue - The node representation of the shape, if known and trusted.
  • Method Details

    • toShapeId

      public final ShapeId toShapeId()
      Description copied from interface: Trait
      Gets the shape ID of the trait.
      Specified by:
      toShapeId in interface ToShapeId
      Specified by:
      toShapeId in interface Trait
      Returns:
      Returns the fully-qualified shape ID of the trait.
    • getSourceLocation

      public final SourceLocation getSourceLocation()
      Description copied from interface: FromSourceLocation
      Gets the source location of a value.
      Specified by:
      getSourceLocation in interface FromSourceLocation
      Returns:
      Returns the source location of the value.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toNode

      public final Node toNode()
      Description copied from interface: ToNode
      Converts a value to a Node.
      Specified by:
      toNode in interface ToNode
      Returns:
      Returns the creates Node.
    • createNode

      protected abstract Node createNode()
      The result of toNode is used for hashCodes and equality. Subclasses must implement createNode to turn the trait into a Node. This is then cached for subsequent retrievals.
      Returns:
      Returns the trait as a node.
    • setNodeCache

      protected final void setNodeCache(Node value)
      Sets the node cache of the trait up front, if known.

      This is useful for maintaining a trait value exactly as provided in a model file, allowing for validation to detect extraneous properties, and removing the need to create the node again when calling createNode.

      Parameters:
      value - Value to set.