org.apache.wicket.util.diff.Diff Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.ops4j.pax.wicket.service Show documentation
Show all versions of org.ops4j.pax.wicket.service Show documentation
Pax Wicket Service is an OSGi extension of the Wicket framework, allowing for dynamic loading and
unloading of Wicket components and pageSources.
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2003 The Apache Software Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [email protected].
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* .
*
*/
package org.apache.wicket.util.diff;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import org.apache.wicket.util.diff.myers.MyersDiff;
/**
* Implements a differencing engine that works on arrays of {@link Object Object}.
*
*
* Within this library, the word text means a unit of information subject to version control.
*
*
* Text is represented as Object[]
because the diff engine is capable of handling more
* than plain ascci. In fact, arrays of any type that implements {@link java.lang.Object#hashCode
* hashCode()} and {@link java.lang.Object#equals equals()} correctly can be subject to differencing
* using this library.
*
*
*
* This library provides a framework in which different differencing algorithms may be used. If no
* algorithm is specified, a default algorithm is used.
*
*
* @version $Revision: 1.1 $ $Date: 2006/03/12 00:24:21 $
* @author Juanco Anez
* @see Delta
* @see DiffAlgorithm modifications:
*
* 27 Apr 2003 bwm
*
* Added some comments whilst trying to figure out the algorithm
*
* 03 May 2003 bwm
*
* Factored out the algorithm implementation into a separate difference algorithm class to
* allow pluggable algorithms.
*/
public class Diff extends ToString
{
/** The standard line separator. */
public static final String NL = System.getProperty("line.separator");
/** The line separator to use in RCS format output. */
public static final String RCS_EOL = "\n";
/** The original sequence. */
protected final Object[] orig;
/** The differencing algorithm to use. */
protected DiffAlgorithm algorithm;
/**
* Create a differencing object using the default algorithm
*
* @param original
* the original text that will be compared
*/
public Diff(Object[] original)
{
this(original, null);
}
/**
* Create a differencing object using the given algorithm
*
* @param original
* the original text that will be compared
* @param algorithm
* the difference algorithm to use.
*/
public Diff(Object[] original, DiffAlgorithm algorithm)
{
if (original == null)
{
throw new IllegalArgumentException();
}
orig = original;
if (algorithm != null)
{
this.algorithm = algorithm;
}
else
{
this.algorithm = defaultAlgorithm();
}
}
protected DiffAlgorithm defaultAlgorithm()
{
return new MyersDiff();
}
/**
* compute the difference between an original and a revision.
*
* @param orig
* the original
* @param rev
* the revision to compare with the original.
* @return a Revision describing the differences
* @throws DifferentiationFailedException
*/
public static Revision diff(Object[] orig, Object[] rev) throws DifferentiationFailedException
{
if (orig == null || rev == null)
{
throw new IllegalArgumentException();
}
return diff(orig, rev, null);
}
/**
* compute the difference between an original and a revision.
*
* @param orig
* the original
* @param rev
* the revision to compare with the original.
* @param algorithm
* the difference algorithm to use
* @return a Revision describing the differences
* @throws DifferentiationFailedException
*/
public static Revision diff(Object[] orig, Object[] rev, DiffAlgorithm algorithm)
throws DifferentiationFailedException
{
if (orig == null || rev == null)
{
throw new IllegalArgumentException();
}
return new Diff(orig, algorithm).diff(rev);
}
/**
* compute the difference between the original and a revision.
*
* @param rev
* the revision to compare with the original.
* @return a Revision describing the differences
* @throws DifferentiationFailedException
*/
public Revision diff(Object[] rev) throws DifferentiationFailedException
{
if (orig.length == 0 && rev.length == 0)
{
return new Revision();
}
else
{
return algorithm.diff(orig, rev);
}
}
/**
* Compares the two input sequences.
*
* @param orig
* The original sequence.
* @param rev
* The revised sequence.
* @return true if the sequences are identical. False otherwise.
*/
public static boolean compare(Object[] orig, Object[] rev)
{
if (orig.length != rev.length)
{
return false;
}
else
{
for (int i = 0; i < orig.length; i++)
{
if (!orig[i].equals(rev[i]))
{
return false;
}
}
return true;
}
}
/**
* Converts an array of {@link Object Object} to a string using {@link Diff#NL Diff.NL} as the
* line separator.
*
* @param o
* the array of objects.
* @return String
*/
public static String arrayToString(Object[] o)
{
return arrayToString(o, Diff.NL);
}
/**
* Edits all of the items in the input sequence.
*
* @param text
* The input sequence.
* @return A sequence of the same length with all the lines differing from the corresponding
* ones in the input.
*/
public static Object[] editAll(Object[] text)
{
Object[] result = new String[text.length];
for (int i = 0; i < text.length; i++)
{
result[i] = text[i] + " ";
}
return result;
}
/**
* Performs random edits on the input sequence. Useful for testing.
*
* @param text
* The input sequence.
* @return The sequence with random edits performed.
*/
public static Object[] randomEdit(Object[] text)
{
return randomEdit(text, text.length);
}
/**
* Performs random edits on the input sequence. Useful for testing.
*
* @param text
* The input sequence.
* @param seed
* A seed value for the randomizer.
* @return The sequence with random edits performed.
*/
public static Object[] randomEdit(Object[] text, long seed)
{
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy