/*
 *  PHEX - The pure-java Gnutella-servent.
 *  Copyright (C) 2001 - 2004 Gregor Koukkoullis ( phex <at> kouk <dot> de )
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package phex.download.swarming;

/**
 * The download set is used to hold everything that is needed for a doing a swarm
 * download. This is the download file, the download segment and the download
 * candidate.
 */
public class SWDownloadSet
{
    private SWDownloadFile downloadFile;
    private SWDownloadSegment downloadSegment;
    private SWDownloadCandidate downloadCandidate;

    public SWDownloadSet( SWDownloadFile aDownloadFile,
        SWDownloadCandidate aDownloadCandidate )
    {
        downloadFile = aDownloadFile;
        downloadCandidate = aDownloadCandidate;
    }

    public SWDownloadFile getDownloadFile()
    {
        return downloadFile;
    }

    public SWDownloadCandidate getDownloadCandidate()
    {
        return downloadCandidate;
    }

    public void setDownloadSegment( SWDownloadSegment downloadSegment )
    {
        this.downloadSegment = downloadSegment;
    }

    public SWDownloadSegment allocateDownloadSegment( SWDownloadWorker worker )
    {
        if ( downloadSegment == null )
        {
            downloadSegment = downloadFile.allocateDownloadSegment( worker,
                downloadCandidate.getAvailableRangeSet(), 
                downloadCandidate.getPreferredSegmentSize(),
                downloadCandidate.getSpeed());
            if ( downloadSegment != null )
            {
                downloadCandidate.associateDownloadSegment( downloadSegment );
            }
        }
        return downloadSegment;
    }

    public SWDownloadSegment getDownloadSegment()
    {
        return downloadSegment;
    }

    /**
     * Releases a allocated download segment.
     */
    public void releaseDownloadSegment( )
    {
        if ( downloadSegment != null )
        {
            downloadFile.releaseDownloadSegment( downloadSegment );
            downloadSegment = null;
        }
        downloadCandidate.releaseDownloadSegment( );
    }

    /**
     * Releases a allocated download set.
     */
    public void releaseDownloadSet( )
    {
        if ( downloadSegment != null )
        {
            downloadFile.releaseDownloadSegment( downloadSegment );
        }
        downloadCandidate.releaseDownloadSegment( );

        downloadFile.releaseDownloadCandidate( downloadCandidate );
        downloadFile.decrementWorkerCount();
    }

    public String toString()
    {
        return "[DownloadSet: (Segment: " + downloadSegment + " - Candidate: "
            + downloadCandidate + " - File: " + downloadFile + ")]";
    }
}
