class Piece extends Object implements Comparable<Piece>
This class is used internally by PeerCoordinator to manage piece selection and distribution strategies. It maintains:
Pieces are sorted by priority (highest first) and then by rarity (fewest peers having it first) to optimize download performance.
Thread Safety: This class is not thread-safe. Callers must synchronize on access to most methods.
| Constructor and Description |
|---|
Piece(int id)Creates a new Piece with the given ID. |
| Modifier and Type | Method and Description |
|---|---|
boolean | addPeer(Peer peer)Adds a peer to the set of peers that have this piece. |
void | clear()Clear all knowledge of peers Caller must synchronize |
int | compareTo(Piece op)Compares by highest priority first, then by rarest first. |
boolean | equals(Object o) |
int | getId()Returns the piece index within the torrent. |
int | getPeerCount()How many peers have this piece? Caller must synchronize. |
int | getPriority()Gets the current priority level for this piece. |
int | getRequestCount()How many peers are requesting this piece? Caller must synchronize. |
int | hashCode() |
boolean | isDisabled()Checks if this piece is disabled (should not be downloaded). |
boolean | isRequested()Checks if any peer is currently requesting this piece. |
boolean | isRequestedBy(Peer peer)Checks if the given peer is currently requesting this piece. |
boolean | removePeer(Peer peer)Removes a peer from those that have this piece. |
void | setDisabled()Disables this piece so it will not be downloaded. |
void | setPriority(int p)Sets the priority level for this piece. |
void | setRequested(Peer peer,
boolean requested)Marks a peer as requesting or no longer requesting this piece. |
String | toString() |
public Piece(int id)
id - the piece index within the torrentpublic boolean addPeer(Peer peer)
peer - the peerpublic void clear()
public int compareTo(Piece op)
compareTo in interface Comparable<Piece>op - the other Piece to compare topublic int getId()
public int getPeerCount()
public int getPriority()
public int getRequestCount()
public boolean isDisabled()
public boolean isRequested()
public boolean isRequestedBy(Peer peer)
peer - the peerpublic boolean removePeer(Peer peer)
peer - the peerpublic void setDisabled()
public void setPriority(int p)
p - priority value (negative to disable, 0 or positive for normal priority)public void setRequested(Peer peer, boolean requested)
peer - the peerrequested - true to mark as requested, false to unmark