import java.awt.Image;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class Categories {
private static Map<String, CategoryMap> categories = new TreeMap<String, CategoryMap>();
public static void addCategory(String category) {
if (! categories.containsKey(category)) {
categories.put(category, new CategoryMap());
}
}
public static void removeCategory(String category) {
categories.remove(category);
}
public static List<Image> getImages(String category, String property) {
return categories.containsKey(category)
? categories.get(category).getImages(property)
: Collections.EMPTY_LIST;
}
public static void addImage(Image image, String category, String property) {
CategoryMap catMap = categories.get(category);
if (catMap == null) {
addCategory(category);
catMap = categories.get(category);
}
catMap.addImage(image, property);
}
public static void removeImage(Image image, String category, String property) {
CategoryMap catMap = categories.get(category);
if (catMap != null) {
catMap.removeImage(image, property);
}
}
public static void removeProperty(String category, String property) {
CategoryMap catMap = categories.get(category);
if (catMap != null) {
catMap.remove(property);
}
}
public static boolean hasCategory(String category) {
return categories.containsKey(category) && ! categories.get(category).isEmpty();
}
public static boolean hasProperty(String category, String property) {
return categories.containsKey(category) &&
categories.get(category).containsKey(property) &&
! categories.get(category).get(property).isEmpty();
}
private static class CategoryMap extends TreeMap<String, List<Image>> {
public List<Image> getImages(String property) {
return (containsKey(property))
? get(property)
: Collections.EMPTY_LIST;
}
public void addImage(Image image, String property) {
List<Image> imgList = get(property);
if (imgList == null) {
imgList = new ArrayList<Image>();
put(property, imgList);
}
imgList.add(image);
}
public void removeImage(Image image, String property) {
List<Image> imgList = get(property);
if (imgList == null) {
imgList.remove(image);
if (imgList.isEmpty()) {
remove(property);
}
}
}
}
}