Skip to content

Commit

Permalink
refactor: extract methods for object mapping operations
Browse files Browse the repository at this point in the history
  • Loading branch information
d4gg4d committed Mar 1, 2015
1 parent f7d8766 commit 15f2bbc
Showing 1 changed file with 43 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ public static List<Edge> mergeBiDirectionals(List<Edge> edges) {
return mergedEdges;
}

private static Collection<List<Edge>> groupEdges(List<Edge> edges) {
return edges.stream()
.collect(groupingBy(EdgeOperations::sameSourceAndTarget))
.values();
private static List<Edge> takeSingleItemsGroups(Collection<List<Edge>> groupedEdges) {
return groupedEdges.stream()
.filter(edgeGroup -> edgeGroup.size() == 1)
.flatMap(Collection::stream)
.collect(toList());
}

private static List<Edge> mergeNonSingleGroups(Collection<List<Edge>> groupedEdges) {
Expand All @@ -50,32 +51,20 @@ private static List<Edge> mergeNonSingleGroups(Collection<List<Edge>> groupedEdg
.collect(toList());
List<Edge> biDirectionals = edgeGroups.stream()
.filter(sourceGroups -> sourceGroups.size() == 2)
.flatMap(groupedBySource -> {
List<Edge> a = groupedBySource.get(0);
List<Edge> b = groupedBySource.get(1);
return makePairs(a, b).stream();
}).map(edgePair -> {
Edge source = edgePair.left;
Edge target = edgePair.right;
return new Edge(source.source, target.source, resolveEdgeType(source.type, target.type), BI_DIRECTIONAL);
}).collect(toList());
.map(EdgeOperations.Tuple::createPairs)
.flatMap(Collection::stream)
.map(EdgeOperations::mergeEdges)
.collect(toList());
List<Edge> newEdges = Lists.newArrayList();
newEdges.addAll(multiReferenceUniDirectionals);
newEdges.addAll(biDirectionals);
return newEdges;
}

private static List<Edge> takeSingleItemsGroups(Collection<List<Edge>> groupedEdges) {
return groupedEdges.stream()
.filter(edgeGroup -> edgeGroup.size() == 1)
.flatMap(Collection::stream)
.collect(toList());
}

private static UnorderedTuple<?, ?> sameSourceAndTarget(Edge edge) {
String sourceId = edge.source.packageName + "." + edge.source.className;
String targetId = edge.target.packageName + "." + edge.target.className;
return UnorderedTuple.of(sourceId, targetId);
private static Collection<List<Edge>> groupEdges(List<Edge> edges) {
return edges.stream()
.collect(groupingBy(EdgeOperations::sameSourceAndTarget))
.values();
}

private static List<List<Edge>> groupBySource(List<Edge> edges) {
Expand All @@ -84,18 +73,16 @@ private static List<List<Edge>> groupBySource(List<Edge> edges) {
.values());
}

private static <T> List<Tuple<T, T>> makePairs(List<T> a, List<T> b) {
List<Tuple<T,T>> pairs = Lists.newArrayList();
if (a.size() > b.size()) {
for (int i = 0; i < a.size(); i++) {
pairs.add(new Tuple<>(a.get(i), b.get(i % b.size())));
}
} else {
for (int i = 0; i < b.size(); i++) {
pairs.add(new Tuple<>(a.get(i % a.size()), b.get(i)));
}
}
return pairs;
private static Edge mergeEdges(Tuple<Edge, Edge> edgePair) {
Edge source = edgePair.left;
Edge target = edgePair.right;
return new Edge(source.source, target.source, resolveEdgeType(source.type, target.type), BI_DIRECTIONAL);
}

private static UnorderedTuple<?, ?> sameSourceAndTarget(Edge edge) {
String sourceId = edge.source.packageName + "." + edge.source.className;
String targetId = edge.target.packageName + "." + edge.target.className;
return UnorderedTuple.of(sourceId, targetId);
}

private static class UnorderedTuple<X, Y> extends Tuple<X, Y> {
Expand Down Expand Up @@ -135,5 +122,25 @@ public Tuple(X left, Y right) {
this.left = left;
this.right = right;
}

public static <T> List<Tuple<T, T>> createPairs(List<List<T>> listOfTwoGroups) {
List<T> a = listOfTwoGroups.get(0);
List<T> b = listOfTwoGroups.get(1);
return makePairs(a, b);
}

private static <T> List<Tuple<T, T>> makePairs(List<T> a, List<T> b) {
List<Tuple<T,T>> pairs = Lists.newArrayList();
if (a.size() > b.size()) {
for (int i = 0; i < a.size(); i++) {
pairs.add(new Tuple<>(a.get(i), b.get(i % b.size())));
}
} else {
for (int i = 0; i < b.size(); i++) {
pairs.add(new Tuple<>(a.get(i % a.size()), b.get(i)));
}
}
return pairs;
}
}
}

0 comments on commit 15f2bbc

Please sign in to comment.