src/main/java/de/blau/android/osm/StorageDelegator.java
/**
     * Compare current list of relations members to new list and apply the necessary changes
     * 
     * @param r the relation
     * @param members new list of members
     */
Compare current list of relations members to new list and apply the necessary changes

@param r the relation
@param members new list of members
Compare current list of relations members to new list and apply the necessary changes @param r the relation @param members new list of members
public void updateRelation(@NonNull Relation r, @NonNull List<RelationMemberDescription> members) {
        dirty = true;
        undo.save(r);
        validateRelationMemberCount(r, members.size() - r.getMemberCount());
        boolean changed = false;
        List<RelationMember> origMembers = new ArrayList<>(r.getMembers());
        LinkedHashMap<String, RelationMember> membersHash = new LinkedHashMap<>();
        for (RelationMember rm : r.getMembers()) {
            membersHash.put(rm.getType() + "-" + rm.getRef(), rm);
        }
        List<RelationMember> newMembers = new ArrayList<>();
        for (int i = 0; i < members.size(); i++) {
            RelationMemberDescription rmd = members.get(i);
            String key = rmd.getType() + "-" + rmd.getRef();
            RelationMember rm = membersHash.get(key);
            if (rm != null) {
                int origPos = origMembers.indexOf(rm);
                String newRole = rmd.getRole();
                if (!rm.getRole().equals(newRole)) {
                    changed = true;
                    rm = new RelationMember(rm); 
                    rm.setRole(newRole);
                }
                newMembers.add(rm); 
                if (origPos != i) {
                    changed = true;
                }
                membersHash.remove(key);
            } else { 
                changed = true;
                RelationMember newMember = null;
                OsmElement e = rmd.getElement();
                if (e != null) { 
                    newMember = new RelationMember(rmd.getRole(), e);
                } else {
                    newMember = new RelationMember(rmd.getType(), rmd.getRef(), rmd.getRole());
                }
                newMembers.add(newMember);
            }
        }
        for (RelationMember rm : membersHash.values()) {
            changed = true;
            OsmElement e = rm.getElement();
            if (e != null) {
                undo.save(e);
                e.removeParentRelation(r);
            }
        }
        if (changed) {
            r.replaceMembers(newMembers);
            r.updateState(OsmElement.STATE_MODIFIED);
            apiStorage.insertElementSafe(r);
            onElementChanged(null, r);
        } else {
            undo.remove(r); 
        }
    }
public void updateRelation ( @ NonNull Relation r , @ NonNull List < RelationMemberDescription > members ) { dirty = true ; undo . save ( r ) ; validateRelationMemberCount ( r , members . size ( ) - r . getMemberCount ( ) ) ; boolean changed = false ; List < RelationMember > origMembers = new ArrayList < > ( r . getMembers ( ) ) ; LinkedHashMap < String , RelationMember > membersHash = new LinkedHashMap < > ( ) ; for ( RelationMember rm : r . getMembers ( ) ) { membersHash . put ( rm . getType ( ) + "-" + rm . getRef ( ) , rm ) ; } List < RelationMember > newMembers = new ArrayList < > ( ) ; for ( int i = 0 ; i < members . size ( ) ; i ++ ) { RelationMemberDescription rmd = members . get ( i ) ; String key = rmd . getType ( ) + "-" + rmd . getRef ( ) ; RelationMember rm = membersHash . get ( key ) ; if ( rm != null ) { int origPos = origMembers . indexOf ( rm ) ; String newRole = rmd . getRole ( ) ; if ( ! rm . getRole ( ) . equals ( newRole ) ) { changed = true ; rm = new RelationMember ( rm ) ; rm . setRole ( newRole ) ; } newMembers . add ( rm ) ; if ( origPos != i ) { changed = true ; } membersHash . remove ( key ) ; } else { changed = true ; RelationMember newMember = null ; OsmElement e = rmd . getElement ( ) ; if ( e != null ) { newMember = new RelationMember ( rmd . getRole ( ) , e ) ; } else { newMember = new RelationMember ( rmd . getType ( ) , rmd . getRef ( ) , rmd . getRole ( ) ) ; } newMembers . add ( newMember ) ; } } for ( RelationMember rm : membersHash . values ( ) ) { changed = true ; OsmElement e = rm . getElement ( ) ; if ( e != null ) { undo . save ( e ) ; e . removeParentRelation ( r ) ; } } if ( changed ) { r . replaceMembers ( newMembers ) ; r . updateState ( OsmElement . STATE_MODIFIED ) ; apiStorage . insertElementSafe ( r ) ; onElementChanged ( null , r ) ; } else { undo . remove ( r ) ; } }
Compare current list of relations members to new list and apply the necessary changes
Compare current list of relations members to new list and apply the necessary changes
// allocate new element // existing member simply add to list // new member // downloaded // nothing changed