Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
UnifiedDiffBlock | 34 | 23 | 0% | 9 | 0 |
1 | /* | |
2 | * See the NOTICE file distributed with this work for additional | |
3 | * information regarding copyright ownership. | |
4 | * | |
5 | * This is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU Lesser General Public License as | |
7 | * published by the Free Software Foundation; either version 2.1 of | |
8 | * the License, or (at your option) any later version. | |
9 | * | |
10 | * This software is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this software; if not, write to the Free | |
17 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
18 | * 02110-1301 USA, or see the FSF site: http://www.fsf.org. | |
19 | */ | |
20 | package org.xwiki.diff.display; | |
21 | ||
22 | import java.util.ArrayList; | |
23 | ||
24 | /** | |
25 | * Represents a list of {@link UnifiedDiffElement}s that share the same context. The context is defined based on the | |
26 | * distance between changes. Changes that are close to each other are grouped in a single block. A block can contain | |
27 | * both added and removed elements. Blocks also contain unmodified elements that put changes in context. | |
28 | * | |
29 | * @param <E> the type of elements that are compared to produce the first level diff | |
30 | * @param <F> the type of sub-element that are compared to produce the second level diff | |
31 | * @version $Id: 0f7fdf1cab8e970df67f1f66cdd9647c50104aec $ | |
32 | * @since 4.1RC1 | |
33 | */ | |
34 | public class UnifiedDiffBlock<E, F> extends ArrayList<UnifiedDiffElement<E, F>> | |
35 | { | |
36 | /** | |
37 | * Needed for serialization. | |
38 | */ | |
39 | private static final long serialVersionUID = 1L; | |
40 | ||
41 | /** | |
42 | * @return the index where this block starts in the previous version; since blocks are most of the time not empty, | |
43 | * the returned value is the index of the first unmodified or removed element in this group | |
44 | */ | |
45 | 21 | public int getPreviousStart() |
46 | { | |
47 | 21 | for (UnifiedDiffElement<E, F> element : this) { |
48 | 23 | if (!element.isAdded()) { |
49 | 20 | return element.getIndex(); |
50 | } | |
51 | } | |
52 | 1 | return 0; |
53 | } | |
54 | ||
55 | /** | |
56 | * @return the size of this block (number of elements) in the previous version; unmodified elements and elements | |
57 | * marked as removed are counted only | |
58 | */ | |
59 | 21 | public int getPreviousSize() |
60 | { | |
61 | 21 | int size = 0; |
62 | 21 | for (UnifiedDiffElement<E, F> element : this) { |
63 | 104 | if (!element.isAdded()) { |
64 | 77 | size++; |
65 | } | |
66 | } | |
67 | 21 | return size; |
68 | } | |
69 | ||
70 | /** | |
71 | * @return the index where this block starts in the next version; since blocks are most of the time not empty, the | |
72 | * returned value is the index of the first unmodified or added element in this group | |
73 | */ | |
74 | 21 | public int getNextStart() |
75 | { | |
76 | 21 | for (UnifiedDiffElement<E, F> element : this) { |
77 | 29 | if (!element.isDeleted()) { |
78 | 20 | return element.getIndex(); |
79 | } | |
80 | } | |
81 | 1 | return 0; |
82 | } | |
83 | ||
84 | /** | |
85 | * @return the size of this block (number of elements) in the next version; unmodified elements and elements marked | |
86 | * as added are counted only | |
87 | */ | |
88 | 21 | public int getNextSize() |
89 | { | |
90 | 21 | int size = 0; |
91 | 21 | for (UnifiedDiffElement<E, F> element : this) { |
92 | 104 | if (!element.isDeleted()) { |
93 | 79 | size++; |
94 | } | |
95 | } | |
96 | 21 | return size; |
97 | } | |
98 | ||
99 | 8 | @Override |
100 | public String toString() | |
101 | { | |
102 | 8 | StringBuilder stringBuilder = new StringBuilder(); |
103 | // The element index is 0-based so we add 1 for display. | |
104 | 8 | stringBuilder.append(String.format("@@ -%s,%s +%s,%s @@\n", getPreviousStart() + 1, getPreviousSize(), |
105 | getNextStart() + 1, getNextSize())); | |
106 | 8 | for (UnifiedDiffElement<E, F> element : this) { |
107 | 49 | stringBuilder.append(element); |
108 | } | |
109 | 8 | return stringBuilder.toString(); |
110 | } | |
111 | } |