From bdc075c4c1f520a6b75198623b99016f74def117 Mon Sep 17 00:00:00 2001 From: Bananeweizen Date: Sun, 20 Nov 2011 14:21:37 +0100 Subject: refactoring: more FindBugs issues --- main/src/cgeo/geocaching/cgBase.java | 4 +-- main/src/cgeo/geocaching/cgData.java | 16 +++------- main/src/cgeo/geocaching/cgGeo.java | 2 +- main/src/cgeo/geocaching/cgeoapplication.java | 6 +++- main/src/cgeo/geocaching/go4cache/Go4Cache.java | 29 ++++++++---------- tests/src/cgeo/geocaching/ParametersTest.java | 4 +-- .../src/cgeo/geocaching/files/GPXImporterTest.java | 34 ++++++++++++---------- tests/src/cgeo/geocaching/test/WhitespaceTest.java | 2 +- tests/src/cgeo/geocaching/test/mock/GC1ZXX2.java | 2 +- tests/src/cgeo/geocaching/test/mock/GC2CJPF.java | 2 +- .../src/cgeo/geocaching/test/mock/MockedCache.java | 2 +- 11 files changed, 48 insertions(+), 55 deletions(-) diff --git a/main/src/cgeo/geocaching/cgBase.java b/main/src/cgeo/geocaching/cgBase.java index ff3f876..54cbe60 100644 --- a/main/src/cgeo/geocaching/cgBase.java +++ b/main/src/cgeo/geocaching/cgBase.java @@ -132,8 +132,8 @@ public class cgBase { gcCustomDateFormats = Collections.unmodifiableMap(map); } - public final static SimpleDateFormat dateTbIn1 = new SimpleDateFormat("EEEEE, dd MMMMM yyyy", Locale.ENGLISH); // Saturday, 28 March 2009 - public final static SimpleDateFormat dateTbIn2 = new SimpleDateFormat("EEEEE, MMMMM dd, yyyy", Locale.ENGLISH); // Saturday, March 28, 2009 + private final static SimpleDateFormat dateTbIn1 = new SimpleDateFormat("EEEEE, dd MMMMM yyyy", Locale.ENGLISH); // Saturday, 28 March 2009 + private final static SimpleDateFormat dateTbIn2 = new SimpleDateFormat("EEEEE, MMMMM dd, yyyy", Locale.ENGLISH); // Saturday, March 28, 2009 public static final float miles2km = 1.609344f; public static final float feet2km = 0.0003048f; public static final float yards2km = 0.0009144f; diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java index 9704c22..cb8f10c 100644 --- a/main/src/cgeo/geocaching/cgData.java +++ b/main/src/cgeo/geocaching/cgData.java @@ -3106,13 +3106,9 @@ public class cgData { databaseRW.delete(dbTableLogsOffline, "geocode = ?", new String[] { geocode }); } - private SQLiteStatement getStatementLogCount() { + private synchronized SQLiteStatement getStatementLogCount() { if (statementLogCount == null) { - synchronized (this) { - if (statementLogCount == null) { - statementLogCount = databaseRO.compileStatement("SELECT count(_id) FROM " + dbTableLogsOffline + " WHERE geocode = ?"); - } - } + statementLogCount = databaseRO.compileStatement("SELECT count(_id) FROM " + dbTableLogsOffline + " WHERE geocode = ?"); } return statementLogCount; } @@ -3358,13 +3354,9 @@ public class cgData { return success; } - private SQLiteStatement getStatementDescription() { + private synchronized SQLiteStatement getStatementDescription() { if (statementDescription == null) { - synchronized (this) { - if (statementDescription == null) { - statementDescription = databaseRO.compileStatement("SELECT description FROM " + dbTableCaches + " WHERE geocode = ?"); - } - } + statementDescription = databaseRO.compileStatement("SELECT description FROM " + dbTableCaches + " WHERE geocode = ?"); } return statementDescription; } diff --git a/main/src/cgeo/geocaching/cgGeo.java b/main/src/cgeo/geocaching/cgGeo.java index ebd6525..f4370a5 100644 --- a/main/src/cgeo/geocaching/cgGeo.java +++ b/main/src/cgeo/geocaching/cgGeo.java @@ -351,7 +351,7 @@ public class cgGeo { } if (gps > -1) { - Go4Cache.signalCoordinates(app, coordsNow); + Go4Cache.signalCoordinates(coordsNow); } } diff --git a/main/src/cgeo/geocaching/cgeoapplication.java b/main/src/cgeo/geocaching/cgeoapplication.java index bb73a13..f0af0e9 100644 --- a/main/src/cgeo/geocaching/cgeoapplication.java +++ b/main/src/cgeo/geocaching/cgeoapplication.java @@ -42,10 +42,14 @@ public class cgeoapplication extends Application { private static cgeoapplication instance; public cgeoapplication() { - instance = this; + setInstance(this); getStorage(); } + private static void setInstance(final cgeoapplication app) { + instance = app; + } + public static cgeoapplication getInstance() { return instance; } diff --git a/main/src/cgeo/geocaching/go4cache/Go4Cache.java b/main/src/cgeo/geocaching/go4cache/Go4Cache.java index 98a77ba..10da7d5 100644 --- a/main/src/cgeo/geocaching/go4cache/Go4Cache.java +++ b/main/src/cgeo/geocaching/go4cache/Go4Cache.java @@ -32,28 +32,23 @@ import java.util.concurrent.ArrayBlockingQueue; * */ -public class Go4Cache extends Thread { +public final class Go4Cache extends Thread { - private final static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 2010-07-25 14:44:01 - private static Go4Cache instance; + private static class InstanceHolder { // initialization on demand holder + private static final Go4Cache INSTANCE = new Go4Cache(); + } + private final static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 2010-07-25 14:44:01 final private ArrayBlockingQueue queue = new ArrayBlockingQueue(1); - final private cgeoapplication app; - private static Go4Cache getInstance(final cgeoapplication app) { - if (null == instance) { - synchronized(Go4Cache.class) { - instance = new Go4Cache(app); - instance.start(); - } - } - return instance; + public static Go4Cache getInstance() { // no need to be synchronized + return InstanceHolder.INSTANCE; } - private Go4Cache(final cgeoapplication app) { + private Go4Cache() { // private singleton constructor super("Go4Cache"); - this.app = app; setPriority(Thread.MIN_PRIORITY); + start(); } /** @@ -64,9 +59,9 @@ public class Go4Cache extends Thread { * @param coords * the current coordinates */ - public static void signalCoordinates(final cgeoapplication app, final Geopoint coords) { + public static void signalCoordinates(final Geopoint coords) { if (Settings.isPublicLoc()) { - getInstance(app).queue.offer(coords); + getInstance().queue.offer(coords); } } @@ -79,7 +74,7 @@ public class Go4Cache extends Thread { try { for (;;) { final Geopoint currentCoords = queue.take(); - final String currentAction = app.getAction(); + final String currentAction = cgeoapplication.getInstance().getAction(); // If we are too close and we haven't changed our current action, no need // to update our situation. diff --git a/tests/src/cgeo/geocaching/ParametersTest.java b/tests/src/cgeo/geocaching/ParametersTest.java index 1855946..6ed8a65 100644 --- a/tests/src/cgeo/geocaching/ParametersTest.java +++ b/tests/src/cgeo/geocaching/ParametersTest.java @@ -1,7 +1,5 @@ package cgeo.geocaching; -import cgeo.geocaching.Parameters; - import android.test.AndroidTestCase; import java.security.InvalidParameterException; @@ -12,8 +10,8 @@ public class ParametersTest extends AndroidTestCase { public static void testException() { try { - @SuppressWarnings("unused") final Parameters params = new Parameters("aaa", "AAA", "bbb"); + params.clear(); // this will never be invoked, but suppresses warnings about unused objects Assert.fail("Exception not raised"); } catch (InvalidParameterException e) { // Ok diff --git a/tests/src/cgeo/geocaching/files/GPXImporterTest.java b/tests/src/cgeo/geocaching/files/GPXImporterTest.java index 55db7b0..e80e254 100644 --- a/tests/src/cgeo/geocaching/files/GPXImporterTest.java +++ b/tests/src/cgeo/geocaching/files/GPXImporterTest.java @@ -56,8 +56,7 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { copyResourceToFile(R.raw.gc31j2h, gc31j2h); GPXImporter.ImportGpxFileThread importThread = new GPXImporter.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler); - importThread.run(); - importStepHandler.waitForCompletion(); + runImportThread(importThread); assertEquals(3, importStepHandler.messages.size()); assertEquals(GPXImporter.IMPORT_STEP_READ_FILE, importStepHandler.messages.get(0).what); @@ -73,14 +72,24 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { // assertNull(cache.waypoints); } + private void runImportThread(GPXImporter.ImportThread importThread) { + importThread.start(); + try { + importThread.join(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + importStepHandler.waitForCompletion(); + } + public void testImportGpxWithWaypoints() throws IOException { File gc31j2h = new File(tempDir, "gc31j2h.gpx"); copyResourceToFile(R.raw.gc31j2h, gc31j2h); copyResourceToFile(R.raw.gc31j2h_wpts, new File(tempDir, "gc31j2h-wpts.gpx")); GPXImporter.ImportGpxFileThread importThread = new GPXImporter.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler); - importThread.run(); - importStepHandler.waitForCompletion(); + runImportThread(importThread); assertEquals(4, importStepHandler.messages.size()); assertEquals(GPXImporter.IMPORT_STEP_READ_FILE, importStepHandler.messages.get(0).what); @@ -100,8 +109,7 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { copyResourceToFile(R.raw.oc5952_loc, oc5952); GPXImporter.ImportLocFileThread importThread = new GPXImporter.ImportLocFileThread(oc5952, listId, importStepHandler, progressHandler); - importThread.run(); - importStepHandler.waitForCompletion(); + runImportThread(importThread); assertEquals(3, importStepHandler.messages.size()); assertEquals(GPXImporter.IMPORT_STEP_READ_FILE, importStepHandler.messages.get(0).what); @@ -124,8 +132,7 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { copyResourceToFile(R.raw.gc31j2h_err, gc31j2h); GPXImporter.ImportGpxFileThread importThread = new GPXImporter.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler); - importThread.run(); - importStepHandler.waitForCompletion(); + runImportThread(importThread); assertEquals(2, importStepHandler.messages.size()); assertEquals(GPXImporter.IMPORT_STEP_READ_FILE, importStepHandler.messages.get(0).what); @@ -138,8 +145,7 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { progressHandler.cancel(); GPXImporter.ImportGpxFileThread importThread = new GPXImporter.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler); - importThread.run(); - importStepHandler.waitForCompletion(); + runImportThread(importThread); assertEquals(2, importStepHandler.messages.size()); assertEquals(GPXImporter.IMPORT_STEP_READ_FILE, importStepHandler.messages.get(0).what); @@ -151,8 +157,7 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { copyResourceToFile(R.raw.pq7545915, pq7545915); GPXImporter.ImportGpxZipFileThread importThread = new GPXImporter.ImportGpxZipFileThread(pq7545915, listId, importStepHandler, progressHandler); - importThread.run(); - importStepHandler.waitForCompletion(); + runImportThread(importThread); assertEquals(4, importStepHandler.messages.size()); assertEquals(GPXImporter.IMPORT_STEP_READ_FILE, importStepHandler.messages.get(0).what); @@ -173,8 +178,7 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { copyResourceToFile(R.raw.pq7545915, pq1); GPXImporter.ImportGpxZipFileThread importThread = new GPXImporter.ImportGpxZipFileThread(pq1, listId, importStepHandler, progressHandler); - importThread.run(); - importStepHandler.waitForCompletion(); + runImportThread(importThread); assertEquals(1, importStepHandler.messages.size()); assertEquals(GPXImporter.IMPORT_STEP_FINISHED_WITH_ERROR, importStepHandler.messages.get(0).what); @@ -240,4 +244,4 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { dir.delete(); } -} +} \ No newline at end of file diff --git a/tests/src/cgeo/geocaching/test/WhitespaceTest.java b/tests/src/cgeo/geocaching/test/WhitespaceTest.java index 6fab7fd..f67940a 100644 --- a/tests/src/cgeo/geocaching/test/WhitespaceTest.java +++ b/tests/src/cgeo/geocaching/test/WhitespaceTest.java @@ -22,7 +22,7 @@ import java.util.regex.Pattern; */ public class WhitespaceTest extends AndroidTestCase { - private final int EXPECTED_SIZE = 119418; + private final static int EXPECTED_SIZE = 119418; private String data; @Override diff --git a/tests/src/cgeo/geocaching/test/mock/GC1ZXX2.java b/tests/src/cgeo/geocaching/test/mock/GC1ZXX2.java index f36f3cb..168b7e7 100644 --- a/tests/src/cgeo/geocaching/test/mock/GC1ZXX2.java +++ b/tests/src/cgeo/geocaching/test/mock/GC1ZXX2.java @@ -134,7 +134,7 @@ public class GC1ZXX2 extends MockedCache { @Override public Integer getFavoritePoints() { - return new Integer(47); + return 47; } @Override diff --git a/tests/src/cgeo/geocaching/test/mock/GC2CJPF.java b/tests/src/cgeo/geocaching/test/mock/GC2CJPF.java index e5b443f..b07d62c 100644 --- a/tests/src/cgeo/geocaching/test/mock/GC2CJPF.java +++ b/tests/src/cgeo/geocaching/test/mock/GC2CJPF.java @@ -147,7 +147,7 @@ public class GC2CJPF extends MockedCache { @Override public Integer getFavoritePoints() { - return new Integer(7); + return 7; } } diff --git a/tests/src/cgeo/geocaching/test/mock/MockedCache.java b/tests/src/cgeo/geocaching/test/mock/MockedCache.java index d79510b..04320a2 100644 --- a/tests/src/cgeo/geocaching/test/mock/MockedCache.java +++ b/tests/src/cgeo/geocaching/test/mock/MockedCache.java @@ -115,7 +115,7 @@ public abstract class MockedCache implements ICache { @Override public Integer getFavoritePoints() { - return new Integer(0); + return 0; } @Override -- cgit v1.1