package de.ivu.elect.business.exchange.boundary;

import javax.faces.convert.ConverterException;


import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.normalizeSpace;

public class ExchangeLongConverter implements ExchangeTypeConverter {

  public ExchangeLongConverter() {
    super();
  }

  /**
   * copied from org.jsefa.common.converter
   * modified by WAA
   */
  @Override
  public Long fromString(String value) {
    if (isBlank(value)) {
      return null;
    }
    String valueNormalized = normalizeSpace(value);
    try {
      return new Long(valueNormalized);
    } catch (NumberFormatException e) {
      if (valueNormalized.charAt(0) == '+') {
        return fromString(valueNormalized.substring(1));
      }
      throw new ConverterException("Wrong Long format: " + value);
    }
  }

  /**
   * copied from org.jsefa.common.converter
   * modified by WAA
   */
  @Override
  public String toString(Object value) {
    if (value == null) {
      return "";
    }
    return value.toString();
  }
}
