001    /**
002     * Copyright (c) 2000-2012 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.List;
019    
020    /**
021     * <p>
022     * Represents a change between one or several lines. <code>changeType</code>
023     * tells if the change happened in source or target. <code>lineNumber</code>
024     * holds the line number of the first modified line. This line number refers to
025     * a line in source or target, depending on the <code>changeType</code> value.
026     * <code>changedLines</code> is a list of strings, each string is a line that is
027     * already highlighted, indicating where the changes are.
028     * </p>
029     *
030     * @author Bruno Farache
031     */
032    public class DiffResult {
033    
034            public static final String SOURCE = "SOURCE";
035    
036            public static final String TARGET = "TARGET";
037    
038            public DiffResult(int linePos, List<String> changedLines) {
039                    _lineNumber = linePos + 1;
040                    _changedLines = changedLines;
041            }
042    
043            public DiffResult(int linePos, String changedLine) {
044                    _lineNumber = linePos + 1;
045                    _changedLines = new ArrayList<String>();
046                    _changedLines.add(changedLine);
047            }
048    
049            @Override
050            public boolean equals(Object obj) {
051                    DiffResult diffResult = (DiffResult)obj;
052    
053                    if ((diffResult.getLineNumber() == _lineNumber) &&
054                            diffResult.getChangedLines().equals(_changedLines)) {
055    
056                            return true;
057                    }
058    
059                    return false;
060            }
061    
062            public List<String> getChangedLines() {
063                    return _changedLines;
064            }
065    
066            public int getLineNumber() {
067                    return _lineNumber;
068            }
069    
070            @Override
071            public int hashCode() {
072                    HashCode hashCode = HashCodeFactoryUtil.getHashCode();
073    
074                    hashCode.append(_lineNumber);
075                    hashCode.append(_changedLines);
076    
077                    return hashCode.toHashCode();
078            }
079    
080            public void setChangedLines(List<String> changedLines) {
081                    _changedLines = changedLines;
082            }
083    
084            public void setLineNumber(int lineNumber) {
085                    _lineNumber = lineNumber;
086            }
087    
088            @Override
089            public String toString() {
090                    StringBundler sb = new StringBundler(2 * _changedLines.size() + 3);
091    
092                    sb.append("Line: ");
093                    sb.append(_lineNumber);
094                    sb.append("\n");
095    
096                    for (String changedLine : _changedLines) {
097                            sb.append(changedLine);
098                            sb.append("\n");
099                    }
100    
101                    if (!_changedLines.isEmpty()) {
102                            sb.setIndex(sb.index() - 1);
103                    }
104    
105                    return sb.toString();
106            }
107    
108            private List<String> _changedLines;
109            private int _lineNumber;
110    
111    }