From d6fffc7e559234f3394d6a126261a4ba79e0d4e6 Mon Sep 17 00:00:00 2001 From: Coffeemakr Date: Fri, 22 Sep 2017 12:45:50 +0200 Subject: [PATCH] Use correct long comparison (fixes #726) --- .../giga/get/DownloadManagerImpl.java | 20 ++++++++---- .../{get => }/DownloadManagerImplTest.java | 32 ++++++++++++++++++- 2 files changed, 45 insertions(+), 7 deletions(-) rename app/src/test/java/us/shandian/giga/get/{get => }/DownloadManagerImplTest.java (84%) diff --git a/app/src/main/java/us/shandian/giga/get/DownloadManagerImpl.java b/app/src/main/java/us/shandian/giga/get/DownloadManagerImpl.java index 479a5cee3..acbd41680 100755 --- a/app/src/main/java/us/shandian/giga/get/DownloadManagerImpl.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadManagerImpl.java @@ -101,6 +101,18 @@ public class DownloadManagerImpl implements DownloadManager { } + /** + * Sort a list of mission by its timestamp. Oldest first + * @param missions the missions to sort + */ + static void sortByTimestamp(List missions) { + Collections.sort(missions, new Comparator() { + @Override + public int compare(DownloadMission o1, DownloadMission o2) { + return Long.valueOf(o1.timestamp).compareTo(o2.timestamp); + } + }); + } /** * Loads finished missions from the data source @@ -111,12 +123,8 @@ public class DownloadManagerImpl implements DownloadManager { finishedMissions = new ArrayList<>(); } // Ensure its sorted - Collections.sort(finishedMissions, new Comparator() { - @Override - public int compare(DownloadMission o1, DownloadMission o2) { - return (int) (o1.timestamp - o2.timestamp); - } - }); + sortByTimestamp(finishedMissions); + mMissions.ensureCapacity(mMissions.size() + finishedMissions.size()); for (DownloadMission mission : finishedMissions) { File downloadedFile = mission.getDownloadedFile(); diff --git a/app/src/test/java/us/shandian/giga/get/get/DownloadManagerImplTest.java b/app/src/test/java/us/shandian/giga/get/DownloadManagerImplTest.java similarity index 84% rename from app/src/test/java/us/shandian/giga/get/get/DownloadManagerImplTest.java rename to app/src/test/java/us/shandian/giga/get/DownloadManagerImplTest.java index a7242ba10..6ff702273 100644 --- a/app/src/test/java/us/shandian/giga/get/get/DownloadManagerImplTest.java +++ b/app/src/test/java/us/shandian/giga/get/DownloadManagerImplTest.java @@ -1,4 +1,4 @@ -package us.shandian.giga.get.get; +package us.shandian.giga.get; import org.junit.Ignore; import org.junit.Test; @@ -153,4 +153,34 @@ public class DownloadManagerImplTest { assertSame(missions.get(1), downloadManager.getMission(1)); } + @Test + public void sortByTimestamp() throws Exception { + ArrayList downloadMissions = new ArrayList<>(); + DownloadMission mission = new DownloadMission(); + mission.timestamp = 0; + + DownloadMission mission1 = new DownloadMission(); + mission1.timestamp = Integer.MAX_VALUE + 1L; + + DownloadMission mission2 = new DownloadMission(); + mission2.timestamp = 2L * Integer.MAX_VALUE ; + + DownloadMission mission3 = new DownloadMission(); + mission3.timestamp = 2L * Integer.MAX_VALUE + 5L; + + + downloadMissions.add(mission3); + downloadMissions.add(mission1); + downloadMissions.add(mission2); + downloadMissions.add(mission); + + + DownloadManagerImpl.sortByTimestamp(downloadMissions); + + assertEquals(mission, downloadMissions.get(0)); + assertEquals(mission1, downloadMissions.get(1)); + assertEquals(mission2, downloadMissions.get(2)); + assertEquals(mission3, downloadMissions.get(3)); + } + } \ No newline at end of file