From a73f88b326130d2b35291628c9a24ab33e4957aa Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Sun, 25 Oct 2015 16:51:29 +0100 Subject: [PATCH] STart of refactoring --- .../src/net/osmand/map/OsmandRegions.java | 181 ++++++++---------- OsmAnd/src/net/osmand/plus/WorldRegion.java | 63 ++---- 2 files changed, 98 insertions(+), 146 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/map/OsmandRegions.java b/OsmAnd-java/src/net/osmand/map/OsmandRegions.java index 94e381464c..9ed6c79ba6 100644 --- a/OsmAnd-java/src/net/osmand/map/OsmandRegions.java +++ b/OsmAnd-java/src/net/osmand/map/OsmandRegions.java @@ -24,6 +24,7 @@ import net.osmand.binary.BinaryMapDataObject; import net.osmand.binary.BinaryMapIndexReader; import net.osmand.binary.BinaryMapIndexReader.MapIndex; import net.osmand.binary.BinaryMapIndexReader.TagValuePair; +import net.osmand.data.LatLon; import net.osmand.data.QuadRect; import net.osmand.data.QuadTree; import net.osmand.util.Algorithms; @@ -33,42 +34,51 @@ import net.osmand.util.MapUtils; public class OsmandRegions { private static final String MAP_TYPE = "region_map"; -// regionSrtm = object.getMapIndex().getRule("region_srtm", null); -// regionWiki = object.getMapIndex().getRule("region_wiki", null); -// regionRoads = object.getMapIndex().getRule("region_roads", null); -// regionHillshade = object.getMapIndex().getRule("region_hillshade", null); + + public static final String FIELD_LEFT_HAND_DRIVING = "left_hand_driving"; + public static final String FIELD_DOWNLOAD_NAME = "download_name"; + public static final String FIELD_NAME = "name"; + public static final String FIELD_NAME_EN = "name:en"; + public static final String FIELD_REGION_PARENT_NAME = "region_parent_name"; + public static final String FIELD_REGION_FULL_NAME = "region_full_name"; + public static final String FIELD_LANG = "lang"; + public static final String FIELD_METRIC = "metric"; + public static final String FIELD_ROAD_SIGNS = "road_signs"; private BinaryMapIndexReader reader; + + Map fullNamesToRegionData = new HashMap(); Map> countriesByDownloadName = new HashMap>(); - Map fullNamesToLocaleNames = new HashMap(); - Map fullNamesNoParentToLocaleNames = new HashMap(); - Map fullMapNamesToDownloadNames = new HashMap(); Map downloadNamesToFullNames = new HashMap(); - Map fullNamesToLowercaseIndex = new HashMap(); - Map fullNamesToParentFullNames = new HashMap(); - Map fullNamesToDownloadNames = new HashMap(); - Map fullNamesToLangs = new HashMap(); - Map fullNamesToMetrics = new HashMap(); - Map fullNamesToLeftHandDrivings = new HashMap(); - Map fullNamesToRoadSigns = new HashMap(); +// Map fullNamesToLocaleNames = new HashMap(); +// Map fullNamesNoParentToLocaleNames = new HashMap(); +// Map fullMapNamesToDownloadNames = new HashMap(); +// Map fullNamesToLowercaseIndex = new HashMap(); +// Map fullNamesToParentFullNames = new HashMap(); +// Map fullNamesToDownloadNames = new HashMap(); +// Map fullNamesToLangs = new HashMap(); +// Map fullNamesToMetrics = new HashMap(); +// Map fullNamesToLeftHandDrivings = new HashMap(); +// Map fullNamesToRoadSigns = new HashMap(); QuadTree quadTree = null ; + String locale = "en"; + MapIndexFields mapIndexFields = new MapIndexFields(); - public Map getFullNamesToLowercaseCopy() { - return new HashMap(fullNamesToLowercaseIndex); + private class MapIndexFields { + MapIndex mapIndex; + Integer parentFullName = null; + Integer fullNameType = null; + Integer downloadNameType = null; + Integer nameEnType = null; + Integer nameType = null; + Integer nameLocaleType = null; + Integer langType = null; + Integer metricType = null; + Integer leftHandDrivingType = null; + Integer roadSignsType = null; } - Integer parentFullName = null; - Integer fullNameType = null; - Integer downloadNameType = null; - Integer nameEnType = null; - Integer nameType = null; - Integer nameLocaleType = null; - String locale = "en"; - Integer langType = null; - Integer metricType = null; - Integer leftHandDrivingType = null; - Integer roadSignsType = null; public void prepareFile(String fileName) throws IOException { @@ -80,64 +90,6 @@ public class OsmandRegions { return countriesByDownloadName.containsKey(name); } - public String getLang(String fullName) { - return fullNamesToLangs.get(fullName); - } - - public String getMetric(String fullName) { - return fullNamesToMetrics.get(fullName); - } - - public String getLeftHandDriving(String fullName) { - return fullNamesToLeftHandDrivings.get(fullName); - } - - public String getRoadSigns(String fullName) { - return fullNamesToRoadSigns.get(fullName); - } - - private String getLang(BinaryMapDataObject o) { - if (langType != null) { - return o.getNameByType(langType); - } else { - return null; - } - } - - private String getMetric(BinaryMapDataObject o) { - if (metricType != null) { - return o.getNameByType(metricType); - } else { - return null; - } - } - - private String getLeftHandDriving(BinaryMapDataObject o) { - if (leftHandDrivingType != null) { - return o.getNameByType(leftHandDrivingType); - } else { - return null; - } - } - - private String getRoadSigns(BinaryMapDataObject o) { - if (roadSignsType != null) { - return o.getNameByType(roadSignsType); - } else { - return null; - } - } - - public String getDownloadName(BinaryMapDataObject o) { - if(downloadNameType == null) { - return null; - } - return o.getNameByType(downloadNameType); - } - - public Integer getNameEnType() { - return nameEnType; - } public String getLocaleName(String downloadName, boolean includingParent) { final String lc = downloadName.toLowerCase(); @@ -586,22 +538,20 @@ public class OsmandRegions { private void initTypes(BinaryMapDataObject object) { - if (downloadNameType == null) { - downloadNameType = object.getMapIndex().getRule("download_name", null); - nameType = object.getMapIndex().getRule("name", null); - nameEnType = object.getMapIndex().getRule("name:en", null); - nameLocaleType = object.getMapIndex().getRule("name:" + locale, null); - parentFullName = object.getMapIndex().getRule("region_parent_name", null); - fullNameType = object.getMapIndex().getRule("region_full_name", null); - - langType = object.getMapIndex().getRule("lang", null); - metricType = object.getMapIndex().getRule("metric", null); - leftHandDrivingType = object.getMapIndex().getRule("left_hand_driving", null); - roadSignsType = object.getMapIndex().getRule("road_signs", null); - - if (downloadNameType == null || nameType == null) { - throw new IllegalStateException(); - } + if (mapIndexFields == null) { + mapIndexFields = new MapIndexFields(); + mapIndexFields.mapIndex = object.getMapIndex(); + mapIndexFields.downloadNameType = object.getMapIndex().getRule(FIELD_DOWNLOAD_NAME, null); + mapIndexFields.nameType = object.getMapIndex().getRule(FIELD_NAME, null); + mapIndexFields.nameEnType = object.getMapIndex().getRule(FIELD_NAME_EN, null); + mapIndexFields.nameLocaleType = object.getMapIndex().getRule(FIELD_NAME+":"+locale, null); + mapIndexFields.parentFullName = object.getMapIndex().getRule(FIELD_REGION_PARENT_NAME, null); + mapIndexFields.fullNameType = object.getMapIndex().getRule(FIELD_REGION_FULL_NAME, null); + mapIndexFields.langType = object.getMapIndex().getRule(FIELD_LANG, null); + mapIndexFields.metricType = object.getMapIndex().getRule(FIELD_METRIC, null); + mapIndexFields.leftHandDrivingType = object.getMapIndex().getRule(FIELD_LEFT_HAND_DRIVING, null); + mapIndexFields.roadSignsType = object.getMapIndex().getRule(FIELD_ROAD_SIGNS, null); + mapIndexFields.nameType = object.getMapIndex().getRule(FIELD_NAME, null); } } @@ -649,5 +599,32 @@ public class OsmandRegions { } - + + public static class RegionData { + String regionId; + private String downloadsId; + private String name; + private String searchText; + private LatLon center; + + public LatLon getCenter() { + return center; + } + + public String getSearchText() { + return searchText; + } + + public String getName() { + return name; + } + + public String getRegionId() { + return regionId; + } + + public String getDownloadsId() { + return downloadsId; + } + } } diff --git a/OsmAnd/src/net/osmand/plus/WorldRegion.java b/OsmAnd/src/net/osmand/plus/WorldRegion.java index f3f7910870..bc6d0a531e 100644 --- a/OsmAnd/src/net/osmand/plus/WorldRegion.java +++ b/OsmAnd/src/net/osmand/plus/WorldRegion.java @@ -30,43 +30,23 @@ public class WorldRegion { private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(WorldRegion.class); // Region data - private String regionId; - private String downloadsId; - private String name; - private String searchText; - private LatLon center; + private OsmandRegions.RegionData regionData; // Hierarchy private WorldRegion superregion; private List subregions; private List flattenedSubregions; - - public String getLang(OsmandRegions osmandRegions) { - return osmandRegions.getLang(regionId); - } - - public String getMetric(OsmandRegions osmandRegions) { - return osmandRegions.getMetric(regionId); - } - - public String getLeftHandDriving(OsmandRegions osmandRegions) { - return osmandRegions.getLeftHandDriving(regionId); - } - - public String getRoadSigns(OsmandRegions osmandRegions) { - return osmandRegions.getRoadSigns(regionId); - } - - public String getRegionId() { - return regionId; - } - - public String getDownloadsId() { - return downloadsId; + + public OsmandRegions.RegionData getRegionData() { + return regionData; } public String getName() { - return name; + return regionData.getName(); + } + + private String getRegionId() { + return regionData.getRegionId(); } public WorldRegion getSuperregion() { @@ -88,12 +68,12 @@ public class WorldRegion { WorldRegion that = (WorldRegion) o; - return !(name != null ? !name.toLowerCase().equals(that.name.toLowerCase()) : that.name != null); + return !(getName() != null ? !getName().toLowerCase().equals(that.getName().toLowerCase()) : that.getName() != null); } @Override public int hashCode() { - return name != null ? name.hashCode() : 0; + return getName() != null ? getName().hashCode() : 0; } public WorldRegion() { @@ -129,9 +109,6 @@ public class WorldRegion { return this; } - public String getSearchText() { - return searchText; - } private void addSubregion(WorldRegion subregion, WorldRegion world) { subregion.superregion = this; @@ -167,27 +144,27 @@ public class WorldRegion { WorldRegion centralAmericaRegion = createRegionAs(CENTRAL_AMERICA_REGION_ID, loadedItems, osmandRegions, res.getString(R.string.index_name_central_america)); addSubregion(centralAmericaRegion, this); - regionsLookupTable.put(centralAmericaRegion.regionId, centralAmericaRegion); + regionsLookupTable.put(centralAmericaRegion.getRegionId(), centralAmericaRegion); WorldRegion europeRegion = createRegionAs(EUROPE_REGION_ID, loadedItems, osmandRegions, res.getString(R.string.index_name_europe)); addSubregion(europeRegion, this); - regionsLookupTable.put(europeRegion.regionId, europeRegion); + regionsLookupTable.put(europeRegion.getRegionId(), europeRegion); WorldRegion northAmericaRegion = createRegionAs(NORTH_AMERICA_REGION_ID, loadedItems, osmandRegions, res.getString(R.string.index_name_north_america)); addSubregion(northAmericaRegion, this); - regionsLookupTable.put(northAmericaRegion.regionId, northAmericaRegion); + regionsLookupTable.put(northAmericaRegion.getRegionId(), northAmericaRegion); WorldRegion russiaRegion = createRegionAs(RUSSIA_REGION_ID, loadedItems, osmandRegions, res.getString(R.string.index_name_russia)); addSubregion(russiaRegion, this); - regionsLookupTable.put(russiaRegion.regionId, russiaRegion); + regionsLookupTable.put(russiaRegion.getRegionId(), russiaRegion); WorldRegion southAmericaRegion = createRegionAs(SOUTH_AMERICA_REGION_ID, loadedItems, osmandRegions, res.getString(R.string.index_name_south_america)); addSubregion(southAmericaRegion, this); - regionsLookupTable.put(southAmericaRegion.regionId, southAmericaRegion); + regionsLookupTable.put(southAmericaRegion.getRegionId(), southAmericaRegion); // Process all regions for (; ; ) { @@ -208,7 +185,7 @@ public class WorldRegion { WorldRegion newRegion = new WorldRegion().init(regionId, osmandRegions, null); parentRegion.addSubregion(newRegion, this); - regionsLookupTable.put(newRegion.regionId, newRegion); + regionsLookupTable.put(newRegion.getRegionId(), newRegion); // Remove processedRegions++; @@ -252,10 +229,6 @@ public class WorldRegion { return worldRegion; } - public LatLon getCenter() { - // TODO - return center; - } private String capitalize(String s) { String[] words = s.split(" "); @@ -286,4 +259,6 @@ public class WorldRegion { } + + } \ No newline at end of file