if (relFix.size() < 2)
return null;
long idToKeep = 0;
Relation relationToKeep = relFix.iterator().next();
// Only one relation will be kept - the one with lowest positive ID, if such exist
// or one "at random" if no such exists. Rest of the relations will be deleted
for (Relation w: relFix) {
if (!w.isNew() && (idToKeep == 0 || w.getId() < idToKeep)) {
idToKeep = w.getId();
relationToKeep = w;
}
}
// Find the relation that is member of one or more relations. (If any)
Relation relationWithRelations = null;
List<Relation> relRef = null;
for (Relation w : relFix) {
List<Relation> rel = OsmPrimitive.getFilteredList(w.getReferrers(), Relation.class);
if (!rel.isEmpty()) {
if (relationWithRelations != null)
throw new AssertionError("Cannot fix duplicate relations: More than one relation is member of another relation.");
relationWithRelations = w;
relRef = rel;
}
}
Collection<Command> commands = new LinkedList<>();
// Fix relations.
if (relationWithRelations != null && relationToKeep != relationWithRelations) {
for (Relation rel : relRef) {
Relation newRel = new Relation(rel);
for (int i = 0; i < newRel.getMembers().size(); ++i) {
RelationMember m = newRel.getMember(i);
if (relationWithRelations.equals(m.getMember())) {
newRel.setMember(i, new RelationMember(m.getRole(), relationToKeep));
}
}
commands.add(new ChangeCommand(rel, newRel));
}
}