package cnp.ew.spin;

import java.io.*;

public class CpNumericFormatField implements CpFormatField
{
    String format;
    String value;

    CpNumericFormatField(String s)
    {
        format = s;
    }

    public int getDigitCount()
    {
        return format.length();
    }

    public void readValueFrom(InputStream stream)
    {
        value = "";
        int charCount = 0;
        int nextChar;

        while (charCount < getDigitCount()) {
            try {
                nextChar = stream.read();
            }
            catch (IOException e) {
                throw new IllegalArgumentException("illegal formattable string");
            }

            if (nextChar < 0) {
                throw new IllegalArgumentException("illegal formattable string");
            }
            if (Character.isDigit((char)nextChar)) {
                value = value + new Character((char)nextChar);
                charCount++;
            }
        }
    }
    public void writeValueTo(PrintStream stream)
    {
        stream.print(value);
    }

    public void writeFormattedValueTo(PrintStream stream)
    {
        stream.print(value);
    }

    public void setValue(String newValue)
    {
        value = newValue;
    }

    public String getValue()
    {
        return value;
    }

    public String getFormat()
    {
        return format;
    }

    public CpSpinFieldLc createSpinField(int index)
    {
        return new CpIndexedModelNumericSpinFieldLc(index, format.length());
    }
}
