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