View Javadoc

1   package org.musicontroller.core;
2   
3   import java.util.LinkedList;
4   import java.util.List;
5   
6   /**
7    * Implements a bag of keywords that can be attached to a song, band, ...
8    * 
9    * @author verstoep
10   * @version $Id: Keywordbag.java,v 1.1 2010/03/16 18:55:42 varienaja Exp $
11   */
12  public class Keywordbag {
13  	private long id;
14  	private List<Keyword> keywords;
15  	
16  	public Keywordbag() {
17  		id=-1L;
18  		keywords = new LinkedList<Keyword>();
19  	}
20  
21  	public List<Keyword> getKeywords() {
22  		return keywords;
23  	}
24  	
25  
26  	public void setKeywords(List<Keyword> keywords) {
27  		this.keywords = keywords;
28  	}
29  
30  	public long getId() {
31  		return id;
32  	}
33  	
34  
35  	public void setId(long id) {
36  		this.id = id;
37  	}	
38  	
39  	/**
40  	 * Implement equality test on keyword bags. Two keyword bags
41  	 * are equal if they contain the same set of keywords.
42  	 */
43  	@Override
44  	public boolean equals(Object other) {
45  		if (this==other) {
46  			return true;
47  		}
48  		if (other instanceof Keywordbag) {
49  			Keywordbag otherBag = (Keywordbag) other;
50  			boolean result = this.getKeywords().containsAll(otherBag.getKeywords()) &&
51  				otherBag.getKeywords().containsAll(this.getKeywords());
52  			return result;
53  		} else {
54  			return false;
55  		}
56  	}
57  	
58  	@Override
59  	/**
60  	 * Two keyword bags are equal iff they contain the same keywords. Compute
61  	 * the hash code from all keywords in the bag to reflect this.
62  	 */
63  	public int hashCode() {
64  		int result = 0;
65  		for (Keyword kw : getKeywords()) {
66  			result = (17 * result) + kw.hashCode();
67  		}
68  		return result;
69  	}
70  
71  	/*
72  	 * (non-Javadoc)
73  	 * @see java.lang.Object#toString()
74  	 */
75  	public String toString() {
76  		StringBuffer sb = new StringBuffer();
77  		boolean first = true;
78  		for (Keyword kw : getKeywords()) {
79  			if(!first) {
80  				sb.append(",");
81  			} else {
82  				first = false;
83  			}
84  			sb.append(kw.toString());
85  		}
86  		return sb.toString();
87  	}
88  
89  }