MCPcopy Create free account
hub / github.com/xmlet/HtmlFlow / HtmlView

Class HtmlView

htmlflow-core/src/main/java/htmlflow/HtmlView.java:37–168  ·  view source on GitHub ↗

Dynamic views can be bound to a Model object. @param Type of the model rendered with this view. @author Miguel Gamboa, Luís Duare

Source from the content-addressed store, hash-verified

35 * @author Miguel Gamboa, Luís Duare
36 */
37public class HtmlView<M> extends HtmlPage {
38
39 /** Function that consumes an HtmlView to produce HTML elements. */
40 protected final HtmlTemplate template;
41
42 /**
43 * This field is like a union with the threadLocalVisitor, being used alternatively. For non
44 * thread safe scenarios Visitors maybe shared concurrently by multiple threads. On the
45 * other-hand, in thread-safe scenarios each thread must have its own visitor to emit HTML to the
46 * output, and we use the threadLocalVisitor field instead.
47 */
48 private final HtmlVisitor visitor;
49
50 /**
51 * This issue is regarding ThreadLocal variables that are supposed to be garbage collected. The
52 * given example deals with a static field of ThreadLocal which persists beyond an instance. In
53 * this case the ThreadLocal is hold in an instance field and should stay with all thread local
54 * instances during its entire life cycle.
55 */
56 @SuppressWarnings("squid:S5164")
57 private final ThreadLocal<HtmlVisitor> threadLocalVisitor;
58
59 protected final Supplier<HtmlVisitor> visitorSupplier;
60 protected final boolean threadSafe;
61
62 /** Auxiliary constructor used by clone(). */
63 HtmlView(
64 Supplier<HtmlVisitor> visitorSupplier,
65 HtmlTemplate template,
66 boolean threadSafe
67 ) {
68 this.visitorSupplier = visitorSupplier;
69 this.template = template;
70 this.threadSafe = threadSafe;
71 if (threadSafe) {
72 this.visitor = null;
73 this.threadLocalVisitor = ThreadLocal.withInitial(visitorSupplier);
74 } else {
75 this.visitor = visitorSupplier.get();
76 this.threadLocalVisitor = null;
77 }
78 }
79
80 public final Html<HtmlPage> html() {
81 this.getVisitor().write(HEADER);
82 return new Html<>(this);
83 }
84
85 public HtmlView<M> threadSafe() {
86 return clone(visitorSupplier, true);
87 }
88
89 @Override
90 public HtmlVisitor getVisitor() {
91 return threadSafe ? threadLocalVisitor.get() : visitor;
92 }
93
94 public HtmlView<M> setOut(Appendable out) {

Callers 1

preprocessingSuspendFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected