001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.util.ArrayList;
018    import java.util.Iterator;
019    import java.util.List;
020    
021    /**
022     * <p>
023     * Represents a change between one or several lines. <code>changeType</code>
024     * tells if the change happened in source or target. <code>lineNumber</code>
025     * holds the line number of the first modified line. This line number refers to
026     * a line in source or target, depending on the <code>changeType</code> value.
027     * <code>changedLines</code> is a list of strings, each string is a line that is
028     * already highlighted, indicating where the changes are.
029     * </p>
030     *
031     * @author Bruno Farache
032     */
033    public class DiffResult {
034    
035            public static final String SOURCE = "SOURCE";
036    
037            public static final String TARGET = "TARGET";
038    
039            public DiffResult(int linePos, List<String> changedLines) {
040                    _lineNumber = linePos + 1;
041                    _changedLines = changedLines;
042            }
043    
044            public DiffResult(int linePos, String changedLine) {
045                    _lineNumber = linePos + 1;
046                    _changedLines = new ArrayList<String>();
047                    _changedLines.add(changedLine);
048            }
049    
050            @Override
051            public boolean equals(Object obj) {
052                    DiffResult diffResult = (DiffResult)obj;
053    
054                    if ((diffResult.getLineNumber() == _lineNumber) &&
055                            diffResult.getChangedLines().equals(_changedLines)) {
056    
057                            return true;
058                    }
059    
060                    return false;
061            }
062    
063            public List<String> getChangedLines() {
064                    return _changedLines;
065            }
066    
067            public int getLineNumber() {
068                    return _lineNumber;
069            }
070    
071            @Override
072            public int hashCode() {
073                    HashCode hashCode = HashCodeFactoryUtil.getHashCode();
074    
075                    hashCode.append(_lineNumber);
076                    hashCode.append(_changedLines);
077    
078                    return hashCode.toHashCode();
079            }
080    
081            public void setChangedLines(List<String> changedLines) {
082                    _changedLines = changedLines;
083            }
084    
085            public void setLineNumber(int lineNumber) {
086                    _lineNumber = lineNumber;
087            }
088    
089            @Override
090            public String toString() {
091                    StringBundler sb = new StringBundler(2 * _changedLines.size() + 2);
092    
093                    sb.append("Line: ");
094                    sb.append(_lineNumber);
095                    sb.append("\n");
096    
097                    Iterator<String> itr = _changedLines.iterator();
098    
099                    while (itr.hasNext()) {
100                            sb.append(itr.next());
101    
102                            if (itr.hasNext()) {
103                                    sb.append("\n");
104                            }
105                    }
106    
107                    return sb.toString();
108            }
109    
110            private List<String> _changedLines;
111            private int _lineNumber;
112    
113    }