All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xwiki.diff.internal.AbstractDelta Maven / Gradle / Ivy

The newest version!
/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.diff.internal;

import java.util.Objects;

import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.xwiki.diff.Chunk;
import org.xwiki.diff.Delta;

/**
 * Base class used for various types of {@link Delta}s.
 *
 * @param  the type of compared elements
 * @version $Id: b6d83b749899d6f6b088a2d9a39a3fc1866cfe19 $
 */
public abstract class AbstractDelta implements Delta
{
    /**
     * @see #getType()
     */
    private Type type;

    /**
     * @see #getPrevious()
     */
    private Chunk previous;

    /**
     * @see #getNext()
     */
    private Chunk next;

    /**
     * @param previous the chunk before the modification
     * @param next the chunk after the modification
     * @param type the type of modification applied to the list
     */
    public AbstractDelta(Chunk previous, Chunk next, Type type)
    {
        this.type = type;
        this.previous = previous;
        this.next = next;
    }

    /**
     * @param original the chunk before the modification
     * @param revised the chunk after the modification
     * @param type the type of modification applied to the list
     */
    public AbstractDelta(difflib.Chunk original, difflib.Chunk revised, Type type)
    {
        this.type = type;
        this.previous = new DefaultChunk(original);
        this.next = new DefaultChunk(revised);
    }

    @Override
    public Type getType()
    {
        return this.type;
    }

    @Override
    public Chunk getPrevious()
    {
        return this.previous;
    }

    /**
     * @param previous the chunk before the modification
     */
    public void setPrevious(Chunk previous)
    {
        this.previous = previous;
    }

    @Override
    public Chunk getNext()
    {
        return this.next;
    }

    /**
     * @param next the chunk after the modification
     */
    public void setNext(Chunk next)
    {
        this.next = next;
    }

    @Override
    public int hashCode()
    {
        HashCodeBuilder builder = new HashCodeBuilder();

        builder.append(getType());
        builder.append(getPrevious());
        builder.append(getNext());

        return builder.toHashCode();
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj) {
            return true;
        }

        if (obj instanceof Delta) {
            Delta otherDelta = (Delta) obj;

            return getType() == otherDelta.getType() && Objects.equals(getPrevious(), otherDelta.getPrevious())
                && Objects.equals(getNext(), otherDelta.getNext());
        }

        return false;
    }

    @Override
    public String toString()
    {
        return "[-" + getPrevious() + " +" + getNext() + "]";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy