| package com.google.lizlooney.shoppinglist; |
| |
| import java.util.List; |
| import java.util.TreeSet; |
| |
| /** |
| * Class maintaining the set of all categories. |
| * |
| * @author lizlooney@gmail.com (Liz Looney) |
| */ |
| public final class Categories { |
| private final Object lock = new Object(); |
| private final TreeSet<String> categories = new TreeSet<>(); |
| |
| public void loadCategories(List<Item> items) { |
| synchronized (lock) { |
| // Loads the categories TreeSet with the categories that are used in the items. |
| clear(); |
| for (Item item: items) { |
| add(item); |
| } |
| } |
| } |
| |
| public void clear() { |
| synchronized (lock) { |
| categories.clear(); |
| } |
| } |
| |
| public void add(Item item) { |
| synchronized (lock) { |
| categories.add(item.getCategory()); |
| } |
| } |
| |
| public String[] getCategoriesArray() { |
| synchronized (lock) { |
| return categories.toArray(new String[0]); |
| } |
| } |
| } |