OsmAnd/DataExtractionOSM/src/com/osmand/osm/Entity.java
Victor Shcherb 26eb8107f1 refactor relation members
git-svn-id: https://osmand.googlecode.com/svn/trunk@264 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
2010-06-30 14:33:13 +00:00

165 lines
3.1 KiB
Java

package com.osmand.osm;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import com.osmand.osm.OSMSettings.OSMTagKey;
public abstract class Entity {
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;
public EntityId(EntityType type, Long id){
this.type = type;
this.id = id;
}
@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;
}
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;
}
}
// lazy initializing
private Map<String, String> tags = null;
private final long id;
public Entity(long id) {
this.id = id;
}
public long getId() {
return id;
}
public String removeTag(String key){
return tags.remove(key);
}
public String putTag(String key, String value){
if(tags == null){
tags = new LinkedHashMap<String, String>();
}
return tags.put(key, value);
}
public String getTag(OSMTagKey key){
return getTag(key.getValue());
}
public String getTag(String key){
if(tags == null){
return null;
}
return tags.get(key);
}
public Map<String, String> getTags() {
if(tags == null){
return Collections.emptyMap();
}
return Collections.unmodifiableMap(tags);
}
public Collection<String> getTagKeySet(){
if(tags == null){
return Collections.emptyList();
}
return tags.keySet();
}
public abstract void initializeLinks(Map<EntityId, Entity> entities);
/**
* @return middle point for entity
*/
public abstract LatLon getLatLon();
public boolean isVirtual(){
return id < 0;
}
@Override
public int hashCode() {
if (id < 0) {
return System.identityHashCode(this);
}
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;
// virtual are not equal
if(id < 0){
return false;
}
return true;
}
}