001package org.jsoup.helper; 002 003/** 004 * Simple validation methods. Designed for jsoup internal use 005 */ 006public final class Validate { 007 008 private Validate() {} 009 010 /** 011 * Validates that the object is not null 012 * @param obj object to test 013 */ 014 public static void notNull(Object obj) { 015 if (obj == null) 016 throw new IllegalArgumentException("Object must not be null"); 017 } 018 019 /** 020 * Validates that the object is not null 021 * @param obj object to test 022 * @param msg message to output if validation fails 023 */ 024 public static void notNull(Object obj, String msg) { 025 if (obj == null) 026 throw new IllegalArgumentException(msg); 027 } 028 029 /** 030 * Validates that the value is true 031 * @param val object to test 032 */ 033 public static void isTrue(boolean val) { 034 if (!val) 035 throw new IllegalArgumentException("Must be true"); 036 } 037 038 /** 039 * Validates that the value is true 040 * @param val object to test 041 * @param msg message to output if validation fails 042 */ 043 public static void isTrue(boolean val, String msg) { 044 if (!val) 045 throw new IllegalArgumentException(msg); 046 } 047 048 /** 049 * Validates that the value is false 050 * @param val object to test 051 */ 052 public static void isFalse(boolean val) { 053 if (val) 054 throw new IllegalArgumentException("Must be false"); 055 } 056 057 /** 058 * Validates that the value is false 059 * @param val object to test 060 * @param msg message to output if validation fails 061 */ 062 public static void isFalse(boolean val, String msg) { 063 if (val) 064 throw new IllegalArgumentException(msg); 065 } 066 067 /** 068 * Validates that the array contains no null elements 069 * @param objects the array to test 070 */ 071 public static void noNullElements(Object[] objects) { 072 noNullElements(objects, "Array must not contain any null objects"); 073 } 074 075 /** 076 * Validates that the array contains no null elements 077 * @param objects the array to test 078 * @param msg message to output if validation fails 079 */ 080 public static void noNullElements(Object[] objects, String msg) { 081 for (Object obj : objects) 082 if (obj == null) 083 throw new IllegalArgumentException(msg); 084 } 085 086 /** 087 * Validates that the string is not empty 088 * @param string the string to test 089 */ 090 public static void notEmpty(String string) { 091 if (string == null || string.length() == 0) 092 throw new IllegalArgumentException("String must not be empty"); 093 } 094 095 /** 096 * Validates that the string is not empty 097 * @param string the string to test 098 * @param msg message to output if validation fails 099 */ 100 public static void notEmpty(String string, String msg) { 101 if (string == null || string.length() == 0) 102 throw new IllegalArgumentException(msg); 103 } 104 105 /** 106 Cause a failure. 107 @param msg message to output. 108 */ 109 public static void fail(String msg) { 110 throw new IllegalArgumentException(msg); 111 } 112}