package vermilion;

/**
 * The simplest of UsefulFileObjects, which stores either a String or a long.
 */
public class UsefulFileObjectSimple extends UsefulFileObject
{
    /**
     * String value for the object.
     */
    public String strValue;

    /**
     * Numeric value for the object.
     */
    public long longValue;

    /**
     * Construct a new object with the given string value.
     */
    public UsefulFileObjectSimple(String value)
    {
        strValue = value;
        longValue = -1;
    }

    /**
     * Construct a new object with the given numeric value.
     */
    public UsefulFileObjectSimple(long value)
    {
        longValue = value;
        strValue = null;
    }

    /**
     * The serialisation type for this object is 'SIMPLE'.
     */
    @Override
    public String getType() { return "SIMPLE"; }

    /**
     * The current serialisation version for this object is 1.
     */
    @Override
    public int getVersion() { return 1; }

    /**
     * Loads the object from serialised form.
     */
    @Override
    public UsefulFileObject loadImpl(String str, int version)
    {
        if (version == 1)
        {
            if (str.startsWith("$"))
            {
                return new UsefulFileObjectSimple(str.substring(1));
            }
            else
            {
                return new UsefulFileObjectSimple(Integer.parseInt(str));
            }
        }

        return null;
    }

    /**
     * Saves the object to serialised form.
     */
    @Override
    public String saveImpl()
    {
        if (strValue != null) return "$" + strValue;
        else return String.valueOf(longValue);
    }
}