View Javadoc

1   package org.varienaja.comments;
2   
3   import java.io.Serializable;
4   import java.util.Date;
5   import java.util.LinkedList;
6   import java.util.List;
7   
8   /**
9    * Class representing a Comment
10   * @author Varienaja
11   */
12  public class Comment implements Serializable, Comparable<Comment> {
13  	private static final long serialVersionUID = 20080224172312L;
14  	
15  	private String _title;
16  	private String _source;
17  	private List<CommentElement> _elements;
18  	private Date _date;
19  	
20  	public Comment(String source, String title, Date date) {
21  		_source = source==null ? "" : source;
22  		_title = title==null ? "" : title;
23  		_date = date==null ? new Date() : date;
24  		_elements = new LinkedList<CommentElement>();
25  	}
26  	
27  	public String getTitle() {
28  		return _title;
29  	}
30  	
31  	public void setTitle(String title) {
32  		_title = title;
33  	}
34  	
35  	public String getSource() {
36  		return _source;
37  	}
38  	
39  	public List<CommentElement> getElements() {
40  		return _elements;
41  	}
42  	
43  	public void addElement(CommentElement element) {
44  		_elements.add(element);
45  	}
46  	
47  	/**
48  	 * Splits an existing CommentElement into several others.
49  	 * @param src The source element.
50  	 * @param dest The destination elements.
51  	 */
52  	public void splitElement(CommentElement src, List<CommentElement> dest) {
53  		int index = _elements.indexOf(src);
54  		if (index!=-1) {
55  			_elements.remove(index);
56  			for (CommentElement element : dest) {
57  				_elements.add(index++, element);
58  			}
59  		}
60  	}
61  	
62  	public void setDate(Date date) {
63  		_date = date;
64  	}
65  	
66  	public Date getDate() {
67  		return _date;
68  	}
69  	
70  	public boolean equals(Object o) {
71  		if (!(o instanceof Comment)) return false;
72  		
73  		Comment other = (Comment) o;
74  		return _title.equals(other._title) &&
75  			_source.equals(other._source) &&
76  			_date.equals(other._date);
77  	}
78  	
79  	public int hashCode() {
80  		return _date.hashCode()*_source.hashCode()*_title.hashCode();
81  	}
82  
83  	/**
84  	 * Sort by date descending
85  	 */
86  	public int compareTo(Comment other) {
87  		return -_date.compareTo(other._date);
88  	}
89  	
90  	/**
91  	 * Returns the title and contents of the review.
92  	 */
93  	@Override
94  	public String toString() {
95  		StringBuilder sb = new StringBuilder();
96  		sb.append(getTitle());
97  		sb.append(' ');
98  		for (CommentElement element : getElements()) {
99  			sb.append(element.getText());
100 			sb.append(' ');
101 		}
102 		return sb.toString();
103 	}
104 	
105 
106 }