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