AppliedTextPatch (@NotNull List<? extends AppliedSplitPatchHunk> splitPatchHunkList) { List<AppliedSplitPatchHunk> hunks = new ArrayList<>(splitPatchHunkList); // ensure, that `appliedTo` ranges do not overlap BitSet appliedLines = new BitSet(); for (int i = 0; i < hunks.size(); i++) { AppliedSplitPatchHunk hunk = hunks.get(i); LineRange appliedTo = hunk.getAppliedTo(); if (appliedTo == null) continue; int nextAppliedLine = appliedLines.nextSetBit(appliedTo.start); if (nextAppliedLine != -1 && nextAppliedLine < appliedTo.end) { hunks.set(i, new AppliedSplitPatchHunk(hunk, -1, -1, NOT_APPLIED)); } else { appliedLines.set(appliedTo.start, appliedTo.end, true); } } ContainerUtil.sort(hunks, Comparator.comparingInt(o -> o.getLineRangeBefore().start)); return new AppliedTextPatch(hunks); }