Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.eclipse.compare.structuremergeviewer
Class Differencer  view Differencer download Differencer.java

java.lang.Object
  extended byorg.eclipse.compare.structuremergeviewer.Differencer

public class Differencer
extends java.lang.Object

A generic two-way or three-way differencing engine.

The engine is used by calling one of the findDifferences methods and passing in the objects to compare. The engine calls the following methods on the input objects to perform the compare:

Clients may use as is, or subclass to provide a custom implementation for the three hooks. However the default implementation already deals with the typical case: The different kind of changes detected by the engine are decoded as follows: In the two-way case only NO_CHANGE, ADDITION, DELETION, and CHANGE are used. In the three-way case these constants are bitwise ORed with one of directional constants LEFT, RIGHT, and CONFLICTING.


Nested Class Summary
(package private) static class Differencer.Node
           
 
Field Summary
static int ADDITION
          Difference constant (value 1) indicating one side was added.
static int CHANGE
          Difference constant (value 3) indicating side changed.
static int CHANGE_TYPE_MASK
          Bit mask (value 3) for extracting the kind of difference.
static int CONFLICTING
          Three-way change constant (value 12) indicating a change on left and right sides.
static int DELETION
          Difference constant (value 2) indicating one side was removed.
static int DIRECTION_MASK
          Bit mask (value 12) for extracting the direction of a three-way change.
static int LEFT
          Three-way change constant (value 4) indicating a change on left side.
static int NO_CHANGE
          Difference constant (value 0) indicating no difference.
static int PSEUDO_CONFLICT
          Constant (value 16) indicating a change on left and right side (with respect to ancestor) but left and right are identical.
static int RIGHT
          Three-way change constant (value 8) indicating a change on right side.
 
Constructor Summary
Differencer()
          Creates a new differencing engine.
 
Method Summary
private  int compare(boolean threeway, java.lang.Object ancestor, java.lang.Object left, java.lang.Object right)
          Performs a 2-way or 3-way compare of the given leaf elements and returns an integer describing the kind of difference.
protected  boolean contentsEqual(java.lang.Object input1, java.lang.Object input2)
          Performs a content compare on the two given inputs.
 java.lang.Object findDifferences(boolean threeWay, org.eclipse.core.runtime.IProgressMonitor pm, java.lang.Object data, java.lang.Object ancestor, java.lang.Object left, java.lang.Object right)
          Starts the differencing engine on the three input objects.
protected  java.lang.Object[] getChildren(java.lang.Object input)
          Returns the children of the given input or null if there are no children.
private  java.io.InputStream getStream(java.lang.Object o)
          Tries to return an InputStream for the given object.
private  int traverse(boolean threeWay, Differencer.Node parent, org.eclipse.core.runtime.IProgressMonitor pm, java.lang.Object ancestor, java.lang.Object left, java.lang.Object right)
          Traverse tree in postorder.
protected  void updateProgress(org.eclipse.core.runtime.IProgressMonitor progressMonitor, java.lang.Object node)
          Called for every leaf or node compare to update progress information.
protected  java.lang.Object visit(java.lang.Object data, int result, java.lang.Object ancestor, java.lang.Object left, java.lang.Object right)
          Called for every node or leaf comparison.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_CHANGE

public static final int NO_CHANGE
Difference constant (value 0) indicating no difference.

See Also:
Constant Field Values

ADDITION

public static final int ADDITION
Difference constant (value 1) indicating one side was added.

See Also:
Constant Field Values

DELETION

public static final int DELETION
Difference constant (value 2) indicating one side was removed.

See Also:
Constant Field Values

CHANGE

public static final int CHANGE
Difference constant (value 3) indicating side changed.

See Also:
Constant Field Values

CHANGE_TYPE_MASK

public static final int CHANGE_TYPE_MASK
Bit mask (value 3) for extracting the kind of difference.

See Also:
Constant Field Values

LEFT

public static final int LEFT
Three-way change constant (value 4) indicating a change on left side.

See Also:
Constant Field Values

RIGHT

public static final int RIGHT
Three-way change constant (value 8) indicating a change on right side.

See Also:
Constant Field Values

CONFLICTING

public static final int CONFLICTING
Three-way change constant (value 12) indicating a change on left and right sides.

See Also:
Constant Field Values

DIRECTION_MASK

public static final int DIRECTION_MASK
Bit mask (value 12) for extracting the direction of a three-way change.

See Also:
Constant Field Values

PSEUDO_CONFLICT

public static final int PSEUDO_CONFLICT
Constant (value 16) indicating a change on left and right side (with respect to ancestor) but left and right are identical.

See Also:
Constant Field Values
Constructor Detail

Differencer

public Differencer()
Creates a new differencing engine.

Method Detail

findDifferences

public java.lang.Object findDifferences(boolean threeWay,
                                        org.eclipse.core.runtime.IProgressMonitor pm,
                                        java.lang.Object data,
                                        java.lang.Object ancestor,
                                        java.lang.Object left,
                                        java.lang.Object right)
Starts the differencing engine on the three input objects. If threeWay is true a three-way comparison is performed, otherwise a two-way compare (in the latter case the ancestor argument is ignored). The progress monitor is passed to the method updateProgress which is called for every node or leaf compare. The method returns the object that was returned from the top-most call to method visit. At most two of the ancestor, left, and right parameters are allowed to be null.


traverse

private int traverse(boolean threeWay,
                     Differencer.Node parent,
                     org.eclipse.core.runtime.IProgressMonitor pm,
                     java.lang.Object ancestor,
                     java.lang.Object left,
                     java.lang.Object right)
Traverse tree in postorder.


visit

protected java.lang.Object visit(java.lang.Object data,
                                 int result,
                                 java.lang.Object ancestor,
                                 java.lang.Object left,
                                 java.lang.Object right)
Called for every node or leaf comparison. The differencing engine passes in the input objects of the compare and the result of the compare. The data object is the value returned from a call to the visit method on the parent input. It can be considered the "parent" reference and is useful when building a tree.

The Differencer implementation returns a new DiffNode which is initialized with the corresponding values. Subclasses may override.


compare

private int compare(boolean threeway,
                    java.lang.Object ancestor,
                    java.lang.Object left,
                    java.lang.Object right)
Performs a 2-way or 3-way compare of the given leaf elements and returns an integer describing the kind of difference.


contentsEqual

protected boolean contentsEqual(java.lang.Object input1,
                                java.lang.Object input2)
Performs a content compare on the two given inputs.

The Differencer implementation returns true if both inputs implement IStreamContentAccessor and their byte contents is identical. Subclasses may override to implement a different content compare on the given inputs.


getStream

private java.io.InputStream getStream(java.lang.Object o)
Tries to return an InputStream for the given object. Returns null if the object not an IStreamContentAccessor or an error occured.


getChildren

protected java.lang.Object[] getChildren(java.lang.Object input)
Returns the children of the given input or null if there are no children.

The Differencer implementation checks whether the input implements the IStructureComparator interface. If yes it is used to return an array containing all children. Otherwise null is returned. Subclasses may override to implement a different strategy to enumerate children.


updateProgress

protected void updateProgress(org.eclipse.core.runtime.IProgressMonitor progressMonitor,
                              java.lang.Object node)
Called for every leaf or node compare to update progress information.

The Differencer implementation shows the name of the input object as a subtask. Subclasses may override.