Package org.apache.sis.internal.metadata
Class Merger
java.lang.Object
org.apache.sis.internal.metadata.Merger
Merges the content of two metadata instances.
For each non-null and non-empty property
value from the source metadata, the merge operation is defined as below:
- If the target metadata does not have a non-null and non-empty value for the same property, then the reference to the value from the source metadata is stored as-is in the target metadata.
- Otherwise if the target value is a collection, then:
- For each element of the source collection, a corresponding element of the target collection is searched.
A pair of source and target elements is established if the pair meets all of the following conditions:
- The standard type of the source element is assignable to the type of the target element.
- There is no conflict, i.e. no property value that are not collection and not equal.
This condition can be modified by overriding
resolve(Object, ModifiableMetadata)
.
- All other source elements will be added as new elements in the target collection.
- For each element of the source collection, a corresponding element of the target collection is searched.
A pair of source and target elements is established if the pair meets all of the following conditions:
- Otherwise the
copy(…)
method is invoked.
- Since:
- 0.8
- Version:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate final class
Helper class for merging the content of two maps where values may be other metadata objects.static enum
The action to perform when a source metadata element is about to be written in an existing target element. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal void
copy
(Object source, ModifiableMetadata target) Merges the data from the given source into the given target.private boolean
copy
(Object source, ModifiableMetadata target, boolean dryRun) Implementation ofcopy(Object, ModifiableMetadata)
method, to be invoked recursively for all child properties to merge.private Errors
errors()
Returns the resources for error messages.protected void
merge
(ModifiableMetadata target, String propertyName, Object sourceValue, Object targetValue) Invoked whenMerger
cannot merge a metadata value by itself.private static String
name
(ModifiableMetadata target, String propertyName) Returns the name of the given property in the given metadata instance.protected Merger.Resolution
resolve
(Object source, ModifiableMetadata target) Invoked when a source metadata element is about to be written in an existing target element.
-
Field Details
-
done
The source and target values that have already been copied, for avoiding never-ending loop in cyclic graphs. The value isBoolean.FALSE
if the key is a source, orBoolean.TRUE
if the key is a target. -
locale
The locale to use for formatting error messages, ornull
for the default locale.
-
-
Constructor Details
-
Merger
Creates a new merger.- Parameters:
locale
- the locale to use for formatting error messages, ornull
for the default locale.
-
-
Method Details
-
errors
Returns the resources for error messages. -
name
Returns the name of the given property in the given metadata instance. This is used for formatting error messages. -
copy
Merges the data from the given source into the given target. See class javadoc for a description of the merge process.- Parameters:
source
- the source metadata to merge into the target. Will never be modified.target
- the target metadata where to merge values. Will be modified as a result of this call.- Throws:
ClassCastException
- if the source and target are not instances of the same metadata standard.InvalidMetadataException
- if thetarget
metadata cannot hold allsource
properties, for example because the source class is a more specialized type than the target class.IllegalArgumentException
- if this method detects a cross-reference between source and target metadata.
-
copy
Implementation ofcopy(Object, ModifiableMetadata)
method, to be invoked recursively for all child properties to merge.- Parameters:
dryRun
-true
for simulating the merge operation instead of performing the actual merge. Used for verifying if there is a merge conflict before to perform the actual operation.- Returns:
true
if the merge operation is valid, orfalse
if the given arguments are valid metadata but the merge operation can nevertheless not be executed because it could cause data lost.
-
resolve
Invoked when a source metadata element is about to be written in an existing target element. The default implementation returnsMerger.Resolution.MERGE
if writing in the given target would only fill holes, without overwriting any existing value. Otherwise this method returnsResolution#SEPARATE
.- Parameters:
source
- the source metadata to copy.target
- where the source metadata would be copied if this method returnsMerger.Resolution.MERGE
.- Returns:
Merger.Resolution.MERGE
for writingsource
intotarget
, orMerger.Resolution.SEPARATE
for writingsource
in a separated metadata element, orMerger.Resolution.IGNORE
for discardingsource
.
-
merge
protected void merge(ModifiableMetadata target, String propertyName, Object sourceValue, Object targetValue) Invoked whenMerger
cannot merge a metadata value by itself. The default implementation throws anInvalidMetadataException
. Subclasses can override this method if they want to perform a different processing.- Parameters:
target
- the metadata instance in which the value should have been written.propertyName
- the name of the property to write.sourceValue
- the value to write.targetValue
- the value that already exist in the target metadata.
-