001    /**
002     * Copyright (c) 2000-2013 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                    if (this == obj) {
052                            return true;
053                    }
054    
055                    if (!(obj instanceof DiffResult)) {
056                            return false;
057                    }
058    
059                    DiffResult diffResult = (DiffResult)obj;
060    
061                    if ((diffResult.getLineNumber() == _lineNumber) &&
062                            diffResult.getChangedLines().equals(_changedLines)) {
063    
064                            return true;
065                    }
066    
067                    return false;
068            }
069    
070            public List<String> getChangedLines() {
071                    return _changedLines;
072            }
073    
074            public int getLineNumber() {
075                    return _lineNumber;
076            }
077    
078            @Override
079            public int hashCode() {
080                    HashCode hashCode = HashCodeFactoryUtil.getHashCode();
081    
082                    hashCode.append(_lineNumber);
083                    hashCode.append(_changedLines);
084    
085                    return hashCode.toHashCode();
086            }
087    
088            public void setChangedLines(List<String> changedLines) {
089                    _changedLines = changedLines;
090            }
091    
092            public void setLineNumber(int lineNumber) {
093                    _lineNumber = lineNumber;
094            }
095    
096            @Override
097            public String toString() {
098                    StringBundler sb = new StringBundler(2 * _changedLines.size() + 3);
099    
100                    sb.append("Line: ");
101                    sb.append(_lineNumber);
102                    sb.append("\n");
103    
104                    for (String changedLine : _changedLines) {
105                            sb.append(changedLine);
106                            sb.append("\n");
107                    }
108    
109                    if (!_changedLines.isEmpty()) {
110                            sb.setIndex(sb.index() - 1);
111                    }
112    
113                    return sb.toString();
114            }
115    
116            private List<String> _changedLines;
117            private int _lineNumber;
118    
119    }