001/*
002 * #%L
003 * Netarchivesuite - common - test
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023/** This code is taken from http://www.incava.org/projects/java/java-diff/
024 * It was there released under the LGPL as of Jan 31. 2007.
025 */
026package dk.netarkivet.testutils;
027
028/**
029 * Represents a difference, as used in <code>Diff</code>. A difference consists of two pairs of starting and ending
030 * points, each pair representing either the "from" or the "to" collection passed to <code>Diff</code>. If an ending
031 * point is -1, then the difference was either a deletion or an addition. For example, if <code>getDeletedEnd()</code>
032 * returns -1, then the difference represents an addition.
033 */
034public class Difference {
035    public static final int NONE = -1;
036
037    /**
038     * The point at which the deletion starts.
039     */
040    private int delStart = NONE;
041
042    /**
043     * The point at which the deletion ends.
044     */
045    private int delEnd = NONE;
046
047    /**
048     * The point at which the addition starts.
049     */
050    private int addStart = NONE;
051
052    /**
053     * The point at which the addition ends.
054     */
055    private int addEnd = NONE;
056
057    /**
058     * Creates the difference for the given start and end points for the deletion and addition.
059     */
060    public Difference(int delStart, int delEnd, int addStart, int addEnd) {
061        this.delStart = delStart;
062        this.delEnd = delEnd;
063        this.addStart = addStart;
064        this.addEnd = addEnd;
065    }
066
067    /**
068     * The point at which the deletion starts, if any. A value equal to <code>NONE</code> means this is an addition.
069     */
070    public int getDeletedStart() {
071        return delStart;
072    }
073
074    /**
075     * The point at which the deletion ends, if any. A value equal to <code>NONE</code> means this is an addition.
076     */
077    public int getDeletedEnd() {
078        return delEnd;
079    }
080
081    /**
082     * The point at which the addition starts, if any. A value equal to <code>NONE</code> means this must be an
083     * addition.
084     */
085    public int getAddedStart() {
086        return addStart;
087    }
088
089    /**
090     * The point at which the addition ends, if any. A value equal to <code>NONE</code> means this must be an addition.
091     */
092    public int getAddedEnd() {
093        return addEnd;
094    }
095
096    /**
097     * Sets the point as deleted. The start and end points will be modified to include the given line.
098     */
099    public void setDeleted(int line) {
100        delStart = Math.min(line, delStart);
101        delEnd = Math.max(line, delEnd);
102    }
103
104    /**
105     * Sets the point as added. The start and end points will be modified to include the given line.
106     */
107    public void setAdded(int line) {
108        addStart = Math.min(line, addStart);
109        addEnd = Math.max(line, addEnd);
110    }
111
112    @Override
113    public int hashCode() {
114        final int prime = 31;
115        int result = 1;
116        result = prime * result + addEnd;
117        result = prime * result + addStart;
118        result = prime * result + delEnd;
119        result = prime * result + delStart;
120        return result;
121    }
122
123    /**
124     * Compares this object to the other for equality. Both objects must be of type Difference, with the same starting
125     * and ending points.
126     */
127    @Override
128    public boolean equals(Object obj) {
129        if (this == obj) {
130            return true;
131        }
132        if (obj == null) {
133            return false;
134        }
135        if (getClass() != obj.getClass()) {
136            return false;
137        }
138        Difference other = (Difference) obj;
139        if (addEnd != other.addEnd) {
140            return false;
141        }
142        if (addStart != other.addStart) {
143            return false;
144        }
145        if (delEnd != other.delEnd) {
146            return false;
147        }
148        if (delStart != other.delStart) {
149            return false;
150        }
151        return true;
152    }
153
154    /**
155     * Returns a string representation of this difference.
156     */
157    public String toString() {
158        StringBuffer buf = new StringBuffer();
159        buf.append("del: [" + delStart + ", " + delEnd + "]");
160        buf.append(" ");
161        buf.append("add: [" + addStart + ", " + addEnd + "]");
162        return buf.toString();
163    }
164
165}