private void addDuplicationInstances()

in codeanalyzer/src/main/java/nl/obren/sokrates/sourcecode/duplication/impl/Blocks.java [139:202]


    private void addDuplicationInstances(FileInfoForDuplication fileInfoForDuplication1,
                                         FileInfoForDuplication fileInfoForDuplication2,
                                         final int blockSize) {
        final List<Block> blocks = fileInfoForDuplication1.extractBlocks(blockSize);
        blocks.forEach(block1 -> {
            block1.extractAllPossibleSubBlocks(blockSize).forEach(subBlock1 -> {
                DuplicationInstance instance = duplicationInstances.get(subBlock1.getStringKey());
                if (instance == null) {
                    instance = new DuplicationInstance();
                    instance.setBlockSize(blockSize);
                }

                Integer cleanedStartLine1 = fileInfoForDuplication1.indexesOf(subBlock1).get(0);
                addFileToDuplicationInstance(instance, fileInfoForDuplication1.getSourceFile(), cleanedStartLine1 + 1, blockSize);

                final DuplicationInstance currentInstance = instance;
                List<Integer> foundBlockIDs = fileInfoForDuplication2.indexesOf(subBlock1);
                if (foundBlockIDs.size() > 0) {
                    Integer cleanedStartLine2 = foundBlockIDs.get(0);

                    DuplicateRange range1 = new DuplicateRange(cleanedStartLine1, cleanedStartLine1 + blockSize - 1);
                    DuplicateRange range2 = new DuplicateRange(cleanedStartLine2, cleanedStartLine2 + blockSize - 1);

                    DuplicateRangePair pair1 = new DuplicateRangePair(range1, range2);
                    DuplicateRangePair pair2 = new DuplicateRangePair(range2, range1);

                    File file1 = fileInfoForDuplication1.getSourceFile().getFile();
                    File file2 = fileInfoForDuplication2.getSourceFile().getFile();
                    String key1 = file1.getPath() + "::" + file2.getPath();
                    String key2 = file2.getPath() + "::" + file1.getPath();

                    boolean alreadyIncluded = false;
                    DuplicateRangePairs duplicateRangePairs1 = fileRangePairs.get(key1);
                    DuplicateRangePairs duplicateRangePairs2 = fileRangePairs.get(key2);

                    if (duplicateRangePairs1 != null && duplicateRangePairs1.includes(pair1)) {
                        alreadyIncluded = true;
                    }

                    if (duplicateRangePairs2 != null && duplicateRangePairs2.includes(pair2)) {
                        alreadyIncluded = true;
                    }

                    if (!alreadyIncluded) {
                        duplicationInstances.put(subBlock1.getStringKey(), currentInstance);
                        addFileToDuplicationInstance(currentInstance, fileInfoForDuplication2.getSourceFile(), cleanedStartLine2 + 1, blockSize);

                        if (duplicateRangePairs1 == null) {
                            duplicateRangePairs1 = new DuplicateRangePairs();
                            fileRangePairs.put(key1, duplicateRangePairs1);
                        }
                        duplicateRangePairs1.getRanges().add(pair1);

                        if (duplicateRangePairs2 == null) {
                            duplicateRangePairs2 = new DuplicateRangePairs();
                            fileRangePairs.put(key2, duplicateRangePairs2);
                        }
                        duplicateRangePairs2.getRanges().add(pair2);
                    }
                    // fileInfoForDuplication2.clearSubBlock(subBlock1);
                }
            });
        });
    }