1 /* 2 * Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com> 3 * and other copyright owners as documented in the project's IP log. 4 * 5 * This program and the accompanying materials are made available 6 * under the terms of the Eclipse Distribution License v1.0 which 7 * accompanies this distribution, is reproduced below, and is 8 * available at http://www.eclipse.org/org/documents/edl-v10.php 9 * 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or 13 * without modification, are permitted provided that the following 14 * conditions are met: 15 * 16 * - Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * - Redistributions in binary form must reproduce the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer in the documentation and/or other materials provided 22 * with the distribution. 23 * 24 * - Neither the name of the Eclipse Foundation, Inc. nor the 25 * names of its contributors may be used to endorse or promote 26 * products derived from this software without specific prior 27 * written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 42 */ 43 44 package org.eclipse.jgit.merge; 45 46 import java.util.Iterator; 47 import java.util.List; 48 49 import org.eclipse.jgit.diff.Sequence; 50 import org.eclipse.jgit.merge.MergeChunk.ConflictState; 51 import org.eclipse.jgit.util.IntList; 52 53 /** 54 * The result of merging a number of {@link org.eclipse.jgit.diff.Sequence} 55 * objects. These sequences have one common predecessor sequence. The result of 56 * a merge is a list of MergeChunks. Each MergeChunk contains either a range (a 57 * subsequence) from one of the merged sequences, a range from the common 58 * predecessor or a conflicting range from one of the merged sequences. A 59 * conflict will be reported as multiple chunks, one for each conflicting range. 60 * The first chunk for a conflict is marked specially to distinguish the border 61 * between two consecutive conflicts. 62 * <p> 63 * This class does not know anything about how to present the merge result to 64 * the end-user. MergeFormatters have to be used to construct something human 65 * readable. 66 * 67 * @param <S> 68 * type of sequence. 69 */ 70 public class MergeResult<S extends Sequence> implements Iterable<MergeChunk> { 71 private final List<S> sequences; 72 73 final IntList chunks = new IntList(); 74 75 private boolean containsConflicts = false; 76 77 /** 78 * Creates a new empty MergeResult 79 * 80 * @param sequences 81 * contains the common predecessor sequence at position 0 82 * followed by the merged sequences. This list should not be 83 * modified anymore during the lifetime of this 84 * {@link org.eclipse.jgit.merge.MergeResult}. 85 */ 86 public MergeResult(List<S> sequences) { 87 this.sequences = sequences; 88 } 89 90 /** 91 * Adds a new range from one of the merged sequences or from the common 92 * predecessor. This method can add conflicting and non-conflicting ranges 93 * controlled by the conflictState parameter 94 * 95 * @param srcIdx 96 * determines from which sequence this range comes. An index of 97 * x specifies the x+1 element in the list of sequences 98 * specified to the constructor 99 * @param begin 100 * the first element from the specified sequence which should be 101 * included in the merge result. Indexes start with 0. 102 * @param end 103 * specifies the end of the range to be added. The element this 104 * index points to is the first element which not added to the 105 * merge result. All elements between begin (including begin) and 106 * this element are added. 107 * @param conflictState 108 * when set to NO_CONLICT a non-conflicting range is added. 109 * This will end implicitly all open conflicts added before. 110 */ 111 public void add(int srcIdx, int begin, int end, ConflictState conflictState) { 112 chunks.add(conflictState.ordinal()); 113 chunks.add(srcIdx); 114 chunks.add(begin); 115 chunks.add(end); 116 if (conflictState != ConflictState.NO_CONFLICT) 117 containsConflicts = true; 118 } 119 120 /** 121 * Returns the common predecessor sequence and the merged sequence in one 122 * list. The common predecessor is is the first element in the list 123 * 124 * @return the common predecessor at position 0 followed by the merged 125 * sequences. 126 */ 127 public List<S> getSequences() { 128 return sequences; 129 } 130 131 static final ConflictState[] states = ConflictState.values(); 132 133 /** {@inheritDoc} */ 134 @Override 135 public Iterator<MergeChunk> iterator() { 136 return new Iterator<MergeChunk>() { 137 int idx; 138 139 @Override 140 public boolean hasNext() { 141 return (idx < chunks.size()); 142 } 143 144 @Override 145 public MergeChunk next() { 146 ConflictState state = states[chunks.get(idx++)]; 147 int srcIdx = chunks.get(idx++); 148 int begin = chunks.get(idx++); 149 int end = chunks.get(idx++); 150 return new MergeChunk(srcIdx, begin, end, state); 151 } 152 153 @Override 154 public void remove() { 155 throw new UnsupportedOperationException(); 156 } 157 }; 158 } 159 160 /** 161 * Whether this merge result contains conflicts 162 * 163 * @return true if this merge result contains conflicts 164 */ 165 public boolean containsConflicts() { 166 return containsConflicts; 167 } 168 169 /** 170 * Sets explicitly whether this merge should be seen as containing a 171 * conflict or not. Needed because during RecursiveMerger we want to do 172 * content-merges and take the resulting content (even with conflict 173 * markers!) as new conflict-free content 174 * 175 * @param containsConflicts 176 * whether this merge should be seen as containing a conflict or 177 * not. 178 * @since 3.5 179 */ 180 protected void setContainsConflicts(boolean containsConflicts) { 181 this.containsConflicts = containsConflicts; 182 } 183 }