001/*
002 * Copyright (C) 2023-present The Prometheus jmx_exporter Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package io.prometheus.jmx.common.configuration;
018
019import io.prometheus.jmx.common.util.Precondition;
020import java.util.function.Function;
021import java.util.function.Supplier;
022
023public class ValidateIntegerInRange implements Function<Integer, Integer> {
024
025    private final int minimum;
026    private final int maximum;
027    private final Supplier<? extends RuntimeException> supplier;
028
029    /**
030     * Constructor
031     *
032     * @param minimum minimum
033     * @param maximum maximum
034     * @param supplier supplier
035     */
036    public ValidateIntegerInRange(
037            int minimum, int maximum, Supplier<? extends RuntimeException> supplier) {
038        Precondition.notNull(supplier);
039        this.minimum = minimum;
040        this.maximum = maximum;
041        this.supplier = supplier;
042    }
043
044    /**
045     * Method to apply a function
046     *
047     * @param value value
048     * @return the return value
049     */
050    @Override
051    public Integer apply(Integer value) {
052        if (value < minimum || value > maximum) {
053            throw supplier.get();
054        }
055
056        return value;
057    }
058}