Use sets instead of lists where possible (don't download duplicates)
parent
fb1b0d3162
commit
b3cd39c72b
|
@ -10,8 +10,10 @@ import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import max.music_cyclon.SynchronizeConfig;
|
import max.music_cyclon.SynchronizeConfig;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
|
@ -29,7 +31,7 @@ public class BeetsFetcher {
|
||||||
this.resources = resources;
|
this.resources = resources;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Item> fetch(SynchronizeConfig config,
|
public Set<Item> fetch(SynchronizeConfig config,
|
||||||
String username, String password) throws IOException {
|
String username, String password) throws IOException {
|
||||||
StringBuilder get;
|
StringBuilder get;
|
||||||
|
|
||||||
|
@ -58,18 +60,18 @@ public class BeetsFetcher {
|
||||||
|
|
||||||
if (response.code() != 200) {
|
if (response.code() != 200) {
|
||||||
Log.e("ERROR", "Server returned HTTP " + response.message());
|
Log.e("ERROR", "Server returned HTTP " + response.message());
|
||||||
return Collections.emptyList();
|
return Collections.emptySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
InputStream stream = response.body().byteStream();
|
InputStream stream = response.body().byteStream();
|
||||||
List<Item> items = parseJson(stream, config.getSize(resources), config.isAlbum(resources));
|
Set<Item> items = parseJson(stream, config.getSize(resources), config.isAlbum(resources));
|
||||||
stream.close();
|
stream.close();
|
||||||
|
|
||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Item> parseJson(InputStream stream, int size, boolean isAlbums) throws IOException {
|
private Set<Item> parseJson(InputStream stream, int size, boolean isAlbums) throws IOException {
|
||||||
JsonReader reader = new JsonReader(new BufferedReader(new InputStreamReader(stream, "UTF-8")));
|
JsonReader reader = new JsonReader(new BufferedReader(new InputStreamReader(stream, "UTF-8")));
|
||||||
List<Item> items = new ArrayList<>();
|
List<Item> items = new ArrayList<>();
|
||||||
List<ArrayList<Item>> albums = new ArrayList<>();
|
List<ArrayList<Item>> albums = new ArrayList<>();
|
||||||
|
@ -90,29 +92,31 @@ public class BeetsFetcher {
|
||||||
|
|
||||||
// Select random
|
// Select random
|
||||||
if (isAlbums) {
|
if (isAlbums) {
|
||||||
List<ArrayList<Item>> randomAlbums = selectRandom(albums, size);
|
Set<ArrayList<Item>> randomAlbums = selectRandom(albums, size);
|
||||||
|
|
||||||
for (ArrayList<Item> album : randomAlbums) {
|
for (List<Item> album : randomAlbums) {
|
||||||
items.addAll(album);
|
items.addAll(album);
|
||||||
}
|
}
|
||||||
return Collections.unmodifiableList(items);
|
|
||||||
|
return Collections.unmodifiableSet(new HashSet<Item>(items));
|
||||||
} else {
|
} else {
|
||||||
return selectRandom(items, size);
|
return selectRandom(items, size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> List<T> selectRandom(List<T> list, int n) {
|
public <T> Set<T> selectRandom(List<T> list, int n) {
|
||||||
if (list.isEmpty()) {
|
if (list.isEmpty()) {
|
||||||
return Collections.emptyList();
|
return Collections.emptySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayList<T> out = new ArrayList<>();
|
Set<T> out = new HashSet<T>();
|
||||||
|
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
int item = list.size() > 1 ? RANDOM.nextInt(list.size() - 1) : 0;
|
int item = list.size() > 1 ? RANDOM.nextInt(list.size() - 1) : 0;
|
||||||
out.add(list.get(item));
|
out.add(list.get(item));
|
||||||
}
|
}
|
||||||
|
|
||||||
return Collections.unmodifiableList(out);
|
return Collections.unmodifiableSet(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ArrayList<Item> parseAlbum(JsonReader reader) throws IOException {
|
private ArrayList<Item> parseAlbum(JsonReader reader) throws IOException {
|
||||||
|
|
|
@ -26,6 +26,7 @@ import java.io.IOException;
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
@ -134,7 +135,7 @@ public class LibraryService extends IntentService {
|
||||||
|
|
||||||
for (Parcelable parcelable : configs) {
|
for (Parcelable parcelable : configs) {
|
||||||
SynchronizeConfig config = (SynchronizeConfig) parcelable;
|
SynchronizeConfig config = (SynchronizeConfig) parcelable;
|
||||||
List<Item> items;
|
Set<Item> items;
|
||||||
try {
|
try {
|
||||||
updater.showOngoingMessage("Fetching music information for %s", config.getName());
|
updater.showOngoingMessage("Fetching music information for %s", config.getName());
|
||||||
items = fetcher.fetch(config,
|
items = fetcher.fetch(config,
|
||||||
|
|
Loading…
Reference in New Issue