
package de.ivu.elect.presentation.ui;

import static java.util.stream.Collectors.toMap;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import de.ivu.elect.business.ui.entity.UITableColumnOrder;

public class UITableColumnOrderConverter implements Converter {

  private final Map<String, UITableColumnOrder> mapped;

  public UITableColumnOrderConverter(List<UITableColumnOrder> availableUITableColumnOrders) {
    super();

    this.mapped = availableUITableColumnOrders.stream().collect(toMap(UITableColumnOrder::getColumnKey, Function.identity()));
  }

  @Override
  public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String s) {
    return mapped.get(s);
  }

  @Override
  public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object o) {
    return ((UITableColumnOrder) o).getColumnKey();
  }
}
