001/*
002 * (C) Copyright 2011 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     tdelprat
016 *
017 */
018package org.nuxeo.wizard.download;
019
020import java.io.File;
021
022public class PendingDownload {
023
024    protected final DownloadPackage pkg;
025
026    protected PendingDownloadStatus status = PendingDownloadStatus.PENDING;
027
028    protected float expectedLength;
029
030    protected File dowloadingFile;
031
032    public PendingDownload(DownloadPackage pkg) {
033        this.pkg = pkg;
034    }
035
036    public PendingDownloadStatus getStatus() {
037        return status;
038    }
039
040    public void setStatus(PendingDownloadStatus status) {
041        this.status = status;
042    }
043
044    public DownloadPackage getPkg() {
045        return pkg;
046    }
047
048    public int getProgress() {
049        if (expectedLength == 0 || dowloadingFile == null || dowloadingFile.length() == 0) {
050            return 0;
051        } else {
052            return new Float((dowloadingFile.length() / expectedLength) * 100).intValue();
053        }
054    }
055
056    @Override
057    public boolean equals(Object other) {
058        if (other instanceof PendingDownload) {
059            return ((PendingDownload) other).getPkg().getId().equals(pkg.getId());
060        }
061        return false;
062    }
063
064    @Override
065    public int hashCode() {
066        return pkg.getId().hashCode();
067    }
068
069    public void setFile(long expectedLength, File dowloadingFile) {
070        this.expectedLength = expectedLength;
071        this.dowloadingFile = dowloadingFile;
072    }
073
074    public File getDowloadingFile() {
075        return dowloadingFile;
076    }
077
078    @Override
079    public String toString() {
080        StringBuilder builder = new StringBuilder();
081        builder.append("PendingDownload ").append(pkg).append(", status=").append(status);
082        return builder.toString();
083    }
084
085}