001/* 002 * Copyright (C) 2022-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.util; 018 019public class Precondition { 020 021 private Precondition() { 022 // DO NOTHING 023 } 024 025 /** 026 * Method to check an Object is not null 027 * 028 * @param object object 029 */ 030 public static void notNull(Object object) { 031 notNull(object, "object is null"); 032 } 033 034 /** 035 * Method to check an Object is not null 036 * 037 * @param object object 038 * @param message message 039 */ 040 public static void notNull(Object object, String message) { 041 if (object == null) { 042 throw new IllegalArgumentException(message); 043 } 044 } 045 046 /** 047 * Method to check that a String is not null and not empty 048 * 049 * @param string string 050 */ 051 public static void notNullOrEmpty(String string) { 052 notNullOrEmpty(string, String.format("string [%s] is null or empty", string)); 053 } 054 055 /** 056 * Method to check that a String is not null and not empty 057 * 058 * @param string string 059 * @param message message 060 */ 061 public static void notNullOrEmpty(String string, String message) { 062 if (string == null || string.trim().isEmpty()) { 063 throw new IllegalArgumentException(message); 064 } 065 } 066 067 /** 068 * Method to check that an integer is greater than or equal to a value 069 * 070 * @param value value 071 * @param minimumValue minimumValue 072 */ 073 public static void isGreaterThanOrEqualTo(int value, int minimumValue) { 074 isGreaterThanOrEqualTo( 075 value, 076 minimumValue, 077 String.format("value [%s] is less than minimum value [%s]", value, minimumValue)); 078 } 079 080 /** 081 * Method to check that an integer is greater than or equal to a value 082 * 083 * @param value value 084 * @param minimumValue minimumValue 085 * @param message message 086 */ 087 public static void isGreaterThanOrEqualTo(int value, int minimumValue, String message) { 088 if (value < minimumValue) { 089 throw new IllegalArgumentException(message); 090 } 091 } 092}