View Javadoc

1   package org.musicontroller.core;
2   
3   import java.util.LinkedList;
4   import java.util.List;
5   
6   /**
7    * @author Varienaja
8    */
9   public class AIBag {
10  	public static List<AIBag> aibags;
11  	
12  	private long id;
13  	private List<AIRelation> relations;
14  	
15  	public AIBag() {
16  		id=-1;
17  		relations = new LinkedList<AIRelation>();
18  	}
19  	
20  	public long getId() {
21  		return id;
22  	}
23  	
24  	public void setId(long id) {
25  		this.id = id;
26  	}
27  	
28  	public List<AIRelation> getRelations() {
29  		return relations;
30  	}
31  	
32  	public void setRelations(List<AIRelation> relations) {
33  		this.relations = relations;
34  	}
35  	
36  	/**
37  	 * Implement equality test on artist-instrument bags. Two bags
38  	 * are equal if they contain the same set of artist instrument relations.
39  	 */
40  	@Override
41  	public boolean equals(Object other) {
42  		if (this==other) {
43  			return true;
44  		}
45  		if (other instanceof AIBag) {
46  			AIBag otherBag = (AIBag) other;
47  			boolean result = true;
48  			for (AIRelation inMyBag : this.getRelations()) {
49  				boolean foundInOtherBag = false;
50  				for (AIRelation inOtherBag : otherBag.getRelations()) {
51  					if (inMyBag.equals(inOtherBag)) {
52  						foundInOtherBag = true;
53  						break;
54  					}
55  				}
56  				if(!foundInOtherBag) {
57  					result = false;
58  					break;
59  				}
60  			}
61  			for (AIRelation inOtherBag : otherBag.getRelations()) {
62  				boolean foundInMyBag = false;
63  				for (AIRelation inMyBag : this.getRelations()) {
64  					if (inMyBag.equals(inOtherBag)) {
65  						foundInMyBag = true;
66  						break;
67  					}
68  				}
69  				if(!foundInMyBag) {
70  					result = false;
71  					break;
72  				}
73  			}
74  			return result;
75  		} else {
76  			return false;
77  		}
78  	}
79  	
80  	@Override
81  	/**
82  	 * Two artist instrument bags are equal iff they contain the same artist-instrument relations.
83  	 * Compute the hash code from all relations in the bag to reflect this.
84  	 */
85  	public int hashCode() {
86  		int result = 0;
87  		for (AIRelation rel : getRelations()) {
88  			result += 3; // Just having a AIRelation must alter the hash code.
89  			result = (17 * result) + rel.hashCode();
90  		}
91  		return result;
92  	}
93  
94  }