If you think the Android project android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.github.digin.android.repositories;
/*www.java2s.com*/import android.app.Activity;
import android.content.Context;
import com.github.digin.android.fragments.BreweryDetailsFragment;
import com.github.digin.android.listeners.OnParticipantQueryListener;
import com.github.digin.android.listeners.OnSingleParticipantQueryListener;
import com.github.digin.android.logging.Logger;
import com.github.digin.android.models.Brewery;
import com.github.digin.android.models.Chef;
import com.github.digin.android.models.Winery;
import com.github.digin.android.tasks.ParseAllBreweriesTask;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
/**
* Contains methods which return a list of breweries either stored
* locally or on parse.
* <p/>
* Created by mike on 8/3/14.
*/publicabstractclass BreweryStore {
privatestatic List<Brewery> breweriesCache;
publicstaticvoid getBreweries(Context context, final OnParticipantQueryListener<Brewery> listener) {
if (breweriesCache != null) {
if (listener != null) {
listener.onComplete(breweriesCache);
}
return;
}
ParseAllBreweriesTask task = new ParseAllBreweriesTask(context, new OnParticipantQueryListener<Brewery>() {
@Override
publicvoid onComplete(List<Brewery> breweries) {
// Sort
Collections.sort(breweries, new Comparator<Brewery>() {
publicint compare(Brewery lhs, Brewery rhs) {
return lhs.getName().compareTo(rhs.getName());
}
});
breweriesCache = breweries;
if (listener != null) {
listener.onComplete(breweries);
}
}
});
task.execute();
}
publicstaticvoid getBreweriesNoCache(Context context, final OnParticipantQueryListener<Brewery> listener) {
breweriesCache = null;
getBreweries(context, listener);
}
//TODO: Change wine to beer.
publicstaticvoid getBreweryById(Context context, final String id, final OnSingleParticipantQueryListener<Brewery> listener) {
Logger.log(ChefsStore.class, "Getting chef for ID");
getBreweries(context, new OnParticipantQueryListener<Brewery>() {
@Override
publicvoid onComplete(List<Brewery> wineries) {
for (Brewery winery : wineries) {
if (winery.getId().equals(id)) {
listener.onComplete(winery);
return;
}
}
Logger.log(BreweryStore.class, "Found no brewery matching given ID");
listener.onComplete(null);
}
});
}
publicstaticvoid batchGetBreweryById(Context context, final Set<String> ids, final OnParticipantQueryListener<Brewery> listener) {
Logger.log(ChefsStore.class, "Getting a subset of chefs by id");
getBreweries(context, new OnParticipantQueryListener<Brewery>() {
publicvoid onComplete(List<Brewery> chefs) {
List<Brewery> subset = new LinkedList<Brewery>();
for (Brewery chef : chefs) {
if (ids.contains(chef.getId())) {
subset.add(chef);
}
}
if (listener != null) {
listener.onComplete(subset);
}
}
});
}
}