@SuppressWarnings("unchecked")
private List<LeafPair> matchLeaves(Node left, Node right) {
List<LeafPair> matchedLeafs = new ArrayList<LeafPair>();
for (Enumeration<Node> leftNodes = left.postorderEnumeration(); leftNodes.hasMoreElements();) {
Node x = leftNodes.nextElement();
if (x.isLeaf()) {
for (Enumeration<Node> rightNodes = right.postorderEnumeration(); rightNodes.hasMoreElements();) {
Node y = rightNodes.nextElement();
if (y.isLeaf() && haveSameLabel(x, y)) {
double similarity = 0;
if (x.getLabel().isComment()) {
similarity =
fLeafCommentStringSimilarityCalculator.calculateSimilarity(
x.getValue(),
y.getValue());
// Important! Otherwhise nodes that match poorly will make it into final matching set,
// if no better matches are found!
if (similarity >= LEAF_COMMENT_STRING_SIMILARITY_THRESHOLD) {
matchedLeafs.add(new LeafPair(x, y, similarity));
}
} else { // ...other statements.
similarity =
fLeafGenericStringSimilarityCalculator.calculateSimilarity(
x.getValue(),
y.getValue());
// Important! Otherwise nodes that match poorly will make it into final matching set,
// if no better matches are found!
if (similarity >= fLeafGenericStringSimilarityThreshold) {
matchedLeafs.add(new LeafPair(x, y, similarity));