package org.apache.commons.beanutils.converters;

import java.math.BigDecimal;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;

public final class BigDecimalConverter implements Converter {
  private Object defaultValue;
  
  private boolean useDefault;
  
  public BigDecimalConverter() {
    this.defaultValue = null;
    this.useDefault = true;
    this.defaultValue = null;
    this.useDefault = false;
  }
  
  public BigDecimalConverter(Object defaultValue) {
    this.defaultValue = null;
    this.useDefault = true;
    this.defaultValue = defaultValue;
    this.useDefault = true;
  }
  
  public Object convert(Class type, Object value) {
    if (value == null) {
      if (this.useDefault)
        return this.defaultValue; 
      throw new ConversionException("No value specified");
    } 
    if (value instanceof BigDecimal)
      return value; 
    try {
      return new BigDecimal(value.toString());
    } catch (Exception e) {
      if (this.useDefault)
        return this.defaultValue; 
      throw new ConversionException(e);
    } 
  }
}
