public List<Block> chunk(String resourceId, List<Statement> statements) {
List<Statement> filtered = Lists.newArrayList();
int i = 0;
while (i < statements.size()) {
Statement first = statements.get(i);
int j = i + 1;
while (j < statements.size() && statements.get(j).getValue().equals(first.getValue())) {
j++;
}
filtered.add(statements.get(i));
if (i < j - 1) {
filtered.add(statements.get(j - 1));
}
i = j;
}
statements = filtered;
if (statements.size() < blockSize) {
return Collections.emptyList();
}
Statement[] statementsArr = statements.toArray(new Statement[statements.size()]);
List<Block> blocks = Lists.newArrayListWithCapacity(statementsArr.length - blockSize + 1);
long hash = 0;
int first = 0;
int last = 0;
for (; last < blockSize - 1; last++) {
hash = hash * PRIME_BASE + statementsArr[last].getValue().hashCode();
}
Block.Builder blockBuilder = Block.builder().setResourceId(resourceId);
for (; last < statementsArr.length; last++, first++) {
Statement firstStatement = statementsArr[first];
Statement lastStatement = statementsArr[last];
// add last statement to hash
hash = hash * PRIME_BASE + lastStatement.getValue().hashCode();
// create block
Block block = blockBuilder.setBlockHash(new ByteArray(hash))
.setIndexInFile(first)
.setLines(firstStatement.getStartLine(), lastStatement.getEndLine())
.build();
blocks.add(block);
// remove first statement from hash
hash -= power * firstStatement.getValue().hashCode();
}