2525import org .bson .Document ;
2626import org .springframework .data .mongodb .core .schema .TypedJsonSchemaObject .ObjectJsonSchemaObject ;
2727import org .springframework .lang .Nullable ;
28+ import org .springframework .util .Assert ;
2829
2930/**
3031 * Interface defining MongoDB-specific JSON schema object. New objects can be built with {@link #builder()}, for
@@ -79,7 +80,7 @@ default Document toDocument() {
7980
8081 /**
8182 * Create the {@link Document} defining the schema. <br />
82- * Property and field names need to be mapped to the domain type ones by running the {@link Document} through a
83+ * Property and field names need to be mapped to the domain type property by running the {@link Document} through a
8384 * {@link org.springframework.data.mongodb.core.convert.JsonSchemaMapper} to apply field name customization.
8485 *
8586 * @return never {@literal null}.
@@ -108,69 +109,69 @@ static MongoJsonSchema of(Document document) {
108109 }
109110
110111 /**
111- * Create a new {@link MongoJsonSchema} combining properties from the given sources.
112+ * Create a new {@link MongoJsonSchema} merging properties from the given sources.
112113 *
113114 * @param sources must not be {@literal null}.
114115 * @return new instance of {@link MongoJsonSchema}.
115116 * @since 3.4
116117 */
117- static MongoJsonSchema combined (MongoJsonSchema ... sources ) {
118- return combined ((path , a , b ) -> {
119- throw new IllegalStateException (
120- String . format ( "Failure combining schema for path %s holding values a) %s and b) %s." , path . dotPath (), a , b ));
118+ static MongoJsonSchema merge (MongoJsonSchema ... sources ) {
119+ return merge ((path , left , right ) -> {
120+ throw new IllegalStateException (String . format ( "Cannot merge schema for path '%s' holding values '%s' and '%s'." ,
121+ path . dotPath (), left , right ));
121122 }, sources );
122123 }
123124
124125 /**
125- * Create a new {@link MongoJsonSchema} combining properties from the given sources.
126+ * Create a new {@link MongoJsonSchema} merging properties from the given sources.
126127 *
127128 * @param sources must not be {@literal null}.
128129 * @return new instance of {@link MongoJsonSchema}.
129130 * @since 3.4
130131 */
131- static MongoJsonSchema combined (ConflictResolutionFunction mergeFunction , MongoJsonSchema ... sources ) {
132- return new CombinedJsonSchema (Arrays .asList (sources ), mergeFunction );
132+ static MongoJsonSchema merge (ConflictResolutionFunction mergeFunction , MongoJsonSchema ... sources ) {
133+ return new MergedJsonSchema (Arrays .asList (sources ), mergeFunction );
133134 }
134135
135136 /**
136- * Create a new {@link MongoJsonSchema} combining properties from the given sources.
137+ * Create a new {@link MongoJsonSchema} merging properties from the given sources.
137138 *
138139 * @param sources must not be {@literal null}.
139140 * @return new instance of {@link MongoJsonSchema}.
140141 * @since 3.4
141142 */
142- default MongoJsonSchema combineWith (MongoJsonSchema ... sources ) {
143- return combineWith (Arrays .asList (sources ));
143+ default MongoJsonSchema mergeWith (MongoJsonSchema ... sources ) {
144+ return mergeWith (Arrays .asList (sources ));
144145 }
145146
146147 /**
147- * Create a new {@link MongoJsonSchema} combining properties from the given sources.
148+ * Create a new {@link MongoJsonSchema} merging properties from the given sources.
148149 *
149150 * @param sources must not be {@literal null}.
150151 * @return new instance of {@link MongoJsonSchema}.
151152 * @since 3.4
152153 */
153- default MongoJsonSchema combineWith (Collection <MongoJsonSchema > sources ) {
154- return combineWith (sources , (path , a , b ) -> {
155- throw new IllegalStateException (
156- String . format ( "Failure combining schema for path %s holding values a) %s and b) %s." , path . dotPath (), a , b ));
154+ default MongoJsonSchema mergeWith (Collection <MongoJsonSchema > sources ) {
155+ return mergeWith (sources , (path , left , right ) -> {
156+ throw new IllegalStateException (String . format ( "Cannot merge schema for path '%s' holding values '%s' and '%s'." ,
157+ path . dotPath (), left , right ));
157158 });
158159 }
159160
160161 /**
161- * Create a new {@link MongoJsonSchema} combining properties from the given sources.
162+ * Create a new {@link MongoJsonSchema} merging properties from the given sources.
162163 *
163164 * @param sources must not be {@literal null}.
164165 * @return new instance of {@link MongoJsonSchema}.
165166 * @since 3.4
166167 */
167- default MongoJsonSchema combineWith (Collection <MongoJsonSchema > sources ,
168+ default MongoJsonSchema mergeWith (Collection <MongoJsonSchema > sources ,
168169 ConflictResolutionFunction conflictResolutionFunction ) {
169170
170171 List <MongoJsonSchema > schemaList = new ArrayList <>(sources .size () + 1 );
171172 schemaList .add (this );
172173 schemaList .addAll (new ArrayList <>(sources ));
173- return new CombinedJsonSchema (schemaList , conflictResolutionFunction );
174+ return new MergedJsonSchema (schemaList , conflictResolutionFunction );
174175 }
175176
176177 /**
@@ -183,8 +184,8 @@ static MongoJsonSchemaBuilder builder() {
183184 }
184185
185186 /**
186- * A resolution function that may be called on conflicting paths. Eg. when trying to merge properties with different
187- * values into one .
187+ * A resolution function that is called on conflicting paths when trying to merge properties with different values
188+ * into a single value .
188189 *
189190 * @author Christoph Strobl
190191 * @since 3.4
@@ -193,12 +194,14 @@ static MongoJsonSchemaBuilder builder() {
193194 interface ConflictResolutionFunction {
194195
195196 /**
197+ * Resolve the conflict for two values under the same {@code path}.
198+ *
196199 * @param path the {@link Path} leading to the conflict.
197- * @param a can be {@literal null}.
198- * @param b can be {@literal null}.
200+ * @param left can be {@literal null}.
201+ * @param right can be {@literal null}.
199202 * @return never {@literal null}.
200203 */
201- Resolution resolveConflict (Path path , @ Nullable Object a , @ Nullable Object b );
204+ Resolution resolveConflict (Path path , @ Nullable Object left , @ Nullable Object right );
202205
203206 /**
204207 * @author Christoph Strobl
@@ -218,7 +221,7 @@ interface Path {
218221 }
219222
220223 /**
221- * The result after processing a conflict when combining schemas. May indicate to {@link #SKIP skip} the entry
224+ * The result after processing a conflict when merging schemas. May indicate to {@link #SKIP skip} the entry
222225 * entirely.
223226 *
224227 * @author Christoph Strobl
@@ -260,6 +263,42 @@ public Object setValue(Object value) {
260263 static Resolution skip () {
261264 return SKIP ;
262265 }
266+
267+ /**
268+ * Construct a resolution for a {@link Path} using the given {@code value}.
269+ *
270+ * @param path the conflicting path.
271+ * @param value the value to apply.
272+ * @return
273+ */
274+ static Resolution ofValue (Path path , Object value ) {
275+
276+ Assert .notNull (path , "Path must not be null" );
277+
278+ return ofValue (path .currentElement (), value );
279+ }
280+
281+ /**
282+ * Construct a resolution from a {@code key} and {@code value}.
283+ *
284+ * @param key name of the path segment, typically {@link Path#currentElement()}
285+ * @param value the value to apply.
286+ * @return
287+ */
288+ static Resolution ofValue (String key , Object value ) {
289+
290+ return new Resolution () {
291+ @ Override
292+ public String getKey () {
293+ return key ;
294+ }
295+
296+ @ Override
297+ public Object getValue () {
298+ return value ;
299+ }
300+ };
301+ }
263302 }
264303 }
265304
0 commit comments