|
|
|
/*
|
|
|
|
* To change this license header, choose License Headers in Project Properties.
|
|
|
|
* To change this template file, choose Tools | Templates
|
|
|
|
* and open the template in the editor.
|
|
|
|
*/
|
|
|
|
package moe.nekojimi.musicsearcher.providers;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
import moe.nekojimi.musicsearcher.Result;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author jim
|
|
|
|
*/
|
|
|
|
public class MetaSearcher extends Searcher
|
|
|
|
{
|
|
|
|
|
|
|
|
private final Set<Searcher> searchers = new HashSet<>();
|
|
|
|
private int minSearchTime = 10000; // ms
|
|
|
|
private int maxSearchTime = 30000; // ms
|
|
|
|
|
|
|
|
public MetaSearcher()
|
|
|
|
{
|
|
|
|
super("meta");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected List<Result> doSearch(String query)
|
|
|
|
{
|
|
|
|
List<Result> results = new ArrayList<>();
|
|
|
|
List<CompletableFuture<List<Result>>> searches = new ArrayList<>();
|
|
|
|
for (Searcher searcher: searchers)
|
|
|
|
{
|
|
|
|
CompletableFuture<List<Result>> search = searcher.search(query);
|
|
|
|
searches.add(search);
|
|
|
|
search.whenComplete((t, u) ->
|
|
|
|
{
|
|
|
|
if (u == null)
|
|
|
|
{
|
|
|
|
results.addAll(t);
|
|
|
|
// searches.remove(search);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
CompletableFuture.allOf((CompletableFuture<?>[]) searches.toArray());
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|