001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     tdelprat
018 *
019 */
020package org.nuxeo.wizard.download;
021
022import java.io.File;
023
024public class PendingDownload {
025
026    protected final DownloadPackage pkg;
027
028    protected PendingDownloadStatus status = PendingDownloadStatus.PENDING;
029
030    protected float expectedLength;
031
032    protected File dowloadingFile;
033
034    public PendingDownload(DownloadPackage pkg) {
035        this.pkg = pkg;
036    }
037
038    public PendingDownloadStatus getStatus() {
039        return status;
040    }
041
042    public void setStatus(PendingDownloadStatus status) {
043        this.status = status;
044    }
045
046    public DownloadPackage getPkg() {
047        return pkg;
048    }
049
050    public int getProgress() {
051        if (expectedLength == 0 || dowloadingFile == null || dowloadingFile.length() == 0) {
052            return 0;
053        } else {
054            return (int) ((dowloadingFile.length() / expectedLength) * 100);
055        }
056    }
057
058    @Override
059    public boolean equals(Object other) {
060        if (other instanceof PendingDownload) {
061            return ((PendingDownload) other).getPkg().getId().equals(pkg.getId());
062        }
063        return false;
064    }
065
066    @Override
067    public int hashCode() {
068        return pkg.getId().hashCode();
069    }
070
071    public void setFile(long expectedLength, File dowloadingFile) {
072        this.expectedLength = expectedLength;
073        this.dowloadingFile = dowloadingFile;
074    }
075
076    public File getDowloadingFile() {
077        return dowloadingFile;
078    }
079
080    @Override
081    public String toString() {
082        StringBuilder builder = new StringBuilder();
083        builder.append("PendingDownload ").append(pkg).append(", status=").append(status);
084        return builder.toString();
085    }
086
087}