package com.atlassian.bitbucket.mesh.throttle;

import javax.annotation.Nonnull;
import java.util.Optional;

/**
 * The two currently supported throttling strategies
 */
public enum ThrottlingStrategy {

    /**
     * Provides for a fixed maximum number of tickets for the throttles operations
     */
    FIXED("fixed"),
    /**
     * Provides for a variable number of tickets between a min and max dependin on the number of queued operations
     * and the performance of the machine over time
     */
    ADAPTIVE("adaptive");

    private final String id;

    ThrottlingStrategy(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public static Optional<ThrottlingStrategy> fromId(@Nonnull String id) {
        for (ThrottlingStrategy value : values()) {
            if (value.getId().equalsIgnoreCase(id)) {
                return Optional.of(value);
            }
        }

        return Optional.empty();
    }
}
