Skip to content

Instantly share code, notes, and snippets.

@tkstone
Created May 31, 2019 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkstone/3375632bdae0ec092fb4d4fc7503d389 to your computer and use it in GitHub Desktop.
Save tkstone/3375632bdae0ec092fb4d4fc7503d389 to your computer and use it in GitHub Desktop.
import java.util.HashMap;
import java.util.Map;
import org.springframework.batch.core.partition.support.Partitioner;
import org.springframework.batch.item.ExecutionContext;
public class DayOfWeekPartitioner implements Partitioner {
@Override
public Map<String, ExecutionContext> partition(int gridSize) {
if(gridSize >= 7) {
gridSize = 7;
}
int [] slots = calcSlotSize(gridSize);
int startIdx = 1;
Map<String, ExecutionContext> partitionMap = new HashMap<String, ExecutionContext>();
for(int i = 0; i < slots.length; i++) {
ExecutionContext context = new ExecutionContext();
context.put("StartDay", startIdx);
context.put("EndDay", startIdx + slots[i] - 1);
partitionMap.put("Partition-" + i, context);
startIdx += slots[i];
}
return partitionMap;
}
private int [] calcSlotSize(int gridSize) {
int [] slots = new int[gridSize];
int totalSlot = 7;
for(int i = 0; i < slots.length; i++) {
slots[i] = 0;
}
int idx = 0;
while(true) {
if(totalSlot == 0) break;
slots[idx % slots.length]++;
idx++;
totalSlot--;
}
return slots;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment