Class LocaleAndCharset

java.lang.Object
org.apache.sis.internal.jaxb.lan.LocaleAndCharset
All Implemented Interfaces:
TreeTable.Node

public final class LocaleAndCharset extends Object implements TreeTable.Node
Utility methods for handling Map<Locale,Charset> as separated collections. Locale and character set were separated properties in legacy ISO 19115:2003 but become combined under a single PT_Locale entity in ISO 19115:2014. This change is not really convenient in Java since the standard Locale and Charset objects are separated entities. This class provides two services for managing that:
  • Static methods, used mostly for JAXB marshalling and unmarshalling.
  • Implementation of TreeTable.Node for viewing a Map.Entry<Locale,Charset> as a Locale node with a Charset child. This is used for providing textual representation of metadata.
Example:
Since:
1.0
Version:
1.0
  • Field Details

    • node

      private final TreeTable.Node node
      The node containing a Map.Entry<Locale,Charset> value. This is the node to replace by this LocaleAndCharset view.
  • Constructor Details

    • LocaleAndCharset

      public LocaleAndCharset(TreeTable.Node node)
      Creates a new node for the given entry. The user object associated to the given node must be an instance of Map.Entry<Locale,Charset>.
      Parameters:
      node - the node to wrap.
  • Method Details

    • getParent

      public TreeTable.Node getParent()
      Delegates to wrapped node since this LocaleAndCharset is a substitute for that node.
      Specified by:
      getParent in interface TreeTable.Node
      Returns:
      the parent, or null if none.
    • isEditable

      public boolean isEditable(TableColumn<?> column)
      Considers this node as non-editable since it represents the key in a map, and keys cannot be modified through the Map.Entry interface. However, LocaleAndCharset.Child will be editable for the value column.
      Specified by:
      isEditable in interface TreeTable.Node
      Parameters:
      column - the column to query.
      Returns:
      true if the given column is a legal column for this Node implementation and the corresponding value is editable, or false otherwise.
    • isLeaf

      public boolean isLeaf()
      Returns false since this node can have a children, which is the LocaleAndCharset.Child.
      Specified by:
      isLeaf in interface TreeTable.Node
      Returns:
      true if this node cannot have any children.
    • keyOrValue

      private static Object keyOrValue(Object object, boolean key)
      Returns the key or the value of the given Map.Entry. If the given object is not a map entry or is null, then it is returned as-is. This latter case should never happen (the object shall always be a non-null map entry), but we nevertheless check for making the code more robust to ill-formed metadata. We apply this tolerance because this method is used (indirectly) for toString() implementations, and failure in those methods make debugging more difficult (string representations are often requested when the developer knows that there is a problem to investigate).
      Parameters:
      object - the map entry for which to get the key or the value.
      key - true for fetching the key, or false for fetching the value.
      Returns:
      the requested key or value, or the given object itself if it is not a map entry.
    • getUserObject

      public Object getUserObject()
      Returns the user object associated to this node. For this node, that object is the key (a Locale) of the map entry. For the LocaleAndCharset.Child, the user object will be the value (a Charset) of the same map entry.
      Specified by:
      getUserObject in interface TreeTable.Node
      Returns:
      any object stored at this node by the user, or null if none.
    • getValue

      public <V> V getValue(TableColumn<V> column)
      Returns the value associated to the given column of this node. This method delegates to the wrapped node, then extract the key component of the map entry if the requested column is the value.
      Specified by:
      getValue in interface TreeTable.Node
      Type Parameters:
      V - the base type of values in the given column.
      Parameters:
      column - identifier of the column from which to get the value.
      Returns:
      the value in the given column, or null if none.
      See Also:
    • separateValue

      private <V> V separateValue(TableColumn<V> column, boolean key)
      Implementation of getValue(TableColumn) also used by the LocaleAndCharset.Child.
    • setValue

      public <V> void setValue(TableColumn<V> column, V value)
      Always throws an exception since we cannot edit the key of a map entry. Attempts to edit other columns than the value column will also cause an exception to be thrown, but the error message provided by the wrapped node is more detailed.
      Specified by:
      setValue in interface TreeTable.Node
      Type Parameters:
      V - the base type of values in the given column.
      Parameters:
      column - identifier of the column into which to set the value.
      value - the value to set.
      See Also:
    • getChildren

      public Collection<TreeTable.Node> getChildren()
      Returns the list of children, which is implemented by this class itself. The children are Charset values associated to the Locale. The list contains O or 1 element.
      Specified by:
      getChildren in interface TreeTable.Node
      Returns:
      the children, or an empty collection if none.
    • newChild

      public TreeTable.Node newChild()
      Creates a new child only if none exists.
      Specified by:
      newChild in interface TreeTable.Node
      Returns:
      the new child.
    • hashCode

      public int hashCode()
      Returns a hash code value for this node.
      Specified by:
      hashCode in interface TreeTable.Node
      Overrides:
      hashCode in class Object
      Returns:
      a hash code for this node, potentially based on values and children but ignoring parent.
    • equals

      public boolean equals(Object other)
      Tests this node with the given object for equality. Two LocaleAndCharset instances are considered equal if they wrap the same node.
      Specified by:
      equals in interface TreeTable.Node
      Overrides:
      equals in class Object
      Parameters:
      other - the other object to compare with this node.
      Returns:
      whether the two objects are nodes with equal values and equal children, ignoring parents.
    • getLanguages

      public static Collection<Locale> getLanguages(Map<Locale,Charset> locales)
      Returns the language(s) used within the resource. The returned collection supports the add(Locale) method in order to enable XML unmarshalling of legacy ISO 19157 metadata documents. That hack is not needed for newer XML documents (ISO 19115-3:2016).
      Parameters:
      locales - the map of locales and character sets, or null.
      Returns:
      language(s) used within the resource, or null.
    • getCharacterSets

      public static Collection<Charset> getCharacterSets(Map<Locale,Charset> locales)
      Returns the character coding standard used for the resource. The returned collection supports add(Charset) method in order to enable XML unmarshalling of legacy ISO 19157 metadata documents. That hack is not be needed for newer (ISO 19115-3:2016) XML documents.
      Parameters:
      locales - the map of locales and character sets, or null.
      Returns:
      character coding standard(s) used, or null.
    • setLanguages

      public static Map<Locale,Charset> setLanguages(Map<Locale,Charset> locales, Collection<? extends Locale> newValues)
      Sets the language(s) used within the resource. This method preserves the character sets if possible.
      Parameters:
      locales - the map of locales and character sets, or null.
      newValues - the new languages.
      Returns:
      the given map, or a new map if necessary and the given map was null.
    • setCharacterSets

      public static Map<Locale,Charset> setCharacterSets(Map<Locale,Charset> locales, Collection<? extends Charset> newValues)
      Sets the character coding standard(s) used within the resource. Current implementation takes only the first Charset and set the encoding of all locales to that character set. This is suboptimal, but this approach is used only for implementation of deprecated methods.
      Parameters:
      locales - the map of locales and character sets, or null.
      newValues - the new character coding standard(s).
      Returns:
      the given map, or a new map if necessary and the given map was null.