From 5ad7ca3184379a45e579b966d2e3be377ae3013f Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Thu, 20 Sep 2018 11:28:01 +0300 Subject: [PATCH] Reorder fields, methods and classes in the SearchHistoryHelper --- .../plus/helpers/SearchHistoryHelper.java | 136 +++++++++--------- 1 file changed, 66 insertions(+), 70 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/helpers/SearchHistoryHelper.java b/OsmAnd/src/net/osmand/plus/helpers/SearchHistoryHelper.java index 2c8f3fb3e5..decc023bfe 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/SearchHistoryHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/SearchHistoryHelper.java @@ -18,6 +18,11 @@ import java.util.Map; public class SearchHistoryHelper { private static final int HISTORY_LIMIT = 1500; + private static final int[] DEF_INTERVALS_MIN = new int[]{ + 5, 60, 60 * 24, 5 * 60 * 24, 10 * 60 * 24, 30 * 60 * 24 + }; + + private static SearchHistoryHelper instance = null; private OsmandApplication context; private List loadedEntries = null; @@ -27,8 +32,6 @@ public class SearchHistoryHelper { this.context = context; } - private static SearchHistoryHelper instance = null; - public static SearchHistoryHelper getInstance(OsmandApplication context) { if (instance == null) { instance = new SearchHistoryHelper(context); @@ -36,18 +39,62 @@ public class SearchHistoryHelper { return instance; } - private static final int[] DEF_INTERVALS_MIN = new int[]{ - 5, 60, 60 * 24, 5 * 60 * 24, 10 * 60 * 24, 30 * 60 * 24 - }; + public void addNewItemToHistory(double latitude, double longitude, PointDescription pointDescription) { + addNewItemToHistory(new HistoryEntry(latitude, longitude, pointDescription)); + } - private static class HistoryEntryComparator implements Comparator { - long time = System.currentTimeMillis(); + public List getHistoryEntries() { + if (loadedEntries == null) { + checkLoadedEntries(); + } + return new ArrayList<>(loadedEntries); + } - @Override - public int compare(HistoryEntry lhs, HistoryEntry rhs) { - double l = lhs.getRank(time); - double r = rhs.getRank(time); - return -Double.compare(l, r); + public void remove(HistoryEntry model) { + HistoryItemDBHelper helper = checkLoadedEntries(); + if (helper.remove(model)) { + loadedEntries.remove(model); + mp.remove(model.getName()); + } + } + + public void removeAll() { + HistoryItemDBHelper helper = checkLoadedEntries(); + if (helper.removeAll()) { + loadedEntries.clear(); + mp.clear(); + } + } + + private HistoryItemDBHelper checkLoadedEntries() { + HistoryItemDBHelper helper = new HistoryItemDBHelper(); + if (loadedEntries == null) { + loadedEntries = helper.getEntries(); + Collections.sort(loadedEntries, new HistoryEntryComparator()); + for (HistoryEntry he : loadedEntries) { + mp.put(he.getName(), he); + } + } + return helper; + } + + private void addNewItemToHistory(HistoryEntry model) { + HistoryItemDBHelper helper = checkLoadedEntries(); + if (mp.containsKey(model.getName())) { + model = mp.get(model.getName()); + model.markAsAccessed(System.currentTimeMillis()); + helper.update(model); + } else { + loadedEntries.add(model); + mp.put(model.getName(), model); + model.markAsAccessed(System.currentTimeMillis()); + helper.add(model); + } + Collections.sort(loadedEntries, new HistoryEntryComparator()); + if (loadedEntries.size() > HISTORY_LIMIT) { + if (helper.remove(loadedEntries.get(loadedEntries.size() - 1))) { + loadedEntries.remove(loadedEntries.size() - 1); + } } } @@ -191,62 +238,17 @@ public class SearchHistoryHelper { } - public List getHistoryEntries() { - if (loadedEntries == null) { - checkLoadedEntries(); - } - return new ArrayList<>(loadedEntries); - } + private static class HistoryEntryComparator implements Comparator { + long time = System.currentTimeMillis(); - private HistoryItemDBHelper checkLoadedEntries() { - HistoryItemDBHelper helper = new HistoryItemDBHelper(); - if (loadedEntries == null) { - loadedEntries = helper.getEntries(); - Collections.sort(loadedEntries, new HistoryEntryComparator()); - for (HistoryEntry he : loadedEntries) { - mp.put(he.getName(), he); - } - } - return helper; - } - - public void remove(HistoryEntry model) { - HistoryItemDBHelper helper = checkLoadedEntries(); - if (helper.remove(model)) { - loadedEntries.remove(model); - mp.remove(model.getName()); + @Override + public int compare(HistoryEntry lhs, HistoryEntry rhs) { + double l = lhs.getRank(time); + double r = rhs.getRank(time); + return -Double.compare(l, r); } } - public void removeAll() { - HistoryItemDBHelper helper = checkLoadedEntries(); - if (helper.removeAll()) { - loadedEntries.clear(); - mp.clear(); - } - } - - private void addNewItemToHistory(HistoryEntry model) { - HistoryItemDBHelper helper = checkLoadedEntries(); - if (mp.containsKey(model.getName())) { - model = mp.get(model.getName()); - model.markAsAccessed(System.currentTimeMillis()); - helper.update(model); - } else { - loadedEntries.add(model); - mp.put(model.getName(), model); - model.markAsAccessed(System.currentTimeMillis()); - helper.add(model); - } - Collections.sort(loadedEntries, new HistoryEntryComparator()); - if (loadedEntries.size() > HISTORY_LIMIT) { - if (helper.remove(loadedEntries.get(loadedEntries.size() - 1))) { - loadedEntries.remove(loadedEntries.size() - 1); - } - } - } - - private class HistoryItemDBHelper { private static final String DB_NAME = "search_history"; @@ -455,11 +457,5 @@ public class SearchHistoryHelper { } return entries; } - - } - - public void addNewItemToHistory(double latitude, double longitude, PointDescription pointDescription) { - addNewItemToHistory(new HistoryEntry(latitude, longitude, pointDescription)); - } }