All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dashbuilder.dataset.ChronometerImpl Maven / Gradle / Ivy

There is a newer version: 1.0.0.Final
Show newest version
/**
 * Copyright 2014 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dashbuilder.dataset;

import org.dashbuilder.dataset.engine.Chronometer;

public class ChronometerImpl implements Chronometer {

    protected Long startTime;
    protected Long stopTime;

    public long start() {
        startTime = System.nanoTime();
        stopTime = null;
        return startTime;
    }

    public long stop() {
        stopTime = System.nanoTime();
        return stopTime;
    }

    public long elapsedTime() {
        long stop = stopTime != null ? stopTime : System.nanoTime();
        return stop - startTime;
    }

    public String formatElapsedTime(long millis) {
        long milliseconds = millis;
        long seconds = milliseconds / 1000; milliseconds %= 1000;
        long minutes = seconds / 60; seconds %= 60;
        long hours = minutes / 60; minutes %= 60;
        long days = hours / 24; hours %= 24;
        long weeks = days / 7; days %= 7;
        double secondsd = (double) (seconds * 1000 + milliseconds) / 1000;

        StringBuilder buf = new StringBuilder();
        if (weeks > 0) buf.append(weeks).append(" weeks ");
        if (days > 0) buf.append(days).append("d ");
        if (hours > 0) buf.append(hours).append("h ");
        if (minutes > 0) buf.append(minutes).append("m ");
        if (secondsd > 0) buf.append(secondsd).append("s");
        if (buf.length() == 0) return "0s";
        return buf.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy