1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.util.ArrayList;
18  import java.util.Iterator;
19  import java.util.List;
20  
21  /**
22   * <a href="DiffResult.java.html"><b><i>View Source</i></b></a>
23   *
24   * <p>
25   * Represents a change between one or several lines. <code>changeType</code>
26   * tells if the change happened in source or target. <code>lineNumber</code>
27   * holds the line number of the first modified line. This line number refers to
28   * a line in source or target, depending on the <code>changeType</code> value.
29   * <code>changedLines</code> is a list of strings, each string is a line that is
30   * already highlighted, indicating where the changes are.
31   * </p>
32   *
33   * @author Bruno Farache
34   */
35  public class DiffResult {
36  
37      public static final String SOURCE = "SOURCE";
38  
39      public static final String TARGET = "TARGET";
40  
41      public DiffResult(int linePos, List<String> changedLines) {
42          _lineNumber = linePos + 1;
43          _changedLines = changedLines;
44      }
45  
46      public DiffResult(int linePos, String changedLine) {
47          _lineNumber = linePos + 1;
48          _changedLines = new ArrayList<String>();
49          _changedLines.add(changedLine);
50      }
51  
52      public List<String> getChangedLines() {
53          return _changedLines;
54      }
55  
56      public void setChangedLines(List<String> changedLines) {
57          _changedLines = changedLines;
58      }
59  
60      public int getLineNumber() {
61          return _lineNumber;
62      }
63  
64      public void setLineNumber(int lineNumber) {
65          _lineNumber = lineNumber;
66      }
67  
68      public boolean equals(Object obj) {
69          DiffResult diffResult = (DiffResult)obj;
70  
71          if ((diffResult.getLineNumber() == _lineNumber) &&
72              (diffResult.getChangedLines().equals(_changedLines))) {
73  
74              return true;
75          }
76  
77          return false;
78      }
79  
80      public int hashCode() {
81          HashCode hashCode = HashCodeFactoryUtil.getHashCode();
82  
83          hashCode.append(_lineNumber);
84          hashCode.append(_changedLines);
85  
86          return hashCode.toHashCode();
87      }
88  
89      public String toString() {
90          StringBundler sb = new StringBundler(2 * _changedLines.size() + 2);
91  
92          sb.append("Line: ");
93          sb.append(_lineNumber);
94          sb.append("\n");
95  
96          Iterator<String> itr = _changedLines.iterator();
97  
98          while (itr.hasNext()) {
99              sb.append(itr.next());
100 
101             if (itr.hasNext()) {
102                 sb.append("\n");
103             }
104         }
105 
106         return sb.toString();
107     }
108 
109     private int _lineNumber;
110     private List<String> _changedLines;
111 
112 }