Interface TypeVisitor<T,U>

Type Parameters:
T - A data type to pass around through calls.
U - A type that visiting methods must return.

public interface TypeVisitor<T,U>

A Visitor interface for descending through an arbitrary type 'tree' - the other half of Type.visit(TypeVisitor, Object). Allows calling code to traverse through atomic and compound types without having to know in advance about every single type class.

For the purposes of this interface, an atomic types is defined as one whose identity is not defined by other types and where it is not possible to recurse in to it to visit other types. A "compound type" is one whose identity is based on other sub-types, and so can be visited with the visitor.

Note that these concepts are quite specific to the visitor and are mostly orthogonal to concepts like a SimpleType or the two construction methods available on a TypeConstructor

Types are required to implement Type.visit(TypeVisitor, Object), which should in turn call either atomicType(Type, Object) or compoundType(Type, List, Object), depending on which sort of Type it is, e.g. does the type declare any other types that are part of its identity.

Compound types should pass a list of all the types that form part of their identity to the compoundType visiting method, along with some optional metadata for each child. The metadata can be used to disambiguate children and provide context where required, the NO_META object is available of a child does not need any metadata. There is no need to provide context that is not already implicit based on the parent type.

The TypeVisitor interface does not control the flow of visiting - an implementor might decide to implement a DFS or a BFS, and is not obliged or forced (via the API) to descend in to all types that are part of the tree.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Object
    Null-alternative when the child of a compound type has no metadata
  • Method Summary

    Modifier and Type
    Method
    Description
    atomicType(Type atomicType, T data)
    Visit an atomic type - there is no further recursion possible here
    static <T> T
    bfs(Type root, T visitData, BiConsumer<T,Type> consumer)
    Convenience function for doing a breadth first search of a type, passing the given visit data to each seen type.
    static List<Pair<Type,?>>
    children(Type type0)
    Handy list constructor for a single child type with no metadata
    static List<Pair<Type,?>>
    children(Type type0, Object meta0)
    Handy list constructor for a single child type with metadata
    static List<Pair<Type,?>>
    children(Type type0, Object meta0, Type type1, Object meta1)
    Handy list constructor for two child types with metadata
    static List<Pair<Type,?>>
    children(Type type0, Type type1)
    Handy list constructor for two child types with no metadata
    compoundType(Type compoundType, List<Pair<Type,?>> children, T data)
    Visit a compound type, allowing possible recursion via the list of children.
  • Field Details

    • NO_META

      static final Object NO_META

      Null-alternative when the child of a compound type has no metadata

  • Method Details

    • bfs

      static <T> T bfs(Type root, T visitData, BiConsumer<T,Type> consumer)

      Convenience function for doing a breadth first search of a type, passing the given visit data to each seen type. Lets you do simple one liners, e.g. List<Type> types = bfs(someType, new LinkedList<Type>(), (l, t) -> l.add(t))

    • children

      static List<Pair<Type,?>> children(Type type0)

      Handy list constructor for a single child type with no metadata

    • children

      static List<Pair<Type,?>> children(Type type0, Object meta0)

      Handy list constructor for a single child type with metadata

    • children

      static List<Pair<Type,?>> children(Type type0, Type type1)

      Handy list constructor for two child types with no metadata

    • children

      static List<Pair<Type,?>> children(Type type0, Object meta0, Type type1, Object meta1)

      Handy list constructor for two child types with metadata

    • atomicType

      U atomicType(Type atomicType, T data)

      Visit an atomic type - there is no further recursion possible here

    • compoundType

      U compoundType(Type compoundType, List<Pair<Type,?>> children, T data)

      Visit a compound type, allowing possible recursion via the list of children. It is up to the implementor to do any recursive visiting by calling the visit method on any children and also to decide/declare traversal order.