import java.io.*; import java.util.*; public abstract class AbstractHandler implements Handler { private static List list(String s) { List lst = new ArrayList(); lst.add(s); return lst; } /** The extensions for the files about which we know. */ private final List exts = new ArrayList(); /** The hash of key-value pairs for current file (not multi-threaded). */ private Map hash; /** The keys of key-value pairs for current file (not multi-threaded). */ private List keys; public AbstractHandler() { this((String[])null); } public AbstractHandler(String... exts) { if (exts != null) { for (String ext : exts) { this.exts.add(ext); } } } public final List extensions() {return this.exts;} public final Map explain(File f) { // Create the new values for member state hash = new HashMap(); keys = new ArrayList(); // Use some default key-value pairs add("name", f.getName()); add("size", Util.getInstance().niceSize(f)); add("modified", new Date(f.lastModified())); explainRest(f); return hash; } public final List keys() { return keys; } /** * Subclasses use {@see #add(String,String)} to add relevant * key-value pairs for the {@see File} f. * * @param f File in question */ abstract void explainRest(File f); /** * Adds a key-value pair to the current file's hash. * * @param key new key * @param val new value */ protected final void add(String key,Object val) { if (val == null || val.equals("")) return; key = capitalize(key); hash.put(key,String.valueOf(val)); keys.add(key); } /** * Capitalizes s. * * @param s string to capitalize */ protected final String capitalize(String s) { if (s == null) return s; char first = Character.toUpperCase(s.charAt(0)); return first + s.substring(1); } }