/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.duplications.block;
import com.google.common.collect.Lists;
import org.sonar.duplications.statement.Statement;
import java.util.Collections;
import java.util.List;
/**
* Creates blocks from statements, each block will contain specified number of statements (blockSize
) and 64-bits (8-bytes) hash value.
* Hash value computed using
* Rabin-Karp rolling hash :
*
* using* s[0]*31^(blockSize-1) + s[1]*31^(blockSize-2) + ... + s[blockSize-1] *
long
arithmetic, where s[i]
* is the hash code of String
(which is cached) for statement with number i.
* Thus running time - O(N), where N - number of statements.
* Implementation fully thread-safe.
*/
public class BlockChunker {
private static final long PRIME_BASE = 31;
private final int blockSize;
private final long power;
public BlockChunker(int blockSize) {
this.blockSize = blockSize;
long pow = 1;
for (int i = 0; i < blockSize - 1; i++) {
pow = pow * PRIME_BASE;
}
this.power = pow;
}
public List