001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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            public List<String> getChangedLines() {
051                    return _changedLines;
052            }
053    
054            public void setChangedLines(List<String> changedLines) {
055                    _changedLines = changedLines;
056            }
057    
058            public int getLineNumber() {
059                    return _lineNumber;
060            }
061    
062            public void setLineNumber(int lineNumber) {
063                    _lineNumber = lineNumber;
064            }
065    
066            public boolean equals(Object obj) {
067                    DiffResult diffResult = (DiffResult)obj;
068    
069                    if ((diffResult.getLineNumber() == _lineNumber) &&
070                            (diffResult.getChangedLines().equals(_changedLines))) {
071    
072                            return true;
073                    }
074    
075                    return false;
076            }
077    
078            public int hashCode() {
079                    HashCode hashCode = HashCodeFactoryUtil.getHashCode();
080    
081                    hashCode.append(_lineNumber);
082                    hashCode.append(_changedLines);
083    
084                    return hashCode.toHashCode();
085            }
086    
087            public String toString() {
088                    StringBundler sb = new StringBundler(2 * _changedLines.size() + 2);
089    
090                    sb.append("Line: ");
091                    sb.append(_lineNumber);
092                    sb.append("\n");
093    
094                    Iterator<String> itr = _changedLines.iterator();
095    
096                    while (itr.hasNext()) {
097                            sb.append(itr.next());
098    
099                            if (itr.hasNext()) {
100                                    sb.append("\n");
101                            }
102                    }
103    
104                    return sb.toString();
105            }
106    
107            private int _lineNumber;
108            private List<String> _changedLines;
109    
110    }