Created
May 31, 2019 00:43
-
-
Save tkstone/3375632bdae0ec092fb4d4fc7503d389 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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