Class IdentifierMapAdapter

java.lang.Object
java.util.AbstractMap<org.opengis.metadata.citation.Citation,String>
org.apache.sis.internal.jaxb.IdentifierMapAdapter
All Implemented Interfaces:
Serializable, Map<org.opengis.metadata.citation.Citation,String>, IdentifierMap
Direct Known Subclasses:
ModifiableIdentifierMap

public class IdentifierMapAdapter extends AbstractMap<org.opengis.metadata.citation.Citation,String> implements IdentifierMap, Serializable
Implementation of the map of identifiers associated to IdentifiedObject instances. This base class implements an unmodifiable map, but the ModifiableIdentifierMap subclass add write capabilities.

This class works as a wrapper around a collection of identifiers. Because all operations are performed by an iteration over the collection elements, this implementation is suitable only for small maps (less than 10 elements). Given that objects typically have only one or two identifiers, this is considered acceptable.

Special cases

The identifiers for the following authorities are handled in a special way:

Handling of duplicated authorities

The collection shall not contain more than one identifier for the same authority. However, duplications may happen if the user has direct access to the list, for example through Citation.getIdentifiers(). If such duplication is found, then this map implementation applies the following rules:
  • All getter methods (including the iterators and the values returned by the put and remove methods) return only the identifier code associated to the first occurrence of each authority. Any subsequent occurrences of the same authorities are silently ignored.
  • All setter methods may affect all identifiers previously associated to the given authority, not just the first occurrence. The only guarantee is that the list is update in such a way that the effect of setter methods are visible to subsequent calls to getter methods.

Handling of null identifiers

The collection of identifiers shall not contain any null element. This is normally ensured by the ModifiableMetadata internal collection implementations. This class performs opportunist null checks as an additional safety, but consistency is not guaranteed. See size() for more information.

Thread safety

