001    package aima.basic;
002    
003    import java.util.Iterator;
004    
005    /**
006     * @author Ravi Mohan
007     * 
008     */
009    public class Percept extends ObjectWithDynamicAttributes {
010            public Percept() {
011    
012            }
013    
014            public Percept(Object key1, Object value1) {
015                    setAttribute(key1, value1);
016            }
017    
018            public Percept(Object key1, Object value1, Object key2, Object value2) {
019                    setAttribute(key1, value1);
020                    setAttribute(key2, value2);
021            }
022    
023            public Percept(Object[] keys, Object[] values) {
024                    assert (keys.length == values.length);
025    
026                    for (int i = 0; i < keys.length; i++) {
027                            setAttribute(keys[i], values[i]);
028                    }
029            }
030    
031            @Override
032            public boolean equals(Object o) {
033                    if (o == null || !(o instanceof Percept)) {
034                            return super.equals(o);
035                    }
036                    return (toString().equals(((Percept) o).toString()));
037            }
038    
039            @Override
040            public int hashCode() {
041                    return toString().hashCode();
042            }
043    
044            @Override
045            public String toString() {
046                    StringBuffer sb = new StringBuffer();
047    
048                    boolean first = true;
049                    Iterator<Object> keys = getSortedAttributeKeys();
050                    while (keys.hasNext()) {
051                            Object key = keys.next();
052    
053                            if (first) {
054                                    sb.append("[");
055                                    first = false;
056                            }
057    
058                            sb.append(key);
059                            sb.append("==");
060                            sb.append(getAttribute(key));
061                            if (keys.hasNext()) {
062                                    sb.append(", ");
063                            } else {
064                                    sb.append("]");
065                            }
066                    }
067    
068                    return sb.toString();
069            }
070    }