Make MapWithAILayerInfo.instance private

Signed-off-by: Taylor Smock <taylor.smock@kaart.com>
pull/1/head
Taylor Smock 2020-06-24 13:50:39 -06:00
rodzic 1fa9bc076a
commit 35768d1997
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 625F6A74A3E4311A
9 zmienionych plików z 42 dodań i 34 usunięć

Wyświetl plik

@ -247,7 +247,7 @@ public class DataAvailability {
* @return List of terms of use urls * @return List of terms of use urls
*/ */
public static final List<String> getTermsOfUse() { public static final List<String> getTermsOfUse() {
return Stream.concat(MapWithAILayerInfo.instance.getLayers().stream().map(MapWithAIInfo::getTermsOfUseURL), return Stream.concat(MapWithAILayerInfo.getInstance().getLayers().stream().map(MapWithAIInfo::getTermsOfUseURL),
DATA_SOURCES.stream().map(clazz -> { DATA_SOURCES.stream().map(clazz -> {
try { try {
return clazz.getConstructor().newInstance().getTermsOfUseUrl(); return clazz.getConstructor().newInstance().getTermsOfUseUrl();
@ -266,7 +266,8 @@ public class DataAvailability {
* @return List of privacy policy urls * @return List of privacy policy urls
*/ */
public static final List<String> getPrivacyPolicy() { public static final List<String> getPrivacyPolicy() {
return Stream.concat(MapWithAILayerInfo.instance.getLayers().stream().map(MapWithAIInfo::getPrivacyPolicyURL), return Stream
.concat(MapWithAILayerInfo.getInstance().getLayers().stream().map(MapWithAIInfo::getPrivacyPolicyURL),
DATA_SOURCES.stream().map(clazz -> { DATA_SOURCES.stream().map(clazz -> {
try { try {
return clazz.getConstructor().newInstance().getPrivacyPolicyUrl(); return clazz.getConstructor().newInstance().getPrivacyPolicyUrl();
@ -275,7 +276,8 @@ public class DataAvailability {
Logging.debug(e); Logging.debug(e);
} }
return EMPTY_STRING; return EMPTY_STRING;
})).filter(Objects::nonNull).filter(str -> !Utils.removeWhiteSpaces(str).isEmpty()).distinct() }))
.filter(Objects::nonNull).filter(str -> !Utils.removeWhiteSpaces(str).isEmpty()).distinct()
.collect(Collectors.toList()); .collect(Collectors.toList());
} }

Wyświetl plik

@ -511,7 +511,7 @@ public class GetDataRunnable extends RecursiveTask<DataSet> {
final DataSet dataSet = new DataSet(); final DataSet dataSet = new DataSet();
dataSet.setUploadPolicy(UploadPolicy.DISCOURAGED); dataSet.setUploadPolicy(UploadPolicy.DISCOURAGED);
new ArrayList<>(MapWithAILayerInfo.instance.getLayers()).parallelStream().forEach(map -> { new ArrayList<>(MapWithAILayerInfo.getInstance().getLayers()).parallelStream().forEach(map -> {
try { try {
Bounds bound = new Bounds(bbox.getBottomRight()); Bounds bound = new Bounds(bbox.getBottomRight());
bound.extend(bbox.getTopLeft()); bound.extend(bbox.getTopLeft());

Wyświetl plik

@ -194,7 +194,7 @@ public final class MapWithAIDataUtils {
final List<Bounds> realBounds = realBBoxes.stream() final List<Bounds> realBounds = realBBoxes.stream()
.flatMap(tBBox -> MapWithAIDataUtils.reduceBBoxSize(tBBox, maximumDimensions).stream()) .flatMap(tBBox -> MapWithAIDataUtils.reduceBBoxSize(tBBox, maximumDimensions).stream())
.map(MapWithAIDataUtils::bboxToBounds).collect(Collectors.toList()); .map(MapWithAIDataUtils::bboxToBounds).collect(Collectors.toList());
if (!MapWithAILayerInfo.instance.getLayers().isEmpty()) { if (!MapWithAILayerInfo.getInstance().getLayers().isEmpty()) {
if ((realBBoxes.size() < TOO_MANY_BBOXES) || confirmBigDownload(realBBoxes)) { if ((realBBoxes.size() < TOO_MANY_BBOXES) || confirmBigDownload(realBBoxes)) {
final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor(); final PleaseWaitProgressMonitor monitor = new PleaseWaitProgressMonitor();
monitor.beginTask(tr("Downloading {0} Data", MapWithAIPlugin.NAME), realBounds.size()); monitor.beginTask(tr("Downloading {0} Data", MapWithAIPlugin.NAME), realBounds.size());
@ -228,8 +228,8 @@ public final class MapWithAIDataUtils {
noUrls.setHelpTopic(ht("Plugin/MapWithAI#Preferences")); noUrls.setHelpTopic(ht("Plugin/MapWithAI#Preferences"));
GuiHelper.runInEDT(noUrls::show); GuiHelper.runInEDT(noUrls::show);
if (MapWithAIPreferenceHelper.getMapWithAIUrl().isEmpty() if (MapWithAIPreferenceHelper.getMapWithAIUrl().isEmpty()
&& MapWithAILayerInfo.instance.getDefaultLayers().isEmpty()) { && MapWithAILayerInfo.getInstance().getDefaultLayers().isEmpty()) {
MapWithAILayerInfo.instance.loadDefaults(true, MainApplication.worker, false, MapWithAILayerInfo.getInstance().loadDefaults(true, MainApplication.worker, false,
() -> Logging.info("MapWithAI Sources: Initialized sources")); () -> Logging.info("MapWithAI Sources: Initialized sources"));
} }
} }

Wyświetl plik

@ -41,7 +41,7 @@ public final class MapWithAIPreferenceHelper {
public static List<MapWithAIInfo> getMapWithAIUrl() { public static List<MapWithAIInfo> getMapWithAIUrl() {
return MapWithAIDataUtils.getLayer(false) == null return MapWithAIDataUtils.getLayer(false) == null
|| MapWithAIDataUtils.getLayer(false).getMapWithAIUrl() == null || MapWithAIDataUtils.getLayer(false).getMapWithAIUrl() == null
? MapWithAILayerInfo.instance.getLayers() ? MapWithAILayerInfo.getInstance().getLayers()
: Collections.singletonList(MapWithAIDataUtils.getLayer(false).getMapWithAIUrl()); : Collections.singletonList(MapWithAIDataUtils.getLayer(false).getMapWithAIUrl());
} }
@ -92,8 +92,8 @@ public final class MapWithAIPreferenceHelper {
*/ */
public static void setMapWithAIUrl(MapWithAIInfo info, boolean enabled, boolean permanent) { public static void setMapWithAIUrl(MapWithAIInfo info, boolean enabled, boolean permanent) {
if (permanent && enabled) { if (permanent && enabled) {
MapWithAILayerInfo.instance.add(info); MapWithAILayerInfo.getInstance().add(info);
MapWithAILayerInfo.instance.save(); MapWithAILayerInfo.getInstance().save();
} else if (enabled && MapWithAIDataUtils.getLayer(false) != null) { } else if (enabled && MapWithAIDataUtils.getLayer(false) != null) {
MapWithAIDataUtils.getLayer(false).setMapWithAIUrl(info); MapWithAIDataUtils.getLayer(false).setMapWithAIUrl(info);
} }

Wyświetl plik

@ -57,9 +57,14 @@ public class MapWithAILayerInfo {
"https://gitlab.com/gokaart/JOSM_MapWithAI/-/raw/pages/public/json/sources.json" }; "https://gitlab.com/gokaart/JOSM_MapWithAI/-/raw/pages/public/json/sources.json" };
/** Unique instance -- MUST be after DEFAULT_LAYER_SITES */ /** Unique instance -- MUST be after DEFAULT_LAYER_SITES */
public static final MapWithAILayerInfo instance = new MapWithAILayerInfo(); private static MapWithAILayerInfo instance;
public static MapWithAILayerInfo getInstance() { public static MapWithAILayerInfo getInstance() {
synchronized (DEFAULT_LAYER_SITES) {
if (instance == null) {
instance = new MapWithAILayerInfo();
}
}
return instance; return instance;
} }

Wyświetl plik

@ -32,7 +32,7 @@ public class DownloadMapWithAITaskTest {
DownloadMapWithAITask task = new DownloadMapWithAITask(); DownloadMapWithAITask task = new DownloadMapWithAITask();
Future<?> future = task.download( Future<?> future = task.download(
new BoundingBoxMapWithAIDownloader(MapWithAIDataUtilsTest.getTestBounds(), new BoundingBoxMapWithAIDownloader(MapWithAIDataUtilsTest.getTestBounds(),
MapWithAILayerInfo.instance.getLayers().get(0), false), MapWithAILayerInfo.getInstance().getLayers().get(0), false),
new DownloadParams(), MapWithAIDataUtilsTest.getTestBounds(), NullProgressMonitor.INSTANCE); new DownloadParams(), MapWithAIDataUtilsTest.getTestBounds(), NullProgressMonitor.INSTANCE);
future.get(); future.get();
assertNotNull(task.getDownloadedData(), "Data should be downloaded"); assertNotNull(task.getDownloadedData(), "Data should be downloaded");

Wyświetl plik

@ -69,13 +69,14 @@ public class MapWithAIAvailabilityTest {
@Test @Test
public void testNoURLs() { public void testNoURLs() {
new ArrayList<>(MapWithAILayerInfo.instance.getLayers()).forEach(i -> MapWithAILayerInfo.instance.remove(i)); new ArrayList<>(MapWithAILayerInfo.getInstance().getLayers())
.forEach(i -> MapWithAILayerInfo.getInstance().remove(i));
DataAvailability.getInstance(); DataAvailability.getInstance();
testgetDataLatLon(); testgetDataLatLon();
MapWithAILayerInfo.instance.getLayers().forEach(i -> MapWithAILayerInfo.instance.remove(i)); MapWithAILayerInfo.getInstance().getLayers().forEach(i -> MapWithAILayerInfo.getInstance().remove(i));
DataAvailability.getInstance(); DataAvailability.getInstance();
testHasDataLatLon(); testHasDataLatLon();
MapWithAILayerInfo.instance.getLayers().forEach(i -> MapWithAILayerInfo.instance.remove(i)); MapWithAILayerInfo.getInstance().getLayers().forEach(i -> MapWithAILayerInfo.getInstance().remove(i));
DataAvailability.getInstance(); DataAvailability.getInstance();
testHasDataBBox(); testHasDataBBox();
} }

Wyświetl plik

@ -67,8 +67,8 @@ public class MapWithAIRemoteControlTest {
*/ */
@Test @Test
public void testNominalRequest() throws Exception { public void testNominalRequest() throws Exception {
newHandler("https://localhost?url=" + Utils.encodeUrl(MapWithAILayerInfo.instance.getLayers().get(0).getUrl())) newHandler("https://localhost?url="
.handle(); + Utils.encodeUrl(MapWithAILayerInfo.getInstance().getLayers().get(0).getUrl())).handle();
Awaitility.await().atMost(Durations.ONE_SECOND) Awaitility.await().atMost(Durations.ONE_SECOND)
.until(() -> !MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).isEmpty()); .until(() -> !MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).isEmpty());

Wyświetl plik

@ -70,9 +70,6 @@ public class MapWithAITestRules extends JOSMTestRules {
super.before(); super.before();
Logging.getLogger().setFilter(record -> record.getLevel().intValue() >= Level.WARNING.intValue() Logging.getLogger().setFilter(record -> record.getLevel().intValue() >= Level.WARNING.intValue()
|| record.getSourceClassName().startsWith("org.openstreetmap.josm.plugins.mapwithai")); || record.getSourceClassName().startsWith("org.openstreetmap.josm.plugins.mapwithai"));
if (sources) {
MapWithAILayerInfo.instance.load(false);
}
if (wiremock) { if (wiremock) {
wireMock = new WireMockServer(options().usingFilesUnderDirectory("test/resources/wiremock")); wireMock = new WireMockServer(options().usingFilesUnderDirectory("test/resources/wiremock"));
wireMock.start(); wireMock.start();
@ -89,6 +86,9 @@ public class MapWithAITestRules extends JOSMTestRules {
Logging.error(e); Logging.error(e);
} }
} }
if (sources) {
MapWithAILayerInfo.getInstance().load(false);
}
if (workerExceptions) { if (workerExceptions) {
currentExceptionHandler = Thread.getDefaultUncaughtExceptionHandler(); currentExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler((t, e) -> { Thread.setDefaultUncaughtExceptionHandler((t, e) -> {
@ -119,18 +119,18 @@ public class MapWithAITestRules extends JOSMTestRules {
private static void setupMapWithAILayerInfo(WireMockServer wireMockServer) { private static void setupMapWithAILayerInfo(WireMockServer wireMockServer) {
synchronized (MapWithAITestRules.class) { synchronized (MapWithAITestRules.class) {
resetMapWithAILayerInfo(); resetMapWithAILayerInfo();
MapWithAILayerInfo.instance.getLayers().stream().forEach( MapWithAILayerInfo.getInstance().getLayers().stream().forEach(
i -> i.setUrl(GetDataRunnableTest.getDefaultMapWithAIAPIForTest(wireMockServer, i.getUrl()))); i -> i.setUrl(GetDataRunnableTest.getDefaultMapWithAIAPIForTest(wireMockServer, i.getUrl())));
MapWithAILayerInfo.instance.save(); MapWithAILayerInfo.getInstance().save();
} }
} }
private static void resetMapWithAILayerInfo() { private static void resetMapWithAILayerInfo() {
synchronized (MapWithAILayerInfo.class) { synchronized (MapWithAILayerInfo.class) {
MapWithAILayerInfo.instance.clear(); MapWithAILayerInfo.getInstance().clear();
MapWithAILayerInfo.instance.getDefaultLayers().stream().filter(MapWithAIInfo::isDefaultEntry) MapWithAILayerInfo.getInstance().getDefaultLayers().stream().filter(MapWithAIInfo::isDefaultEntry)
.forEach(MapWithAILayerInfo.instance::add); .forEach(MapWithAILayerInfo.getInstance()::add);
MapWithAILayerInfo.instance.save(); MapWithAILayerInfo.getInstance().save();
} }
} }