2010-04-27 14:37:49 +02:00
|
|
|
package com.osmand.osm;
|
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2010-04-27 23:42:19 +02:00
|
|
|
import com.osmand.osm.OSMSettings.OSMTagKey;
|
|
|
|
|
2010-04-27 14:37:49 +02:00
|
|
|
public abstract class Entity {
|
2010-06-30 16:33:13 +02:00
|
|
|
public enum EntityType {
|
|
|
|
NODE,
|
|
|
|
WAY,
|
|
|
|
RELATION;
|
|
|
|
|
|
|
|
public static EntityType valueOf(Entity e){
|
|
|
|
if(e instanceof Node){
|
|
|
|
return NODE;
|
|
|
|
} else if(e instanceof Way){
|
|
|
|
return WAY;
|
|
|
|
} else if(e instanceof Relation){
|
|
|
|
return RELATION;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class EntityId {
|
|
|
|
private final EntityType type;
|
|
|
|
private final Long id;
|
|
|
|
|
2010-08-04 23:07:15 +02:00
|
|
|
|
2010-06-30 16:33:13 +02:00
|
|
|
public EntityId(EntityType type, Long id){
|
|
|
|
this.type = type;
|
|
|
|
this.id = id;
|
|
|
|
}
|
2010-08-04 23:07:15 +02:00
|
|
|
|
|
|
|
public static EntityId valueOf(Entity e){
|
|
|
|
return new EntityId(EntityType.valueOf(e), e.getId());
|
|
|
|
}
|
2010-06-30 16:33:13 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
final int prime = 31;
|
|
|
|
int result = 1;
|
|
|
|
result = prime * result + ((id == null) ? 0 : id.hashCode());
|
|
|
|
result = prime * result + ((type == null) ? 0 : type.hashCode());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-08-04 23:07:15 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2010-08-16 00:22:24 +02:00
|
|
|
return type + " " + id; //$NON-NLS-1$
|
2010-08-04 23:07:15 +02:00
|
|
|
}
|
|
|
|
|
2010-06-30 16:33:13 +02:00
|
|
|
public EntityType getType() {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Long getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (this == obj)
|
|
|
|
return true;
|
|
|
|
if (obj == null)
|
|
|
|
return false;
|
|
|
|
if (getClass() != obj.getClass())
|
|
|
|
return false;
|
|
|
|
EntityId other = (EntityId) obj;
|
|
|
|
if (id == null) {
|
|
|
|
if (other.id != null)
|
|
|
|
return false;
|
|
|
|
} else if (!id.equals(other.id))
|
|
|
|
return false;
|
|
|
|
if (type == null) {
|
|
|
|
if (other.type != null)
|
|
|
|
return false;
|
|
|
|
} else if (!type.equals(other.type))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-04-27 14:37:49 +02:00
|
|
|
// lazy initializing
|
|
|
|
private Map<String, String> tags = null;
|
|
|
|
private final long id;
|
|
|
|
|
|
|
|
public Entity(long id) {
|
|
|
|
this.id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2010-06-16 17:05:06 +02:00
|
|
|
public String removeTag(String key){
|
|
|
|
return tags.remove(key);
|
|
|
|
}
|
|
|
|
|
2010-04-27 14:37:49 +02:00
|
|
|
public String putTag(String key, String value){
|
|
|
|
if(tags == null){
|
|
|
|
tags = new LinkedHashMap<String, String>();
|
|
|
|
}
|
|
|
|
return tags.put(key, value);
|
|
|
|
}
|
|
|
|
|
2010-04-27 23:42:19 +02:00
|
|
|
public String getTag(OSMTagKey key){
|
|
|
|
return getTag(key.getValue());
|
|
|
|
}
|
|
|
|
|
2010-04-27 14:37:49 +02:00
|
|
|
public String getTag(String key){
|
|
|
|
if(tags == null){
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return tags.get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, String> getTags() {
|
2010-04-27 22:37:40 +02:00
|
|
|
if(tags == null){
|
|
|
|
return Collections.emptyMap();
|
|
|
|
}
|
2010-04-27 14:37:49 +02:00
|
|
|
return Collections.unmodifiableMap(tags);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Collection<String> getTagKeySet(){
|
|
|
|
if(tags == null){
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
return tags.keySet();
|
|
|
|
}
|
|
|
|
|
2010-06-30 16:33:13 +02:00
|
|
|
public abstract void initializeLinks(Map<EntityId, Entity> entities);
|
2010-04-27 14:37:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return middle point for entity
|
|
|
|
*/
|
|
|
|
public abstract LatLon getLatLon();
|
|
|
|
|
2010-05-16 09:22:09 +02:00
|
|
|
|
|
|
|
public boolean isVirtual(){
|
|
|
|
return id < 0;
|
|
|
|
}
|
2010-04-27 14:37:49 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
2010-05-16 09:22:09 +02:00
|
|
|
if (id < 0) {
|
|
|
|
return System.identityHashCode(this);
|
|
|
|
}
|
2010-04-27 14:37:49 +02:00
|
|
|
return (int) id;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
if (this == obj)
|
|
|
|
return true;
|
|
|
|
if (obj == null)
|
|
|
|
return false;
|
|
|
|
if (getClass() != obj.getClass())
|
|
|
|
return false;
|
|
|
|
Entity other = (Entity) obj;
|
|
|
|
if (id != other.id)
|
|
|
|
return false;
|
2010-05-16 09:22:09 +02:00
|
|
|
// virtual are not equal
|
|
|
|
if(id < 0){
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-27 14:37:49 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|