Class StringTemplate

  • All Implemented Interfaces:
    java.lang.Comparable<FreeColObject>, ObjectWithId
    Direct Known Subclasses:
    HistoryEvent, ModelMessage

    public class StringTemplate
    extends FreeColObject
    The StringTemplate represents a non-localized string that can be localized by looking up its value in a message bundle or similar Map. The StringTemplate may contain variables (keys) delimited by the '%' character, such as "%amount%" that will be replaced with a string or a StringTemplate. If the StringTemplate contains replacement values but no keys, then it is considered a "label" StringTemplate, and its value will be used to join the replacement values.
    Version:
    1.0
    • Field Detail

      • emptyList

        private static final java.util.List<java.util.AbstractMap.SimpleEntry<java.lang.String,​StringTemplate>> emptyList
        Fixed trivial return value for entryList().
      • defaultId

        private java.lang.String defaultId
        An alternative key to use if the identifier is not contained in the message bundle.
      • kv

        private java.util.List<java.util.AbstractMap.SimpleEntry<java.lang.String,​StringTemplate>> kv
        The key,value paris to apply within the string template.
      • TEMPLATE_TYPE_TAG

        private static final java.lang.String TEMPLATE_TYPE_TAG
        See Also:
        Constant Field Values
    • Constructor Detail

      • StringTemplate

        public StringTemplate()
        Trivial constructor to allow creation with Game.newInstance.
      • StringTemplate

        protected StringTemplate​(java.lang.String id,
                                 StringTemplate template)
        Copy an existing template, but with a new identifier.
        Parameters:
        id - The object identifier.
        template - A StringTemplate to copy.
      • StringTemplate

        protected StringTemplate​(java.lang.String id,
                                 java.lang.String defaultId,
                                 StringTemplate.TemplateType templateType)
        Creates a new StringTemplate instance.
        Parameters:
        id - The object identifier.
        defaultId - The default identifier.
        templateType - The TemplateType for this template.
      • StringTemplate

        public StringTemplate​(FreeColXMLReader xr)
                       throws javax.xml.stream.XMLStreamException
        Create a new StringTemplate by reading a stream.
        Parameters:
        xr - The FreeColXMLReader to read.
        Throws:
        javax.xml.stream.XMLStreamException - if there is a problem reading the stream.
    • Method Detail

      • name

        public static StringTemplate name​(java.lang.String value)
      • template

        public static StringTemplate template​(java.lang.String value)
      • label

        public static StringTemplate label​(java.lang.String value)
      • getDefaultId

        public final java.lang.String getDefaultId()
        Get the default identifier.
        Returns:
        The default identifier.
      • setDefaultId

        public final void setDefaultId​(java.lang.String id)
        Set the default identifier.
        Parameters:
        id - The new default identifier.
      • setDefaultId

        protected <T extends StringTemplate> T setDefaultId​(java.lang.String id,
                                                            java.lang.Class<T> returnClass)
        Wrapper for subclasses to set the default identifier and return the setting object.
        Type Parameters:
        T - The actual return type.
        Parameters:
        id - The new default identifier
        returnClass - The expected return class.
        Returns:
        The setting object.
      • isEmpty

        public boolean isEmpty()
        Has nothing been added to this template?
        Returns:
        True if the template is empty.
      • entryList

        public java.util.List<java.util.AbstractMap.SimpleEntry<java.lang.String,​StringTemplate>> entryList()
        Get the list of key, value pairs.
        Returns:
        The pairs.
      • getReplacement

        public StringTemplate getReplacement​(java.lang.String key)
        Try to find the replacement for a given key.
        Parameters:
        key - The key to look for.
        Returns:
        The value found, otherwise null.
      • addPair

        private void addPair​(java.lang.String key,
                             StringTemplate value)
        Add a key, value pair.
        Parameters:
        key - The String key.
        value - The StringTemplate value.
      • addKey

        private void addKey​(java.lang.String key)
        Add a key.
        Parameters:
        key - The key to add.
      • addReplacement

        private void addReplacement​(StringTemplate replacement)
        Add a replacement.
        Parameters:
        replacement - The StringTemplate replacement to add.
      • complete

        private final <T extends StringTemplate> T complete​(java.lang.String key,
                                                            StringTemplate value)
        Add an optional key and replacement. Helper function for the add*() routines that follow.
        Type Parameters:
        T - The actual return type.
        Parameters:
        key - The optional key.
        value - The replacement StringTemplate.
        Returns:
        This object, cast back to its original class.
      • add

        public <T extends StringTemplate> T add​(java.lang.String key,
                                                java.lang.String value)
        Add a new key and replacement value to this template. This is only possible if the template is of type TEMPLATE.
        Type Parameters:
        T - The actual return type.
        Parameters:
        key - The key to add.
        value - The corresponding replacement.
        Returns:
        This.
      • add

        public <T extends StringTemplate> T add​(java.lang.String value)
        Add a replacement value without a key to this template. This is only possible if the template is of type LABEL.
        Type Parameters:
        T - The actual return type.
        Parameters:
        value - The replacement value.
        Returns:
        This.
      • addName

        public <T extends StringTemplate> T addName​(java.lang.String key,
                                                    java.lang.String value)
        Add a new key and replacement proper name to this template. This is only possible if the template is of type TEMPLATE.
        Type Parameters:
        T - The actual return type.
        Parameters:
        key - The key to add.
        value - The corresponding replacement.
        Returns:
        This.
      • addName

        public <T extends StringTemplate> T addName​(java.lang.String key,
                                                    FreeColObject object)
        Add a new key and replacement namable object to this template. This is only possible if the StringTemplate is of type TEMPLATE.
        Type Parameters:
        T - The actual return type.
        Parameters:
        key - The key to add.
        object - The replacement FreeColObject.
        Returns:
        This.
      • addName

        public <T extends StringTemplate> T addName​(java.lang.String value)
        Add a replacement proper name without a key to this template. This is only possible if the StringTemplate is of type LABEL.
        Type Parameters:
        T - The actual return type.
        Parameters:
        value - The replacement value.
        Returns:
        This.
      • addNamed

        public <T extends StringTemplate> T addNamed​(java.lang.String key,
                                                     Named named)
        Add a key and named object to this template.
        Type Parameters:
        T - The actual return type.
        Parameters:
        key - The key to add.
        named - The Named to add.
        Returns:
        This.
      • addNamed

        public <T extends StringTemplate> T addNamed​(Named named)
        Add named object without key to this template.
        Type Parameters:
        T - The actual return type.
        Parameters:
        named - The Named to add.
        Returns:
        This.
      • addAmount

        public <T extends StringTemplate> T addAmount​(java.lang.String key,
                                                      java.lang.Number amount)
        Add a key and an integer value to replace it to this template.
        Type Parameters:
        T - The actual return type.
        Parameters:
        key - The key to add.
        amount - The Number value to add.
        Returns:
        This.
      • addStringTemplate

        public <T extends StringTemplate> T addStringTemplate​(java.lang.String key,
                                                              StringTemplate template)
        Add a key and a replacement StringTemplate to this template. This is only possible if the StringTemplate is of type TEMPLATE.
        Type Parameters:
        T - The actual return type.
        Parameters:
        key - The key to add.
        template - The StringTemplate value.
        Returns:
        This.
      • addStringTemplate

        public <T extends StringTemplate> T addStringTemplate​(StringTemplate template)
        Add a StringTemplate to this template. This is only possible if the StringTemplate is of type LABEL.
        Type Parameters:
        T - The actual return type.
        Parameters:
        template - The replacement StringTemplate.
        Returns:
        This.
      • addTagged

        public <T extends StringTemplate> T addTagged​(java.lang.String key,
                                                      java.lang.String value)
        Add a tagged value. Functionally identical to add(), but used to distinguish the special cases at the point they are used.
        Type Parameters:
        T - The actual return type.
        Parameters:
        key - The tag.
        value - The special tag value.
        Returns:
        This.
      • copyIn

        public <T extends FreeColObject> boolean copyIn​(T other)
        Copy another FreeColObject into this one if it is compatible.
        Overrides:
        copyIn in class FreeColObject
        Type Parameters:
        T - The FreeColObject subclass of the object to copy in.
        Parameters:
        other - The other object.
        Returns:
        True if the copy in is succesful.
      • writeAttributes

        protected void writeAttributes​(FreeColXMLWriter xw)
                                throws javax.xml.stream.XMLStreamException
        Write the attributes of this object to a stream. To be overridden if required by any object that has attributes and uses the toXML(FreeColXMLWriter, String) call.
        Overrides:
        writeAttributes in class FreeColObject
        Parameters:
        xw - The FreeColXMLWriter to write to.
        Throws:
        javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.
      • writeChildren

        protected void writeChildren​(FreeColXMLWriter xw)
                              throws javax.xml.stream.XMLStreamException
        Write the children of this object to a stream. To be overridden if required by any object that has children and uses the toXML(FreeColXMLWriter, String) call.
        Overrides:
        writeChildren in class FreeColObject
        Parameters:
        xw - The FreeColXMLWriter to write to.
        Throws:
        javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.
      • readAttributes

        protected void readAttributes​(FreeColXMLReader xr)
                               throws javax.xml.stream.XMLStreamException
        Reads the attributes of this object from an XML stream.
        Overrides:
        readAttributes in class FreeColObject
        Parameters:
        xr - The FreeColXMLReader to read from.
        Throws:
        javax.xml.stream.XMLStreamException - if there is a problem reading the stream.
      • readChildren

        protected void readChildren​(FreeColXMLReader xr)
                             throws javax.xml.stream.XMLStreamException
        Reads the children of this object from an XML stream.
        Overrides:
        readChildren in class FreeColObject
        Parameters:
        xr - The FreeColXMLReader to read from.
        Throws:
        javax.xml.stream.XMLStreamException - if there is a problem reading the stream.
      • readChild

        protected void readChild​(FreeColXMLReader xr)
                          throws javax.xml.stream.XMLStreamException
        Reads a single child object. Subclasses must override to read their enclosed elements. This particular instance of the routine always throws XMLStreamException because we should never arrive here. However it is very useful to always call super.readChild() when an unexpected tag is encountered, as the exception thrown here provides some useful debugging context.
        Overrides:
        readChild in class FreeColObject
        Parameters:
        xr - The FreeColXMLReader to read from.
        Throws:
        javax.xml.stream.XMLStreamException - if there is a problem reading the stream.
      • getXMLTagName

        public java.lang.String getXMLTagName()
        Get the serialization tag for this object.
        Specified by:
        getXMLTagName in class FreeColObject
        Returns:
        The tag.
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class FreeColObject