001    package aima.basic;
002    
003    import java.util.ArrayList;
004    import java.util.Iterator;
005    import java.util.List;
006    
007    /**
008     * A simple object for maintaining a list of Percepts and performing equality based on the
009     * value of the Percepts it contains.
010     */
011    
012    /**
013     * @author Ciaran O'Reilly
014     * 
015     */
016    public class PerceptSequence {
017            private final List<Percept> percepts = new ArrayList<Percept>();
018    
019            public PerceptSequence() {
020            }
021    
022            public PerceptSequence(Percept p1) {
023                    append(p1);
024            }
025    
026            public PerceptSequence(Percept p1, Percept p2) {
027                    append(p1);
028                    append(p2);
029            }
030    
031            public PerceptSequence(Percept p1, Percept p2, Percept p3) {
032                    append(p1);
033                    append(p2);
034                    append(p3);
035            }
036    
037            public void append(Percept percept) {
038                    assert (null != percept);
039    
040                    percepts.add(percept);
041            }
042    
043            @Override
044            public boolean equals(Object o) {
045                    if (o == null || !(o instanceof PerceptSequence)) {
046                            return super.equals(o);
047                    }
048                    return (toString().equals(((PerceptSequence) o).toString()));
049            }
050    
051            @Override
052            public int hashCode() {
053                    return toString().hashCode();
054            }
055    
056            @Override
057            public String toString() {
058                    StringBuffer sb = new StringBuffer();
059    
060                    Iterator<Percept> pers = percepts.iterator();
061                    while (pers.hasNext()) {
062                            Percept p = pers.next();
063    
064                            sb.append(p);
065    
066                            if (pers.hasNext()) {
067                                    sb.append(", ");
068                            }
069                    }
070    
071                    return sb.toString();
072            }
073    }