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 boolean
asBoolean(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 void
each(IModel self, Consumer<ITemplateEvent> closure)
Iterate through each event in the model.static boolean
equals(IModel self, Object other)
Compare 2 models, returningtrue
if all of the model's events are equal.static boolean
everyWithIndex(IModel self, BiPredicate<ITemplateEvent,Integer> closure)
Returntrue
only if all the events in the model returntrue
for the given closure.static ITemplateEvent
find(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 int
findIndexOf(IModel self, Predicate<ITemplateEvent> closure)
Returns the index of the first event in the model that meets the criteria of the given closure.static int
findIndexOfModel(IModel self, IModel model)
A special variant offindIndexOf
that uses models, as I seem to be using those a lot.static IModel
findModel(IModel self, Predicate<ITemplateEvent> closure)
Returns the first instance of a model that meets the given closure criteria.static ITemplateEvent
first(IModel self)
Returns the first event on the model.static IModel
getModel(IModel self, int pos)
Returns the model at the given index.static void
insertModelWithWhitespace(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 void
insertWithWhitespace(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 boolean
isElement(IModel self)
Returns whether or not this model represents a single HTML element.static boolean
isWhitespace(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 ITemplateEvent
last(IModel self)
Returns the last event on the model.static void
removeAllModels(IModel self, Predicate<ITemplateEvent> closure)
Remove a model identified by an event matched by the given closure.static void
removeChildren(IModel self)
If the model represents an element open to close tags, then this method removes all of the inner events.static void
removeFirst(IModel self)
Removes the first event on the model.static void
removeLast(IModel self)
Removes the last event on the model.static void
removeModel(IModel self, int pos)
Removes a models-worth of events from the specified position.static void
replaceModel(IModel self, int pos, IModel model)
Replaces the model at the specified index with the given model.static int
sizeOfModelAt(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 void
trim(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:
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.
-
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, returningtrue
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)
Returntrue
only if all the events in the model returntrue
for the given closure.- Parameters:
self
-closure
-- Returns:
true
if 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
null
if 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
-1
if nothing matched.
-
findIndexOfModel
public static int findIndexOfModel(@Nonnull IModel self, IModel model)
A special variant offindIndexOf
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.
-
-