2005-10-20 22:05:51 +00:00
|
|
|
package org.klomp.snark;
|
|
|
|
|
2005-10-21 00:10:13 +00:00
|
|
|
import java.util.Collections;
|
2008-07-16 13:42:54 +00:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
2005-10-20 22:05:51 +00:00
|
|
|
|
|
|
|
public class Piece implements Comparable {
|
|
|
|
|
|
|
|
private int id;
|
|
|
|
private Set peers;
|
|
|
|
private boolean requested;
|
|
|
|
|
|
|
|
public Piece(int id) {
|
|
|
|
this.id = id;
|
2005-10-21 00:10:13 +00:00
|
|
|
this.peers = Collections.synchronizedSet(new HashSet());
|
2005-10-20 22:31:28 +00:00
|
|
|
this.requested = false;
|
2005-10-20 22:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int compareTo(Object o) throws ClassCastException {
|
|
|
|
return this.peers.size() - ((Piece)o).peers.size();
|
|
|
|
}
|
|
|
|
|
2009-08-11 21:58:56 +00:00
|
|
|
@Override
|
2005-10-20 22:05:51 +00:00
|
|
|
public boolean equals(Object o) {
|
2009-08-11 21:58:56 +00:00
|
|
|
if (o instanceof Piece) {
|
|
|
|
if (o == null) return false;
|
|
|
|
try {
|
|
|
|
return this.id == ((Piece)o).id;
|
|
|
|
} catch (ClassCastException cce) {
|
|
|
|
return false;
|
|
|
|
}
|
2005-10-20 22:05:51 +00:00
|
|
|
}
|
2009-08-11 21:58:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
int hash = 5;
|
|
|
|
hash = 31 * hash + this.id;
|
|
|
|
return hash;
|
2005-10-20 22:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getId() { return this.id; }
|
|
|
|
public Set getPeers() { return this.peers; }
|
|
|
|
public boolean addPeer(Peer peer) { return this.peers.add(peer.getPeerID()); }
|
2005-10-20 23:28:32 +00:00
|
|
|
public boolean removePeer(Peer peer) { return this.peers.remove(peer.getPeerID()); }
|
2005-10-20 22:05:51 +00:00
|
|
|
public boolean isRequested() { return this.requested; }
|
|
|
|
public void setRequested(boolean requested) { this.requested = requested; }
|
2005-12-30 23:33:52 +00:00
|
|
|
|
2009-08-11 21:58:56 +00:00
|
|
|
@Override
|
2005-12-30 23:33:52 +00:00
|
|
|
public String toString() {
|
|
|
|
return String.valueOf(id);
|
|
|
|
}
|
2005-10-20 22:05:51 +00:00
|
|
|
}
|