diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AdditionalInfoBundle.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AdditionalInfoBundle.java index f61dd18cec4..7c94d010081 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AdditionalInfoBundle.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AdditionalInfoBundle.java @@ -49,7 +49,7 @@ public AdditionalInfoBundle(@NonNull OsmandApplication app, @NonNull public Map getFilteredLocalizedInfo() { if (localizedAdditionalInfo == null) { - localizedAdditionalInfo = MergeLocalizedTagsAlgorithm.execute(app, getFilteredInfo()); + localizedAdditionalInfo = MergeLocalizedTagsAlgorithm.Companion.execute(app, getFilteredInfo()); } return localizedAdditionalInfo; } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/MergeLocalizedTagsAlgorithm.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/MergeLocalizedTagsAlgorithm.java deleted file mode 100644 index cfa623e5691..00000000000 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/MergeLocalizedTagsAlgorithm.java +++ /dev/null @@ -1,145 +0,0 @@ -package net.osmand.plus.mapcontextmenu.builders; - -import androidx.annotation.NonNull; - -import net.osmand.osm.AbstractPoiType; -import net.osmand.osm.MapPoiTypes; -import net.osmand.plus.OsmandApplication; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class MergeLocalizedTagsAlgorithm { - - private static final List NAME_TAG_PREFIXES = Arrays.asList( - "name", "int_name", "nat_name", "reg_name", "loc_name", - "old_name", "alt_name", "short_name", "official_name", "lock_name" - ); - - private final OsmandApplication app; - - private MergeLocalizedTagsAlgorithm(@NonNull OsmandApplication app) { - this.app = app; - } - - @NonNull - public static Map execute(@NonNull OsmandApplication app, - @NonNull Map originalDict) { - MergeLocalizedTagsAlgorithm instance = new MergeLocalizedTagsAlgorithm(app); - return instance.executeImpl(new HashMap<>(originalDict)); - } - - @NonNull - private Map executeImpl(@NonNull Map originalDict) { - Map resultDict = new HashMap<>(); - Map> localizationsDict = new HashMap<>(); - - for (String key : originalDict.keySet()) { - processAdditionalTypeWithKey(key, convertKey(key), originalDict, localizationsDict, resultDict); - } - - List keysToUpdate = findKeysToUpdate(localizationsDict); - for (String baseKey : keysToUpdate) { - Map localizations = localizationsDict.get(baseKey); - if (localizations != null) { - localizations.put(baseKey, originalDict.get(baseKey)); - } - } - Map finalDict = finalizeLocalizationDict(localizationsDict); - addRemainingEntriesFrom(resultDict, finalDict); - return finalDict; - } - - private void processNameTagWithKey(@NonNull String key, - @NonNull String convertedKey, - @NonNull Map originalDict, - @NonNull Map> localizationsDict) { - if (key.contains(":")) { - String[] components = convertedKey.split(":"); - if (components.length == 2) { - String baseKey = components[0]; - String localeKey = baseKey + ":" + components[1]; - - Map nameDict = dictionaryForKey("name", localizationsDict); - nameDict.put(localeKey, originalDict.get(convertedKey)); - } - } else { - Map nameDict = dictionaryForKey("name", localizationsDict); - nameDict.put(convertedKey, originalDict.get(key)); - } - } - - private void processAdditionalTypeWithKey(@NonNull String key, - @NonNull String convertedKey, - @NonNull Map originalDict, - @NonNull Map> localizationsDict, - @NonNull Map resultDict) { - MapPoiTypes poiTypes = app.getPoiTypes(); - AbstractPoiType poiType = poiTypes.getAnyPoiAdditionalTypeByKey(convertedKey); - if (poiType != null && poiType.getLang() != null && key.contains(":")) { - String[] components = key.split(":"); - if (components.length == 2) { - String baseKey = components[0]; - String localeKey = baseKey + ":" + components[1]; - - Map baseDict = dictionaryForKey(baseKey, localizationsDict); - baseDict.put(localeKey, originalDict.get(key)); - } - } else { - resultDict.put(key, originalDict.get(key)); - } - } - - @NonNull - private Map dictionaryForKey(@NonNull String key, @NonNull Map> dict) { - return dict.computeIfAbsent(key, k -> new HashMap<>()); - } - - @NonNull - private List findKeysToUpdate(@NonNull Map> localizationsDict) { - List keysToUpdate = new ArrayList<>(); - for (String baseKey : localizationsDict.keySet()) { - Map localizations = localizationsDict.get(baseKey); - if (localizations != null && !localizations.containsKey(baseKey)) { - keysToUpdate.add(baseKey); - } - } - return keysToUpdate; - } - - @NonNull - private Map finalizeLocalizationDict(@NonNull Map> localizationsDict) { - Map finalDict = new HashMap<>(); - - for (String baseKey : localizationsDict.keySet()) { - Map entryDict = new HashMap<>(); - Map localizations = localizationsDict.get(baseKey); - entryDict.put("localizations", localizations); - finalDict.put(baseKey, entryDict); - } - return finalDict; - } - - private void addRemainingEntriesFrom(@NonNull Map resultDict, - @NonNull Map finalDict) { - for (String key : resultDict.keySet()) { - finalDict.putIfAbsent(key, resultDict.get(key)); - } - } - - public boolean isNameTag(String tag) { - for (String prefix : NAME_TAG_PREFIXES) { - if (tag.startsWith(prefix)) { - return true; - } - } - return false; - } - - private String convertKey(String key) { - return key.replace("_-_", ":"); - } -} diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/MergeLocalizedTagsAlgorithm.kt b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/MergeLocalizedTagsAlgorithm.kt new file mode 100644 index 00000000000..8863503dfd8 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/MergeLocalizedTagsAlgorithm.kt @@ -0,0 +1,140 @@ +package net.osmand.plus.mapcontextmenu.builders + +import net.osmand.osm.AbstractPoiType +import net.osmand.osm.MapPoiTypes +import net.osmand.plus.OsmandApplication + +private val NAME_TAG_PREFIXES = listOf( + "name", "int_name", "nat_name", "reg_name", "loc_name", + "old_name", "alt_name", "short_name", "official_name", "lock_name" +) + +class MergeLocalizedTagsAlgorithm private constructor(private val app: OsmandApplication) { + + companion object { + fun execute( + app: OsmandApplication, + originalDict: Map + ): Map { + val instance = MergeLocalizedTagsAlgorithm(app) + return instance.executeImpl(HashMap(originalDict)) + } + } + + private fun executeImpl(originalDict: Map): Map { + val resultDict = mutableMapOf() + val localizationsDict = mutableMapOf>() + + for (key in originalDict.keys) { + processAdditionalTypeWithKey( + key, convertKey(key), originalDict, localizationsDict, resultDict + ) + } + + val keysToUpdate = findKeysToUpdate(localizationsDict) + for (baseKey in keysToUpdate) { + val localizations = localizationsDict[baseKey] + localizations?.put(baseKey, originalDict[baseKey] ?: continue) + } + + val finalDict = finalizeLocalizationDict(localizationsDict) + addRemainingEntriesFrom(resultDict, finalDict) + return finalDict + } + + private fun processNameTagWithKey( + key: String, + convertedKey: String, + originalDict: Map, + localizationsDict: MutableMap> + ) { + if (key.contains(":")) { + val components = convertedKey.split(":") + if (components.size == 2) { + val baseKey = components[0] + val localeKey = "$baseKey:${components[1]}" + + val nameDict = dictionaryForKey("name", localizationsDict) + nameDict[localeKey] = originalDict[convertedKey] ?: return + } + } else { + val nameDict = dictionaryForKey("name", localizationsDict) + nameDict[convertedKey] = originalDict[key] ?: return + } + } + + private fun processAdditionalTypeWithKey( + key: String, + convertedKey: String, + originalDict: Map, + localizationsDict: MutableMap>, + resultDict: MutableMap + ) { + val poiTypes: MapPoiTypes = app.poiTypes + val poiType: AbstractPoiType? = poiTypes.getAnyPoiAdditionalTypeByKey(convertedKey) + if (poiType?.lang != null && key.contains(":")) { + val components = key.split(":") + if (components.size == 2) { + val baseKey = components[0] + val localeKey = "$baseKey:${components[1]}" + + val baseDict = dictionaryForKey(baseKey, localizationsDict) + baseDict[localeKey] = originalDict[key] ?: return + } + } else { + resultDict[key] = originalDict[key] ?: return + } + } + + private fun dictionaryForKey( + key: String, + dict: MutableMap> + ): MutableMap { + return dict.getOrPut(key) { mutableMapOf() } + } + + private fun findKeysToUpdate(localizationsDict: Map>): List { + val keysToUpdate = mutableListOf() + for (baseKey in localizationsDict.keys) { + val localizations = localizationsDict[baseKey] + if (localizations != null && !localizations.containsKey(baseKey)) { + keysToUpdate.add(baseKey) + } + } + return keysToUpdate + } + + private fun finalizeLocalizationDict(localizationsDict: Map>): MutableMap { + val finalDict = mutableMapOf() + + for (baseKey in localizationsDict.keys) { + val entryDict = mutableMapOf() + val localizations = localizationsDict[baseKey] + entryDict["localizations"] = localizations ?: continue + finalDict[baseKey] = entryDict + } + return finalDict + } + + private fun addRemainingEntriesFrom( + resultDict: Map, + finalDict: MutableMap + ) { + for (key in resultDict.keys) { + finalDict.putIfAbsent(key, resultDict[key] ?: continue) + } + } + + fun isNameTag(tag: String): Boolean { + for (prefix in NAME_TAG_PREFIXES) { + if (tag.startsWith(prefix)) { + return true + } + } + return false + } + + private fun convertKey(key: String): String { + return key.replace("_-_", ":") + } +}