All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fitnesse.slim.converters.GenericCollectionConverter Maven / Gradle / Ivy

There is a newer version: 20181217
Show newest version
package fitnesse.slim.converters;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;

import fitnesse.slim.Converter;
import fitnesse.util.StringUtils;

public class GenericCollectionConverter> implements Converter {

  public static final Map, Class> DEFAULT_COLLECTION_IMPL = new HashMap<>();
  static {
    DEFAULT_COLLECTION_IMPL.put(List.class, ArrayList.class);
    DEFAULT_COLLECTION_IMPL.put(Set.class, HashSet.class);
    DEFAULT_COLLECTION_IMPL.put(Queue.class, PriorityQueue.class);
    DEFAULT_COLLECTION_IMPL.put(Collection.class, ArrayList.class);
  }

  private final Class collectionClass;
  private final Converter componentConverter;

  @SuppressWarnings("unchecked")
  public GenericCollectionConverter(Class collectionClass, Converter componentConverter) {
    if (collectionClass.isInterface())
      collectionClass = (Class) DEFAULT_COLLECTION_IMPL.get(collectionClass);

    this.collectionClass = (Class) collectionClass;
    this.componentConverter = componentConverter;
  }

  @Override
  public String toString(C collection) {
    if (collection == null)
      return NULL_VALUE;

    int size = collection.size();
    List ret = new ArrayList<>(size);
    for (T item : collection) {
      ret.add(getElementString(item));
    }
    return ListConverterHelper.toString(ret);
  }

  private String getElementString(T item) {
    String result;
    if (item == null) {
      result = componentConverter.toString(item);
    } else {
      result = ElementConverterHelper.elementToString(item);
    }
    return result;
  }

  @Override
  public C fromString(String arg) {
    if (StringUtils.isBlank(arg))
      return null;

    C collection;
    try {
      collection = collectionClass.newInstance();
    } catch (Exception e) {
      throw new IllegalStateException("Collection should have a default constructor", e);
    }
    String[] strings = ListConverterHelper.fromStringToArrayOfStrings(arg);
    for (String string : strings) {
      collection.add(componentConverter.fromString(string));
    }
    return collection;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy