42
42
*/
43
43
public OptBoolean useInput () default OptBoolean .DEFAULT ;
44
44
45
+ /**
46
+ * Whether to throw an exception when the {@code ObjectMapper} does not find
47
+ * the value to inject.
48
+ *<p>
49
+ * Default is {@code OptBoolean.DEFAULT} for backwards-compatibility: in this
50
+ * case {@code ObjectMapper} defaults are used (which in turn are same
51
+ * as {code OptBoolean.FALSE}).
52
+ *
53
+ * @return {@link OptBoolean#FALSE} to throw an exception; {@link OptBoolean#TRUE}
54
+ * to avoid throwing it; or {@link OptBoolean#DEFAULT} to use configure defaults
55
+ * (which are same as {@link OptBoolean#FALSE} for Jackson 2.x)
56
+ *
57
+ * @since 2.20
58
+ */
59
+ public OptBoolean optional () default OptBoolean .DEFAULT ;
60
+
45
61
/*
46
62
/**********************************************************
47
63
/* Value class used to enclose information, allow for
@@ -63,7 +79,7 @@ public static class Value
63
79
{
64
80
private static final long serialVersionUID = 1L ;
65
81
66
- protected final static Value EMPTY = new Value (null , null );
82
+ protected final static Value EMPTY = new Value (null , null , null );
67
83
68
84
/**
69
85
* Id to use to access injected value; if `null`, "default" name, derived
@@ -73,9 +89,12 @@ public static class Value
73
89
74
90
protected final Boolean _useInput ;
75
91
76
- protected Value (Object id , Boolean useInput ) {
92
+ protected final Boolean _optional ;
93
+
94
+ protected Value (Object id , Boolean useInput , Boolean optional ) {
77
95
_id = id ;
78
96
_useInput = useInput ;
97
+ _optional = optional ;
79
98
}
80
99
81
100
@ Override
@@ -93,25 +112,33 @@ public static Value empty() {
93
112
return EMPTY ;
94
113
}
95
114
115
+ @ Deprecated //since 2.20
96
116
public static Value construct (Object id , Boolean useInput ) {
117
+ return construct (id , useInput , null );
118
+ }
119
+
120
+ /**
121
+ * @since 2.20
122
+ */
123
+ public static Value construct (Object id , Boolean useInput , Boolean optional ) {
97
124
if ("" .equals (id )) {
98
125
id = null ;
99
126
}
100
- if (_empty (id , useInput )) {
127
+ if (_empty (id , useInput , optional )) {
101
128
return EMPTY ;
102
129
}
103
- return new Value (id , useInput );
130
+ return new Value (id , useInput , optional );
104
131
}
105
132
106
133
public static Value from (JacksonInject src ) {
107
134
if (src == null ) {
108
135
return EMPTY ;
109
136
}
110
- return construct (src .value (), src .useInput ().asBoolean ());
137
+ return construct (src .value (), src .useInput ().asBoolean (), src . optional (). asBoolean () );
111
138
}
112
139
113
140
public static Value forId (Object id ) {
114
- return construct (id , null );
141
+ return construct (id , null , null );
115
142
}
116
143
117
144
/*
@@ -128,7 +155,7 @@ public Value withId(Object id) {
128
155
} else if (id .equals (_id )) {
129
156
return this ;
130
157
}
131
- return new Value (id , _useInput );
158
+ return new Value (id , _useInput , _optional );
132
159
}
133
160
134
161
public Value withUseInput (Boolean useInput ) {
@@ -139,7 +166,18 @@ public Value withUseInput(Boolean useInput) {
139
166
} else if (useInput .equals (_useInput )) {
140
167
return this ;
141
168
}
142
- return new Value (_id , useInput );
169
+ return new Value (_id , useInput , _optional );
170
+ }
171
+
172
+ public Value withOptional (Boolean optional ) {
173
+ if (optional == null ) {
174
+ if (_optional == null ) {
175
+ return this ;
176
+ }
177
+ } else if (optional .equals (_optional )) {
178
+ return this ;
179
+ }
180
+ return new Value (_id , _useInput , optional );
143
181
}
144
182
145
183
/*
@@ -150,6 +188,7 @@ public Value withUseInput(Boolean useInput) {
150
188
151
189
public Object getId () { return _id ; }
152
190
public Boolean getUseInput () { return _useInput ; }
191
+ public Boolean getOptional () { return _optional ; }
153
192
154
193
public boolean hasId () {
155
194
return _id != null ;
@@ -167,8 +206,8 @@ public boolean willUseInput(boolean defaultSetting) {
167
206
168
207
@ Override
169
208
public String toString () {
170
- return String .format ("JacksonInject.Value(id=%s,useInput=%s)" ,
171
- _id , _useInput );
209
+ return String .format ("JacksonInject.Value(id=%s,useInput=%s,optional=%s )" ,
210
+ _id , _useInput , _optional );
172
211
}
173
212
174
213
@ Override
@@ -180,6 +219,9 @@ public int hashCode() {
180
219
if (_useInput != null ) {
181
220
h += _useInput .hashCode ();
182
221
}
222
+ if (_optional != null ) {
223
+ h += _optional .hashCode ();
224
+ }
183
225
return h ;
184
226
}
185
227
@@ -189,12 +231,13 @@ public boolean equals(Object o) {
189
231
if (o == null ) return false ;
190
232
if (o .getClass () == getClass ()) {
191
233
Value other = (Value ) o ;
192
- if (OptBoolean .equals (_useInput , other ._useInput )) {
193
- if (_id == null ) {
194
- return other ._id == null ;
195
- }
196
- return _id .equals (other ._id );
197
- }
234
+
235
+ return (_id == null && other ._id == null
236
+ || _id != null && _id .equals (other ._id ))
237
+ && (_useInput == null && other ._useInput == null
238
+ || _useInput != null && _useInput .equals (other ._useInput ))
239
+ && (_optional == null && other ._optional == null
240
+ || _optional != null && _optional .equals (other ._optional ));
198
241
}
199
242
return false ;
200
243
}
@@ -205,8 +248,8 @@ public boolean equals(Object o) {
205
248
/**********************************************************
206
249
*/
207
250
208
- private static boolean _empty (Object id , Boolean useInput ) {
209
- return (id == null ) && (useInput == null );
251
+ private static boolean _empty (Object id , Boolean useInput , Boolean optional ) {
252
+ return (id == null ) && (useInput == null ) && optional == null ;
210
253
}
211
254
}
212
255
}
0 commit comments