view src/main/java/org/icedrobot/ika/output/DefaultWorkerStatus.java @ 29:3b0d6002605a

Add patch to output commands names. Contributed by Giulio Franco.
author Mario Torre <neugens.limasoftware@gmail.com>
date Fri, 15 Apr 2011 17:13:03 +0200
parents
children
line wrap: on
line source

/*
 * IKA - IcedRobot Kiosk Application
 * Copyright (C) 2011  IcedRobot team
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

package org.icedrobot.ika.output;

/**
 * Immutable implementation of the {@link WorkerStatus} interface.
 */
public final class DefaultWorkerStatus
        implements WorkerStatus {

    private long amountDone;
    private long totalAmount;
    private boolean detailedProgress;
    private String status;
    private float speed;
    private String name;


    public DefaultWorkerStatus(String name) {
        this.name = name;
        amountDone = 0;
        totalAmount = 100;
        detailedProgress = false;
        status = "";
        speed = -1;
    }

    public DefaultWorkerStatus(String name, float percentage, String status) {
        this(name);
        amountDone = (long) (percentage * 100);
        this.status = status;
    }

    public DefaultWorkerStatus( String name,
                                float percentage,
                                String status,
                                float speed) {

        this(name, percentage, status);
        this.speed = speed;
    }

    public DefaultWorkerStatus( String name,
                                long done,
                                long total,
                                String status) {
        this(name);
        amountDone = done;
        totalAmount = total;
        this.status = status;
    }

    public DefaultWorkerStatus( String name,
                                long done,
                                long total,
                                String status,
                                float speed) {

        this(name, done, total, status);
        this.speed = speed;
    }


    @Override
    public float getPercentage() {
        return amountDone / (float) totalAmount;
    }

    @Override
    public boolean completed() {
        return amountDone >= totalAmount;
    }

    private UnsupportedOperationException unsupportedDetailedProgress()
            throws UnsupportedOperationException {

        return new UnsupportedOperationException(
                "Detailed progress information is not available");
    }

    @Override
    public long getDoneWork() throws UnsupportedOperationException {
        if (detailedProgress) {
            return amountDone;
        } else {
            throw unsupportedDetailedProgress();
        }
    }

    @Override
    public long getTargetWork() throws UnsupportedOperationException {
        if (detailedProgress) {
            return totalAmount;
        } else {
            throw unsupportedDetailedProgress();
        }
    }

    @Override
    public boolean hasDetailedProgress() {
        return detailedProgress;
    }

    @Override
    public String getStatus() {
        return status;
    }

    @Override
    public float getSpeed() throws UnsupportedOperationException {
        if (speed >= 0) {
            return speed;
        } else {
            throw new UnsupportedOperationException(
                    "Speed information is not available");
        }
    }

    @Override
    public boolean hasSpeed() {
        return speed >= 0;
    }

    @Override
    public String getName() {
        return name;
    }
}