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

nl.hsac.fitnesse.testrun.DurationRecord Maven / Gradle / Ivy

There is a newer version: 1.32.11
Show newest version
package nl.hsac.fitnesse.testrun;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Stores duration needed for an element.
 * @param  element type
 */
public class DurationRecord implements Comparable> {
    private final long duration;
    private final T element;

    public static  Map toMap(List> recordList) {
        return recordList.stream()
                .collect(Collectors.toMap(DurationRecord::getElement, DurationRecord::getDuration));
    }

    public static  List> toList(Map map) {
        return map.entrySet().stream()
                .map(e -> new DurationRecord(e.getKey(), e.getValue()))
                .collect(Collectors.toList());
    }

    public DurationRecord(T element, long duration) {
        if (element == null) {
            throw new IllegalArgumentException("element cannot be null");
        }
        this.duration = duration;
        this.element = element;
    }

    public T getElement() {
        return element;
    }

    /**
     * @return time needed (in milliseconds)
     */
    public long getDuration() {
        return duration;
    }

    @Override
    public String toString() {
        return element + ": " + duration;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        DurationRecord that = (DurationRecord) o;
        return duration == that.duration &&
                Objects.equals(element, that.element);
    }

    @Override
    public int hashCode() {
        return Objects.hash(duration, element);
    }

    @Override
    public int compareTo(DurationRecord o) {
        return Long.compare(duration, o.duration);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy