001/*
002 * Copyright (C) 2023 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;
020
021import java.util.function.Function;
022import java.util.function.Supplier;
023
024/**
025 * Class to validate a String is not blank, throwing a RuntimeException
026 * from the Supplier if there is a ClassCastException
027 */
028public class ValidatStringIsNotBlank implements Function<String, String> {
029
030    private Supplier<? extends RuntimeException> supplier;
031
032    /**
033     * Constructor
034     *
035     * @param supplier supplier
036     */
037    public ValidatStringIsNotBlank(Supplier<? extends RuntimeException> supplier) {
038        Precondition.notNull(supplier);
039        this.supplier = supplier;
040    }
041
042    /**
043     * Method to apply a function
044     *
045     * @param value value
046     * @return the return value
047     */
048    @Override
049    public String apply(String value) {
050        if (value.trim().isEmpty()) {
051            throw supplier.get();
052        }
053
054        return value;
055    }
056}