| Class | Line # | Actions | |||||
|---|---|---|---|---|---|---|---|
| UnifiedDiffElement | 35 | 11 | 0% | 9 | 0 | ||
| UnifiedDiffElement.Type | 40 | 2 | 0% | 2 | 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.List; | |
| 23 | ||
| 24 | /** | |
| 25 | * Wraps the elements that are compared to produce a diff, holding information like their index and type of change | |
| 26 | * (added, removed, unmodified) to simplify the process of displaying them in an unified diff. If the wrapped element is | |
| 27 | * a composite element (can be split in sub-elements) and was modified (replaced by another element) then this class can | |
| 28 | * also store information about changes at the level of sub-elements. | |
| 29 | * | |
| 30 | * @param <E> the type of elements that are compared to produce the first-level diff | |
| 31 | * @param <F> the type of sub-elements that are compared to produce the second-level diff | |
| 32 | * @version $Id: f5c1862cc802133dcea13747efebe6b796910d30 $ | |
| 33 | * @since 4.1RC1 | |
| 34 | */ | |
| 35 | public class UnifiedDiffElement<E, F> | |
| 36 | { | |
| 37 | /** | |
| 38 | * The possible types of elements you can find within a unified diff. | |
| 39 | */ | |
| 40 | public enum Type | |
| 41 | { | |
| 42 | /** An element that was added. */ | |
| 43 | ADDED('+'), | |
| 44 | ||
| 45 | /** An element that was removed. */ | |
| 46 | DELETED('-'), | |
| 47 | ||
| 48 | /** An element that was left unmodified. This type of elements show the context where a change was made. */ | |
| 49 | CONTEXT(' '); | |
| 50 | ||
| 51 | /** | |
| 52 | * The symbol associated with this element type. This is need only for the default string serialization. | |
| 53 | * | |
| 54 | * @see UnifiedDiffElement#toString() | |
| 55 | */ | |
| 56 | private final char symbol; | |
| 57 | ||
| 58 | /** | |
| 59 | * Creates a new element type that has the given symbol associated. | |
| 60 | * | |
| 61 | * @param symbol the symbol associated with this element type | |
| 62 | */ | |
| 63 | 6 | Type(char symbol) |
| 64 | { | |
| 65 | 6 | this.symbol = symbol; |
| 66 | } | |
| 67 | ||
| 68 | /** | |
| 69 | * @return the symbol associated with this element type | |
| 70 | */ | |
| 71 | 122 | public char getSymbol() |
| 72 | { | |
| 73 | 122 | return this.symbol; |
| 74 | } | |
| 75 | } | |
| 76 | ||
| 77 | /** | |
| 78 | * The element index. | |
| 79 | */ | |
| 80 | private final int index; | |
| 81 | ||
| 82 | /** | |
| 83 | * The element type. | |
| 84 | */ | |
| 85 | private final Type type; | |
| 86 | ||
| 87 | /** | |
| 88 | * The wrapped element. | |
| 89 | */ | |
| 90 | private final E value; | |
| 91 | ||
| 92 | /** | |
| 93 | * The list of chunks of sub-elements that form this element. | |
| 94 | */ | |
| 95 | private List<InlineDiffChunk<F>> chunks; | |
| 96 | ||
| 97 | /** | |
| 98 | * Creates a new element in a unified diff. | |
| 99 | * | |
| 100 | * @param index the element index | |
| 101 | * @param type the element type | |
| 102 | * @param value the wrapped element | |
| 103 | */ | |
| 104 | 104 | public UnifiedDiffElement(int index, Type type, E value) |
| 105 | { | |
| 106 | 104 | this.index = index; |
| 107 | 104 | this.type = type; |
| 108 | 104 | this.value = value; |
| 109 | } | |
| 110 | ||
| 111 | /** | |
| 112 | * @return the element index | |
| 113 | */ | |
| 114 | 68 | public int getIndex() |
| 115 | { | |
| 116 | 68 | return this.index; |
| 117 | } | |
| 118 | ||
| 119 | /** | |
| 120 | * @return the element type | |
| 121 | */ | |
| 122 | 84 | public Type getType() |
| 123 | { | |
| 124 | 84 | return this.type; |
| 125 | } | |
| 126 | ||
| 127 | /** | |
| 128 | * @return the wrapped element | |
| 129 | */ | |
| 130 | 38 | public E getValue() |
| 131 | { | |
| 132 | 38 | return this.value; |
| 133 | } | |
| 134 | ||
| 135 | /** | |
| 136 | * @return the list of chunks of sub-elements that form this element | |
| 137 | */ | |
| 138 | 160 | public List<InlineDiffChunk<F>> getChunks() |
| 139 | { | |
| 140 | 160 | return this.chunks; |
| 141 | } | |
| 142 | ||
| 143 | /** | |
| 144 | * Sets the list of chunks of sub-elements that form this element. | |
| 145 | * | |
| 146 | * @param chunks the list of chunks | |
| 147 | */ | |
| 148 | 26 | public void setChunks(List<InlineDiffChunk<F>> chunks) |
| 149 | { | |
| 150 | 26 | this.chunks = chunks; |
| 151 | } | |
| 152 | ||
| 153 | /** | |
| 154 | * @return {@code true} if this element was added, {@code false} otherwise | |
| 155 | */ | |
| 156 | 155 | public boolean isAdded() |
| 157 | { | |
| 158 | 155 | return this.type == Type.ADDED; |
| 159 | } | |
| 160 | ||
| 161 | /** | |
| 162 | * @return {@code true} if this element was deleted, {@code false} otherwise | |
| 163 | */ | |
| 164 | 161 | public boolean isDeleted() |
| 165 | { | |
| 166 | 161 | return this.type == Type.DELETED; |
| 167 | } | |
| 168 | ||
| 169 | 66 | @Override |
| 170 | public String toString() | |
| 171 | { | |
| 172 | 66 | return this.type.getSymbol() + String.valueOf(this.value) + '\n'; |
| 173 | } | |
| 174 | } |