1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.diff;
24  
25  import com.liferay.portal.kernel.util.StringMaker;
26  
27  import java.util.ArrayList;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  /**
32   * <a href="DiffResult.java.html"><b><i>View Source</i></b></a>
33   *
34   * <p>
35   * Represents a change between one or several lines. <code>changeType</code>
36   * tells if the change happened in source or target. <code>lineNumber</code>
37   * holds the line number of the first modified line. This line number refers to
38   * a line in source or target, depending on the <code>changeType</code> value.
39   * <code>changedLines</code> is a list of strings, each string is a line that
40   * is already highlighted, indicating where the changes are.
41   * </p>
42   *
43   * @author Bruno Farache
44   *
45   */
46  public class DiffResult {
47  
48      public static final String SOURCE = "SOURCE";
49  
50      public static final String TARGET = "TARGET";
51  
52      public DiffResult(int linePos, List<String> changedLines) {
53          _lineNumber = linePos + 1;
54          _changedLines = changedLines;
55      }
56  
57      public DiffResult(int linePos, String changedLine) {
58          _lineNumber = linePos + 1;
59          _changedLines = new ArrayList<String>();
60          _changedLines.add(changedLine);
61      }
62  
63      public List<String> getChangedLines() {
64          return _changedLines;
65      }
66  
67      public void setChangedLines(List<String> changedLines) {
68          _changedLines = changedLines;
69      }
70  
71      public int getLineNumber() {
72          return _lineNumber;
73      }
74  
75      public void setLineNumber(int lineNumber) {
76          _lineNumber = lineNumber;
77      }
78  
79      public boolean equals(Object obj) {
80          DiffResult diffResult = (DiffResult)obj;
81  
82          if ((diffResult.getLineNumber() == _lineNumber) &&
83              (diffResult.getChangedLines().equals(_changedLines))) {
84  
85              return true;
86          }
87  
88          return false;
89      }
90  
91      public String toString() {
92          StringMaker sm = new StringMaker();
93  
94          sm.append("Line: ");
95          sm.append(_lineNumber);
96          sm.append("\n");
97  
98          Iterator<String> itr = _changedLines.iterator();
99  
100         while (itr.hasNext()) {
101             sm.append(itr.next());
102 
103             if (itr.hasNext()) {
104                 sm.append("\n");
105             }
106         }
107 
108         return sm.toString();
109     }
110 
111     private int _lineNumber;
112     private List<String> _changedLines;
113 
114 }