Interface As


  • public interface As
    Objects implementing this interface can provide am adapter of the required type. The adapter can be found with a variety of approaches that depend on the implementation. This capability can be used to implement a design based on the Data, Context and Interaction pattern (DCI). For further details, please look at the project website, where a tutorial is available.
    Author:
    Fabrizio Giudici
    Status: stable API
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static class  As.Type<T>
      A type reference for roles that can be used in place of a class literal, especially when roles with generics are used.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      default <T> T as​(As.Type<? extends T> type)
      Returns a role for this object of the specified type.
      default <T> T as​(java.lang.Class<? extends T> type)
      Returns an adapter to this object of the specified type.
      default <T> java.util.Collection<T> asMany​(As.Type<? extends T> type)
      Returns the requested role or an empty Optional.
      <T> java.util.Collection<T> asMany​(java.lang.Class<? extends T> type)
      Searches for multiple adapters of the given type and returns them.
      static As forObject​(java.lang.Object object)
      Creates an As implementation delegate for the given object (or returns the object itself if it is the default implementation of As).
      static As forObject​(java.lang.Object object, java.lang.Object role)
      Creates an As implementation delegate for the given object.
      static As forObject​(java.lang.Object object, java.util.Collection<java.lang.Object> roles)
      Creates an As implementation delegate for the given object.
      default <T> java.util.Optional<T> maybeAs​(As.Type<? extends T> type)
      Returns the requested role or an empty Optional.
      <T> java.util.Optional<T> maybeAs​(java.lang.Class<? extends T> type)
      Returns the requested role or an empty Optional.
      static <T> As.Type<T> type​(java.lang.Class<?> type)
      Creates a role type reference.
    • Method Detail

      • forObject

        @Nonnull
        static As forObject​(@Nonnull
                            java.lang.Object object)
        Creates an As implementation delegate for the given object (or returns the object itself if it is the default implementation of As).
        Parameters:
        object - the object
        Returns:
        the implementation
        Since:
        3.2-ALPHA-12
      • forObject

        @Nonnull
        static As forObject​(@Nonnull
                            java.lang.Object object,
                            @Nonnull
                            java.lang.Object role)
        Creates an As implementation delegate for the given object. It accepts a single pre-instantiated role, or a RoleFactory that will be invoked to create additional roles.
        Parameters:
        object - the object
        role - the role or RoleFactory
        Returns:
        the implementation
        Since:
        3.2-ALPHA-13
      • forObject

        @Nonnull
        static As forObject​(@Nonnull
                            java.lang.Object object,
                            @Nonnull
                            java.util.Collection<java.lang.Object> roles)
        Creates an As implementation delegate for the given object. It accepts a collection of pre-instantiated roles, or instances of RoleFactory that will be invoked to create additional roles.
        Parameters:
        object - the object
        roles - roles or RoleFactory instances
        Returns:
        the implementation
        Since:
        3.2-ALPHA-13
      • as

        @Nonnull
        default <T> T as​(@Nonnull
                         java.lang.Class<? extends T> type)
        Returns an adapter to this object of the specified type. If the implementation can find multiple compliant adapters, only one will be returned.
        Type Parameters:
        T - the static type
        Parameters:
        type - the dynamic type
        Returns:
        the adapter
        Throws:
        AsException - if no adapter is found
      • maybeAs

        @Nonnull
        <T> java.util.Optional<T> maybeAs​(@Nonnull
                                          java.lang.Class<? extends T> type)
        Returns the requested role or an empty Optional.
        Type Parameters:
        T - the static type
        Parameters:
        type - the dynamic type
        Returns:
        the optional role
        Since:
        3.2-ALPHA-3
      • asMany

        @Nonnull
        <T> java.util.Collection<T> asMany​(@Nonnull
                                           java.lang.Class<? extends T> type)
        Searches for multiple adapters of the given type and returns them.
        Type Parameters:
        T - the static type
        Parameters:
        type - the dynamic type
        Returns:
        a collection of adapters, possibly empty
      • type

        @Nonnull
        static <T> As.Type<T> type​(@Nonnull
                                   java.lang.Class<?> type)
        Creates a role type reference.
        Type Parameters:
        T - the static type
        Parameters:
        type - the dynamic type
        Returns:
        the type reference
        Since:
        3.2-ALPHA-12
      • as

        @Nonnull
        default <T> T as​(@Nonnull
                         As.Type<? extends T> type)
        Returns a role for this object of the specified type. If the implementation can find multiple compliant roles, only one will be returned.
        Type Parameters:
        T - the static type
        Parameters:
        type - the type reference
        Returns:
        the role
        Since:
        3.2-ALPHA-12
      • maybeAs

        @Nonnull
        default <T> java.util.Optional<T> maybeAs​(@Nonnull
                                                  As.Type<? extends T> type)
        Returns the requested role or an empty Optional.
        Type Parameters:
        T - the static type
        Parameters:
        type - the type reference
        Returns:
        the optional role
        Since:
        3.2-ALPHA-12
      • asMany

        @Nonnull
        default <T> java.util.Collection<T> asMany​(@Nonnull
                                                   As.Type<? extends T> type)
        Returns the requested role or an empty Optional.
        Type Parameters:
        T - the static type
        Parameters:
        type - the type reference
        Returns:
        the roles
        Since:
        3.2-ALPHA-12