forked from spullara/mustache.java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectHandler.java
More file actions
79 lines (70 loc) · 2.41 KB
/
ObjectHandler.java
File metadata and controls
79 lines (70 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.github.mustachejava;
import com.github.mustachejava.util.IndentWriter;
import com.github.mustachejava.util.InternalArrayList;
import com.github.mustachejava.util.Wrapper;
import java.io.Writer;
import java.util.List;
/**
* The ObjectHandler is responsible for creating wrappers to find values
* in scopes at runtime and to coerce those results to the appropriate Java types
*/
public interface ObjectHandler {
/**
* Find a value named "name" in the array of scopes in reverse order.
*
* @param name the variable name
* @param scopes the ordered list of scopes
* @return a wrapper that can be used to extract a value
*/
Wrapper find(String name, List<Object> scopes);
/**
* Coerce results to Java native iterables, functions, callables.
*
* @param object transform an unknown type to a known type
* @return the new object
*/
Object coerce(Object object);
/**
* Iterate over an object by calling Iteration.next for each value.
*
* @param iteration callback for the next iteration
* @param writer the writer to write to
* @param object the current object
* @param scopes the scopes present
* @return the current writer
*/
IndentWriter iterate(Iteration iteration, IndentWriter writer, Object object, List<Object> scopes);
/**
* Call Iteration.next() either 0 (true) or 1 (fale) times.
*
* @param iteration callback for the next iteration
* @param writer the writer to write to
* @param object the current object
* @param scopes the scopes present
* @return the current writer
*/
IndentWriter falsey(Iteration iteration, IndentWriter writer, Object object, List<Object> scopes);
/**
* Each call site has its own binding to allow for fine grained caching without
* a separate parallel hierarchy of objects.
*
* @param name the name that we bound
* @param tc the textual context of the binding site
* @param code the code that was bound
* @return the binding
*/
Binding createBinding(String name, TemplateContext tc, Code code);
/**
* Turns an object into the string representation that should be displayed
* in templates.
*
* @param object the object to be displayed
* @return a string representation of the object.
*/
String stringify(Object object);
static List<Object> makeList(Object scope) {
List<Object> scopes = new InternalArrayList<>();
scopes.add(scope);
return scopes;
}
}