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;
//fromwww.java2s.comimport android.content.Context;
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.Chef;
import com.github.digin.android.models.Winery;
import com.github.digin.android.tasks.ParseAllWineriesTask;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
/**
* Created by mike on 8/10/14.
*/publicabstractclass WineryStore {
privatestatic List<Winery> wineryCache;
publicstaticvoid getWineries(Context context, final OnParticipantQueryListener<Winery> listener) {
if (wineryCache != null) {
if (listener != null) {
listener.onComplete(wineryCache);
}
return;
}
ParseAllWineriesTask task = new ParseAllWineriesTask(context, new OnParticipantQueryListener<Winery>() {
@Override
publicvoid onComplete(List<Winery> wineries) {
// Sort
Collections.sort(wineries, new Comparator<Winery>() {
publicint compare(Winery lhs, Winery rhs) {
return lhs.getName().compareTo(rhs.getName());
}
});
wineryCache = wineries;
if (listener != null) {
listener.onComplete(wineries);
}
}
});
task.execute();
}
publicstaticvoid getWineriesNoCache(Context context, OnParticipantQueryListener<Winery> listener) {
wineryCache = null;
getWineries(context, listener);
}
publicstaticvoid getWineryById(Context context, final String id, final OnSingleParticipantQueryListener<Winery> listener) {
Logger.log(ChefsStore.class, "Getting chef for ID");
getWineries(context, new OnParticipantQueryListener<Winery>() {
@Override
publicvoid onComplete(List<Winery> wineries) {
for (Winery winery : wineries) {
if (winery.getId().equals(id)) {
listener.onComplete(winery);
return;
}
}
Logger.log(WineryStore.class, "Found no winery matching given ID");
listener.onComplete(null);
}
});
}
publicstaticvoid batchGetWineryById(Context context, final Set<String> ids, final OnParticipantQueryListener<Winery> listener) {
Logger.log(ChefsStore.class, "Getting a subset of chefs by id");
getWineries(context, new OnParticipantQueryListener<Winery>() {
publicvoid onComplete(List<Winery> chefs) {
List<Winery> subset = new LinkedList<Winery>();
for (Winery chef : chefs) {
if (ids.contains(chef.getId())) {
subset.add(chef);
}
}
if (listener != null) {
listener.onComplete(subset);
}
}
});
}
}