-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3edc483
commit 87dfe9c
Showing
7 changed files
with
428 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/BuildNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableSet; | ||
import com.google.common.collect.ImmutableSortedMap; | ||
import com.google.common.collect.ImmutableSortedSet; | ||
import com.google.common.collect.Maps; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.SortedMap; | ||
import java.util.TreeMap; | ||
|
||
@AutoValue | ||
abstract class BuildNode<T> { | ||
static <T> BuildNode<T> create() { | ||
return new AutoValue_BuildNode<>(new TreeMap<>(), new HashSet<>()); | ||
} | ||
|
||
static <T> BuildNode<T> create(SortedMap<String, BuildNode<T>> children, Set<T> candidateRules) { | ||
return new AutoValue_BuildNode<>(children, candidateRules); | ||
} | ||
|
||
@SuppressWarnings("AutoValueImmutableFields" /* The BuildNode is used to construct the tree. */) | ||
public abstract SortedMap<String, BuildNode<T>> children(); | ||
|
||
@SuppressWarnings("AutoValueImmutableFields" /* The BuildNode is used to construct the tree. */) | ||
public abstract Set<T> candidateRules(); | ||
|
||
public void register(ImmutableSet<ImmutableSortedSet<String>> identifierCombinations, T rule) { | ||
for (ImmutableSet<String> path : identifierCombinations) { | ||
registerPath(path.asList(), rule); | ||
} | ||
} | ||
|
||
void registerPath(ImmutableList<String> path, T rule) { | ||
path.stream() | ||
.findFirst() | ||
.ifPresentOrElse( | ||
edge -> | ||
children() | ||
.computeIfAbsent(edge, k -> BuildNode.create()) | ||
.registerPath(path.subList(1, path.size()), rule), | ||
() -> candidateRules().add(rule)); | ||
} | ||
|
||
Node<T> immutable() { | ||
return Node.create( | ||
Maps.transformValues(ImmutableSortedMap.copyOfSorted(children()), BuildNode::immutable), | ||
ImmutableSet.copyOf(candidateRules())); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/Node.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import static com.google.common.collect.ImmutableList.toImmutableList; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableSet; | ||
import com.google.common.collect.ImmutableSortedSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
@AutoValue | ||
abstract class Node<T> { | ||
static <T> Node<T> create(Map<String, Node<T>> children, ImmutableSet<T> candidateRules) { | ||
return new AutoValue_Node<>(ImmutableMap.copyOf(children), candidateRules); | ||
} | ||
|
||
public abstract ImmutableMap<String, Node<T>> children(); | ||
|
||
public abstract ImmutableSet<T> candidateRules(); | ||
|
||
static <C> Node<C> createRefasterTemplateTree( | ||
List<C> refasterRules, Function<C, ImmutableSet<ImmutableSortedSet<String>>> edgeExtractor) { | ||
// XXX: Improve this method... | ||
List<ImmutableSet<ImmutableSortedSet<String>>> beforeTemplateIdentifiers = | ||
refasterRules.stream().map(edgeExtractor).collect(toImmutableList()); | ||
|
||
BuildNode<C> tree = BuildNode.create(); | ||
for (int i = 0; i < refasterRules.size(); i++) { | ||
tree.register(beforeTemplateIdentifiers.get(i), refasterRules.get(i)); | ||
} | ||
return tree.immutable(); | ||
} | ||
|
||
void collectCandidateTemplates(ImmutableList<String> sourceIdentifiers, Consumer<T> sink) { | ||
candidateRules().forEach(sink); | ||
|
||
if (sourceIdentifiers.isEmpty() || children().isEmpty()) { | ||
return; | ||
} | ||
|
||
if (children().size() < sourceIdentifiers.size()) { | ||
for (Map.Entry<String, Node<T>> e : children().entrySet()) { | ||
if (sourceIdentifiers.contains(e.getKey())) { | ||
e.getValue().collectCandidateTemplates(sourceIdentifiers, sink); | ||
} | ||
} | ||
} else { | ||
ImmutableList<String> remainingSourceCandidateEdges = | ||
sourceIdentifiers.subList(1, sourceIdentifiers.size()); | ||
Node<T> child = children().get(sourceIdentifiers.get(0)); | ||
if (child != null) { | ||
child.collectCandidateTemplates(remainingSourceCandidateEdges, sink); | ||
} | ||
collectCandidateTemplates(remainingSourceCandidateEdges, sink); | ||
} | ||
} | ||
} |
Oops, something went wrong.