View Javadoc

1   package org.musicontroller.core;
2   
3   import java.util.Date;
4   import java.util.HashSet;
5   import java.util.Iterator;
6   import java.util.Set;
7   
8   import org.musicontroller.model.Linkable;
9   
10  /**
11   * Implements an artist that performs in songs.
12   * 
13   * Naming conventions for artists:
14   * <ul>
15   *   <li>Each artist must have a last name.</li>
16   *   <li>An artist can have a set of first names.</li>
17   *   <li>Artist names that are not a persons real name, like "Prince", are
18   *       stored in the lastname property.</li>.
19   * </ul>
20   * @author varienaja
21   * @version $Id: Artist.java,v 1.1 2010/03/16 18:55:42 varienaja Exp $
22   */
23  public class Artist implements Linkable {
24  	private long id;
25  	private Date changed,inserted,birth,decease;
26  	private String lastname,comments,firstname;
27  	private Link link;
28  	private Set<Contract_BA> bands;
29  	
30  	public Artist() {
31  		id=-1L;
32  		inserted=new Date();
33  		lastname="";
34  		firstname="";
35  		comments="";
36  		link=null;
37  		birth=null;
38  		decease=null;
39  		bands=new HashSet<Contract_BA>();
40  	}
41  	
42  	public Date getBirth() {
43  		return birth;
44  	}
45  	
46  	public void setBirth(Date birth) {
47  		this.birth = birth;
48  	}
49  	
50  	public Date getChanged() {
51  		return changed;
52  	}
53  	
54  	public void setChanged(Date changed) {
55  		this.changed = changed;
56  	}
57  	
58  	public String getComments() {
59  		return comments;
60  	}
61  	
62  	public void setComments(String comments) {
63  		this.comments = comments;
64  	}
65  	
66  	public Date getDecease() {
67  		return decease;
68  	}
69  	
70  	public void setDecease(Date decease) {
71  		this.decease = decease;
72  	}
73  	
74  	public Link getLink() {
75  		return link;
76  	}
77  	
78  	public void setLink(Link link) {
79  		this.link = link;
80  	}
81  	
82  	public long getId() {
83  		return id;
84  	}
85  	
86  	public void setId(long id) {
87  		this.id = id;
88  	}
89  	
90  	public Date getInserted() {
91  		return inserted;
92  	}
93  	
94  	public void setInserted(Date inserted) {
95  		this.inserted = inserted;
96  	}
97  	
98  	public String getLastname() {
99  		return lastname;
100 	}
101 	
102 	/**
103 	 * Setter for the last name of the artist. White space
104 	 * gets trimmed. Each artist should have a last name.
105 	 * @param lastname The last name of the artist.
106 	 */
107 	public void setLastname(String lastname) {
108 		if (lastname==null) {
109 			this.lastname = null;
110 		} else {
111 			this.lastname = lastname.trim();
112 		}
113 	}
114 
115 	/**
116 	 * Returns the name of the artist as "firstname lastname".
117 	 * The result is "lastname" if the first name part is empty.
118 	 * @return The name of the artist.
119 	 */
120 	public String getName() {
121 		StringBuilder sb = new StringBuilder();
122 		if(getFirstname()!=null) {
123 			sb.append(getFirstname().trim());
124 			sb.append(" ");
125 		}
126 		sb.append(getLastname().trim());
127 		return sb.toString();
128 	}
129 
130 	public Set<Contract_BA> getBands() {
131 		return bands;
132 	}
133 	
134 
135 	public void setBands(Set<Contract_BA> bands) {
136 		if (bands!=null) this.bands = bands;
137 	}
138 
139 	public void addBand(Band band) {
140 		if(band==null) {
141 			return;
142 		}
143 		Iterator<Contract_BA> i = bands.iterator();
144 		boolean bandalreadyincontracts = false;
145 		while (i.hasNext()) {
146 			Contract_BA c_ba= i.next();
147 			bandalreadyincontracts = c_ba.getBand().equals(band);
148 		}
149 		if (!bandalreadyincontracts) {
150 			Contract_BA newcontract = new Contract_BA();
151 			newcontract.setBand(band);
152 			newcontract.setArtist(this);
153 			bands.add(newcontract);
154 		}
155 	}
156 	
157 	/**
158 	 * Getter for the first name of the artist.
159 	 * @return The first name of the artist.
160 	 */
161 	public String getFirstname() {
162 		return firstname;
163 	}
164 	
165 
166 	/**
167 	 * Setter for the first name of the artist. Whitespace
168 	 * gets trimmed.
169 	 * @param firstname The new first name.
170 	 */
171 	public void setFirstname(String firstname) {
172 		if(firstname==null) {
173 			this.firstname = null;
174 		} else {
175 			this.firstname = firstname.trim();
176 		}
177 	}
178 	
179 	/**
180 	 * Returns the artistname in the format "lastname, firstname".
181 	 * This is suitable for sorted lists of artists. If the first
182 	 * name is empty, this returns "lastname".
183 	 * @return The formatted name of this Artist
184 	 */
185 	public String getFormattedName() {
186 		String result = null;
187 		if(getFirstname()==null || getFirstname().length()<1) {
188 			result = getLastname();
189 		} else {
190 			result = getLastname()+", "+getFirstname();
191 		}
192 		return result;
193 	}
194 	
195 	/**
196 	 * Two artists are the same if their names are the same
197 	 * (both first and last names) disregarding case.
198 	 * (You cannot have two "David Bowies"!)
199 	 * @return True if the artists are equal, false otherwise.
200 	 */
201 	@Override
202 	public boolean equals(Object obj) {
203 		boolean result = false;
204 		if (this==obj) {
205 			return true;
206 		} else {
207 			if (obj instanceof Artist) {
208 				Artist other = (Artist) obj;
209 				if(this.getFirstname()==null) {
210 					result = other.getFirstname()==null;
211 				} else {
212 					result = this.getFirstname().equalsIgnoreCase(other.getFirstname());
213 				}
214 				// Compare last name if first names are equal.
215 				if(result && this.getLastname()==null) {
216 					result = other.getLastname()==null;
217 				} else {
218 					result = this.getLastname().equalsIgnoreCase(other.getLastname());
219 				}
220 					
221 			}
222 		}
223 		return result;
224 	}
225 	
226 	@Override
227 	public int hashCode() {
228 		int result = 0;
229 		if(getFirstname()!=null) {
230 			result += getFirstname().toLowerCase().hashCode();
231 		}
232 		if(getLastname()!=null) {
233 			result += (17 * result) + getLastname().toLowerCase().hashCode();
234 		}
235 		return result;
236 	}
237 
238 	/**
239 	 * Returns a String representation of the artist suitable for human
240 	 * consumption.
241 	 */
242 	public String toString() {
243 		return getName();
244 	}
245 
246 	public String getType() {
247 		return "A";
248 	}
249 }