package org.apache.commons.beanutils.converters;

import java.util.List;
import org.apache.commons.beanutils.ConversionException;

public final class StringArrayConverter extends AbstractArrayConverter {
  public StringArrayConverter() {
    this.defaultValue = null;
    this.useDefault = false;
  }
  
  public StringArrayConverter(Object defaultValue) {
    this.defaultValue = defaultValue;
    this.useDefault = true;
  }
  
  private static String[] model = new String[0];
  
  public Object convert(Class type, Object value) {
    if (value == null) {
      if (this.useDefault)
        return this.defaultValue; 
      throw new ConversionException("No value specified");
    } 
    if (model.getClass() == value.getClass())
      return value; 
    try {
      List list = parseElements(value.toString());
      String[] results = new String[list.size()];
      for (int i = 0; i < results.length; i++)
        results[i] = list.get(i); 
      return results;
    } catch (Exception e) {
      if (this.useDefault)
        return this.defaultValue; 
      throw new ConversionException(value.toString(), e);
    } 
  }
}
