Refactor & hide internal method about additional info

This commit is contained in:
Victor Shcherb 2020-11-19 15:58:31 +01:00
parent bcea2e4e0e
commit 3fc2dd89b9
5 changed files with 46 additions and 18 deletions

View file

@ -11,8 +11,6 @@ import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.osmand.Collator;
import net.osmand.CollatorStringMatcher;
@ -576,13 +574,12 @@ public class BinaryMapPoiReaderAdapter {
}
}
if (!matches) {
Map<String, String> lt = am.getAdditionalInfo();
for (Entry<String, String> e : lt.entrySet()) {
if(!e.getKey().contains("_name") &&
!e.getKey().equals("brand")) {
for (String key : am.getAdditionalInfoKeys()) {
if(!key.contains("_name") &&
!key.equals("brand")) {
continue;
}
matches = matcher.matches(e.getValue());
matches = matcher.matches(am.getAdditionalInfo(key));
if (matches) {
break;
}
@ -812,7 +809,6 @@ public class BinaryMapPoiReaderAdapter {
}
private boolean checkCategories(SearchRequest<Amenity> req, PoiRegion region) throws IOException {
StringBuilder subType = new StringBuilder();
while (true) {
int t = codedIS.readTag();
int tag = WireFormat.getTagFieldNumber(t);

View file

@ -91,13 +91,40 @@ public class Amenity extends MapObject {
}
public Map<String, String> getAdditionalInfo() {
// this method should be used carefully
public Map<String, String> getInternalAdditionalInfoMap() {
if (additionalInfo == null) {
return Collections.emptyMap();
}
return additionalInfo;
}
public Collection<String> getAdditionalInfoValues(boolean excludeZipped) {
if (additionalInfo == null) {
return Collections.emptyList();
}
boolean zipped = false;
for(String v : additionalInfo.values()) {
if(isContentZipped(v)) {
zipped = true;
break;
}
}
if(zipped) {
List<String> r = new ArrayList<>(additionalInfo.size());
for(String str : additionalInfo.values()) {
if(excludeZipped && isContentZipped(str)) {
} else {
r.add(unzipContent(str));
}
}
return r;
} else {
return additionalInfo.values();
}
}
public Collection<String> getAdditionalInfoKeys() {
if (additionalInfo == null) {
return Collections.emptyList();
@ -184,7 +211,7 @@ public class Amenity extends MapObject {
}
int maxLen = 0;
String lng = defLang;
for (String nm : getAdditionalInfo().keySet()) {
for (String nm : getAdditionalInfoKeys()) {
if (nm.startsWith(tag + ":")) {
String key = nm.substring(tag.length() + 1);
String cnt = getAdditionalInfo(tag + ":" + key);
@ -206,7 +233,7 @@ public class Amenity extends MapObject {
public List<String> getNames(String tag, String defTag) {
List<String> l = new ArrayList<String>();
for (String nm : getAdditionalInfo().keySet()) {
for (String nm : getAdditionalInfoKeys()) {
if (nm.startsWith(tag + ":")) {
l.add(nm.substring(tag.length() + 1));
} else if (nm.equals(tag)) {
@ -231,7 +258,7 @@ public class Amenity extends MapObject {
if (!Algorithms.isEmpty(enName)) {
return enName;
}
for (String nm : getAdditionalInfo().keySet()) {
for (String nm : getAdditionalInfoKeys()) {
if (nm.startsWith(tag + ":")) {
return getAdditionalInfo(nm);
}
@ -347,4 +374,6 @@ public class Amenity extends MapObject {
}
return a;
}
}

View file

@ -13,7 +13,6 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
@ -351,8 +350,8 @@ public abstract class MapObject implements Comparable<MapObject> {
return json;
}
public String unzipContent(String str) {
if (str != null && str.startsWith(" gz ")) {
String unzipContent(String str) {
if (isContentZipped(str)) {
try {
int ind = 4;
byte[] bytes = new byte[str.length() - ind];
@ -376,6 +375,10 @@ public abstract class MapObject implements Comparable<MapObject> {
return str;
}
boolean isContentZipped(String str) {
return str != null && str.startsWith(" gz ");
}
protected static void parseJSON(JSONObject json, MapObject o) {
if (json.has("name")) {
o.name = json.getString("name");

View file

@ -741,7 +741,7 @@ public class SearchUICore {
}
}
if (Algorithms.isEmpty(object.alternateName) && object.object instanceof Amenity) {
for (String value : ((Amenity) object.object).getAdditionalInfo().values()) {
for (String value : ((Amenity) object.object).getAdditionalInfoValues(true)) {
if (phrase.getFirstUnknownNameStringMatcher().matches(value)) {
object.alternateName = value;
break;

View file

@ -602,7 +602,7 @@ public class SearchCoreFactory {
sr.localeName = object.getName(phrase.getSettings().getLang(),
phrase.getSettings().isTransliterate());
if (!nm.matches(sr.localeName) && !nm.matches(sr.otherNames)
&& !nm.matches(object.getAdditionalInfo().values())) {
&& !nm.matches(object.getAdditionalInfoValues(false))) {
return false;
}
sr.object = object;
@ -1059,7 +1059,7 @@ public class SearchCoreFactory {
if (!poiAdditionals.isEmpty()) {
boolean found = false;
for (String add : poiAdditionals) {
if(object.getAdditionalInfo().containsKey(add)) {
if (object.getAdditionalInfoKeys().contains(add)) {
found = true;
break;
}