package nl.hsac.fitnesse.testrun;

import fitnesse.util.partitioner.EqualLengthListPartitioner;
import fitnesse.util.partitioner.ListPartitioner;
import fitnesse.util.partitioner.MapBasedListPartitioner;
import fitnesse.wiki.WikiPage;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Partitions pages based on provided DurationRecords.
 */
public class DurationBasedWikiPagePartitioner implements ListPartitioner<WikiPage> {
  private List<DurationRecord<String>> durationRecords;

  public DurationBasedWikiPagePartitioner(List<DurationRecord<String>> durationRecords) {
    if (durationRecords != null) {
      setDurationRecords(durationRecords);
    }
  }

  public void setDurationRecords(List<DurationRecord<String>> durationRecords) {
    this.durationRecords = durationRecords;
  }

  @Override
  public List<List<WikiPage>> split(List<WikiPage> pages, int partitionCount) {
    List<String> names = pages.stream().map(this::getName).collect(Collectors.toList());
    Map<String, Integer> positionMap = new PositionFinder<String>().getPositionMap(names, durationRecords, partitionCount);
    ListPartitioner<WikiPage> partitioner = createPartitioner(positionMap);
    return partitioner.split(pages, partitionCount);
  }

  protected ListPartitioner<WikiPage> createPartitioner(Map<String, Integer> positionMap) {
    return new MapBasedListPartitioner<>(this::getName, positionMap, this::handleUnknownPages);
  }

  protected String getName(WikiPage wikiPage) {
    return wikiPage.getFullPath().toString();
  }

  protected List<List<WikiPage>> handleUnknownPages(
    List<List<WikiPage>> partitionsFromFile,
    List<WikiPage> pagesNotPresent) {
    return new EqualLengthListPartitioner<WikiPage>().split(pagesNotPresent, partitionsFromFile.size());
  }
}

@@ -48,144 +48,39 @@
 sse.
-util.partitioner.EqualLengthListPartitioner;%0Aimport fitnesse.util.partitioner.ListPartitioner;%0Aimport fitnesse.util.part
+testrunner.run.Pos
 ition
-er.
 MapBased
 List
@@ -67,36 +67,40 @@
 PositionMapBased
-List
+WikiPage
 Partitioner;%0Aimp
@@ -323,44 +323,51 @@
 ner 
-implements ListPartitioner%3CWikiPage%3E
+extends PositionMapBasedWikiPagePartitioner
  %7B%0A 
@@ -607,421 +607,160 @@
 %0A%0A  
-public void setDurationRecords(List%3CDurationRecord%3CString%3E%3E durationRecords) %7B%0A    this.durationRecords = durationRecords;%0A  %7D%0A%0A  @Override%0A  public List%3CList%3CWikiPage%3E%3E split(List%3CWikiPage%3E pages, int partitionCount) %7B%0A    List%3CString%3E names = pages.stream().map(this::getName).collect(Collectors.toList());%0A    Map%3CString, Integer%3E positionMap = new PositionFinder%3CString%3E().getPositionMap(names, durationRecord
+@Override%0A  public List%3CList%3CWikiPage%3E%3E split(List%3CWikiPage%3E pages, int partitionCount) %7B%0A    Map%3CString, Integer%3E positionMap = createPartitionMap(page
 s, p
@@ -779,19 +779,18 @@
 t);%0A    
-Li
 s
+e
 tPartiti
@@ -795,52 +795,11 @@
 tion
-er%3CWikiPage%3E partitioner = createPartitioner
+Map
 (pos
@@ -820,25 +820,19 @@
  return 
-partition
+sup
 er.split
@@ -873,20 +873,43 @@
 otected 
-List
+Map%3CString, Integer%3E create
 Partitio
@@ -901,34 +901,40 @@
  createPartition
-er
+Map(List
 %3CWikiPage%3E creat
@@ -932,155 +932,176 @@
 ge%3E 
-createPartitioner(Map%3CString, Integer%3E positionMap) %7B%0A    return new MapBasedListPartitioner%3C%3E(this::getName, positionMap, this::handleUnknownPages
+pages, int partitionCount) %7B%0A    List%3CString%3E names = getFullPaths(pages);%0A    return new PositionFinder%3CString%3E().getPositionMap(names, durationRecords, partitionCount
 );%0A 
@@ -1120,31 +1120,47 @@
 ted 
+List%3C
 String
+%3E
  get
-Name(
+FullPaths(List%3C
 WikiPage
  wik
@@ -1155,25 +1155,23 @@
 WikiPage
- wikiP
+%3E p
 age
+s
 ) %7B%0A    
@@ -1181,17 +1181,33 @@
 urn 
-wikiPage.
+pages.stream().map(this::
 getF
@@ -1217,267 +1217,262 @@
 Path
-(
 ).
-toString();%0A  %7D%0A%0A  protected List%3CList%3CWikiPage%3E%3E handleUnknownPages(%0A    List%3CList%3CWikiPage%3E%3E partitionsFromFile,%0A    List%3CWikiPage%3E pagesNotPresent) %7B%0A    return new EqualLengthListPartitioner%3CWikiPage%3E().split(pagesNotPresent, partitionsFromFile.size())
+collect(Collectors.toList());%0A  %7D%0A%0A  public void setDurationRecords(List%3CDurationRecord%3CString%3E%3E durationRecords) %7B%0A    this.durationRecords = durationRecords;%0A  %7D%0A%0A  public List%3CDurationRecord%3CString%3E%3E getDurationRecords() %7B%0A    return durationRecords
 ;%0A  

Use PositionMapBasedWikiPagePartitioner