Package org.geotools.metadata
Class ModifiableMetadata
Object
AbstractMetadata
ModifiableMetadata
- All Implemented Interfaces:
Cloneable
- Direct Known Subclasses:
MetadataEntity
Base class for metadata that may (or may not) be modifiable. Implementations will typically provide
set*(...)
methods for each corresponding get*() method. An initially modifiable metadata may become unmodifiable at a
later stage (typically after its construction is completed) by the call to the freeze() method.
Subclasses should follow the pattern below for every get and set methods, with a special
processing for collections:
private Foo property;
public Foo getProperty() {
return property;
}
public synchronized void setProperty(Foo newValue) {
checkWritePermission();
property = newValue;
}
For collections (note that the call to checkWritePermission() is implicit):
private Collection<Foo> properties;
public synchronized Collection<Foo> getProperties() {
return properties = nonNullCollection(properties, Foo.class);
}
public synchronized void setProperties(Collection<Foo> newValues) {
properties = copyCollection(newValues, properties, Foo.class);
}
- Since:
- 2.4
- Author:
- Martin Desruisseaux
-
Field Summary
Fields inherited from class AbstractMetadata
LOGGER -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructs an initially empty metadata.protectedModifiableMetadata(Object source) Constructs a metadata entity initialized with the values from the specified metadata. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidChecks if changes in the metadata are allowed.protected ModifiableMetadataclone()Returns a shallow copy of this metadata.protected final <E> Collection<E>copyCollection(Collection<? extends E> source, Collection<E> target, Class<E> elementType) Copies the content of one collection (source) into an other (target).protected final <E> List<E>copyList(Collection<? extends E> source, List<E> target, Class<E> elementType) Copies the content of one list (source) into an other (target).voidfreeze()Declares this metadata and all its attributes as unmodifiable.final booleanReturnstrueif this metadata is modifiable.protected final <E> Collection<E>nonNullCollection(Collection<E> c, Class<E> elementType) Returns the specified collection, or a new one ifcis null.protected final <E> List<E>nonNullList(List<E> c, Class<E> elementType) Returns the specified list, or a new one ifcis null.protected final <E> Set<E>nonNullSet(Set<E> c, Class<E> elementType) Returns the specified set, or a new one ifcis null.Returns an unmodifiable copy of this metadata.Methods inherited from class AbstractMetadata
asMap, asTree, equals, getInterface, getStandard, hashCode, toString
-
Constructor Details
-
ModifiableMetadata
protected ModifiableMetadata()Constructs an initially empty metadata. -
ModifiableMetadata
protected ModifiableMetadata(Object source) throws ClassCastException, UnmodifiableMetadataException Constructs a metadata entity initialized with the values from the specified metadata. This constructor behavior is as in superclass constructor.- Parameters:
source- The metadata to copy values from.- Throws:
ClassCastException- if the specified metadata don't implements the expected metadata interface.UnmodifiableMetadataException- if this class don't definesetmethods corresponding to thegetmethods found in the implemented interface, or if this instance is not modifiable for some other reason.
-
-
Method Details
-
isModifiable
public final boolean isModifiable()Returnstrueif this metadata is modifiable. This method returnsfalseiffreeze()has been invoked on this object.- Returns:
trueif this metadata is modifiable.
-
unmodifiable
Returns an unmodifiable copy of this metadata. Any attempt to modify an attribute of the returned object will throw anUnmodifiableMetadataException. If this metadata is already unmodifiable, then this method returnsthis.The default implementation clone this metadata and freeze the clone before to return it.
- Returns:
- An unmodifiable copy of this metadata.
-
freeze
public void freeze()Declares this metadata and all its attributes as unmodifiable. This method is invoked automatically by theunmodifiable()method. Subclasses usually don't need to override it since the default implementation performs its work using Java reflection. -
checkWritePermission
Checks if changes in the metadata are allowed. AllsetFoo(...)methods in subclasses should invoke this method (directly or indirectly) before to apply any change.- Throws:
UnmodifiableMetadataException- if this metadata is unmodifiable.
-
copyList
protected final <E> List<E> copyList(Collection<? extends E> source, List<E> target, Class<E> elementType) throws UnmodifiableMetadataException Copies the content of one list (source) into an other (target). If the target list isnull, a new target list is created.A call to
checkWritePermission()is implicit before the copy is performed.- Type Parameters:
E- The type of elements in the list.- Parameters:
source- The source list.nullis synonymous to empty.target- The target list, ornullif not yet created.elementType- The base type of elements to put in the list.- Returns:
target, or a newly created list.- Throws:
UnmodifiableMetadataException- if this metadata is unmodifiable.- Since:
- 2.5
-
copyCollection
protected final <E> Collection<E> copyCollection(Collection<? extends E> source, Collection<E> target, Class<E> elementType) throws UnmodifiableMetadataException Copies the content of one collection (source) into an other (target). If the target collection isnull, or if its type (ListvsSet) doesn't matches the type of the source collection, a new target collection is created.A call to
checkWritePermission()is implicit before the copy is performed.- Type Parameters:
E- The type of elements in the collection.- Parameters:
source- The source collection.nullis synonymous to empty.target- The target collection, ornullif not yet created.elementType- The base type of elements to put in the collection.- Returns:
target, or a newly created collection.- Throws:
UnmodifiableMetadataException- if this metadata is unmodifiable.
-
nonNullCollection
Returns the specified collection, or a new one ifcis null. This is a convenience method for implementation ofgetFoo()methods.- Type Parameters:
E- The type of elements in the collection.- Parameters:
c- The collection to checks.elementType- The element type (used only ifcis null).- Returns:
c, or a new collection ifcis null.
-
nonNullSet
Returns the specified set, or a new one ifcis null. This is a convenience method for implementation ofgetFoo()methods.- Type Parameters:
E- The type of elements in the set.- Parameters:
c- The set to checks.elementType- The element type (used only ifcis null).- Returns:
c, or a new set ifcis null.- Since:
- 2.5
-
nonNullList
Returns the specified list, or a new one ifcis null. This is a convenience method for implementation ofgetFoo()methods.- Type Parameters:
E- The type of elements in the list.- Parameters:
c- The list to checks.elementType- The element type (used only ifcis null).- Returns:
c, or a new list ifcis null.
-
clone
Returns a shallow copy of this metadata.While cloneable, this class do not provides the
clone()operation as part of the public API. The clone operation is required for the internal working of theunmodifiable()method, which expect fromclone()a shallow copy of this metadata entity. The default implementation ofObject.clone()is suffisient for most use.- Overrides:
clonein classObject- Returns:
- A shallow copy of this metadata.
- Throws:
CloneNotSupportedException- if the clone is not supported.
-