-
-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathModelAndView.java
More file actions
146 lines (130 loc) · 3.89 KB
/
Copy pathModelAndView.java
File metadata and controls
146 lines (130 loc) · 3.89 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.jspecify.annotations.Nullable;
/**
* Used by template engines to renderer views.
*
* @since 2.0.0
* @author edgar
* @param <T> Model type.
*/
public class ModelAndView<T> {
/** Thrown by template engine when they are not capable of rendering a {@link ModelAndView}. */
public static class UnsupportedModelAndView extends IllegalArgumentException {
/**
* Constructor.
*
* @param supported List of supported model implementation.
*/
public UnsupportedModelAndView(Class<?>... supported) {
super(
"Only "
+ Set.of(supported).stream().map(Class::getName).collect(Collectors.joining(", "))
+ " are supported");
}
}
/** View name. */
private final String view;
/** View data. */
protected final T model;
/** Locale used when rendering the view. */
private Locale locale;
/**
* Creates a new model and view.
*
* @param view View name must include file extension.
* @param model View model.
*/
public ModelAndView(String view, T model) {
this.view = view;
this.model = model;
}
/**
* Creates a model and view backed by a map.
*
* @param view View name.
* @return A map model and view.
*/
public static MapModelAndView map(String view) {
return new MapModelAndView(view);
}
/**
* Creates a model and view backed by a map.
*
* @param view View name.
* @param model Map instance.
* @return A map model and view.
*/
public static MapModelAndView map(String view, Map<String, Object> model) {
return new MapModelAndView(view, model);
}
/**
* Creates a model and view based on the provided view name and model. If the model is null, a
* map-based model and view is created. If the model is an instance of {@code Map}, a map-based
* model and view is created using the provided map. Otherwise, a generic model and view is
* created with the specified view name and model.
*
* @param view The name of the view, which may include a file extension.
* @param model The data model to be associated with the view. This can be null, a {@code Map}, or
* any other object.
* @return A {@code ModelAndView} instance corresponding to the specified view and model.
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static <T> ModelAndView<T> of(String view, @Nullable Object model) {
if (model == null) {
return (ModelAndView<T>) map(view);
}
if (model instanceof Map mapModel) {
return (ModelAndView<T>) map(view, mapModel);
}
return new ModelAndView(view, model);
}
/**
* Sets the locale used when rendering the view, if the template engine supports setting it.
* Specifying {@code null} triggers a fallback to a locale determined by the current request.
*
* @param locale The locale used when rendering the view.
* @return This instance.
*/
public ModelAndView<T> setLocale(@Nullable Locale locale) {
this.locale = locale;
return this;
}
/**
* View data (a.k.a as model).
*
* @return View data (a.k.a as model).
*/
public T getModel() {
return model;
}
/**
* View name with file extension, like: <code>index.html</code>.
*
* @return View name with file extension, like: <code>index.html</code>.
*/
public String getView() {
return view;
}
/**
* Returns the locale used when rendering the view. Defaults to {@code null}, which triggers a
* fallback to a locale determined by the current request.
*
* @return The locale used when rendering the view.
*/
@Nullable public Locale getLocale() {
return locale;
}
@Override
public String toString() {
return view;
}
}