23
23
import org .springframework .util .Assert ;
24
24
import org .springframework .util .StringUtils ;
25
25
26
+ import com .fasterxml .jackson .annotation .JsonIgnore ;
26
27
import com .fasterxml .jackson .annotation .JsonInclude ;
27
28
import com .fasterxml .jackson .annotation .JsonInclude .Include ;
28
29
import com .fasterxml .jackson .annotation .JsonProperty ;
@@ -61,7 +62,7 @@ public static Inline inline(Collection<? extends Object> values) {
61
62
62
63
Assert .notNull (values , "Values must not be null!" );
63
64
64
- return new Inline (values , null , null , null , null );
65
+ return new Inline (values , null , null , null , null , null );
65
66
}
66
67
67
68
/**
@@ -74,7 +75,7 @@ public static Remote remote(Link link) {
74
75
75
76
Assert .notNull (link , "Link must not be null!" );
76
77
77
- return new Remote (link , null , null , null , null );
78
+ return new Remote (link , null , null , null , null , null );
78
79
}
79
80
80
81
/**
@@ -122,21 +123,26 @@ public static Remote remote(String href) {
122
123
@ Nullable
123
124
Long getMaxItems ();
124
125
126
+ @ Nullable
127
+ Object getSelectedValue ();
128
+
125
129
public static abstract class AbstractHalFormsOptions <T extends AbstractHalFormsOptions <T >>
126
130
implements HalFormsOptions {
127
131
128
132
private final @ Nullable String promptField , valueField ;
129
133
private final @ Nullable Long minItems , maxItems ;
134
+ private final @ Nullable Object selectedValue ;
130
135
131
136
protected AbstractHalFormsOptions (@ Nullable String promptRef , @ Nullable String valueRef , @ Nullable Long minItems ,
132
- @ Nullable Long maxItems ) {
137
+ @ Nullable Long maxItems , @ Nullable Object selectedValue ) {
133
138
134
139
Assert .isTrue (minItems == null || minItems >= 0 , "MinItems must be greater than or equal to 0!" );
135
140
136
141
this .promptField = promptRef ;
137
142
this .valueField = valueRef ;
138
143
this .minItems = minItems ;
139
144
this .maxItems = maxItems ;
145
+ this .selectedValue = selectedValue ;
140
146
}
141
147
142
148
/*
@@ -179,6 +185,17 @@ public Long getMaxItems() {
179
185
return maxItems ;
180
186
}
181
187
188
+ /*
189
+ * (non-Javadoc)
190
+ * @see org.springframework.hateoas.mediatype.hal.forms.HalFormsOptions#getSelectedValue()
191
+ */
192
+ @ Nullable
193
+ @ Override
194
+ @ JsonIgnore
195
+ public Object getSelectedValue () {
196
+ return selectedValue ;
197
+ }
198
+
182
199
/**
183
200
* Configures the given field to be used as prompt field.
184
201
*
@@ -191,7 +208,7 @@ public T withPromptField(String promptField) {
191
208
throw new IllegalArgumentException ("Prompt field has to either be null or actually have text!" );
192
209
}
193
210
194
- return with (promptField , valueField , minItems , maxItems );
211
+ return with (promptField , valueField , minItems , maxItems , selectedValue );
195
212
}
196
213
197
214
/**
@@ -206,7 +223,7 @@ public T withValueField(String valueField) {
206
223
throw new IllegalArgumentException ("Value field has to either be null or actually have text!" );
207
224
}
208
225
209
- return with (promptField , valueField , minItems , maxItems );
226
+ return with (promptField , valueField , minItems , maxItems , selectedValue );
210
227
}
211
228
212
229
/**
@@ -221,7 +238,7 @@ public T withMinItems(Long minItems) {
221
238
throw new IllegalArgumentException ("minItems has to be null or greater or equal to zero!" );
222
239
}
223
240
224
- return with (promptField , valueField , minItems , maxItems );
241
+ return with (promptField , valueField , minItems , maxItems , selectedValue );
225
242
}
226
243
227
244
/**
@@ -230,13 +247,23 @@ public T withMinItems(Long minItems) {
230
247
* @param maxItems must be {@literal null} or greater than zero.
231
248
* @return
232
249
*/
233
- public T withMaxItems (Long maxItems ) {
250
+ public T withMaxItems (@ Nullable Long maxItems ) {
234
251
235
252
if (maxItems != null && maxItems <= 0 ) {
236
253
throw new IllegalArgumentException ("maxItems has to be null or greater than zero!" );
237
254
}
238
255
239
- return with (promptField , valueField , minItems , maxItems );
256
+ return with (promptField , valueField , minItems , maxItems , selectedValue );
257
+ }
258
+
259
+ /**
260
+ * Configured the value to be initially selected
261
+ *
262
+ * @param value
263
+ * @return
264
+ */
265
+ public T withSelectedValue (@ Nullable Object value ) {
266
+ return with (promptField , valueField , minItems , maxItems , value );
240
267
}
241
268
242
269
/**
@@ -249,7 +276,7 @@ public T withMaxItems(Long maxItems) {
249
276
* @return
250
277
*/
251
278
protected abstract T with (@ Nullable String promptRef , @ Nullable String valueRef , @ Nullable Long minItems ,
252
- @ Nullable Long maxItems );
279
+ @ Nullable Long maxItems , @ Nullable Object selectedValue );
253
280
}
254
281
255
282
public static class Inline extends AbstractHalFormsOptions <Inline > {
@@ -262,9 +289,9 @@ public static class Inline extends AbstractHalFormsOptions<Inline> {
262
289
* @param valueRef
263
290
*/
264
291
private Inline (Collection <? extends Object > values , @ Nullable String promptRef , @ Nullable String valueRef ,
265
- @ Nullable Long minItems , @ Nullable Long maxItems ) {
292
+ @ Nullable Long minItems , @ Nullable Long maxItems , @ Nullable Object selectedValue ) {
266
293
267
- super (promptRef , valueRef , minItems , maxItems );
294
+ super (promptRef , valueRef , minItems , maxItems , selectedValue );
268
295
269
296
Assert .notNull (values , "Values must not be null!" );
270
297
@@ -283,12 +310,12 @@ public Collection<? extends Object> getInline() {
283
310
284
311
/*
285
312
* (non-Javadoc)
286
- * @see org.springframework.hateoas.mediatype.hal.forms.HalFormsOptions.AbstractHalFormsOptions#with(java.lang.String, java.lang.String, java.lang.Long, java.lang.Long)
313
+ * @see org.springframework.hateoas.mediatype.hal.forms.HalFormsOptions.AbstractHalFormsOptions#with(java.lang.String, java.lang.String, java.lang.Long, java.lang.Long, java.lang.Object )
287
314
*/
288
315
@ Override
289
316
protected Inline with (@ Nullable String promptRef , @ Nullable String valueRef , @ Nullable Long minItems ,
290
- @ Nullable Long maxItems ) {
291
- return new Inline (inline , promptRef , valueRef , minItems , maxItems );
317
+ @ Nullable Long maxItems , @ Nullable Object selectedValue ) {
318
+ return new Inline (inline , promptRef , valueRef , minItems , maxItems , selectedValue );
292
319
}
293
320
}
294
321
@@ -302,9 +329,9 @@ public static class Remote extends AbstractHalFormsOptions<Remote> {
302
329
private final Link link ;
303
330
304
331
private Remote (Link link , @ Nullable String promptRef , @ Nullable String valueRef , @ Nullable Long minItems ,
305
- @ Nullable Long maxItems ) {
332
+ @ Nullable Long maxItems , @ Nullable Object selectedValue ) {
306
333
307
- super (promptRef , valueRef , minItems , maxItems );
334
+ super (promptRef , valueRef , minItems , maxItems , selectedValue );
308
335
309
336
Assert .notNull (link , "Link must not be null!" );
310
337
@@ -323,12 +350,12 @@ public Link getLink() {
323
350
324
351
/*
325
352
* (non-Javadoc)
326
- * @see org.springframework.hateoas.mediatype.hal.forms.HalFormsOptions.Foo#withFoo (java.lang.String, java.lang.String)
353
+ * @see org.springframework.hateoas.mediatype.hal.forms.HalFormsOptions.AbstractHalFormsOptions#with (java.lang.String, java.lang.String, java.lang.Long, java.lang.Long, java.lang.Object )
327
354
*/
328
355
@ Override
329
356
protected Remote with (@ Nullable String promptRef , @ Nullable String valueRef , @ Nullable Long minItems ,
330
- @ Nullable Long maxItems ) {
331
- return new Remote (link , promptRef , valueRef , minItems , maxItems );
357
+ @ Nullable Long maxItems , @ Nullable Object selectedValue ) {
358
+ return new Remote (link , promptRef , valueRef , minItems , maxItems , selectedValue );
332
359
}
333
360
}
334
361
}
0 commit comments