blob: 9a885ca856bd96fed2b49fd4d390e43b8d410fc6 [file] [log] [blame]
package com.google.lizlooney.shoppinglist;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
/**
* Class maintaining the set of all stores.
*
* @author lizlooney@gmail.com (Liz Looney)
*/
public class Stores {
private final Object lock = new Object();
private final Set<String> stores = new TreeSet<>();
private boolean missingStore;
public void loadStores(List<Item> items) {
synchronized (lock) {
// Loads the stores TreeSet with the stores that are used in the items.
clear();
missingStore = false;
for (Item item: items) {
add(item);
}
}
}
public void clear() {
synchronized (lock) {
stores.clear();
missingStore = false;
}
}
public void add(Item item) {
synchronized (lock) {
if (item.isMissingStore()) {
missingStore = true;
}
for (String store : item.getStores()) {
stores.add(store);
}
}
}
public String[] getStoresArray() {
synchronized (lock) {
return stores.toArray(new String[0]);
}
}
public Collection<String> getStoresForStoreFilter() {
synchronized (lock) {
List<String> storesForStoreFilter = new ArrayList<>();
storesForStoreFilter.add(ShoppingList.STORE_FILTER_ALL);
if (missingStore) {
storesForStoreFilter.add(ShoppingList.STORE_FILTER_MISSING);
}
storesForStoreFilter.addAll(stores);
return storesForStoreFilter;
}
}
}