Merge remote-tracking branch 'origin/master'

This commit is contained in:
Weblate 2016-08-25 01:13:05 +02:00
commit 254d9a90bb
4 changed files with 97 additions and 74 deletions

View file

@ -54,7 +54,7 @@ public abstract class Entity implements Serializable {
result = prime * result + ((type == null) ? 0 : type.hashCode()); result = prime * result + ((type == null) ? 0 : type.hashCode());
return result; return result;
} }
@Override @Override
public String toString() { public String toString() {
return type + " " + id; //$NON-NLS-1$ return type + " " + id; //$NON-NLS-1$

View file

@ -17,6 +17,7 @@ import net.osmand.osm.MapPoiTypes;
import net.osmand.osm.MapRenderingTypes; import net.osmand.osm.MapRenderingTypes;
import net.osmand.osm.edit.Entity.EntityType; import net.osmand.osm.edit.Entity.EntityType;
import net.osmand.osm.edit.OSMSettings.OSMTagKey; import net.osmand.osm.edit.OSMSettings.OSMTagKey;
import net.osmand.osm.edit.Relation.RelationMember;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
public class EntityParser { public class EntityParser {
@ -73,9 +74,9 @@ public class EntityParser {
nodes = ((Way) e).getNodes(); nodes = ((Way) e).getNodes();
} else if (e instanceof Relation) { } else if (e instanceof Relation) {
nodes = new ArrayList<Node>(); nodes = new ArrayList<Node>();
for (Entity member : ((Relation) e).getMembers(null)) { for (RelationMember member : ((Relation) e).getMembers(null)) {
if (member instanceof Way) { if (member.getEntity() instanceof Way) {
nodes.addAll(((Way) member).getNodes()); nodes.addAll(((Way) member.getEntity()).getNodes());
} }
} }
} }

View file

@ -7,6 +7,7 @@ import java.util.Comparator;
import java.util.List; import java.util.List;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.osm.edit.Relation.RelationMember;
import net.osmand.util.MapAlgorithms; import net.osmand.util.MapAlgorithms;
import net.osmand.util.MapUtils; import net.osmand.util.MapUtils;
@ -31,11 +32,11 @@ public class OsmMapUtils {
return getWeightCenterForWay(((Way) e)); return getWeightCenterForWay(((Way) e));
} else if (e instanceof Relation) { } else if (e instanceof Relation) {
List<LatLon> list = new ArrayList<LatLon>(); List<LatLon> list = new ArrayList<LatLon>();
for (Entity fe : ((Relation) e).getMembers(null)) { for (RelationMember fe : ((Relation) e).getMembers()) {
LatLon c = null; LatLon c = null;
// skip relations to avoid circular dependencies // skip relations to avoid circular dependencies
if (!(fe instanceof Relation)) { if (!(fe.getEntity() instanceof Relation) && fe.getEntity() != null) {
c = getCenter(fe); c = getCenter(fe.getEntity());
} }
if (c != null) { if (c != null) {
list.add(c); list.add(c);

View file

@ -3,18 +3,51 @@ package net.osmand.osm.edit;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.osm.edit.Entity.EntityId;
import net.osmand.osm.edit.Relation.RelationMember;
public class Relation extends Entity { public class Relation extends Entity {
// lazy loading public static class RelationMember {
Map<EntityId, String> members = null; private EntityId entityId;
Map<Entity, String> memberEntities = null; private Entity entity;
private String role;
public RelationMember(EntityId entityId, String role) {
this.entityId = entityId;
this.role = role;
}
public EntityId getEntityId() {
if(entityId == null && entity != null) {
return EntityId.valueOf(entity);
}
return entityId;
}
public String getRole() {
return role;
}
public Entity getEntity() {
return entity;
}
@Override
public String toString() {
return entityId.toString() + " " + role;
}
}
// lazy loading
List<RelationMember> members = null;
public Relation(long id) { public Relation(long id) {
super(id); super(id);
@ -22,94 +55,56 @@ public class Relation extends Entity {
public void addMember(Long id, EntityType type, String role){ public void addMember(Long id, EntityType type, String role){
if(members == null){ if(members == null){
members = new LinkedHashMap<EntityId, String>(); members = new ArrayList<>();
} }
members.put(new EntityId(type, id), role); members.add(new RelationMember(new EntityId(type, id), role));
} }
public String removeMember(EntityType e, Long id){ public List<RelationMember> getMembers(String role) {
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<EntityId> getMemberIds() {
return getMemberIds(null);
}
public Map<EntityId, String> getMembersMap() {
if(members == null){
return Collections.emptyMap();
}
return Collections.unmodifiableMap(members);
}
public Map<EntityId, String> getModifiableMembersMap() {
if(members == null){
return Collections.emptyMap();
}
return members;
}
public Collection<EntityId> getMemberIds(String role) {
if (members == null) { if (members == null) {
return Collections.emptyList(); return Collections.emptyList();
} }
if (role == null) { if (role == null) {
return members.keySet(); return members;
} }
List<EntityId> l = new ArrayList<EntityId>(); List<RelationMember> l = new ArrayList<>();
for (EntityId m : members.keySet()) { for (RelationMember m : members) {
if (role.equals(members.get(m))) { if (role.equals(m.role)) {
l.add(m); l.add(m);
} }
} }
return l; return l;
} }
public Map<Entity, String> getMemberEntities() { public List<Entity> getMemberEntities(String role) {
if(memberEntities == null){ if (members == null) {
return Collections.emptyMap();
}
return memberEntities;
}
public Collection<Entity> getMembers(String role) {
if (memberEntities == null) {
return Collections.emptyList(); return Collections.emptyList();
} }
if (role == null) { List<Entity> l = new ArrayList<>();
return memberEntities.keySet(); for (RelationMember m : members) {
} if (role == null || role.equals(m.role)) {
List<Entity> l = new ArrayList<Entity>(); if(m.entity != null) {
for (Entity m : memberEntities.keySet()) { l.add(m.entity);
if (role.equals(memberEntities.get(m))) { }
l.add(m);
} }
} }
return l; return l;
} }
public List<RelationMember> getMembers() {
if(members == null){
return Collections.emptyList();
}
return members;
}
@Override @Override
public void initializeLinks(Map<EntityId, Entity> entities){ public void initializeLinks(Map<EntityId, Entity> entities){
if (members != null) { if (members != null) {
if(memberEntities == null){ for(RelationMember rm : members) {
memberEntities = new LinkedHashMap<Entity, String>(); if(rm.entityId != null && entities.containsKey(rm.entityId)) {
} else { rm.entity = entities.get(rm.entityId);
memberEntities.clear();
}
for(EntityId l : members.keySet()){
if(l != null && entities.get(l) != null){
memberEntities.put(entities.get(l), members.get(l));
} }
} }
} }
@ -121,4 +116,30 @@ public class Relation extends Entity {
return null; return null;
} }
public boolean remove(EntityId key) {
if(members != null) {
Iterator<RelationMember> it = members.iterator();
while(it.hasNext()) {
RelationMember rm = it.next();
if(key.equals(rm.getEntityId())) {
return true;
}
}
}
return false;
}
public boolean remove(RelationMember key) {
if(members != null) {
Iterator<RelationMember> it = members.iterator();
while(it.hasNext()) {
RelationMember rm = it.next();
if(rm == key) {
return true;
}
}
}
return false;
}
} }