001package org.clafer.common; 002 003import java.util.Iterator; 004 005/** 006 * Various static utility functions for checking input. 007 * 008 * @author jimmy 009 */ 010public class Check { 011 012 private Check() { 013 } 014 015 /** 016 * Check that the item is non-null. 017 * 018 * @param <T> the type of the object 019 * @param message the message of the exception if thrown 020 * @param obj the object to check 021 * @return the original object 022 * @throws NullPointerException if the object is null 023 */ 024 public static <T> T notNull(String message, T obj) throws NullPointerException { 025 if (obj == null) { 026 throw new NullPointerException(message); 027 } 028 return obj; 029 } 030 031 /** 032 * Check that the item is non-null. 033 * 034 * @param <T> the type of the object 035 * @param obj the item to check 036 * @return the original object 037 * @throws NullPointerException if the object is null 038 */ 039 public static <T> T notNull(T obj) throws NullPointerException { 040 if (obj == null) { 041 throw new NullPointerException(); 042 } 043 return obj; 044 } 045 046 /** 047 * @param <T> the type of the elements 048 * @param array not null and cannot contain null 049 * @return the original array 050 * @throws NullPointerException if the array is null or contains a null 051 * element 052 */ 053 public static <T> T[] noNulls(T... array) throws NullPointerException { 054 Check.notNull(array); 055 for (T t : array) { 056 Check.notNull(t); 057 } 058 return array; 059 } 060 061 /** 062 * @param <T> the type of the elements 063 * @param items not null and cannot contain null 064 * @return the original items 065 * @throws NullPointerException if the items is null or contains a null 066 * element 067 */ 068 public static <T extends Iterable<T>> T noNulls(T items) throws NullPointerException { 069 Check.notNull(items); 070 for (T t : items) { 071 Check.notNull(t); 072 } 073 return items; 074 } 075 076 /** 077 * @param <T> the type of the elements 078 * @param array not null, cannot contain null, and is non-empty 079 * @return the original array 080 * @throws IllegalArgumentException if the array is empty 081 * @throws NullPointerException if the array is null or contains a null 082 * element 083 */ 084 public static <T> T[] noNullsNotEmpty(T... array) 085 throws IllegalArgumentException, NullPointerException { 086 Check.notNull(array); 087 if (array.length == 0) { 088 throw new IllegalArgumentException(); 089 } 090 for (T t : array) { 091 Check.notNull(t); 092 } 093 return array; 094 } 095 096 /** 097 * @param <T> the type of the elements 098 * @param items not null, cannot contain null, and is non-empty 099 * @return the original items 100 * @throws IllegalArgumentException if the items is empty 101 * @throws NullPointerException if the items is null or contains a null 102 * element 103 */ 104 public static <T extends Iterable<T>> T noNullsNotEmpty(T items) 105 throws IllegalArgumentException, NullPointerException { 106 Check.notNull(items); 107 Iterator<T> iter = items.iterator(); 108 if (!iter.hasNext()) { 109 throw new IllegalArgumentException(); 110 } 111 do { 112 Check.notNull(iter.next()); 113 } while (iter.hasNext()); 114 return items; 115 } 116}