OsmAnd/OsmAnd-java/src/net/osmand/data/MapObject.java

227 lines
4.8 KiB
Java
Raw Normal View History

package net.osmand.data;
2015-06-29 17:26:32 +02:00
import java.util.ArrayList;
2015-06-30 10:31:26 +02:00
import java.util.Collections;
import java.util.Comparator;
2015-06-29 17:26:32 +02:00
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.osmand.Collator;
2014-04-12 23:37:18 +02:00
import net.osmand.OsmAndCollator;
2015-06-29 17:26:32 +02:00
import net.osmand.util.Algorithms;
import net.sf.junidecode.Junidecode;
2015-11-12 10:30:43 +01:00
public abstract class MapObject implements Comparable<MapObject> {
protected String name = null;
protected String enName = null;
2015-06-29 17:26:32 +02:00
protected Map<String, String> names = null;
protected LatLon location = null;
protected int fileOffset = 0;
protected Long id = null;
2016-05-17 16:35:21 +02:00
public static final Comparator<MapObject> comparator = new MapObjectComparator();
public void setId(Long id) {
this.id = id;
}
2016-05-17 13:46:36 +02:00
public Long getId() {
2016-05-17 13:46:36 +02:00
if (id != null) {
return id;
}
return null;
}
2016-05-17 13:46:36 +02:00
public String getName() {
if (this.name != null) {
return this.name;
}
return ""; //$NON-NLS-1$
}
2016-05-17 13:46:36 +02:00
public void setName(String name) {
this.name = name;
}
2016-05-17 13:46:36 +02:00
2015-06-29 17:26:32 +02:00
public void setName(String lang, String name) {
2016-05-17 13:46:36 +02:00
if (names == null) {
2015-06-29 17:26:32 +02:00
names = new HashMap<String, String>();
2016-05-17 13:46:36 +02:00
2015-06-29 17:26:32 +02:00
}
names.put(lang, name);
}
2016-05-17 13:46:36 +02:00
2015-06-30 10:31:26 +02:00
public Map<String, String> getNamesMap(boolean includeEn) {
if (!includeEn || Algorithms.isEmpty(enName)) {
if (names == null) {
return Collections.emptyMap();
}
return names;
}
Map<String, String> mp = new HashMap<String, String>();
2016-05-17 13:46:36 +02:00
if (names != null) {
2015-06-30 10:31:26 +02:00
mp.putAll(names);
}
2016-05-17 13:46:36 +02:00
mp.put("en", enName);
2015-06-30 10:31:26 +02:00
return mp;
}
2016-05-17 13:46:36 +02:00
2015-06-29 17:26:32 +02:00
public List<String> getAllNames() {
List<String> l = new ArrayList<String>();
2016-05-17 13:46:36 +02:00
if (!Algorithms.isEmpty(enName)) {
2015-06-29 17:26:32 +02:00
l.add(enName);
}
2016-05-17 13:46:36 +02:00
if (names != null) {
2015-06-29 17:26:32 +02:00
l.addAll(names.values());
}
return l;
}
2016-05-17 13:46:36 +02:00
2015-06-29 17:26:32 +02:00
public void copyNames(MapObject s) {
2016-05-17 13:46:36 +02:00
if (Algorithms.isEmpty(name)) {
2015-06-29 17:26:32 +02:00
name = s.name;
}
2016-05-17 13:46:36 +02:00
if (Algorithms.isEmpty(enName)) {
2015-06-29 17:26:32 +02:00
enName = s.enName;
}
2015-06-30 10:31:26 +02:00
copyNames(s.names);
}
2016-05-17 13:46:36 +02:00
2015-06-30 10:31:26 +02:00
public void copyNames(Map<String, String> snames) {
2016-05-17 13:46:36 +02:00
if (snames != null && snames.containsKey("name:en")) {
2015-06-30 10:31:26 +02:00
enName = snames.get("name:en");
}
2016-05-17 13:46:36 +02:00
if (snames != null && snames.containsKey("en")) {
2015-06-30 10:31:26 +02:00
enName = snames.get("en");
}
2016-05-17 13:46:36 +02:00
if (snames != null) {
2015-06-30 10:31:26 +02:00
Iterator<Entry<String, String>> it = snames.entrySet().iterator();
2016-05-17 13:46:36 +02:00
while (it.hasNext()) {
2015-06-29 17:26:32 +02:00
Entry<String, String> e = it.next();
2015-06-30 10:31:26 +02:00
String key = e.getKey();
2016-05-17 13:46:36 +02:00
if (key.startsWith("name:")) {
2015-06-30 10:31:26 +02:00
key = key.substring("name:".length());
}
2016-05-17 13:46:36 +02:00
if (names == null) {
2015-06-30 13:16:01 +02:00
names = new HashMap<String, String>();
}
2016-05-17 13:46:36 +02:00
if (Algorithms.isEmpty(names.get(key))) {
2015-06-30 10:31:26 +02:00
names.put(key, e.getValue());
2015-06-29 17:26:32 +02:00
}
}
2016-05-17 13:46:36 +02:00
}
2015-06-29 17:26:32 +02:00
}
2016-05-17 13:46:36 +02:00
2015-06-29 17:26:32 +02:00
public String getName(String lang) {
return getName(lang, false);
}
2016-05-17 13:46:36 +02:00
2015-06-29 17:26:32 +02:00
public String getName(String lang, boolean transliterate) {
if (lang != null) {
if (lang.equals("en")) {
// ignore transliterate option here for backward compatibility
return getEnName(true);
} else {
// get name
2016-05-17 13:46:36 +02:00
if (names != null) {
2015-06-29 17:26:32 +02:00
String nm = names.get(lang);
2016-05-17 13:46:36 +02:00
if (!Algorithms.isEmpty(nm)) {
2015-06-29 17:26:32 +02:00
return nm;
}
2016-05-17 13:46:36 +02:00
if (transliterate) {
2015-06-29 17:26:32 +02:00
return Junidecode.unidecode(getName());
}
}
}
}
return getName();
}
2016-05-17 13:46:36 +02:00
2015-06-29 17:26:32 +02:00
public String getEnName(boolean transliterate) {
2016-05-17 13:46:36 +02:00
if (!Algorithms.isEmpty(enName)) {
return this.enName;
2016-05-17 13:46:36 +02:00
} else if (!Algorithms.isEmpty(getName()) && transliterate) {
2015-06-29 17:26:32 +02:00
return Junidecode.unidecode(getName());
}
return ""; //$NON-NLS-1$
}
2016-05-17 13:46:36 +02:00
public void setEnName(String enName) {
this.enName = enName;
}
2016-05-17 13:46:36 +02:00
public LatLon getLocation() {
return location;
}
2016-05-17 13:46:36 +02:00
public void setLocation(double latitude, double longitude) {
location = new LatLon(latitude, longitude);
}
@Override
public int compareTo(MapObject o) {
2014-04-12 23:37:18 +02:00
return OsmAndCollator.primaryCollator().compare(getName(), o.getName());
}
2016-05-17 13:46:36 +02:00
public int getFileOffset() {
return fileOffset;
}
public void setFileOffset(int fileOffset) {
this.fileOffset = fileOffset;
}
@Override
public String toString() {
return getClass().getSimpleName() + " " + name +"("+id+")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MapObject other = (MapObject) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
2016-05-17 13:46:36 +02:00
public static class MapObjectComparator implements Comparator<MapObject> {
2015-06-29 17:26:32 +02:00
private final String l;
2014-04-12 23:37:18 +02:00
Collator collator = OsmAndCollator.primaryCollator();
2016-05-17 16:35:21 +02:00
public MapObjectComparator() {
this.l = null;
}
public MapObjectComparator(String lang) {
2015-06-29 17:26:32 +02:00
this.l = lang;
}
@Override
public int compare(MapObject o1, MapObject o2) {
2015-06-29 17:26:32 +02:00
return collator.compare(o1.getName(l), o2.getName(l));
}
}
}