From 7c6d09f5f2e4e35d3218985dd4ac2d3a7b55a75d Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Wed, 19 Sep 2018 14:23:33 +0300 Subject: [PATCH] Fix formatting in SearchHistoryHelper --- .../plus/helpers/SearchHistoryHelper.java | 219 +++++++++--------- 1 file changed, 108 insertions(+), 111 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/helpers/SearchHistoryHelper.java b/OsmAnd/src/net/osmand/plus/helpers/SearchHistoryHelper.java index 8bb29014ed..2c8f3fb3e5 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/SearchHistoryHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/SearchHistoryHelper.java @@ -16,40 +16,41 @@ import java.util.List; import java.util.Map; public class SearchHistoryHelper { - + private static final int HISTORY_LIMIT = 1500; + private OsmandApplication context; private List loadedEntries = null; - private Map mp = new HashMap(); - + private Map mp = new HashMap<>(); + public SearchHistoryHelper(OsmandApplication context) { this.context = context; } - private static SearchHistoryHelper instance = null; - public static SearchHistoryHelper getInstance(OsmandApplication context){ - if(instance == null) { + private static SearchHistoryHelper instance = null; + + public static SearchHistoryHelper getInstance(OsmandApplication context) { + if (instance == null) { instance = new SearchHistoryHelper(context); } return instance; } - - - private static final int[] DEF_INTERVALS_MIN = new int[] { - 5, 60, 60 * 24, 5 * 60 * 24, 10 * 60 * 24, 30 * 60 * 24 + + private static final int[] DEF_INTERVALS_MIN = new int[]{ + 5, 60, 60 * 24, 5 * 60 * 24, 10 * 60 * 24, 30 * 60 * 24 }; - + private static class HistoryEntryComparator implements Comparator { long time = System.currentTimeMillis(); - + @Override public int compare(HistoryEntry lhs, HistoryEntry rhs) { double l = lhs.getRank(time); double r = rhs.getRank(time); return -Double.compare(l, r); } - }; - + } + public static class HistoryEntry { double lat; double lon; @@ -57,15 +58,15 @@ public class SearchHistoryHelper { private long lastAccessedTime; private int[] intervals = new int[0]; private double[] intervalValues = new double[0]; - - public HistoryEntry(double lat, double lon, PointDescription name){ + + HistoryEntry(double lat, double lon, PointDescription name) { this.lat = lat; this.lon = lon; this.name = name; } - + private double rankFunction(double cf, double timeDiff) { - if(timeDiff <= 0) { + if (timeDiff <= 0) { return 0; } return cf / timeDiff; @@ -79,7 +80,7 @@ public class SearchHistoryHelper { for (int k = 0; k < intervals.length; k++) { double ntimeDiff = intervals[k] * 60 * 1000; double nvl = intervalValues[k]; - if(ntimeDiff < timeDiff || nvl <= vl){ + if (ntimeDiff < timeDiff || nvl <= vl) { continue; } rnk += rankFunction(nvl - vl, baseTimeDiff + (ntimeDiff - timeDiff) / 2 + timeDiff); @@ -92,22 +93,23 @@ public class SearchHistoryHelper { public PointDescription getName() { return name; } - + public String getSerializedName() { return PointDescription.serializeToString(name); } - + public double getLat() { return lat; } + public double getLon() { return lon; } - + public void markAsAccessed(long time) { int[] nintervals = new int[DEF_INTERVALS_MIN.length]; double[] nintervalValues = new double[DEF_INTERVALS_MIN.length]; - for(int k = 0; k < nintervals.length; k++) { + for (int k = 0; k < nintervals.length; k++) { nintervals[k] = DEF_INTERVALS_MIN[k]; nintervalValues[k] = getUsageLastTime(time, 0, 0, nintervals[k]) + 1; } @@ -115,8 +117,8 @@ public class SearchHistoryHelper { intervalValues = nintervalValues; this.lastAccessedTime = time; } - - public double getUsageLastTime(long time, int days, int hours, int minutes) { + + double getUsageLastTime(long time, int days, int hours, int minutes) { long mins = (minutes + (hours + 24 * days) * 60); long timeInPast = time - mins * 60 * 1000; if (this.lastAccessedTime <= timeInPast) { @@ -139,7 +141,7 @@ public class SearchHistoryHelper { } public void setFrequency(String intervalsString, String values) { - if(Algorithms.isEmpty(intervalsString) || Algorithms.isEmpty(values)) { + if (Algorithms.isEmpty(intervalsString) || Algorithms.isEmpty(values)) { markAsAccessed(this.lastAccessedTime); return; } @@ -148,7 +150,7 @@ public class SearchHistoryHelper { intervals = new int[ints.length]; intervalValues = new double[ints.length]; try { - for(int i = 0; i < ints.length && i < vsl.length; i++) { + for (int i = 0; i < ints.length && i < vsl.length; i++) { intervals[i] = Integer.parseInt(ints[i]); intervalValues[i] = Double.parseDouble(vsl[i]); } @@ -156,26 +158,26 @@ public class SearchHistoryHelper { e.printStackTrace(); } } - + public long getLastAccessTime() { return lastAccessedTime; } - + public String getIntervalsValues() { StringBuilder s = new StringBuilder(); - for(int i = 0; i < intervalValues.length; i++) { - if(i > 0) { + for (int i = 0; i < intervalValues.length; i++) { + if (i > 0) { s.append(","); } s.append(intervalValues[i]); } return s.toString(); } - + public String getIntervals() { StringBuilder s = new StringBuilder(); - for(int i = 0; i < intervals.length; i++) { - if(i > 0) { + for (int i = 0; i < intervals.length; i++) { + if (i > 0) { s.append(","); } s.append(intervals[i]); @@ -188,20 +190,20 @@ public class SearchHistoryHelper { } } - + public List getHistoryEntries() { - if(loadedEntries == null){ + if (loadedEntries == null) { checkLoadedEntries(); } - return new ArrayList(loadedEntries); + return new ArrayList<>(loadedEntries); } - + private HistoryItemDBHelper checkLoadedEntries() { HistoryItemDBHelper helper = new HistoryItemDBHelper(); if (loadedEntries == null) { loadedEntries = helper.getEntries(); Collections.sort(loadedEntries, new HistoryEntryComparator()); - for(HistoryEntry he : loadedEntries) { + for (HistoryEntry he : loadedEntries) { mp.put(he.getName(), he); } } @@ -224,9 +226,9 @@ public class SearchHistoryHelper { } } - public void addNewItemToHistory(HistoryEntry model) { + private void addNewItemToHistory(HistoryEntry model) { HistoryItemDBHelper helper = checkLoadedEntries(); - if(mp.containsKey(model.getName())) { + if (mp.containsKey(model.getName())) { model = mp.get(model.getName()); model.markAsAccessed(System.currentTimeMillis()); helper.update(model); @@ -237,8 +239,8 @@ public class SearchHistoryHelper { helper.add(model); } Collections.sort(loadedEntries, new HistoryEntryComparator()); - if(loadedEntries.size() > HISTORY_LIMIT){ - if(helper.remove(loadedEntries.get(loadedEntries.size() - 1))){ + if (loadedEntries.size() > HISTORY_LIMIT) { + if (helper.remove(loadedEntries.get(loadedEntries.size() - 1))) { loadedEntries.remove(loadedEntries.size() - 1); } } @@ -247,26 +249,26 @@ public class SearchHistoryHelper { private class HistoryItemDBHelper { - private static final String DB_NAME = "search_history"; //$NON-NLS-1$ + private static final String DB_NAME = "search_history"; private static final int DB_VERSION = 2; - private static final String HISTORY_TABLE_NAME = "history_recents"; //$NON-NLS-1$ - private static final String HISTORY_COL_NAME = "name"; //$NON-NLS-1$ - private static final String HISTORY_COL_TIME = "time"; //$NON-NLS-1$ - private static final String HISTORY_COL_FREQ_INTERVALS = "freq_intervals"; //$NON-NLS-1$ - private static final String HISTORY_COL_FREQ_VALUES = "freq_values"; //$NON-NLS-1$ - private static final String HISTORY_COL_LAT = "latitude"; //$NON-NLS-1$ - private static final String HISTORY_COL_LON = "longitude"; //$NON-NLS-1$ - private static final String HISTORY_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " + HISTORY_TABLE_NAME + " (" + //$NON-NLS-1$ //$NON-NLS-2$ - HISTORY_COL_NAME + " TEXT, " + - HISTORY_COL_TIME + " long, " + - HISTORY_COL_FREQ_INTERVALS + " TEXT, " + - HISTORY_COL_FREQ_VALUES + " TEXT, " + - HISTORY_COL_LAT + " double, " +HISTORY_COL_LON + " double);"; //$NON-NLS-1$ //$NON-NLS-2$ + private static final String HISTORY_TABLE_NAME = "history_recents"; + private static final String HISTORY_COL_NAME = "name"; + private static final String HISTORY_COL_TIME = "time"; + private static final String HISTORY_COL_FREQ_INTERVALS = "freq_intervals"; + private static final String HISTORY_COL_FREQ_VALUES = "freq_values"; + private static final String HISTORY_COL_LAT = "latitude"; + private static final String HISTORY_COL_LON = "longitude"; + private static final String HISTORY_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " + HISTORY_TABLE_NAME + " (" + + HISTORY_COL_NAME + " TEXT, " + + HISTORY_COL_TIME + " long, " + + HISTORY_COL_FREQ_INTERVALS + " TEXT, " + + HISTORY_COL_FREQ_VALUES + " TEXT, " + + HISTORY_COL_LAT + " double, " + HISTORY_COL_LON + " double);"; - - public HistoryItemDBHelper() { + + HistoryItemDBHelper() { } - + private SQLiteConnection openConnection(boolean readonly) { SQLiteConnection conn = context.getSQLiteAPI().getOrCreateDatabase(DB_NAME, readonly); if (conn.getVersion() == 0 || DB_VERSION != conn.getVersion()) { @@ -280,7 +282,6 @@ public class SearchHistoryHelper { onUpgrade(conn, conn.getVersion(), DB_VERSION); } conn.setVersion(DB_VERSION); - } return conn; } @@ -290,17 +291,17 @@ public class SearchHistoryHelper { } public void onUpgrade(SQLiteConnection db, int oldVersion, int newVersion) { - if(newVersion == 2) { + if (newVersion == 2) { db.execSQL(HISTORY_TABLE_CREATE); - for(HistoryEntry he : getLegacyEntries(db)) { + for (HistoryEntry he : getLegacyEntries(db)) { insert(he, db); } } } - - public boolean remove(HistoryEntry e){ + + public boolean remove(HistoryEntry e) { SQLiteConnection db = openConnection(false); - if(db != null){ + if (db != null) { try { removeQuery(e.getSerializedName(), db); } finally { @@ -313,14 +314,14 @@ public class SearchHistoryHelper { private void removeQuery(String name, SQLiteConnection db) { db.execSQL("DELETE FROM " + HISTORY_TABLE_NAME + " WHERE " + HISTORY_COL_NAME + " = ?", - new Object[] { name }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + new Object[]{name}); } - - public boolean removeAll(){ + + public boolean removeAll() { SQLiteConnection db = openConnection(false); - if(db != null){ + if (db != null) { try { - db.execSQL("DELETE FROM " + HISTORY_TABLE_NAME); //$NON-NLS-1$ + db.execSQL("DELETE FROM " + HISTORY_TABLE_NAME); } finally { db.close(); } @@ -328,18 +329,18 @@ public class SearchHistoryHelper { } return false; } - - public boolean update(HistoryEntry e){ + + public boolean update(HistoryEntry e) { SQLiteConnection db = openConnection(false); - if(db != null){ + if (db != null) { try { db.execSQL( - "UPDATE " + HISTORY_TABLE_NAME + " SET " + HISTORY_COL_TIME + "= ? "+ + "UPDATE " + HISTORY_TABLE_NAME + " SET " + HISTORY_COL_TIME + "= ? " + ", " + HISTORY_COL_FREQ_INTERVALS + " = ? " + - ", " +HISTORY_COL_FREQ_VALUES + "= ? WHERE " + - HISTORY_COL_NAME + " = ?", - new Object[] { e.getLastAccessTime(), e.getIntervals(), e.getIntervalsValues(), - e.getSerializedName() }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + ", " + HISTORY_COL_FREQ_VALUES + "= ? WHERE " + + HISTORY_COL_NAME + " = ?", + new Object[]{e.getLastAccessTime(), e.getIntervals(), e.getIntervalsValues(), + e.getSerializedName()}); } finally { db.close(); } @@ -347,10 +348,10 @@ public class SearchHistoryHelper { } return false; } - - public boolean add(HistoryEntry e){ + + public boolean add(HistoryEntry e) { SQLiteConnection db = openConnection(false); - if(db != null){ + if (db != null) { try { insert(e, db); } finally { @@ -363,17 +364,17 @@ public class SearchHistoryHelper { private void insert(HistoryEntry e, SQLiteConnection db) { db.execSQL( - "INSERT INTO " + HISTORY_TABLE_NAME + " VALUES (?, ?, ?, ?, ?, ?)", - new Object[] { e.getSerializedName(), e.getLastAccessTime(), - e.getIntervals(), e.getIntervalsValues(), e.getLat(), e.getLon() }); //$NON-NLS-1$ //$NON-NLS-2$ - } - - public List getLegacyEntries(SQLiteConnection db){ - List entries = new ArrayList(); + "INSERT INTO " + HISTORY_TABLE_NAME + " VALUES (?, ?, ?, ?, ?, ?)", + new Object[]{e.getSerializedName(), e.getLastAccessTime(), + e.getIntervals(), e.getIntervalsValues(), e.getLat(), e.getLon()}); + } + + List getLegacyEntries(SQLiteConnection db) { + List entries = new ArrayList<>(); if (db != null) { // LEGACY QUERY !! SQLiteCursor query = db.rawQuery( - "SELECT name, latitude, longitude, time FROM history ORDER BY time DESC", null); //$NON-NLS-1$//$NON-NLS-2$ + "SELECT name, latitude, longitude, time FROM history ORDER BY time DESC", null); if (query != null && query.moveToFirst()) { do { String name = query.getString(0); @@ -401,51 +402,51 @@ public class SearchHistoryHelper { entries.add(e); } while (query.moveToNext()); } - if(query != null) { + if (query != null) { query.close(); } } return entries; } - - public List getEntries(){ - List entries = new ArrayList(); + + public List getEntries() { + List entries = new ArrayList<>(); SQLiteConnection db = openConnection(true); - if(db != null){ + if (db != null) { try { SQLiteCursor query = db.rawQuery( - "SELECT " + HISTORY_COL_NAME + ", " + HISTORY_COL_LAT + "," + HISTORY_COL_LON +", " + + "SELECT " + HISTORY_COL_NAME + ", " + HISTORY_COL_LAT + "," + HISTORY_COL_LON + ", " + HISTORY_COL_TIME + ", " + HISTORY_COL_FREQ_INTERVALS + ", " + HISTORY_COL_FREQ_VALUES + - " FROM " + HISTORY_TABLE_NAME , null); //$NON-NLS-1$//$NON-NLS-2$ - Map st = new HashMap(); + " FROM " + HISTORY_TABLE_NAME, null); + Map st = new HashMap<>(); if (query != null && query.moveToFirst()) { boolean reinsert = false; do { String name = query.getString(0); PointDescription p = PointDescription.deserializeFromString(name, new LatLon(query.getDouble(1), query.getDouble(2))); - HistoryEntry e = new HistoryEntry(query.getDouble(1), query.getDouble(2), + HistoryEntry e = new HistoryEntry(query.getDouble(1), query.getDouble(2), p); long time = query.getLong(3); e.setLastAccessTime(time); e.setFrequency(query.getString(4), query.getString(5)); - if(st.containsKey(p)) { + if (st.containsKey(p)) { reinsert = true; } entries.add(e); st.put(p, e); } while (query.moveToNext()); - if(reinsert) { + if (reinsert) { System.err.println("Reinsert all values for search history"); - db.execSQL("DELETE FROM " + HISTORY_TABLE_NAME); //$NON-NLS-1$ + db.execSQL("DELETE FROM " + HISTORY_TABLE_NAME); entries.clear(); entries.addAll(st.values()); - for(HistoryEntry he : entries) { + for (HistoryEntry he : entries) { insert(he, db); } - + } } - if(query != null) { + if (query != null) { query.close(); } } finally { @@ -454,15 +455,11 @@ public class SearchHistoryHelper { } return entries; } - + } public void addNewItemToHistory(double latitude, double longitude, PointDescription pointDescription) { addNewItemToHistory(new HistoryEntry(latitude, longitude, pointDescription)); - - } - - - + } }