package be.iminds.ilabt.jfed.groovy_dsl.impl.events;

import be.iminds.ilabt.jfed.groovy_dsl.DSLStreamException;
import be.iminds.ilabt.jfed.groovy_dsl.events.PropertyElement;

import java.io.IOException;
import java.io.Writer;

/**
 * User: twalcari
 * Date: 5/18/2015
 * Time: 16:31
 */
public class NumberPropertyElementImpl extends DSLEventImpl implements PropertyElement<Number> {

    private final String name;
    private final Number value;

    public NumberPropertyElementImpl(String name, Number value) {
        this.name = name;
        this.value = value;
    }

    @Override
    public void write(Writer writer) throws DSLStreamException {
        try {
            writer.write(name);
            writer.write(" = ");
            if (value instanceof Double)
                writer.write(Double.toString(value.doubleValue()));
            if (value instanceof Long)
                writer.write(Long.toString(value.longValue()));
            if (value instanceof Integer)
                writer.write(Integer.toString(value.intValue()));
            if (value instanceof Float)
                writer.write(Float.toString(value.floatValue()));
            if (value instanceof Short)
                writer.write(Short.toString(value.shortValue()));
            writer.write("\n");
        } catch (IOException e) {
            throw new DSLStreamException(e);
        }
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public Number getValue() {
        return value;
    }
}
