Fix some checkstyle issues and add try/catch around Territories.getData()

Signed-off-by: Taylor Smock <taylor.smock@kaart.com>
pull/1/head
Taylor Smock 2019-10-15 08:29:39 -06:00
rodzic 34ccc3a7f0
commit 0f4cf3414d
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 625F6A74A3E4311A
5 zmienionych plików z 47 dodań i 41 usunięć

Wyświetl plik

@ -47,6 +47,7 @@ def versions = [
pmd: "6.6.0", pmd: "6.6.0",
spotbugs: "3.1.12", spotbugs: "3.1.12",
wiremock: "2.25.0", wiremock: "2.25.0",
findsecbugs: "1.9.0",
] ]
repositories { repositories {
@ -57,6 +58,7 @@ dependencies {
if (!JavaVersion.current().isJava9Compatible()) { if (!JavaVersion.current().isJava9Compatible()) {
errorproneJavac("com.google.errorprone:javac:9+181-r4173-1") errorproneJavac("com.google.errorprone:javac:9+181-r4173-1")
} }
spotbugsPlugins "com.h3xstream.findsecbugs:findsecbugs-plugin:${versions.findsecbugs}"
errorprone("com.google.errorprone:error_prone_core:${versions.errorprone}") errorprone("com.google.errorprone:error_prone_core:${versions.errorprone}")
testImplementation("org.junit.jupiter:junit-jupiter-api:${versions.junit}") testImplementation("org.junit.jupiter:junit-jupiter-api:${versions.junit}")

Wyświetl plik

@ -52,13 +52,13 @@ public class MapWithAIAction extends JosmAction {
final StringBuilder message = new StringBuilder(); final StringBuilder message = new StringBuilder();
message.append(MapWithAIPlugin.NAME); message.append(MapWithAIPlugin.NAME);
message.append(": "); message.append(": ");
MapWithAIAvailability availability = MapWithAIAvailability.getInstance(); final MapWithAIAvailability availability = MapWithAIAvailability.getInstance();
Map<String, Boolean> availableTypes = new TreeMap<>(); final Map<String, Boolean> availableTypes = new TreeMap<>();
for (final Bounds bound : bounds) { for (final Bounds bound : bounds) {
availability.getDataTypes(bound.getCenter()) availability.getDataTypes(bound.getCenter())
.forEach((type, available) -> availableTypes.merge(type, available, Boolean::logicalOr)); .forEach((type, available) -> availableTypes.merge(type, available, Boolean::logicalOr));
} }
List<String> types = availableTypes.entrySet().stream().filter(Entry::getValue) final List<String> types = availableTypes.entrySet().stream().filter(Entry::getValue)
.map(entry -> MapWithAIAvailability.getPossibleDataTypesAndMessages().get(entry.getKey())) .map(entry -> MapWithAIAvailability.getPossibleDataTypesAndMessages().get(entry.getKey()))
.collect(Collectors.toList()); .collect(Collectors.toList());
if (types.isEmpty()) { if (types.isEmpty()) {

Wyświetl plik

@ -39,11 +39,11 @@ import org.openstreetmap.josm.tools.Shortcut;
public class MapWithAIArbitraryAction extends JosmAction { public class MapWithAIArbitraryAction extends JosmAction {
private static final long serialVersionUID = 9048113038651190619L; private static final long serialVersionUID = 9048113038651190619L;
private final JosmTextField lowerLat = new JosmTextField(); private final transient JosmTextField lowerLat = new JosmTextField();
private final JosmTextField upperLat = new JosmTextField(); private final transient JosmTextField upperLat = new JosmTextField();
private final JosmTextField leftLon = new JosmTextField(); private final transient JosmTextField leftLon = new JosmTextField();
private final JosmTextField rightLon = new JosmTextField(); private final transient JosmTextField rightLon = new JosmTextField();
private final JCheckBox checkbox = new JCheckBox(); private final transient JCheckBox checkbox = new JCheckBox();
private static final String ARBITRARY_DATA_STRING = tr("Get arbitrary data from {0}", MapWithAIPlugin.NAME); private static final String ARBITRARY_DATA_STRING = tr("Get arbitrary data from {0}", MapWithAIPlugin.NAME);

Wyświetl plik

@ -22,18 +22,23 @@ import javax.json.stream.JsonParser;
import org.openstreetmap.josm.data.coor.LatLon; import org.openstreetmap.josm.data.coor.LatLon;
import org.openstreetmap.josm.data.osm.BBox; import org.openstreetmap.josm.data.osm.BBox;
import org.openstreetmap.josm.data.osm.DataSet;
import org.openstreetmap.josm.data.osm.OsmPrimitive; import org.openstreetmap.josm.data.osm.OsmPrimitive;
import org.openstreetmap.josm.io.CachedFile; import org.openstreetmap.josm.io.CachedFile;
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin; import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin;
import org.openstreetmap.josm.tools.Logging; import org.openstreetmap.josm.tools.Logging;
import org.openstreetmap.josm.tools.Territories; import org.openstreetmap.josm.tools.Territories;
public class MapWithAIAvailability { public final class MapWithAIAvailability {
private static String rapidReleases = "https://github.com/facebookmicrosites/Open-Mapping-At-Facebook/raw/master/data/rapid_realeases.geojson"; private static String rapidReleases = "https://github.com/facebookmicrosites/Open-Mapping-At-Facebook/raw/master/data/rapid_realeases.geojson";
private static MapWithAIAvailability instance = null;
private static final Map<String, Map<String, Boolean>> COUNTRIES = new HashMap<>(); private static final Map<String, Map<String, Boolean>> COUNTRIES = new HashMap<>();
private static final Map<String, String> POSSIBLE_DATA_POINTS = new TreeMap<>(); private static final Map<String, String> POSSIBLE_DATA_POINTS = new TreeMap<>();
private static final Map<String, String> COUNTRY_NAME_FIX = new HashMap<>(); private static final Map<String, String> COUNTRY_NAME_FIX = new HashMap<>();
private static class InstanceHelper {
static final MapWithAIAvailability INSTANCE = new MapWithAIAvailability();
}
static { static {
COUNTRY_NAME_FIX.put("Egypt", "Egypt, Arab Rep."); COUNTRY_NAME_FIX.put("Egypt", "Egypt, Arab Rep.");
COUNTRY_NAME_FIX.put("Dem. Rep. Congo", "Congo, Dem. Rep."); COUNTRY_NAME_FIX.put("Dem. Rep. Congo", "Congo, Dem. Rep.");
@ -44,18 +49,16 @@ public class MapWithAIAvailability {
private MapWithAIAvailability() { private MapWithAIAvailability() {
try (CachedFile cachedRapidReleases = new CachedFile(rapidReleases); try (CachedFile cachedRapidReleases = new CachedFile(rapidReleases);
JsonParser parser = Json.createParser(cachedRapidReleases.getContentReader())) { JsonParser parser = Json.createParser(cachedRapidReleases.getContentReader())) {
if (parser.hasNext()) { final JsonParser.Event event = parser.next();
JsonParser.Event event = parser.next(); if (JsonParser.Event.START_OBJECT.equals(event)) {
if (JsonParser.Event.START_OBJECT.equals(event)) { final Stream<Entry<String, JsonValue>> entries = parser.getObjectStream();
Stream<Entry<String, JsonValue>> entries = parser.getObjectStream(); final Optional<Entry<String, JsonValue>> objects = entries
Optional<Entry<String, JsonValue>> objects = entries.filter(entry -> "objects".equals(entry.getKey())) .filter(entry -> "objects".equals(entry.getKey())).findFirst();
.findFirst(); if (objects.isPresent()) {
if (objects.isPresent()) { final JsonObject value = objects.get().getValue().asJsonObject();
JsonObject value = objects.get().getValue().asJsonObject(); final JsonObject centroid = value.getJsonObject("rapid_releases_1011_centroid");
JsonObject centroid = value.getJsonObject("rapid_releases_1011_centroid"); final JsonArray countries = centroid.getJsonArray("geometries");
JsonArray countries = centroid.getJsonArray("geometries"); parseForCountries(countries);
parseForCountries(countries);
}
} }
} }
} catch (IOException e) { } catch (IOException e) {
@ -67,25 +70,29 @@ public class MapWithAIAvailability {
* @return the unique instance * @return the unique instance
*/ */
public static MapWithAIAvailability getInstance() { public static MapWithAIAvailability getInstance() {
if (instance == null) { return InstanceHelper.INSTANCE;
instance = new MapWithAIAvailability();
}
return instance;
} }
private static void parseForCountries(JsonArray countries) { private static void parseForCountries(JsonArray countries) {
for (int i = 0; i < countries.size(); i++) { for (int i = 0; i < countries.size(); i++) {
JsonObject country = countries.getJsonObject(i).getJsonObject("properties"); final JsonObject country = countries.getJsonObject(i).getJsonObject("properties");
String countryName = cornerCaseNames(country.getString("Country")); final String countryName = cornerCaseNames(country.getString("Country"));
Optional<OsmPrimitive> realCountry = Territories.getDataSet().allPrimitives().parallelStream() DataSet territories;
try {
territories = Territories.getDataSet();
} catch (NullPointerException e) {
Territories.initialize();
territories = Territories.getDataSet();
}
final Optional<OsmPrimitive> realCountry = territories.allPrimitives().parallelStream()
.filter(primitive -> countryName.equalsIgnoreCase(primitive.get("name:en"))) .filter(primitive -> countryName.equalsIgnoreCase(primitive.get("name:en")))
.findFirst(); .findFirst();
if (realCountry.isPresent()) { if (realCountry.isPresent()) {
String key = realCountry.get().get("ISO3166-1:alpha2"); final String key = realCountry.get().get("ISO3166-1:alpha2");
// We need to handle cases like Alaska more elegantly // We need to handle cases like Alaska more elegantly
Map<String, Boolean> data = COUNTRIES.getOrDefault(key, new TreeMap<>()); final Map<String, Boolean> data = COUNTRIES.getOrDefault(key, new TreeMap<>());
for (Entry<String, String> entry : POSSIBLE_DATA_POINTS.entrySet()) { for (final Entry<String, String> entry : POSSIBLE_DATA_POINTS.entrySet()) {
boolean hasData = "yes".equals(country.getString(entry.getValue())); final boolean hasData = "yes".equals(country.getString(entry.getValue()));
if (hasData || !data.containsKey(entry.getKey())) { if (hasData || !data.containsKey(entry.getKey())) {
data.put(entry.getKey(), hasData); data.put(entry.getKey(), hasData);
} }
@ -98,10 +105,11 @@ public class MapWithAIAvailability {
} }
private static String cornerCaseNames(String name) { private static String cornerCaseNames(String name) {
String returnName = name;
if (COUNTRY_NAME_FIX.containsKey(name)) { if (COUNTRY_NAME_FIX.containsKey(name)) {
name = COUNTRY_NAME_FIX.get(name); returnName = COUNTRY_NAME_FIX.get(name);
} }
return name; return returnName;
} }
/** /**
@ -110,7 +118,7 @@ public class MapWithAIAvailability {
* available data. * available data.
*/ */
public boolean hasData(BBox bbox) { public boolean hasData(BBox bbox) {
List<LatLon> corners = new ArrayList<>(); final List<LatLon> corners = new ArrayList<>();
corners.add(bbox.getBottomRight()); corners.add(bbox.getBottomRight());
corners.add(new LatLon(bbox.getBottomRightLat(), bbox.getTopLeftLon())); corners.add(new LatLon(bbox.getBottomRightLat(), bbox.getTopLeftLon()));
corners.add(bbox.getTopLeft()); corners.add(bbox.getTopLeft());
@ -124,7 +132,7 @@ public class MapWithAIAvailability {
*/ */
public boolean hasData(LatLon latLon) { public boolean hasData(LatLon latLon) {
boolean returnBoolean = false; boolean returnBoolean = false;
for (Entry<String, Map<String, Boolean>> entry : COUNTRIES.entrySet()) { for (final Entry<String, Map<String, Boolean>> entry : COUNTRIES.entrySet()) {
if (Territories.isIso3166Code(entry.getKey(), latLon)) { if (Territories.isIso3166Code(entry.getKey(), latLon)) {
returnBoolean = entry.getValue().entrySet().parallelStream().anyMatch(Entry::getValue); returnBoolean = entry.getValue().entrySet().parallelStream().anyMatch(Entry::getValue);
break; break;

Wyświetl plik

@ -25,10 +25,6 @@ public class MapWithAIRemoteControl extends RequestHandler.RawURLParseRequestHan
private static final String MAX_OBJ = "max_obj"; private static final String MAX_OBJ = "max_obj";
private static final String SWITCH_LAYER = "switch_layer"; private static final String SWITCH_LAYER = "switch_layer";
public MapWithAIRemoteControl() {
super();
}
@Override @Override
protected void validateRequest() throws RequestHandlerBadRequestException { protected void validateRequest() throws RequestHandlerBadRequestException {
if (args != null) { if (args != null) {