View Javadoc

1   package org.musicontroller.core;
2   
3   /**
4    * Implements the occurence of a song as part of a playlist at a particular
5    * position. 
6    * 
7    * @author Varienaja
8    * @version $Id: Contract_PS.java,v 1.1 2010/03/16 18:55:42 varienaja Exp $
9    */
10  public class Contract_PS implements Comparable<Contract_PS> {
11  	private long id;
12  	private Playlist playlist;
13  	private Song song;
14  	private int rowno;
15  	
16  	public Contract_PS() {
17  		id=-1L;
18  		song=null;
19  		playlist=null;
20  		rowno=-1;
21  	}
22  	
23  	public long getId() {
24  		return id;
25  	}
26  	
27  	public void setId(long id) {
28  		this.id = id;
29  	}
30  	
31  	public Playlist getPlaylist() {
32  		return playlist;
33  	}
34  	
35  	public void setPlaylist(Playlist playlist) {
36  		this.playlist = playlist;
37  	}
38  	
39  	public int getRowno() {
40  		return rowno;
41  	}
42  	
43  	public void setRowno(int rowno) {
44  		this.rowno = rowno;
45  	}
46  	
47  	public Song getSong() {
48  		return song;
49  	}
50  	
51  	public void setSong(Song song) {
52  		this.song = song;
53  	}
54  
55  	/*
56  	 * Implement partial ordering on Contract_PS. Smaller row numbers come
57  	 * before larger row numbers. Equal row numbers signify identical objects,
58  	 * except for row number 0. This row number has the special meaning 
59  	 * (Unknown row number). If both contract have row number 0, the song id
60  	 * determines the order.
61  	 */
62  	public int compareTo(Contract_PS c) {
63  		
64  		if(this.rowno==0) {
65  			if(c.getRowno()==0) {
66  				if (this.song==null) {
67  					if(c.getSong()==null) {
68  						return 0;
69  					} else {
70  						return -1; 
71  					}
72  				} else if (c.getSong()==null) {
73  					return 1;
74  				} else {
75  					Long thisid = this.getSong().getId();
76  					Long otherid = c.getSong().getId();
77  					return thisid.compareTo(otherid);
78  				}
79  			} else {
80  				return -1;
81  			}
82  		} else {
83  			Integer thisno = this.getRowno();
84  			Integer otherno = c.getRowno();
85  			return thisno.compareTo(otherno);
86  		}
87  	}
88  	
89  	/*
90  	 * (non-Javadoc)
91  	 * @see java.lang.Object#equals(java.lang.Object)
92  	 */
93  	@Override
94  	public boolean equals(Object o) {
95  		if (o==null) return false;
96  		if (o instanceof Contract_PS) {
97  			Contract_PS other = (Contract_PS) o;
98  			if (rowno==other.rowno) {
99  				if ((playlist==null && other.playlist==null) || (playlist!=null && playlist.equals(other.playlist))) {
100 					if ((song==null && other.song==null) || (song!=null && song.equals(other.song))) {
101 						return id==other.id;
102 					}
103 				} 
104 			} 
105 		} 
106 		return false;
107 	}
108 	/*
109 	 * (non-Javadoc)
110 	 * @see java.lang.Object#hashCode()
111 	 */
112 	@Override
113 	public int hashCode() {
114 		return Long.valueOf(id).hashCode();
115 	}
116 
117 	public String toString() {
118 		StringBuilder sb = new StringBuilder();
119 		if(getRowno()==-1 && getPlaylist()==null && getSong()==null) {
120 			return "<contract_PS/>";
121 		}
122 		sb.append("<contract_PS rowno=\"");
123 		sb.append(getRowno());
124 		sb.append("\">");
125 		if(getPlaylist()!=null) {
126 			sb.append(getPlaylist().toString());
127 		}
128 		if(getSong()!=null) {
129 			sb.append(getSong().toString());
130 		}
131 		sb.append("</contract_PS>");
132 		return sb.toString();
133 	}
134 }