Package net.sf.freecol.common.model
Class StringTemplate
- java.lang.Object
-
- net.sf.freecol.common.model.FreeColObject
-
- net.sf.freecol.common.model.StringTemplate
-
- All Implemented Interfaces:
java.lang.Comparable<FreeColObject>,ObjectWithId
- Direct Known Subclasses:
HistoryEvent,ModelMessage
public class StringTemplate extends FreeColObject
TheStringTemplaterepresents 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classStringTemplate.TemplateTypeThe type of this StringTemplate, either NAME, a proper name that must not be localized (e.g.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringDEFAULT_ID_TAGprivate java.lang.StringdefaultIdAn alternative key to use if the identifier is not contained in the message bundle.private static java.util.List<java.util.AbstractMap.SimpleEntry<java.lang.String,StringTemplate>>emptyListFixed trivial return value for entryList().private java.util.List<java.util.AbstractMap.SimpleEntry<java.lang.String,StringTemplate>>kvThe key,value paris to apply within the string template.private static java.lang.StringOLD_KEY_TAGprivate static java.lang.StringPAIR_TAGstatic java.lang.StringTAGprivate static java.lang.StringTEMPLATE_TYPE_TAGprivate StringTemplate.TemplateTypetemplateTypeThe TemplateType of this StringTemplate.-
Fields inherited from class net.sf.freecol.common.model.FreeColObject
ARRAY_SIZE_TAG, DEFAULT_CLASS_INDEX, fcoComparator, ID_ATTRIBUTE_TAG, logger, PARTIAL_ATTRIBUTE_TAG, VALUE_TAG
-
-
Constructor Summary
Constructors Modifier Constructor Description StringTemplate()Trivial constructor to allow creation with Game.newInstance.protectedStringTemplate(java.lang.String id, java.lang.String defaultId, StringTemplate.TemplateType templateType)Creates a newStringTemplateinstance.protectedStringTemplate(java.lang.String id, StringTemplate template)Copy an existing template, but with a new identifier.StringTemplate(FreeColXMLReader xr)Create a newStringTemplateby reading a stream.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <T extends StringTemplate>
Tadd(java.lang.String value)Add a replacement value without a key to this template.<T extends StringTemplate>
Tadd(java.lang.String key, java.lang.String value)Add a new key and replacement value to this template.<T extends StringTemplate>
TaddAmount(java.lang.String key, java.lang.Number amount)Add a key and an integer value to replace it to this template.private voidaddKey(java.lang.String key)Add a key.<T extends StringTemplate>
TaddName(java.lang.String value)Add a replacement proper name without a key to this template.<T extends StringTemplate>
TaddName(java.lang.String key, java.lang.String value)Add a new key and replacement proper name to this template.<T extends StringTemplate>
TaddName(java.lang.String key, FreeColObject object)Add a new key and replacement namable object to this template.<T extends StringTemplate>
TaddNamed(java.lang.String key, Named named)Add a key and named object to this template.<T extends StringTemplate>
TaddNamed(Named named)Add named object without key to this template.private voidaddPair(java.lang.String key, StringTemplate value)Add a key, value pair.private voidaddReplacement(StringTemplate replacement)Add a replacement.<T extends StringTemplate>
TaddStringTemplate(java.lang.String key, StringTemplate template)Add a key and a replacement StringTemplate to this template.<T extends StringTemplate>
TaddStringTemplate(StringTemplate template)Add a StringTemplate to this template.<T extends StringTemplate>
TaddTagged(java.lang.String key, java.lang.String value)Add a tagged value.private <T extends StringTemplate>
Tcomplete(java.lang.String key, StringTemplate value)Add an optional key and replacement.static StringTemplatecopy(java.lang.String id, StringTemplate template)<T extends FreeColObject>
booleancopyIn(T other)Copy another FreeColObject into this one if it is compatible.java.util.List<java.util.AbstractMap.SimpleEntry<java.lang.String,StringTemplate>>entryList()Get the list of key, value pairs.booleanequals(java.lang.Object o)java.lang.StringgetDefaultId()Get the default identifier.StringTemplategetReplacement(java.lang.String key)Try to find the replacement for a given key.StringTemplate.TemplateTypegetTemplateType()Get the template type.java.lang.StringgetXMLTagName()Get the serialization tag for this object.inthashCode()booleanisEmpty()Has nothing been added to this template?static StringTemplatekey(java.lang.String value)static StringTemplatekey(Named named)static StringTemplatelabel(java.lang.String value)static StringTemplatename(java.lang.String value)protected voidreadAttributes(FreeColXMLReader xr)Reads the attributes of this object from an XML stream.protected voidreadChild(FreeColXMLReader xr)Reads a single child object.protected voidreadChildren(FreeColXMLReader xr)Reads the children of this object from an XML stream.voidsetDefaultId(java.lang.String id)Set the default identifier.protected <T extends StringTemplate>
TsetDefaultId(java.lang.String id, java.lang.Class<T> returnClass)Wrapper for subclasses to set the default identifier and return the setting object.static StringTemplatetemplate(java.lang.String value)static StringTemplatetemplate(Named named)java.lang.StringtoString()protected voidwriteAttributes(FreeColXMLWriter xw)Write the attributes of this object to a stream.protected voidwriteChildren(FreeColXMLWriter xw)Write the children of this object to a stream.-
Methods inherited from class net.sf.freecol.common.model.FreeColObject
addAbility, addFeatures, addModifier, addPropertyChangeListener, addPropertyChangeListener, apply, apply, applyModifiers, applyModifiers, arrayKey, compareIds, compareTo, containsAbilityKey, containsModifierKey, copy, copy, copy, copy, copyInCast, dumpObject, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAbilities, getAbilities, getAbilities, getAbilities, getClassIndex, getDefenceModifiers, getDisplayObject, getFeatureContainer, getFreeColObjectClass, getFreeColObjectClassByName, getGame, getId, getIdNumber, getIdType, getIdTypeByName, getModifiers, getModifiers, getModifiers, getModifiers, getObjectClassIndex, getPropertyChangeListeners, getPropertyChangeListeners, getPropertyChangeSupport, getSortedAbilities, getSortedModifiers, getSpecification, getSuffix, getSuffix, hasAbility, hasAbility, hasAbility, hasListeners, hasModifier, hasModifier, hasModifier, idEquals, invokeMethod, logFreeColObjects, readFromXML, readFromXMLPartial, removeAbilities, removeAbility, removeFeatures, removeModifier, removeModifiers, removePropertyChangeListener, removePropertyChangeListener, save, save, save, save, serialize, serialize, serialize, serialize, setGame, setId, setSpecification, toXML, toXML, toXMLPartial, toXMLPartial
-
-
-
-
Field Detail
-
TAG
public static final java.lang.String TAG
- See Also:
- Constant Field Values
-
emptyList
private static final java.util.List<java.util.AbstractMap.SimpleEntry<java.lang.String,StringTemplate>> emptyList
Fixed trivial return value for entryList().
-
templateType
private StringTemplate.TemplateType templateType
The TemplateType of this StringTemplate. Defaults to KEY.
-
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.
-
DEFAULT_ID_TAG
private static final java.lang.String DEFAULT_ID_TAG
- See Also:
- Constant Field Values
-
PAIR_TAG
private static final java.lang.String PAIR_TAG
- See Also:
- Constant Field Values
-
TEMPLATE_TYPE_TAG
private static final java.lang.String TEMPLATE_TYPE_TAG
- See Also:
- Constant Field Values
-
OLD_KEY_TAG
private static final java.lang.String OLD_KEY_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- AStringTemplateto copy.
-
StringTemplate
protected StringTemplate(java.lang.String id, java.lang.String defaultId, StringTemplate.TemplateType templateType)Creates a newStringTemplateinstance.- Parameters:
id- The object identifier.defaultId- The default identifier.templateType- TheTemplateTypefor this template.
-
StringTemplate
public StringTemplate(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Create a newStringTemplateby reading a stream.- Parameters:
xr- TheFreeColXMLReaderto read.- Throws:
javax.xml.stream.XMLStreamException- if there is a problem reading the stream.
-
-
Method Detail
-
copy
public static StringTemplate copy(java.lang.String id, StringTemplate template)
-
name
public static StringTemplate name(java.lang.String value)
-
key
public static StringTemplate key(Named named)
-
key
public static StringTemplate key(java.lang.String value)
-
template
public static StringTemplate template(Named named)
-
template
public static StringTemplate template(java.lang.String value)
-
label
public static StringTemplate label(java.lang.String value)
-
getTemplateType
public final StringTemplate.TemplateType getTemplateType()
Get the template type.- Returns:
- The template type.
-
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 identifierreturnClass- 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- TheStringkey.value- TheStringTemplatevalue.
-
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- TheStringTemplatereplacement 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 replacementStringTemplate.- 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 replacementFreeColObject.- 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- TheNamedto 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- TheNamedto 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- TheNumbervalue 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- TheStringTemplatevalue.- 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 replacementStringTemplate.- 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:
copyInin classFreeColObject- Type Parameters:
T- TheFreeColObjectsubclass 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:
writeAttributesin classFreeColObject- Parameters:
xw- TheFreeColXMLWriterto 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:
writeChildrenin classFreeColObject- Parameters:
xw- TheFreeColXMLWriterto 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:
readAttributesin classFreeColObject- Parameters:
xr- TheFreeColXMLReaderto 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:
readChildrenin classFreeColObject- Parameters:
xr- TheFreeColXMLReaderto 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:
readChildin classFreeColObject- Parameters:
xr- TheFreeColXMLReaderto 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:
getXMLTagNamein classFreeColObject- Returns:
- The tag.
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classFreeColObject
-
hashCode
public int hashCode()
- Overrides:
hashCodein classFreeColObject
-
toString
public java.lang.String toString()
- Overrides:
toStringin classFreeColObject
-
-