use of org.jooq.exception.DataDefinitionException in project jOOQ by jOOQ.
the class AbstractNode method commonAncestor.
@SuppressWarnings("unchecked")
final N commonAncestor(N other) {
if (this.id().equals(other.id()))
return (N) this;
// TODO: Find a better solution than the brute force one
// See e.g. https://en.wikipedia.org/wiki/Lowest_common_ancestor
Map<N, Integer> a1 = ancestors((N) this, new HashMap<>(), 1);
Map<N, Integer> a2 = ancestors(other, new HashMap<>(), 1);
N node = null;
Integer distance = null;
for (Entry<N, Integer> entry : a1.entrySet()) {
if (a2.containsKey(entry.getKey())) {
// TODO: What if there are several conflicting paths?
if (distance == null || distance > entry.getValue()) {
node = entry.getKey();
distance = entry.getValue();
}
}
}
if (node == null)
throw new DataDefinitionException("Versions " + this.id() + " and " + other.id() + " do not have a common ancestor");
return node;
}
Aggregations