Class IModelExtensions
- java.lang.Object
-
- nz.net.ultraq.thymeleaf.layoutdialect.models.extensions.IModelExtensions
-
-
Constructor Summary
Constructors Constructor Description IModelExtensions()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanasBoolean(IModel self)Set that a model evaluates to 'false' if it has no events.static Iterator<IModel>childModelIterator(IModel self)If this model represents an element, then this method returns an iterator over any potential child items as models of their own.static voideach(IModel self, Consumer<ITemplateEvent> closure)Iterate through each event in the model.static booleanequals(IModel self, Object other)Compare 2 models, returningtrueif all of the model's events are equal.static booleaneveryWithIndex(IModel self, BiPredicate<ITemplateEvent,Integer> closure)Returntrueonly if all the events in the model returntruefor the given closure.static ITemplateEventfind(IModel self, Predicate<ITemplateEvent> closure)Returns the first event in the model that meets the criteria of the given closure.static List<ITemplateEvent>findAll(IModel self, Predicate<ITemplateEvent> closure)Find all events in the model that match the given closure.static intfindIndexOf(IModel self, Predicate<ITemplateEvent> closure)Returns the index of the first event in the model that meets the criteria of the given closure.static intfindIndexOfModel(IModel self, IModel model)A special variant offindIndexOfthat uses models, as I seem to be using those a lot.static IModelfindModel(IModel self, Predicate<ITemplateEvent> closure)Returns the first instance of a model that meets the given closure criteria.static ITemplateEventfirst(IModel self)Returns the first event on the model.static IModelgetModel(IModel self, int pos)Returns the model at the given index.static voidinsertModelWithWhitespace(IModel self, int pos, IModel model, IModelFactory modelFactory)Inserts a model, creating whitespace events around it so that it appears in line with all the existing events.static voidinsertWithWhitespace(IModel self, int pos, ITemplateEvent event, IModelFactory modelFactory)Inserts an event, creating a whitespace event before it so that it appears in line with all the existing events.static booleanisElement(IModel self)Returns whether or not this model represents a single HTML element.static booleanisWhitespace(IModel self)Returns whether or not this model represents collapsible whitespace.static Iterator<ITemplateEvent>iterator(IModel self)Used to make this class iterable as an event queue.static ITemplateEventlast(IModel self)Returns the last event on the model.static voidremoveAllModels(IModel self, Predicate<ITemplateEvent> closure)Remove a model identified by an event matched by the given closure.static voidremoveChildren(IModel self)If the model represents an element open to close tags, then this method removes all of the inner events.static voidremoveFirst(IModel self)Removes the first event on the model.static voidremoveLast(IModel self)Removes the last event on the model.static voidremoveModel(IModel self, int pos)Removes a models-worth of events from the specified position.static voidreplaceModel(IModel self, int pos, IModel model)Replaces the model at the specified index with the given model.static intsizeOfModelAt(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).static voidtrim(IModel self)Removes whitespace events from the head and tail of the model's underlying event queue.
-
-
-
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:
trueif 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.
-
each
public static void each(@Nullable IModel self, @Nonnull Consumer<ITemplateEvent> closure)
Iterate through each event in the model.- Parameters:
self-closure-
-
equals
public static boolean equals(IModel self, Object other)
Compare 2 models, returningtrueif all of the model's events are equal.- Parameters:
self-other-- Returns:
trueif this model is the same as the other one.
-
everyWithIndex
public static boolean everyWithIndex(@Nullable IModel self, @Nonnull BiPredicate<ITemplateEvent,Integer> closure)
Returntrueonly if all the events in the model returntruefor the given closure.- Parameters:
self-closure-- Returns:
trueif every event satisfies the closure.
-
find
@Nullable public static ITemplateEvent find(@Nullable IModel self, @Nonnull Predicate<ITemplateEvent> closure)
Returns the first event in the model that meets the criteria of the given closure.- Parameters:
self-closure-- Returns:
- The first event to match the closure criteria, or
nullif nothing matched.
-
findAll
@Nonnull public static List<ITemplateEvent> findAll(@Nullable IModel self, @Nonnull Predicate<ITemplateEvent> closure)
Find all events in the model that match the given closure.- Parameters:
self-closure-- Returns:
- A list of matched events.
-
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
-1if nothing matched.
-
findIndexOfModel
public static int findIndexOfModel(@Nonnull IModel self, IModel model)
A special variant offindIndexOfthat 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
findmethods, 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
nullif 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:
trueif 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:
trueif 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.
-
-