public class MetadataCopier extends Object
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.
ModifiableMetadata.unmodifiable()
method may provide a better way to use a metadata as a template,
as it returns a snapshot and allows the caller to continue to modify the original metadata object and create new
snapshots. Example:
// Prepare a Citation to be used as a template. DefaultCitation citation = new DefaultCitation(); citation.getCitedResponsibleParties(someAuthor); // Set the title and get a first snapshot. citation.setTitle(new SimpleInternationalString("A title")); Citation myFirstCitation = (Citation) citation.unmodifiable(); // Change the title and get another snapshot. citation.setTitle(new SimpleInternationalString("Another title")); Citation mySecondCitation = (Citation) citation.unmodifiable();This approach allows sharing the children that have the same content, thus reducing memory usage. In above example, the
someAuthor
cited responsible party is the same instance in both citations. In comparison, deep copy operations unconditionally
duplicate everything, no matter if it was needed or not. Nevertheless deep copies are still sometime useful,
for example when we do not have the original ModifiableMetadata
instance anymore.
MetadataCopier
is also useful for converting a metadata tree of unknown implementations (for example the
result of a call to MetadataSource.lookup(Class, String)
) into instances of the
public AbstractMetadata
subclasses. But note that shallow copies as provided by the castOrCopy(…)
static methods in each AbstractMetadata
subclass are sometime sufficient.
ModifiableMetadata.unmodifiable()
Defined in the sis-metadata
module
Constructor and Description |
---|
MetadataCopier(MetadataStandard standard)
Creates a new metadata copier.
|
Modifier and Type | Method and Description |
---|---|
<T> T |
copy(Class<T> type,
T metadata)
Performs a potentially deep copy of the given metadata object.
|
Object |
copy(Object metadata)
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.
|
public MetadataCopier(MetadataStandard standard)
standard
- the default metadata standard to use for object that are not AbstractMetadata
instances,
or null
if none.public Object copy(Object metadata)
metadata
- the metadata object to copy, or null
.null
if the given argument is null
.UnsupportedOperationException
- if there is no implementation class for a metadata to copy,
or an implementation class does not provide a public default constructor.public <T> T copy(Class<T> type, T metadata)
copy(Object)
when the type is known.T
- compile-time value of the type
argument.type
- the interface of the metadata object to copy.metadata
- the metadata object to copy, or null
.null
if the given argument is null
.UnsupportedOperationException
- if there is no implementation class for a metadata to copy,
or an implementation class does not provide a public default constructor.protected Object copyRecursively(Class<?> type, Object metadata)
copy(…)
method with the root metadata
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.
type
- the interface of the metadata object to copy, or null
if unspecified.metadata
- the metadata object to copy, or null
.null
if the given argument is null
.UnsupportedOperationException
- if there is no implementation class for a metadata to copy,
or an implementation class does not provide a public default constructor.Copyright © 2010–2017 The Apache Software Foundation. All rights reserved.