package net.osmand.osm.edit; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import net.osmand.data.LatLon; public class Relation extends Entity { // lazy loading Map members = null; Map memberEntities = null; public Relation(long id) { super(id); } public void addMember(Long id, EntityType type, String role){ if(members == null){ members = new LinkedHashMap(); } members.put(new EntityId(type, id), role); } public String removeMember(EntityType e, Long id){ if(members == null){ return null; } return members.remove(id); } public String getRole(Entity e){ return members.get(e.getId()); } public String getRole(Long id){ return members.get(id); } public Collection getMemberIds() { return getMemberIds(null); } public Map getMembersMap() { if(members == null){ return Collections.emptyMap(); } return Collections.unmodifiableMap(members); } public Collection getMemberIds(String role) { if (members == null) { return Collections.emptyList(); } if (role == null) { return members.keySet(); } List l = new ArrayList(); for (EntityId m : members.keySet()) { if (role.equals(members.get(m))) { l.add(m); } } return l; } public Map getMemberEntities() { if(memberEntities == null){ return Collections.emptyMap(); } return memberEntities; } public Collection getMembers(String role) { if (memberEntities == null) { return Collections.emptyList(); } if (role == null) { return memberEntities.keySet(); } List l = new ArrayList(); for (Entity m : memberEntities.keySet()) { if (role.equals(memberEntities.get(m))) { l.add(m); } } return l; } @Override public void initializeLinks(Map entities){ if (members != null) { if(memberEntities == null){ memberEntities = new LinkedHashMap(); } else { memberEntities.clear(); } for(EntityId l : members.keySet()){ if(l != null && entities.get(l) != null){ memberEntities.put(entities.get(l), members.get(l)); } } } } @Override public LatLon getLatLon() { return null; } }