2323import org .springframework .util .Assert ;
2424import org .springframework .util .StringUtils ;
2525
26+ import com .fasterxml .jackson .annotation .JsonIgnore ;
2627import com .fasterxml .jackson .annotation .JsonInclude ;
2728import com .fasterxml .jackson .annotation .JsonInclude .Include ;
2829import com .fasterxml .jackson .annotation .JsonProperty ;
@@ -61,7 +62,7 @@ public static Inline inline(Collection<? extends Object> values) {
6162
6263 Assert .notNull (values , "Values must not be null!" );
6364
64- return new Inline (values , null , null , null , null );
65+ return new Inline (values , null , null , null , null , null );
6566 }
6667
6768 /**
@@ -74,7 +75,7 @@ public static Remote remote(Link link) {
7475
7576 Assert .notNull (link , "Link must not be null!" );
7677
77- return new Remote (link , null , null , null , null );
78+ return new Remote (link , null , null , null , null , null );
7879 }
7980
8081 /**
@@ -122,21 +123,26 @@ public static Remote remote(String href) {
122123 @ Nullable
123124 Long getMaxItems ();
124125
126+ @ Nullable
127+ Object getSelectedValue ();
128+
125129 public static abstract class AbstractHalFormsOptions <T extends AbstractHalFormsOptions <T >>
126130 implements HalFormsOptions {
127131
128132 private final @ Nullable String promptField , valueField ;
129133 private final @ Nullable Long minItems , maxItems ;
134+ private final @ Nullable Object selectedValue ;
130135
131136 protected AbstractHalFormsOptions (@ Nullable String promptRef , @ Nullable String valueRef , @ Nullable Long minItems ,
132- @ Nullable Long maxItems ) {
137+ @ Nullable Long maxItems , @ Nullable Object selectedValue ) {
133138
134139 Assert .isTrue (minItems == null || minItems >= 0 , "MinItems must be greater than or equal to 0!" );
135140
136141 this .promptField = promptRef ;
137142 this .valueField = valueRef ;
138143 this .minItems = minItems ;
139144 this .maxItems = maxItems ;
145+ this .selectedValue = selectedValue ;
140146 }
141147
142148 /*
@@ -179,6 +185,17 @@ public Long getMaxItems() {
179185 return maxItems ;
180186 }
181187
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+
182199 /**
183200 * Configures the given field to be used as prompt field.
184201 *
@@ -191,7 +208,7 @@ public T withPromptField(String promptField) {
191208 throw new IllegalArgumentException ("Prompt field has to either be null or actually have text!" );
192209 }
193210
194- return with (promptField , valueField , minItems , maxItems );
211+ return with (promptField , valueField , minItems , maxItems , selectedValue );
195212 }
196213
197214 /**
@@ -206,7 +223,7 @@ public T withValueField(String valueField) {
206223 throw new IllegalArgumentException ("Value field has to either be null or actually have text!" );
207224 }
208225
209- return with (promptField , valueField , minItems , maxItems );
226+ return with (promptField , valueField , minItems , maxItems , selectedValue );
210227 }
211228
212229 /**
@@ -221,7 +238,7 @@ public T withMinItems(Long minItems) {
221238 throw new IllegalArgumentException ("minItems has to be null or greater or equal to zero!" );
222239 }
223240
224- return with (promptField , valueField , minItems , maxItems );
241+ return with (promptField , valueField , minItems , maxItems , selectedValue );
225242 }
226243
227244 /**
@@ -230,13 +247,23 @@ public T withMinItems(Long minItems) {
230247 * @param maxItems must be {@literal null} or greater than zero.
231248 * @return
232249 */
233- public T withMaxItems (Long maxItems ) {
250+ public T withMaxItems (@ Nullable Long maxItems ) {
234251
235252 if (maxItems != null && maxItems <= 0 ) {
236253 throw new IllegalArgumentException ("maxItems has to be null or greater than zero!" );
237254 }
238255
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 );
240267 }
241268
242269 /**
@@ -249,7 +276,7 @@ public T withMaxItems(Long maxItems) {
249276 * @return
250277 */
251278 protected abstract T with (@ Nullable String promptRef , @ Nullable String valueRef , @ Nullable Long minItems ,
252- @ Nullable Long maxItems );
279+ @ Nullable Long maxItems , @ Nullable Object selectedValue );
253280 }
254281
255282 public static class Inline extends AbstractHalFormsOptions <Inline > {
@@ -262,9 +289,9 @@ public static class Inline extends AbstractHalFormsOptions<Inline> {
262289 * @param valueRef
263290 */
264291 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 ) {
266293
267- super (promptRef , valueRef , minItems , maxItems );
294+ super (promptRef , valueRef , minItems , maxItems , selectedValue );
268295
269296 Assert .notNull (values , "Values must not be null!" );
270297
@@ -283,12 +310,12 @@ public Collection<? extends Object> getInline() {
283310
284311 /*
285312 * (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 )
287314 */
288315 @ Override
289316 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 );
292319 }
293320 }
294321
@@ -302,9 +329,9 @@ public static class Remote extends AbstractHalFormsOptions<Remote> {
302329 private final Link link ;
303330
304331 private Remote (Link link , @ Nullable String promptRef , @ Nullable String valueRef , @ Nullable Long minItems ,
305- @ Nullable Long maxItems ) {
332+ @ Nullable Long maxItems , @ Nullable Object selectedValue ) {
306333
307- super (promptRef , valueRef , minItems , maxItems );
334+ super (promptRef , valueRef , minItems , maxItems , selectedValue );
308335
309336 Assert .notNull (link , "Link must not be null!" );
310337
@@ -323,12 +350,12 @@ public Link getLink() {
323350
324351 /*
325352 * (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 )
327354 */
328355 @ Override
329356 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 );
332359 }
333360 }
334361}
0 commit comments