package caseStudy;

public class SaveableField <T> {
    // Makes it easier to save the fields in the XML file
    private T value;
    private String identifier;
    private String xmlStartTag;
    private String xmlEndTag;
    
    // Constructors
    public SaveableField (String id) {
        identifier = id;
        value = null;
        xmlStartTag = createStartTag();
        xmlEndTag = createEndTag();
    }
    public SaveableField (T value, String id) {
        identifier = id;
        this.value = value;
        xmlStartTag = createStartTag();
        xmlEndTag = createEndTag();
    }
    
    // Accessors
    public T getValue() {
        return value;
    }

    public String save() {
        return xmlStartTag + value.toString() + xmlEndTag;
    }
    
    // Modifier
    public void setValue(T newValue) {
        value = newValue;
    }
    public String getStringDataFromXML(String xml) {
        return SaveAsXML.findInXML(xml, xmlStartTag, xmlEndTag);
    }

    // Derive the tags from the identifier
    private String createStartTag() {
        return "<" + identifier + ">";
    }
    private String createEndTag() {
        return "</" + identifier + ">";
    }
}
