package com.atlassian.bitbucket.mesh.git.gc;

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

/**
 * Describes an action applied while performing garbage collection, encapsulating both the type of the action
 * performed, whether the action succeeded and how long it took.
 */
class GcAction {

    private final GcActionType action;
    private final String detail;
    private final long duration;
    private final boolean failed;

    GcAction(@Nonnull GcActionType action, long startMs, String detail, boolean failed) {
        this.action = requireNonNull(action, "action");
        this.detail = detail;
        this.failed = failed;

        duration = System.currentTimeMillis() - startMs;
    }

    @Nonnull
    public GcActionType getAction() {
        return action;
    }

    public boolean isFailed() {
        return failed;
    }

    @Nonnull
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder(action.toString());
        if (detail != null) {
            builder.append(" - ").append(detail);
        }
        builder.append(" (").append(duration).append("ms");
        if (failed) {
            builder.append(", failed");
        }

        return builder.append(")").toString();
    }
}
