package net.osmand.data; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; 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; import net.osmand.OsmAndCollator; import net.osmand.util.Algorithms; import net.sf.junidecode.Junidecode; public abstract class MapObject implements Comparable { public static final MapObjectComparator BY_NAME_COMPARATOR = new MapObjectComparator(); public static final MapObjectByIdComparator BY_ID_COMPARATOR = new MapObjectByIdComparator(); protected String name = null; protected String enName = null; protected Map names = null; protected LatLon location = null; protected int fileOffset = 0; protected Long id = null; public void setId(Long id) { this.id = id; } public Long getId() { if (id != null) { return id; } return null; } public String getName() { if (this.name != null) { return this.name; } return ""; //$NON-NLS-1$ } public void setName(String name) { this.name = name; } public void setName(String lang, String name) { if (names == null) { names = new HashMap(); } names.put(lang, name); } public Map getNamesMap(boolean includeEn) { if (!includeEn || Algorithms.isEmpty(enName)) { if (names == null) { return Collections.emptyMap(); } return names; } Map mp = new HashMap(); if (names != null) { mp.putAll(names); } mp.put("en", enName); return mp; } public List getAllNames() { List l = new ArrayList(); if (!Algorithms.isEmpty(enName)) { l.add(enName); } if (names != null) { l.addAll(names.values()); } return l; } public void copyNames(MapObject s) { if (Algorithms.isEmpty(name)) { name = s.name; } if (Algorithms.isEmpty(enName)) { enName = s.enName; } copyNames(s.names); } public void copyNames(Map snames) { if (snames != null && snames.containsKey("name:en")) { enName = snames.get("name:en"); } if (snames != null && snames.containsKey("en")) { enName = snames.get("en"); } if (snames != null) { Iterator> it = snames.entrySet().iterator(); while (it.hasNext()) { Entry e = it.next(); String key = e.getKey(); if (key.startsWith("name:")) { key = key.substring("name:".length()); } if (names == null) { names = new HashMap(); } if (Algorithms.isEmpty(names.get(key))) { names.put(key, e.getValue()); } } } } public String getName(String lang) { return getName(lang, false); } 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 if (names != null) { String nm = names.get(lang); if (!Algorithms.isEmpty(nm)) { return nm; } if (transliterate) { return Junidecode.unidecode(getName()); } } } } return getName(); } public String getEnName(boolean transliterate) { if (!Algorithms.isEmpty(enName)) { return this.enName; } else if (!Algorithms.isEmpty(getName()) && transliterate) { return Junidecode.unidecode(getName()); } return ""; //$NON-NLS-1$ } public void setEnName(String enName) { this.enName = enName; } public LatLon getLocation() { return location; } public void setLocation(double latitude, double longitude) { location = new LatLon(latitude, longitude); } @Override public int compareTo(MapObject o) { return OsmAndCollator.primaryCollator().compare(getName(), o.getName()); } 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; } public static class MapObjectComparator implements Comparator { private final String l; Collator collator = OsmAndCollator.primaryCollator(); public MapObjectComparator() { this.l = null; } public MapObjectComparator(String lang) { this.l = lang; } @Override public int compare(MapObject o1, MapObject o2) { if (o1 == null ^ o2 == null) { return (o1 == null) ? -1 : 1; } else if (o1 == o2) { return 0; } else { return collator.compare(o1.getName(l), o2.getName(l)); } } public boolean areEqual(MapObject o1, MapObject o2) { if (o1 == null) { return o2 == null; } else { return collator.equals(o1.getName(l), o2.getName(l)); } } } public static class MapObjectByIdComparator implements Comparator { @Override public int compare(MapObject o1, MapObject o2) { if (o1 == null ^ o2 == null) { return (o1 == null) ? -1 : 1; } else if (o1 == o2) { return 0; } else { return Double.compare(o1.id, o2.id); } } public boolean areEqual(MapObject o1, MapObject o2) { if (o1 == null) { return o2 == null; } else { return o1.id.equals(o2.id); } } } }