123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- /*
- * SonarQube
- * Copyright (C) 2009-2021 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.detector.original;
-
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- import javax.annotation.CheckForNull;
- import org.sonar.duplications.block.Block;
- import org.sonar.duplications.utils.FastStringComparator;
-
- /**
- * Set of {@link Block}s, which internally stored as a sorted list.
- */
- final class BlocksGroup {
- final List<Block> blocks;
-
- /**
- * Factory method.
- *
- * @return new empty group
- */
- public static BlocksGroup empty() {
- return new BlocksGroup();
- }
-
- private BlocksGroup() {
- this.blocks = new ArrayList<>();
- }
-
- public int size() {
- return blocks.size();
- }
-
- /**
- * @return true, if this group subsumed by specified group
- * @see #subsumedBy(BlocksGroup, BlocksGroup, int)
- */
- public boolean subsumedBy(BlocksGroup other, int indexCorrection) {
- return subsumedBy(this, other, indexCorrection);
- }
-
- /**
- * @return intersection of this group with specified
- * @see #intersect(BlocksGroup, BlocksGroup)
- */
- public BlocksGroup intersect(BlocksGroup other) {
- return intersect(this, other);
- }
-
- public List<Block[]> pairs(BlocksGroup other, int len) {
- return pairs(this, other, len);
- }
-
- /**
- * First block from this group with specified resource id.
- */
- @CheckForNull
- public Block first(String resourceId) {
- for (Block block : blocks) {
- if (resourceId.equals(block.getResourceId())) {
- return block;
- }
- }
- return null;
- }
-
- @Override
- public String toString() {
- return blocks.toString();
- }
-
- /**
- * Intersection of two groups is a group, which contains blocks from second group that have corresponding block from first group
- * with same resource id and with corrected index.
- */
- private static BlocksGroup intersect(BlocksGroup group1, BlocksGroup group2) {
- BlocksGroup intersection = new BlocksGroup();
- List<Block> list1 = group1.blocks;
- List<Block> list2 = group2.blocks;
- int i = 0;
- int j = 0;
- while (i < list1.size() && j < list2.size()) {
- Block block1 = list1.get(i);
- Block block2 = list2.get(j);
- int c = RESOURCE_ID_COMPARATOR.compare(block1.getResourceId(), block2.getResourceId());
- if (c > 0) {
- j++;
- continue;
- }
- if (c < 0) {
- i++;
- continue;
- }
- if (c == 0) {
- c = block1.getIndexInFile() + 1 - block2.getIndexInFile();
- }
- if (c == 0) {
- // list1[i] == list2[j]
- i++;
- j++;
- intersection.blocks.add(block2);
- }
- if (c > 0) {
- // list1[i] > list2[j]
- j++;
- }
- if (c < 0) {
- // list1[i] < list2[j]
- i++;
- }
- }
- return intersection;
- }
-
- /**
- * One group is subsumed by another group, when each block from first group has corresponding block from second group
- * with same resource id and with corrected index.
- */
- private static boolean subsumedBy(BlocksGroup group1, BlocksGroup group2, int indexCorrection) {
- List<Block> list1 = group1.blocks;
- List<Block> list2 = group2.blocks;
- int i = 0;
- int j = 0;
- while (i < list1.size() && j < list2.size()) {
- Block block1 = list1.get(i);
- Block block2 = list2.get(j);
- int c = RESOURCE_ID_COMPARATOR.compare(block1.getResourceId(), block2.getResourceId());
- if (c != 0) {
- j++;
- continue;
- }
- c = block1.getIndexInFile() - indexCorrection - block2.getIndexInFile();
- if (c < 0) {
- // list1[i] < list2[j]
- break;
- }
- if (c != 0) {
- // list1[i] != list2[j]
- j++;
- }
- if (c == 0) {
- // list1[i] == list2[j]
- i++;
- j++;
- }
- }
- return i == list1.size();
- }
-
- private static List<Block[]> pairs(BlocksGroup beginGroup, BlocksGroup endGroup, int len) {
- List<Block[]> result = new ArrayList<>();
- List<Block> beginBlocks = beginGroup.blocks;
- List<Block> endBlocks = endGroup.blocks;
- int i = 0;
- int j = 0;
- while (i < beginBlocks.size() && j < endBlocks.size()) {
- Block beginBlock = beginBlocks.get(i);
- Block endBlock = endBlocks.get(j);
- int c = RESOURCE_ID_COMPARATOR.compare(beginBlock.getResourceId(), endBlock.getResourceId());
- if (c == 0) {
- c = beginBlock.getIndexInFile() + len - 1 - endBlock.getIndexInFile();
- }
- if (c == 0) {
- result.add(new Block[] {beginBlock, endBlock});
- i++;
- j++;
- }
- if (c > 0) {
- j++;
- }
- if (c < 0) {
- i++;
- }
- }
- return result;
- }
-
- private static final Comparator<String> RESOURCE_ID_COMPARATOR = FastStringComparator.INSTANCE;
-
- /**
- * Compares {@link Block}s first using {@link Block#getResourceId() resource id} and then using {@link Block#getIndexInFile() index in file}.
- */
- public static class BlockComparator implements Comparator<Block> {
-
- public static final BlockComparator INSTANCE = new BlockComparator();
-
- @Override
- public int compare(Block b1, Block b2) {
- int c = RESOURCE_ID_COMPARATOR.compare(b1.getResourceId(), b2.getResourceId());
- if (c == 0) {
- return b1.getIndexInFile() - b2.getIndexInFile();
- }
- return c;
- }
-
- }
-
- }
|