001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.util.lang; 018 019import org.apache.wicket.util.string.Strings; 020 021/** 022 * 023 */ 024public class Checks 025{ 026 /** 027 * Checks value is not null 028 * 029 * @param argument 030 * @param message 031 * @param params 032 * @throws IllegalStateException 033 */ 034 public static void notNull(final Object argument, final String message, final Object... params) 035 { 036 if (argument == null) 037 { 038 throw new IllegalStateException(Args.format(message, params)); 039 } 040 } 041 042 /** 043 * Checks argument is not empty (not null and has a non-whitespace character) 044 * 045 * @param argument 046 * @param message 047 * @param params 048 * @throws IllegalStateException 049 */ 050 public static void notEmpty(final String argument, final String message, final Object... params) 051 { 052 if (Strings.isEmpty(argument)) 053 { 054 throw new IllegalStateException(Args.format(message, params)); 055 } 056 } 057 058 /** 059 * Checks if argument is within a range 060 * 061 * @param <T> 062 * @param min 063 * @param max 064 * @param value 065 * @param message 066 * @throws IllegalStateException 067 */ 068 public static <T extends Comparable<? super T>> void withinRange(final T min, final T max, 069 final T value, final String message) 070 { 071 notNull(min, message); 072 notNull(max, message); 073 074 if ((value.compareTo(min) < 0) || (value.compareTo(max) > 0)) 075 { 076 throw new IllegalStateException(message); 077 } 078 } 079 080 /** 081 * Checks value is not null 082 * 083 * @param argument 084 * @param name 085 * @throws IllegalStateException 086 */ 087 public static void notNullShort(final Object argument, final String name) 088 { 089 notNull(argument, name + " may not be null."); 090 } 091 092 /** 093 * Checks argument is not empty (not null and has a non-whitespace character) 094 * 095 * @param argument 096 * @param name 097 * @throws IllegalStateException 098 */ 099 public static void notEmptyShort(final String argument, final String name) 100 { 101 notEmpty(argument, name + " may not be null or empty string."); 102 } 103 104 /** 105 * Checks if argument is within a range 106 * 107 * @param <T> 108 * @param min 109 * @param max 110 * @param value 111 * @param name 112 * @throws IllegalStateException 113 */ 114 public static <T extends Comparable<? super T>> void withinRangeShort(final T min, final T max, 115 final T value, final String name) 116 { 117 withinRange(min, max, value, 118 String.format("%s must have a value within [%s,%s], but was %s", name, min, max, value)); 119 } 120}