This class is thread safe if the underlying identifier collection is thread safe.
Since:
0.3
Version:
1.3
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      For cross-version compatibility.
      See Also:
    • EMPTY

      public static final IdentifierMap EMPTY
      An immutable empty instance.
    • identifiers

      public final Collection<org.opengis.metadata.Identifier> identifiers
      The identifiers to wrap in a map view.
      See Also:
  • Constructor Details

    • IdentifierMapAdapter

      public IdentifierMapAdapter(Collection<org.opengis.metadata.Identifier> identifiers)
      Creates a new map which will be a view over the given identifiers.
      Parameters:
      identifiers - the identifiers to wrap in a map view.
  • Method Details

    • getIdentifiers

      public final Collection<org.opengis.metadata.Identifier> getIdentifiers(Class<?> type)
      Returns the identifiers as a collection of the specified type. The given type is the return type of the getIdentifiers() method which is delegating to this method. The returned collection is modifiable only if identifiers is already of the desired type. This is the case for ISOMetadata.getIdentifiers(), which is the API for which we want modifiable collections.
      Parameters:
      type - Collection.class, List.class or Set.class.
      Returns:
      the identifiers as a collection of the specified type.
    • specialCase

      static int specialCase(Object authority)
      If the given authority is a special case, returns its NonMarshalledAuthority integer enum. Otherwise returns -1. See javadoc for more information about special cases.
      Parameters:
      authority - a Citation constant. The type is relaxed to Object because the signature of some Map methods are that way.
    • getHRef

      private URI getHRef()
      Extracts the xlink:href value from the XLink if presents. This method does not test if an explicit xlink:href identifier exists; this check must be done by the caller before to invoke this method.
      See Also:
    • toString

      private static String toString(Object value)
      Returns the string representation of the given value, or null if none.
      Parameters:
      value - the value returned be one of the above getFoo() methods.
    • isModifiable

      boolean isModifiable()
      Whether this map support put and remove operations.
    • isEmpty

      public final boolean isEmpty()
      Returns true if the collection of identifiers contains at least one element. This method does not verify if the collection contains null element (it should not).
      Specified by:
      isEmpty in interface Map<org.opengis.metadata.citation.Citation,String>
      Overrides:
      isEmpty in class AbstractMap<org.opengis.metadata.citation.Citation,String>
    • size

      public final int size()
      Counts the number of entries, ignoring null elements and duplicated authorities.

      Because null elements are ignored, this method may return 0 even if isEmpty() returns false. However, this inconsistency should not happen in practice because ModifiableMetadata internal collection implementations do not allow null values.

      Specified by:
      size in interface Map<org.opengis.metadata.citation.Citation,String>
      Overrides:
      size in class AbstractMap<org.opengis.metadata.citation.Citation,String>
    • containsValue

      public final boolean containsValue(Object code)
      Returns true if at least one identifier declares the given code.
      Specified by:
      containsValue in interface Map<org.opengis.metadata.citation.Citation,String>
      Overrides:
      containsValue in class AbstractMap<org.opengis.metadata.citation.Citation,String>
      Parameters:
      code - the code to search, which should be an instance of String.
      Returns:
      true if at least one identifier uses the given code.
    • containsKey

      public final boolean containsKey(Object authority)
      Returns true if at least one identifier declares the given authority.
      Specified by:
      containsKey in interface Map<org.opengis.metadata.citation.Citation,String>
      Overrides:
      containsKey in class AbstractMap<org.opengis.metadata.citation.Citation,String>
      Parameters:
      authority - the authority to search, which should be an instance of Citation.
      Returns:
      true if at least one identifier uses the given authority.
    • getIdentifier

      final org.opengis.metadata.Identifier getIdentifier(org.opengis.metadata.citation.Citation authority)
      Returns the identifier for the given key, or null if none.
    • getSpecialized

      public final <T> T getSpecialized(IdentifierSpace<T> authority)
      Returns the identifier associated with the given authority, or null if no specialized identifier was found.
      Specified by:
      getSpecialized in interface IdentifierMap
      Type Parameters:
      T - the identifier type.
      Parameters:
      authority - the namespace whose associated identifier is to be returned.
      Returns:
      the identifier to which the given namespace is mapped, or null if this map contains no mapping for the namespace.
    • get

      public final String get(Object authority)
      Returns the code of the first identifier associated with the given authority, or null if no identifier was found.
      Specified by:
      get in interface Map<org.opengis.metadata.citation.Citation,String>
      Overrides:
      get in class AbstractMap<org.opengis.metadata.citation.Citation,String>
      Parameters:
      authority - the authority to search, which should be an instance of Citation.
      Returns:
      the code of the identifier for the given authority, or null if none.
    • remove

      public String remove(Object authority) throws UnsupportedOperationException
      Removes all identifiers associated with the given authority.
      Specified by:
      remove in interface Map<org.opengis.metadata.citation.Citation,String>
      Overrides:
      remove in class AbstractMap<org.opengis.metadata.citation.Citation,String>
      Parameters:
      authority - the authority to search, which should be an instance of Citation.
      Returns:
      the code of the identifier for the given authority, or null if none.
      Throws:
      UnsupportedOperationException - if the collection of identifiers is unmodifiable.
    • clear

      public void clear() throws UnsupportedOperationException
      Removes every entries in the underlying collection.
      Specified by:
      clear in interface Map<org.opengis.metadata.citation.Citation,String>
      Overrides:
      clear in class AbstractMap<org.opengis.metadata.citation.Citation,String>
      Throws:
      UnsupportedOperationException - if the collection of identifiers is unmodifiable.
    • put

      public String put(org.opengis.metadata.citation.Citation authority, String code) throws UnsupportedOperationException
      Sets the code of the identifier having the given authority to the given value. If no identifier is found for the given authority, a new one is created. If more than one identifier is found for the given authority, then all previous identifiers may be removed in order to ensure that the new entry will be the first entry, so it can be find by the get method.
      Specified by:
      put in interface Map<org.opengis.metadata.citation.Citation,String>
      Overrides:
      put in class AbstractMap<org.opengis.metadata.citation.Citation,String>
      Parameters:
      authority - the authority for which to set the code.
      code - the new code for the given authority, or null for removing the entry.
      Returns:
      the previous code for the given authority, or null if none.
      Throws:
      UnsupportedOperationException - if the collection of identifiers is unmodifiable.
    • putSpecialized

      public <T> T putSpecialized(IdentifierSpace<T> authority, T value) throws UnsupportedOperationException
      Sets the identifier associated with the given authority, and returns the previous value.
      Specified by:
      putSpecialized in interface IdentifierMap
      Type Parameters:
      T - the identifier type.
      Parameters:
      authority - the namespace with which the given identifier is to be associated.
      value - the identifier to be associated with the given namespace.
      Returns:
      the previous identifier associated with authority, or null if there was no mapping of the specialized type for authority.
      Throws:
      UnsupportedOperationException - if the identifier map is unmodifiable.
    • entrySet

      public Set<Map.Entry<org.opengis.metadata.citation.Citation,String>> entrySet()
      Returns a view over the collection of identifiers. This view supports removal operation if the underlying collection of identifiers supports the Iterator.remove() method.

      If the backing identifier collection contains null entries, those entries will be ignored. If the backing collection contains many entries for the same authority, then only the first occurrence is included.

      Specified by:
      entrySet in interface Map<org.opengis.metadata.citation.Citation,String>
      Specified by:
      entrySet in class AbstractMap<org.opengis.metadata.citation.Citation,String>
      Returns:
      a view over the collection of identifiers.
    • toString

      public String toString()
      Overrides the string representation in order to use only the authority title as keys. We do that because the string representations of DefaultCitation objects are very big.

      String examples:

      • {gml:id=“myID”}
      • {gco:uuid=“42924124-032a-4dfe-b06e-113e3cb81cf0”}
      • {xlink:href=“http://www.mydomain.org/myHREF”}
      Overrides:
      toString in class AbstractMap<org.opengis.metadata.citation.Citation,String>
      See Also: