001package org.unix4j.util; 002 003public class Assert { 004 005 /** 006 * Returns the given {@code value} if it is not null and throws an exception 007 * otherwise. 008 * 009 * @param message 010 * the error message used if {@code value} is null 011 * @param value 012 * the value to assert 013 * @return the given {@code value} if it is not null 014 * @throws NullPointerException 015 * if {@code value==null} 016 */ 017 public static <T> T assertNotNull(String message, T value) { 018 if (value != null) { 019 return value; 020 } 021 throw new NullPointerException(message); 022 } 023 024 public static void assertArgNotNull(final String message, final Object obj) { 025 if (obj == null) { 026 throw new NullPointerException(message); 027 } 028 } 029 030 public static void assertArgNotNull(final String message, final Object... objects) { 031 for (Object obj : objects) { 032 assertArgNotNull(message, obj); 033 } 034 } 035 036 public static void assertArgTrue(final String message, boolean expression) { 037 if (!expression) { 038 throw new IllegalArgumentException(message); 039 } 040 } 041 042 public static void assertArgGreaterThan(final String message, int argValue, int num) { 043 if (argValue <= num) { 044 throw new IllegalArgumentException(message); 045 } 046 } 047 048 public static void assertArgGreaterThanOrEqualTo(final String message, int argValue, int num) { 049 if (argValue < num) { 050 throw new IllegalArgumentException(message); 051 } 052 } 053 054 public static void assertArgLessThan(final String message, int argValue, int num) { 055 if (argValue >= num) { 056 throw new IllegalArgumentException(message); 057 } 058 } 059 060 public static void assertArgLessThanOrEqualTo(final String message, int argValue, int num) { 061 if (argValue > num) { 062 throw new IllegalArgumentException(message); 063 } 064 } 065 066 public static void assertArgFalse(final String message, boolean expression) { 067 if (expression) { 068 throw new IllegalArgumentException(message); 069 } 070 } 071}