From 069a2ca329a342ad0559a48719f49264d2610f00 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Mon, 25 Apr 2022 22:45:47 +0200 Subject: Decouple preferences --- .../antennapod/storage/database/LongIntMap.java | 263 --------------------- .../antennapod/storage/database/PodDBAdapter.java | 10 +- 2 files changed, 5 insertions(+), 268 deletions(-) delete mode 100644 storage/database/src/main/java/de/danoeh/antennapod/storage/database/LongIntMap.java (limited to 'storage') diff --git a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/LongIntMap.java b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/LongIntMap.java deleted file mode 100644 index 049b24627..000000000 --- a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/LongIntMap.java +++ /dev/null @@ -1,263 +0,0 @@ -package de.danoeh.antennapod.storage.database; - - -import java.util.Arrays; - -/** - * Fast and memory efficient long to long map - */ -public class LongIntMap { - - private long[] keys; - private int[] values; - private int size; - - /** - * Creates a new LongLongMap containing no mappings. - */ - public LongIntMap() { - this(10); - } - - /** - * Creates a new SparseLongArray containing no mappings that will not - * require any additional memory allocation to store the specified - * number of mappings. If you supply an initial capacity of 0, the - * sparse array will be initialized with a light-weight representation - * not requiring any additional array allocations. - */ - public LongIntMap(int initialCapacity) { - if(initialCapacity < 0) { - throw new IllegalArgumentException("initial capacity must be 0 or higher"); - } - keys = new long[initialCapacity]; - values = new int[initialCapacity]; - size = 0; - } - - /** - * Increases size of array if needed - */ - private void growIfNeeded() { - if (size == keys.length) { - // Resize. - long[] newKeysArray = new long[size * 3 / 2 + 10]; - int[] newValuesArray = new int[size * 3 / 2 + 10]; - System.arraycopy(keys, 0, newKeysArray, 0, size); - System.arraycopy(values, 0, newValuesArray, 0, size); - keys = newKeysArray; - values = newValuesArray; - } - } - - /** - * Gets the long mapped from the specified key, or 0 - * if no such mapping has been made. - */ - public int get(long key) { - return get(key, 0); - } - - /** - * Gets the long mapped from the specified key, or the specified value - * if no such mapping has been made. - */ - public int get(long key, int valueIfKeyNotFound) { - int index = indexOfKey(key); - if(index >= 0) { - return values[index]; - } else { - return valueIfKeyNotFound; - } - } - - /** - * Removes the mapping from the specified key, if there was any. - */ - public boolean delete(long key) { - int index = indexOfKey(key); - - if (index >= 0) { - removeAt(index); - return true; - } else { - return false; - } - } - - /** - * Removes the mapping at the given index. - */ - private void removeAt(int index) { - System.arraycopy(keys, index + 1, keys, index, size - (index + 1)); - System.arraycopy(values, index + 1, values, index, size - (index + 1)); - size--; - } - - /** - * Adds a mapping from the specified key to the specified value, - * replacing the previous mapping from the specified key if there - * was one. - */ - public void put(long key, int value) { - int index = indexOfKey(key); - - if (index >= 0) { - values[index] = value; - } else { - growIfNeeded(); - keys[size] = key; - values[size] = value; - size++; - } - } - - /** - * Returns the number of key-value mappings that this SparseIntArray - * currently stores. - */ - public int size() { - return size; - } - - /** - * Given an index in the range 0...size()-1, returns - * the key from the indexth key-value mapping that this - * SparseLongArray stores. - * - *

The keys corresponding to indices in ascending order are guaranteed to - * be in ascending order, e.g., keyAt(0) will return the - * smallest key and keyAt(size()-1) will return the largest - * key.

- */ - private long keyAt(int index) { - if (index >= size) { - throw new IndexOutOfBoundsException("n >= size()"); - } else if(index < 0) { - throw new IndexOutOfBoundsException("n < 0"); - } - return keys[index]; - } - - /** - * Given an index in the range 0...size()-1, returns - * the value from the indexth key-value mapping that this - * SparseLongArray stores. - * - *

The values corresponding to indices in ascending order are guaranteed - * to be associated with keys in ascending order, e.g., - * valueAt(0) will return the value associated with the - * smallest key and valueAt(size()-1) will return the value - * associated with the largest key.

- */ - private int valueAt(int index) { - if (index >= size) { - throw new IndexOutOfBoundsException("n >= size()"); - } else if(index < 0) { - throw new IndexOutOfBoundsException("n < 0"); - } - return values[index]; - } - - /** - * Returns the index for which {@link #keyAt} would return the - * specified key, or a negative number if the specified - * key is not mapped. - */ - public int indexOfKey(long key) { - for(int i=0; i < size; i++) { - if(keys[i] == key) { - return i; - } - } - return -1; - } - - /** - * Returns an index for which {@link #valueAt} would return the - * specified key, or a negative number if no keys map to the - * specified value. - * Beware that this is a linear search, unlike lookups by key, - * and that multiple keys can map to the same value and this will - * find only one of them. - */ - public int indexOfValue(long value) { - for (int i = 0; i < size; i++) { - if (values[i] == value) { - return i; - } - } - return -1; - } - - /** - * Removes all key-value mappings from this SparseIntArray. - */ - public void clear() { - keys = new long[10]; - values = new int[10]; - size = 0; - } - - /** - * Returns a copy of the values contained in this map. - * - * @return a copy of the values contained in this map - */ - public int[] values() { - return Arrays.copyOf(values, size); - } - - @Override - public boolean equals(Object other) { - if (other == this) { - return true; - } - if (! (other instanceof LongIntMap)) { - return false; - } - LongIntMap otherMap = (LongIntMap) other; - if (size != otherMap.size) { - return false; - } - for (int i = 0; i < size; i++) { - if (keys[i] != otherMap.keys[i] || - values[i] != otherMap.values[i]) { - return false; - } - } - return true; - } - - @Override - public int hashCode() { - int hashCode = 1; - for (int i = 0; i < size; i++) { - long value = values[i]; - hashCode = 31 * hashCode + (int)(value ^ (value >>> 32)); - } - return hashCode; - } - - @Override - public String toString() { - if (size() <= 0) { - return "LongLongMap{}"; - } - - StringBuilder buffer = new StringBuilder(size * 28); - buffer.append("LongLongMap{"); - for (int i=0; i < size; i++) { - if (i > 0) { - buffer.append(", "); - } - long key = keyAt(i); - buffer.append(key); - buffer.append('='); - long value = valueAt(i); - buffer.append(value); - } - buffer.append('}'); - return buffer.toString(); - } -} diff --git a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/PodDBAdapter.java b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/PodDBAdapter.java index f66c385b7..173354197 100644 --- a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/PodDBAdapter.java +++ b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/PodDBAdapter.java @@ -1175,7 +1175,7 @@ public class PodDBAdapter { return result; } - public final LongIntMap getFeedCounters(FeedCounter setting, long... feedIds) { + public final Map getFeedCounters(FeedCounter setting, long... feedIds) { String whereRead; switch (setting) { case SHOW_NEW_UNPLAYED_SUM: @@ -1194,12 +1194,12 @@ public class PodDBAdapter { case SHOW_NONE: // deliberate fall-through default: // NONE - return new LongIntMap(0); + return new HashMap<>(); } return conditionalFeedCounterRead(whereRead, feedIds); } - private LongIntMap conditionalFeedCounterRead(String whereRead, long... feedIds) { + private Map conditionalFeedCounterRead(String whereRead, long... feedIds) { String limitFeeds = ""; if (feedIds.length > 0) { // work around TextUtils.join wanting only boxed items @@ -1222,7 +1222,7 @@ public class PodDBAdapter { + whereRead + " GROUP BY " + KEY_FEED; Cursor c = db.rawQuery(query, null); - LongIntMap result = new LongIntMap(c.getCount()); + Map result = new HashMap<>(); if (c.moveToFirst()) { do { long feedId = c.getLong(0); @@ -1234,7 +1234,7 @@ public class PodDBAdapter { return result; } - public final LongIntMap getPlayedEpisodesCounters(long... feedIds) { + public final Map getPlayedEpisodesCounters(long... feedIds) { String whereRead = KEY_READ + "=" + FeedItem.PLAYED; return conditionalFeedCounterRead(whereRead, feedIds); } -- cgit v1.2.3