Class MetadataCopier
- Get the implementation class of the given metadata instance.
- Create a new instance of the implementation class using the public no-argument constructor.
- Invoke all non-deprecated setter methods on the new instance with the corresponding value from the given metadata.
- If any of the values copied in above step is itself a metadata, recursively performs deep copy on those metadata instances too.
MetadataSource.lookup(Class, String)
) into instances of AbstractMetadata
.
The copier may also be used if a modifiable metadata is desired after
the original metadata has been made final.
Default implementation copies all copiable children, regardless their state. Static factory methods allow to construct some variants, for example skipping the copy of unmodifiable metadata instances since they can be safely shared.
This class supports cyclic graphs in the metadata tree. It may return the given metadata
object directly
if the implementation class does not provide any setter method.
This class is not thread-safe.
In multi-threads environment, each thread should use its own MetadataCopier
instance.
- Since:
- 0.8
- Version:
- 1.2
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.sis.metadata.MetadataVisitor
MetadataVisitor.Filter
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final MetadataStandard
The default metadata standard to use for object that are notAbstractMetadata
instances, ornull
if none.private Object
The current metadata instance where to copy the property values.Fields inherited from class org.apache.sis.metadata.MetadataVisitor
SKIP_SIBLINGS
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> T
Performs a potentially deep copy of the given metadata object.Performs a potentially deep copy of a metadata object of unknown type.protected Object
copyRecursively
(Class<?> type, Object metadata) Performs the actual copy operation on a single metadata instance.static MetadataCopier
forModifiable
(MetadataStandard standard) Creates a new metadata copier which avoid copying unmodifiable metadata.Returns the path to the currently copied property.private MetadataStandard
getStandard
(Object metadata) Returns the metadata standard to use for the given metadata object, ornull
if unknown.(package private) final MetadataVisitor.Filter
preVisit
(PropertyAccessor accessor) Invoked before the properties of a metadata instance are visited.(package private) final Object
result()
Returns the metadata instance resulting from the copy.(package private) final Object
Verifies if the given metadata value is a map or a collection before to invokecopyRecursively(Class, Object)
for metadata elements.Methods inherited from class org.apache.sis.metadata.MetadataVisitor
creator, setCurrentProperty, walk
-
Field Details
-
standard
The default metadata standard to use for object that are notAbstractMetadata
instances, ornull
if none. -
target
The current metadata instance where to copy the property values.
-
-
Constructor Details
-
MetadataCopier
Creates a new metadata copier.- Parameters:
standard
- the default metadata standard to use for object that are notAbstractMetadata
instances, ornull
if none.
-
-
Method Details
-
getStandard
Returns the metadata standard to use for the given metadata object, ornull
if unknown. -
forModifiable
Creates a new metadata copier which avoid copying unmodifiable metadata. More specifically, anyModifiableMetadata
instance in final state will be kept as-is; those final metadata will not be copied since they can be safely shared.- Parameters:
standard
- the default metadata standard to use for object that are notAbstractMetadata
instances, ornull
if none.- Returns:
- a metadata copier which skip the copy of unmodifiable metadata.
- Since:
- 1.0
-
copy
Performs a potentially deep copy of a metadata object of unknown type. The return value does not need to be of the same class than the argument.- Parameters:
metadata
- the metadata object to copy, ornull
.- Returns:
- a copy of the given metadata object, or
null
if the given argument isnull
. - Throws:
UnsupportedOperationException
- if there is no implementation class for a metadata to copy, or an implementation class does not provide a public default constructor.
-
copy
Performs a potentially deep copy of the given metadata object. This method is preferred tocopy(Object)
when the type is known. The specified type should be an interface, not an implementation (for exampleMetadata
, notDefaultMetadata
).- Type Parameters:
T
- compile-time value of thetype
argument.- Parameters:
type
- the interface of the metadata object to copy.metadata
- the metadata object to copy, ornull
.- Returns:
- a copy of the given metadata object, or
null
if the given argument isnull
. - Throws:
IllegalArgumentException
- iftype
is an implementation class instead of interface.UnsupportedOperationException
- if there is no implementation class for a metadata to copy, or an implementation class does not provide a public default constructor.
-
copyRecursively
Performs the actual copy operation on a single metadata instance. This method is invoked by all publiccopy(…)
method with the rootmetadata
object in argument, then is invoked recursively for all properties in that metadata object. If a metadata property is a collection, then this method is invoked for each element in the collection.Subclasses can override this method if they need some control on the copy process.
- Parameters:
type
- the interface of the metadata object to copy, ornull
if unspecified.metadata
- the metadata object to copy, ornull
.- Returns:
- a copy of the given metadata object, or
null
if the given argument isnull
. - Throws:
UnsupportedOperationException
- if there is no implementation class for a metadata to copy, or an implementation class does not provide a public default constructor.
-
preVisit
Invoked before the properties of a metadata instance are visited. This method creates a new instance, to be returned byresult()
, and returnsMetadataVisitor.Filter.WRITABLE_RESULT
for notifying the caller that write operations need to be performed on thatresult
object.- Overrides:
preVisit
in classMetadataVisitor<Object>
- Parameters:
accessor
- information about the standard interface and implementation of the metadata being visited.- Returns:
- most common values are
NON_EMPTY
for visiting all non-empty properties (the default), orWRITABLE
for visiting only writable properties.
-
result
Returns the metadata instance resulting from the copy. This method is invoked before metadata properties are visited. The returned value is a new, initially empty, metadata instance created bypreVisit(PropertyAccessor)
.- Overrides:
result
in classMetadataVisitor<Object>
-
visit
Verifies if the given metadata value is a map or a collection before to invokecopyRecursively(Class, Object)
for metadata elements. This method is invoked byPropertyAccessor.walkWritable(MetadataVisitor, Object, Object)
.- Specified by:
visit
in classMetadataVisitor<Object>
- Parameters:
type
- the type of elements. Note that this is not necessarily the type of givenvalue
argument if the latter is a collection.metadata
- value of the metadata property being visited.- Returns:
- the new property value to set, or
MetadataVisitor.SKIP_SIBLINGS
.
-
getCurrentPropertyPath
Returns the path to the currently copied property. Each element in the list is the UML identifier of a property. Element at index 0 is the name of the property of the root metadata object being copied. Element at index 1 is the name of a property which is a children of above property, etc.The returned list is valid only during
copyRecursively(Class, Object)
method execution. The content of this list become undetermined after thecopyRecursively
method returned.- Overrides:
getCurrentPropertyPath
in classMetadataVisitor<Object>
- Returns:
- the path to the currently copied property.
- Since:
- 1.0
-