Class IModelExtensions


  • public class IModelExtensions
    extends Object
    Meta-programming extensions to the IModel class.
    Author:
    zhanhb, Emanuel Rabina
    • Constructor Detail

      • IModelExtensions

        public IModelExtensions()
    • Method Detail

      • asBoolean

        public static boolean asBoolean​(@Nullable
                                        IModel self)
        Set that a model evaluates to 'false' if it has no events.
        Parameters:
        self -
        Returns:
        true if this model has events.
      • childModelIterator

        @Nullable
        public static Iterator<IModel> childModelIterator​(@Nonnull
                                                          IModel self)
        If this model represents an element, then this method returns an iterator over any potential child items as models of their own.
        Parameters:
        self -
        Returns:
        New model iterator.
      • equals

        public static boolean equals​(IModel self,
                                     Object other)
        Compare 2 models, returning true if all of the model's events are equal.
        Parameters:
        self -
        other -
        Returns:
        true if this model is the same as the other one.
      • everyWithIndex

        public static boolean everyWithIndex​(@Nullable
                                             IModel self,
                                             @Nonnull
                                             BiPredicate<ITemplateEvent,​Integer> closure)
        Return true only if all the events in the model return true for the given closure.
        Parameters:
        self -
        closure -
        Returns:
        true if every event satisfies the closure.
      • findIndexOf

        public static int findIndexOf​(@Nonnull
                                      IModel self,
                                      @Nonnull
                                      Predicate<ITemplateEvent> closure)
        Returns the index of the first event in the model that meets the criteria of the given closure.
        Parameters:
        self -
        closure -
        Returns:
        The index of the first event to match the closure criteria, or -1 if nothing matched.
      • findIndexOfModel

        public static int findIndexOfModel​(@Nonnull
                                           IModel self,
                                           IModel model)
        A special variant of findIndexOf that uses models, as I seem to be using those a lot.

        This doesn't use an equality check, but an object reference check, so if a submodel is ever located from a parent (eg: any of the find methods, you can use this method to find the location of that submodel within the event queue.

        Parameters:
        self -
        model -
        Returns:
        Index of an extracted submodel within this model.
      • findModel

        public static IModel findModel​(@Nonnull
                                       IModel self,
                                       @Nonnull
                                       Predicate<ITemplateEvent> closure)
        Returns the first instance of a model that meets the given closure criteria.
        Parameters:
        self -
        closure -
        Returns:
        A model over the event that matches the closure criteria, or null if nothing matched.
      • first

        public static ITemplateEvent first​(@Nonnull
                                           IModel self)
        Returns the first event on the model.
        Parameters:
        self -
        Returns:
        The model's first event.
      • getModel

        @Nullable
        public static IModel getModel​(@Nonnull
                                      IModel self,
                                      int pos)
        Returns the model at the given index. If the event at the index is an opening element, then the returned model will consist of that element and all the way through to the matching closing element.
        Parameters:
        self -
        pos - A valid index within the current model.
        Returns:
        Model at the given position, or `null` if the position is outside of the event queue.
      • insertModelWithWhitespace

        public static void insertModelWithWhitespace​(@Nonnull
                                                     IModel self,
                                                     int pos,
                                                     @Nullable
                                                     IModel model,
                                                     @Nonnull
                                                     IModelFactory modelFactory)
        Inserts a model, creating whitespace events around it so that it appears in line with all the existing events.

        This is currently only targeting uses in the layout dialect so doesn't work very well as a general-purpose whitespace generator.

        Parameters:
        self -
        pos - A valid index within the current model.
        model -
        modelFactory -
      • insertWithWhitespace

        public static void insertWithWhitespace​(@Nonnull
                                                IModel self,
                                                int pos,
                                                @Nullable
                                                ITemplateEvent event,
                                                @Nonnull
                                                IModelFactory modelFactory)
        Inserts an event, creating a whitespace event before it so that it appears in line with all the existing events.
        Parameters:
        self -
        pos - A valid index within the current model.
        event -
        modelFactory -
      • isElement

        public static boolean isElement​(@Nonnull
                                        IModel self)
        Returns whether or not this model represents a single HTML element.
        Parameters:
        self -
        Returns:
        true if the first event in this model is an opening tag and the last event is the matching closing tag.
      • isWhitespace

        public static boolean isWhitespace​(@Nonnull
                                           IModel self)
        Returns whether or not this model represents collapsible whitespace.
        Parameters:
        self -
        Returns:
        true if this is a collapsible text model.
      • iterator

        @Nonnull
        public static Iterator<ITemplateEvent> iterator​(@Nonnull
                                                        IModel self)
        Used to make this class iterable as an event queue.
        Parameters:
        self -
        Returns:
        A new iterator over the events of this model.
      • last

        public static ITemplateEvent last​(@Nonnull
                                          IModel self)
        Returns the last event on the model.
        Parameters:
        self -
        Returns:
        The model's last event.
      • removeAllModels

        public static void removeAllModels​(@Nonnull
                                           IModel self,
                                           Predicate<ITemplateEvent> closure)
        Remove a model identified by an event matched by the given closure. Note that this closure can match any event in the model, including the top-level model itself.
        Parameters:
        self -
        closure -
      • removeChildren

        public static void removeChildren​(@Nonnull
                                          IModel self)
        If the model represents an element open to close tags, then this method removes all of the inner events.
        Parameters:
        self -
      • removeFirst

        public static void removeFirst​(@Nonnull
                                       IModel self)
        Removes the first event on the model.
        Parameters:
        self -
      • removeLast

        public static void removeLast​(@Nonnull
                                      IModel self)
        Removes the last event on the model.
        Parameters:
        self -
      • removeModel

        public static void removeModel​(@Nonnull
                                       IModel self,
                                       int pos)
        Removes a models-worth of events from the specified position. What this means is that, if the event at the position is an opening element, then it, and everything up to and including its matching end element, is removed.
        Parameters:
        self -
        pos - A valid index within the current model.
      • replaceModel

        public static void replaceModel​(@Nonnull
                                        IModel self,
                                        int pos,
                                        @Nullable
                                        IModel model)
        Replaces the model at the specified index with the given model.
        Parameters:
        self -
        pos - A valid index within the current model.
        model -
      • sizeOfModelAt

        public static int sizeOfModelAt​(@Nonnull
                                        IModel self,
                                        int index)
        If an opening element exists at the given position, this method will return the 'size' of that element (number of events from here to its matching closing tag).
        Parameters:
        self -
        index -
        Returns:
        Size of an element from the given position, or 1 if the event at the position isn't an opening element.
      • trim

        public static void trim​(@Nonnull
                                IModel self)
        Removes whitespace events from the head and tail of the model's underlying event queue.
        Parameters:
        self -