Class TypeUsageBuilder


  • public class TypeUsageBuilder
    extends Object
    Builds a TypeUsage for a Type from a ModelKnowledge and attaches it to the type as an additional property (see Type.setAdditionalProperty(String, Object)) rather than as a new field on the Type hierarchy itself. This way a Type's JSON shape is identical whether or not usage was requested -- existing consumers that don't know about it are unaffected (Type already declares @JsonIgnoreProperties(ignoreUnknown = true)) -- and the same attach(Type, ModelKnowledge) call (or, for a whole knowledge base at once, attachAll(ModelKnowledge)) is reusable by any caller that already has a Type and a ModelKnowledge: today's documentation generator, and potentially a future opt-in enrichment on a live type-lookup service.

    Deliberately a separate class from TypeUsage itself: TypeUsage is plain data, and ModelKnowledge already stores TypeUsage instances internally (see ModelKnowledge.staticTypeUsageByName) -- if TypeUsage also knew how to read from a ModelKnowledge, the two classes would depend on each other in both directions. This class depends on both Type and ModelKnowledge and TypeUsage; neither of those depends on this one.

    Author:
    Luca Frosini (ISTI - CNR)
    • Constructor Detail

      • TypeUsageBuilder

        public TypeUsageBuilder()
    • Method Detail

      • attach

        public static void attach​(Type type,
                                  ModelKnowledge<Type,​?> modelKnowledge)
        Builds the TypeUsage for type from modelKnowledge and attaches it to type itself as an additional property. A no-op for any AccessType not in AccessType.getModelTypes() (nothing to attach).
        Parameters:
        type - The type to enrich, mutated in place.
        modelKnowledge - The knowledge base to source usage data from.
      • from

        public static TypeUsage from​(Type type)
        Reads back the TypeUsage previously attached to type via attach(Type, ModelKnowledge), if any.
        Parameters:
        type - The type to read from.
        Returns:
        The attached TypeUsage, or null if none was attached.
      • attachAll

        public static void attachAll​(ModelKnowledge<Type,​?> modelKnowledge)
        Attaches usage to every type known to modelKnowledge (every type in every DAG of AccessType.getModelTypes()), one pass over the whole knowledge base.

        Deliberately not done per-type as each type is added (ModelKnowledge.addType()): usage is an aggregate view that keeps changing as later types are added (see ModelKnowledge.registerAssociation()'s source-vs-target fallback), so it only settles once the knowledge base is complete. Call this once, after every type has been added -- from then on, every Type extracted from modelKnowledge already carries its TypeUsage, with no further per-type call needed at the call site.

        Parameters:
        modelKnowledge - The complete knowledge base to enrich every type of.