// Darren Hardy // MAT 259 - Project One // 11 January 2009 // // Usage: // // String media = ItemType.getFactory().toMedia(t.itemtype); // // XXX: workaround for strange data file errors on laptop String dataPath(String s) { return "/Users/drh/Documents/Processing/HardyProjectOneData/data/" + s; } public class ItemType { private String _itemType = null; private String _media = null; private int _modern = -1; public String toString() { return _media; } public ItemType(String itemType) { _itemType = itemType; if (_itemType != null) { for (Iterator i = _map.entrySet().iterator(); i.hasNext(); ) { Map.Entry me = (Map.Entry)i.next(); String k = (String)me.getKey(); if (_itemType.endsWith(k)) { Object v[] = (Object[])me.getValue(); _media = ((String)v[0]); _modern = ((Integer)v[1]).intValue(); } } } } // returns the item type public String getItemType() { return _itemType; } // returns the media description public String getMedia() { return _media; } public int getModern() { return _modern; } // true if holding is classified for Adult public boolean isAdult() { return _itemType != null ? _itemType.charAt(0) == 'a' : false; } } // ------------------- final private Map _map = loadMap(); // loads an itemtype suffix to media descripton mapping private Map loadMap() { String lines[] = loadStrings(dataPath("mediamap.txt")); if (lines == null) { throw new RuntimeException("ItemType: cannot load mapping data"); } HashMap hm = new HashMap(); for (int i = 0; i < lines.length; i++) { if (lines[i].length() < 1 || lines[i].charAt(0) == '#') { continue; } String s[] = splitTokens(lines[i], WHITESPACE + ","); if (s.length == 3) { String suffix = s[0]; String media = s[1]; Integer modern = new Integer(s[2]); println(suffix + "->[" + media + "," + modern + "]"); Object rec[] = new Object[2]; rec[0] = media; rec[1] = modern; hm.put(suffix, rec); // register } else { println("WARNING: cannot parse: " + lines[i]); } } return hm; }