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

fitnesse.util.partitioner.MapBasedListPartitioner Maven / Gradle / Ivy

The newest version!
package fitnesse.util.partitioner;

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

/**
 * Partitions list based on map indicating known positions for elenents.
 * @param  list element type
 */
public class MapBasedListPartitioner extends FunctionBasedListPartitioner {
  private final Map positionMap;

  public MapBasedListPartitioner(Function keyFunction, Map positionMap) {
    this(keyFunction, positionMap, (parts, nf) -> new EqualLengthListPartitioner().split(nf, parts.size()));
  }

  public MapBasedListPartitioner(Function keyFunction, Map positionMap,
                                 BiFunction>, List, List>> notFoundFunction) {
    super(
      t -> {
        String key = keyFunction.apply(t);
        Integer value = positionMap.get(key);
        return Optional.ofNullable(value);
      },
      notFoundFunction);
    this.positionMap = positionMap;
  }

  @Override
  protected List addUsingPositionFunction(List source, List> result) {
    if (positionMap.isEmpty()) {
      return source;
    } else {
      return super.addUsingPositionFunction(source, result);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy