Skip to content

Method: toString()

1: package migration.difffile;
2:
3: import java.util.ArrayList;
4: import java.util.List;
5:
6: /**
7: * Diff-Part-File (Hunk: Zeilenblock, der in einer Diff-File den alten Code mit dem neuen Code
8: * vergleicht).
9: *
10: * @author admin
11: *
12: */
13: public class DiffPartFile {
14:
15:         /**
16:          * Zeilen einer Diff-Part-File.
17:          */
18:         private List<DiffPartFileLine> lines;
19:
20:         /**
21:          * Konstruktor.
22:          *
23:          * @param lines
24:          * the lines to set in the diff part.
25:          */
26:         public DiffPartFile(final List<DiffPartFileLine> lines) {
27:                 super();
28:                 this.lines = lines;
29:         }
30:
31:         /**
32:          * getter für die Zeilen in einem Diff-Part-File.
33:          *
34:          * @return lines
35:          */
36:         public List<DiffPartFileLine> getLines() {
37:                 return lines;
38:         }
39:
40:         /**
41:          * setter für die Zeilen.
42:          *
43:          * @param lines
44:          * the lines to set in the diff part.
45:          *
46:          */
47:         public void setLines(final List<DiffPartFileLine> lines) {
48:                 this.lines = lines;
49:         }
50:
51:         @Override
52:         public String toString() {
53:                 return "DiffPartFile [lines=\n" + lines + "]";
54:         }
55:
56:         @Override
57:         public int hashCode() {
58:                 final int prime = 31;
59:                 int result = 1;
60:                 result = prime * result + ((lines == null)
61:                                 ? 0
62:                                 : lines.hashCode());
63:                 return result;
64:         }
65:
66:         @Override
67:         public boolean equals(final Object obj) {
68:                 if (this == obj) {
69:                         return true;
70:                 }
71:                 if (obj == null) {
72:                         return false;
73:                 }
74:                 if (getClass() != obj.getClass()) {
75:                         return false;
76:                 }
77:                 final DiffPartFile other = (DiffPartFile) obj;
78:                 if (lines == null) {
79:                         if (other.lines != null) {
80:                                 return false;
81:                         }
82:                 } else if (!lines.equals(other.lines)) {
83:                         return false;
84:                 }
85:                 return true;
86:         }
87:
88:         /**
89:          * returns true if and only if the lines are in the same context and a rename is unambiguous.
90:          * Requires a context around the add and remove line.
91:          *
92:          * @param removeLine
93:          * the remove line
94:          * @param addLine
95:          * the add line
96:          * @return true if and only if the lines are in the same context and a rename is unambiguous.
97:          */
98:         public boolean isRenameInTheSameContext(final DiffPartFileLine removeLine,
99:                         final DiffPartFileLine addLine) {
100:                 if (!removeLine.getFileEntity().hasSameContent(addLine.getFileEntity())) {
101:                         return false;
102:                 }
103:                 if (lines.contains(removeLine) && lines.contains(addLine)) {
104:                         final int indexOfRemoveLine = lines.indexOf(removeLine);
105:                         final int indexOfAddLine = lines.indexOf(addLine);
106:                         final int numberOfContextBeginn = getNumberOfContextBeginn(indexOfRemoveLine);
107:                         if (numberOfContextBeginn != getNumberOfContextBeginn(indexOfAddLine)) {
108:                                 return false;
109:                         }
110:                         final List<DiffPartFileLine> equalRemoveLines = new ArrayList<>();
111:                         final List<DiffPartFileLine> equalAddLines = new ArrayList<>();
112:                         for (int i = numberOfContextBeginn; i < lines.size(); i++) {
113:                                 final DiffPartFileLine currentLine = lines.get(i);
114:                                 if (!currentLine.getState().equals(new NoChangeLine())) {
115:                                         if (currentLine.hasSameStateAndContent(removeLine)) {
116:                                                 equalRemoveLines.add(currentLine);
117:                                                 continue;
118:                                         } else if (currentLine.hasSameStateAndContent(addLine)) {
119:                                                 equalAddLines.add(currentLine);
120:                                                 continue;
121:                                         }
122:                                 } else {
123:                                         return equalAddLines.size() == 1 && equalRemoveLines.size() == 1;
124:                                 }
125:                         }
126:                 }
127:                 return false;
128:         }
129:
130:         /**
131:          * @param indexOfLine
132:          * the line number of the line. Line must be a AddLine or RemoveLine. Must be
133:          * contained in the hunk.
134:          * @return the number where the context begins, that contains the line
135:          */
136:         private int getNumberOfContextBeginn(final int indexOfLine) {
137:                 for (int i = indexOfLine - 1; i > 0; i--) {
138:                         if (lines.get(i).getState().equals(new NoChangeLine())) {
139:                                 return i + 1;
140:                         }
141:                 }
142:                 throw new Error("Kein Kontextbeginn gefunden");
143:         }
144: }