diff --git a/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/parser/antlr/hoisting/pathAnalysis/TokenAnalysisPath.java b/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/parser/antlr/hoisting/pathAnalysis/TokenAnalysisPath.java index 18c6e149e..883383943 100644 --- a/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/parser/antlr/hoisting/pathAnalysis/TokenAnalysisPath.java +++ b/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/parser/antlr/hoisting/pathAnalysis/TokenAnalysisPath.java @@ -23,11 +23,11 @@ public class TokenAnalysisPath { private List remainingIndexes; private int position = 1; - public TokenAnalysisPath(List indexes) { + TokenAnalysisPath(List indexes) { this.remainingIndexes = new LinkedList<>(indexes); } - public TokenAnalysisPath(TokenAnalysisPath prefix) { + TokenAnalysisPath(TokenAnalysisPath prefix) { this(prefix.remainingIndexes); path = new LinkedList<>(prefix.path); position = prefix.position; @@ -37,7 +37,7 @@ public class TokenAnalysisPath { return position - 1; } - public boolean isDone() { + boolean isDone() { return remainingIndexes.isEmpty(); } @@ -49,7 +49,7 @@ public class TokenAnalysisPath { } } - public boolean add(AbstractElement element) { + boolean add(AbstractElement element) { if (isDone()) return false; @@ -93,10 +93,7 @@ public class TokenAnalysisPath { if (getClass() != obj.getClass()) return false; TokenAnalysisPath other = (TokenAnalysisPath) obj; - if (path == null) { - if (other.path != null) - return false; - } else if (!path.equals(other.path)) + if (!path.equals(other.path)) return false; if (position != other.position) return false; diff --git a/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/parser/antlr/hoisting/pathAnalysis/TokenAnalysisPaths.java b/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/parser/antlr/hoisting/pathAnalysis/TokenAnalysisPaths.java index 6c39062b0..58bb83d2c 100644 --- a/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/parser/antlr/hoisting/pathAnalysis/TokenAnalysisPaths.java +++ b/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/parser/antlr/hoisting/pathAnalysis/TokenAnalysisPaths.java @@ -8,30 +8,21 @@ *******************************************************************************/ package org.eclipse.xtext.xtext.generator.parser.antlr.hoisting.pathAnalysis; -import java.util.LinkedHashSet; +import java.util.ArrayList; import java.util.List; -import java.util.Set; import java.util.stream.Collectors; import org.eclipse.xtext.AbstractElement; import org.eclipse.xtext.xtext.generator.parser.antlr.hoisting.token.Token; -import org.eclipse.xtext.xtext.generator.parser.antlr.hoisting.utils.StreamUtils; /** * @author overflow - Initial contribution and APILinkedHashSet */ public class TokenAnalysisPaths { - private Set tokenPaths = new LinkedHashSet<>(); + private List tokenPaths = new ArrayList<>(10); private boolean isEmpty = false; private boolean hasProgress = false; - public List> getTokenPaths() { - return tokenPaths.stream() - .map(TokenAnalysisPath::getTokenPath) - .distinct() - .collect(Collectors.toList()); - } - public TokenAnalysisPaths(List indexes) { tokenPaths.add(new TokenAnalysisPath(indexes)); } @@ -39,10 +30,17 @@ public class TokenAnalysisPaths { public TokenAnalysisPaths(TokenAnalysisPaths prefix) { this.tokenPaths = prefix.tokenPaths.stream() .map(TokenAnalysisPath::new) - .collect(StreamUtils.collectToLinkedHashSet()); + .collect(Collectors.toList()); this.hasProgress = prefix.hasProgress; } + public List> getTokenPaths() { + return tokenPaths.stream() + .map(TokenAnalysisPath::getTokenPath) + .distinct() + .collect(Collectors.toList()); + } + public boolean isDone() { return !isEmpty && tokenPaths.stream().allMatch(TokenAnalysisPath::isDone); } @@ -59,6 +57,17 @@ public class TokenAnalysisPaths { tokenPaths.forEach(p -> hasProgress = p.add(element) || hasProgress); } + private boolean addAllDistinct(TokenAnalysisPaths other) { + boolean changes = false; + for(TokenAnalysisPath path : other.tokenPaths) { + if (!tokenPaths.contains(path)) { + changes = true; + tokenPaths.add(path); + } + } + return changes; + } + public TokenAnalysisPaths merge(TokenAnalysisPaths other) { if (isEmpty) { return other.clone(); @@ -66,7 +75,7 @@ public class TokenAnalysisPaths { return this.clone(); } else { // set hasProgress if other has progress and progress is merged - if (this.tokenPaths.addAll(other.tokenPaths)) { + if (addAllDistinct(other)) { this.hasProgress |= other.hasProgress; } return this;