Class SequencesComparator<T>
- java.lang.Object
-
- org.apache.commons.collections4.sequence.SequencesComparator<T>
-
public class SequencesComparator<T> extends java.lang.Object
This class allows to compare two objects sequences.The two sequences can hold any object type, as only the
equals
method is used to compare the elements of the sequences. It is guaranteed that the comparisons will always be done aso1.equals(o2)
whereo1
belongs to the first sequence ando2
belongs to the second sequence. This can be important if subclassing is used for some elements in the first sequence and theequals
method is specialized.Comparison can be seen from two points of view: either as giving the smallest modification allowing to transform the first sequence into the second one, or as giving the longest sequence which is a subsequence of both initial sequences. The
equals
method is used to compare objects, so any object can be put into sequences. Modifications include deleting, inserting or keeping one object, starting from the beginning of the first sequence.This class implements the comparison algorithm, which is the very efficient algorithm from Eugene W. Myers An O(ND) Difference Algorithm and Its Variations. This algorithm produces the shortest possible
edit script
containing all thecommands
needed to transform the first sequence into the second one.- Since:
- 4.0
- See Also:
EditScript
,EditCommand
,CommandVisitor
-
-
Constructor Summary
Constructors Constructor Description SequencesComparator(java.util.List<T> sequence1, java.util.List<T> sequence2)
Simple constructor.SequencesComparator(java.util.List<T> sequence1, java.util.List<T> sequence2, Equator<? super T> equator)
Simple constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description EditScript<T>
getScript()
Get theEditScript
object.
-
-
-
Constructor Detail
-
SequencesComparator
public SequencesComparator(java.util.List<T> sequence1, java.util.List<T> sequence2)
Simple constructor.Creates a new instance of SequencesComparator using a
DefaultEquator
.It is guaranteed that the comparisons will always be done as
o1.equals(o2)
whereo1
belongs to the first sequence ando2
belongs to the second sequence. This can be important if subclassing is used for some elements in the first sequence and theequals
method is specialized.- Parameters:
sequence1
- first sequence to be comparedsequence2
- second sequence to be compared
-
SequencesComparator
public SequencesComparator(java.util.List<T> sequence1, java.util.List<T> sequence2, Equator<? super T> equator)
Simple constructor.Creates a new instance of SequencesComparator with a custom
Equator
.It is guaranteed that the comparisons will always be done as
Equator.equate(o1, o2)
whereo1
belongs to the first sequence ando2
belongs to the second sequence.- Parameters:
sequence1
- first sequence to be comparedsequence2
- second sequence to be comparedequator
- the equator to use for testing object equality
-
-
Method Detail
-
getScript
public EditScript<T> getScript()
Get theEditScript
object.It is guaranteed that the objects embedded in the
insert commands
come from the second sequence and that the objects embedded in either thedelete commands
orkeep commands
come from the first sequence. This can be important if subclassing is used for some elements in the first sequence and theequals
method is specialized.- Returns:
- the edit script resulting from the comparison of the two sequences
-
-