Breaks.java

package lab.sample.comfort;

import org.apache.commons.lang3.Range;

/**
 * Mandatory breaks on long trips.
 *
 * <p>This class is also the only one to call an <b>external library</b>
 * ({@code commons-lang3}). That is deliberate: a dependency's code does not arrive as a
 * directory of classes but as a jar, and the tool must know how to analyse it in that form —
 * without which a whole swathe of the code actually executed stays invisible.
 *
 * <p>A deliberate peculiarity: the decision is taken on a <b>computed value</b> (the duration
 * obtained so far) and not on an argument received as it is. A tool that only captures the
 * analysed function's input arguments will not be able to explain why this branch was taken —
 * even though it changes the result.
 */
public final class Breaks {

    /** Beyond 2 h of driving or walking, one stops. */
    public static final double THRESHOLD_MINUTES = 120;

    /** Beyond that, the break is no longer a break: it is a stopover, and it gets planned. */
    private static final Range<Integer> REASONABLE = Range.of(0, 6);

    private Breaks() {}

    public static int count(double travelMinutes) {
        if (travelMinutes < THRESHOLD_MINUTES) {
            return 0;
        }
        return REASONABLE.fit((int) (travelMinutes / THRESHOLD_MINUTES));
    }

    public static double totalMinutes(double travelMinutes) {
        return count(travelMinutes) * 15.0;
    }
}