Line data Source code
1 : // Copyright (C) 2009 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.prettify.common; 16 : 17 : import java.util.List; 18 : import org.eclipse.jgit.diff.Edit; 19 : 20 : /** 21 : * This is a legacy class. It was only simplified but not improved regarding readability or code 22 : * health. Feel free to completely rewrite it or replace it with some other, better code. 23 : */ 24 : public class EditHunk { 25 : private final List<Edit> edits; 26 : 27 : private int curIdx; 28 : private Edit curEdit; 29 : 30 : private int aCur; 31 : private int bCur; 32 : private final int aEnd; 33 : private final int bEnd; 34 : 35 14 : public EditHunk(List<Edit> edits, int aSize, int bSize) { 36 14 : this.edits = edits; 37 : 38 14 : curIdx = 0; 39 14 : curEdit = edits.get(curIdx); 40 : 41 14 : aCur = 0; 42 14 : bCur = 0; 43 14 : aEnd = aSize; 44 14 : bEnd = bSize; 45 14 : } 46 : 47 : public int getCurA() { 48 10 : return aCur; 49 : } 50 : 51 : public int getCurB() { 52 14 : return bCur; 53 : } 54 : 55 : public void incA() { 56 10 : aCur++; 57 10 : } 58 : 59 : public void incB() { 60 14 : bCur++; 61 14 : } 62 : 63 : public void incBoth() { 64 6 : incA(); 65 6 : incB(); 66 6 : } 67 : 68 : public boolean isUnmodifiedLine() { 69 14 : return !isDeletedA() && !isInsertedB(); 70 : } 71 : 72 : public boolean isDeletedA() { 73 14 : return curEdit.getBeginA() <= aCur && aCur < curEdit.getEndA(); 74 : } 75 : 76 : public boolean isInsertedB() { 77 14 : return curEdit.getBeginB() <= bCur && bCur < curEdit.getEndB(); 78 : } 79 : 80 : public boolean next() { 81 14 : if (!in(curEdit)) { 82 14 : if (curIdx < edits.size() - 1) { 83 3 : curEdit = edits.get(++curIdx); 84 : } 85 : } 86 14 : return aCur < aEnd || bCur < bEnd; 87 : } 88 : 89 : private boolean in(Edit edit) { 90 14 : return aCur < edit.getEndA() || bCur < edit.getEndB(); 91 : } 92 : }