001 package aima.basic; 002 003 import java.util.Hashtable; 004 import java.util.Iterator; 005 import java.util.TreeSet; 006 007 /** 008 * @author Ravi Mohan 009 * 010 */ 011 public class ObjectWithDynamicAttributes { 012 private Hashtable<Object, Object> attributes = new Hashtable<Object, Object>(); 013 014 public void setAttribute(Object key, Object value) { 015 attributes.put(key, value); 016 } 017 018 public Object getAttribute(Object key) { 019 return attributes.get(key); 020 } 021 022 public void removeAttribute(Object key) { 023 attributes.remove(key); 024 } 025 026 public Iterator<Object> getSortedAttributeKeys() { 027 // Want to guarantee the keys are returned back ordered 028 // Note: This is an inefficient implementation as it creates a new 029 // TreeSet each time it is called 030 // Ideally you be an instance variable that is only updated on the 031 // setAttribute() call. 032 return (new TreeSet<Object>(attributes.keySet())).iterator(); 033 } 034 }