public final class Models
extends java.lang.Object
@Model
annotation.| Modifier and Type | Method and Description |
|---|---|
static void |
applyBindings(java.lang.Object model)
Apply bindings of a model class to overall page.
|
static void |
applyBindings(java.lang.Object model,
java.lang.String targetId)
Apply bindings of a model class.
|
static <T> java.util.List<T> |
asList(T... values)
Wrap provided values into mutable list.
|
static <Model> Model |
bind(Model model,
net.java.html.BrwsrCtx context)
Binds given model to another context.
|
static <M> M |
fromRaw(net.java.html.BrwsrCtx ctx,
java.lang.Class<M> model,
java.lang.Object jsonObject)
Converts an existing, raw, JSON object into a
model class. |
static boolean |
isModel(java.lang.Class<?> clazz)
Finds out whether given class is a model class - e.g.
|
static <M> M |
parse(net.java.html.BrwsrCtx c,
java.lang.Class<M> model,
java.io.InputStream is)
Generic method to parse content of a model class from a stream.
|
static <M> void |
parse(net.java.html.BrwsrCtx c,
java.lang.Class<M> model,
java.io.InputStream is,
java.util.Collection<? super M> collectTo)
Generic method to parse stream, that can possibly contain array
of specified objects.
|
static java.io.Closeable |
react(java.lang.Runnable reaction,
java.util.concurrent.Executor onChange)
Runs provided action immediately and then again and again when reaction
to changes is needed.
|
static java.lang.Object |
toRaw(java.lang.Object model)
Converts an existing
model into its associated, raw
JSON object. |
public static boolean isModel(java.lang.Class<?> clazz)
@Model annotation.clazz - the class to testclazz was generated by Model annotationpublic static <Model> Model bind(Model model,
net.java.html.BrwsrCtx context)
Model - class defined by Model annotationmodel - instance of a model defined by Model annotationcontext - context to which the model should be boundcontextjava.lang.IllegalArgumentException - in case the instance is not generated by model interfacepublic static <M> M parse(net.java.html.BrwsrCtx c,
java.lang.Class<M> model,
java.io.InputStream is)
throws java.io.IOException
M - type of the model classc - context of the technology to use for readingmodel - the model class generated by Model annotationis - input stream with datajava.io.IOException - throw when reading from is faces problemspublic static <M> void parse(net.java.html.BrwsrCtx c,
java.lang.Class<M> model,
java.io.InputStream is,
java.util.Collection<? super M> collectTo)
throws java.io.IOException
M - the type of the individal JSON objectc - context of the technology to use for readingmodel - the model class generated by Model annotationis - input stream with datacollectTo - collection to add the individual model instances to.
If the stream contains an object, one instance will be added, if
it contains an array, the number of array items will be added to
the collectionjava.io.IOException - thrown when an I/O problem appearspublic static <M> M fromRaw(net.java.html.BrwsrCtx ctx,
java.lang.Class<M> model,
java.lang.Object jsonObject)
model class.M - the type of the model classctx - context of the technology to use for convertingmodel - the model classjsonObject - original instance of the JSON objectpublic static java.lang.Object toRaw(java.lang.Object model)
model into its associated, raw
JSON object. The object may, but does not have to, be the same instance
as the model object.model - the model object
(can be null to initialize the associated Technology)null if the model parameter was null)java.lang.IllegalArgumentException - if the model is
not instance of a class
generated by model annotation processor.public static void applyBindings(java.lang.Object model)
model - instance of a classjava.lang.IllegalArgumentException - if the model is not
instance of a class generated by model annotation
processor.public static void applyBindings(java.lang.Object model,
java.lang.String targetId)
model - instance of a classtargetId - the id of the element to apply the binding tojava.lang.IllegalArgumentException - if the model is not
instance of a class generated by model annotation
processor.public static <T> java.util.List<T> asList(T... values)
T - type of the values and resulting listvalues - the values, if anypublic static java.io.Closeable react(java.lang.Runnable reaction,
java.util.concurrent.Executor onChange)
onChange.execute
method is called. The set of accessed properties is re-recorded during
the latest run. Should one of them change, the whole process is repeated.
Simple example that defines a state and observes it follows:
@Model(className = "ReactiveState", properties = { @Property(name = "state", type = int.class) }) static final class ReactiveStateCntrl { } class UseReactiveState { int counter; int value; void observeState() throwsException{ ReactiveState state = new ReactiveState(7);Closeablehandle =Models.react(() -> { counter++; value = state.getState(); }, (c) -> c.run()); assertEquals(counter, 1, "reaction performed once initially"); assertEquals(value, state.getState(), "value recorded"); state.setState(state.getState() * 2); assertEquals(counter, 2, "reaction performed again"); assertEquals(value, state.getState(), "value re-recorded"); handle.close(); state.setState(state.getState() * 3); assertEquals(counter, 2, "changes are no longer observed"); assertNotEquals(value, state.getState(), "value hasn't been updated"); assertEquals(state.getState(), 42, "the meaning is clear"); } }
reaction - the action to (repeatedly) executeonChange - submits the action again when re-run is neededclose() method to stop observing changesCopyright © 2022 The Apache Software Foundation. All rights reserved.