-
Notifications
You must be signed in to change notification settings - Fork 1
/
NullCheck.java
106 lines (97 loc) · 3.63 KB
/
NullCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Copyright 2012-2024 Michael Pozhidaev <[email protected]>
This file is part of LUWRAIN.
LUWRAIN is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
LUWRAIN is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
*/
package org.luwrain.core;
/**
* Simplifies checking of objects validity. This class provides a set of
* static methods which take care of checking the given data and throw
* corresponding exceptions. All method are rather short and purposed
* only for making code more readable and self-documented.
*/
public final class NullCheck
{
/**
* Checks that provided reference isn't null.
*
* @param obj The reference to check
* @param name The reference name to construct better exception message
* @throws NullPointerException
*/
static public void notNull(Object obj, String name)
{
if (obj == null)
throw new NullPointerException(name + " can't be null");
}
/**
* Checks that given reference isn't null and {@code obj.toString.isEmpty()} returns false.
*
* @param obj The reference to check
* @param name The reference name to construct better exception message
* @throws NullPointerException IllegalArgumentException
*/
static public void notEmpty(Object obj, String name)
{
if (obj == null)
throw new NullPointerException(name + " can't be null");
if (obj.toString().isEmpty())
throw new IllegalArgumentException(name + " can't be empty");
}
/**
* Checks that the array reference isn't null and none of its items are null
* (if there are any).
*
* @param items The reference to an array to check
* @param name The reference name to construct better exception message
* @throws NullPointerException
*/
static public void notNullItems(Object[] items, String name)
{
notNull(items, name);
for(int i = 0;i < items.length;++i)
if (items[i] == null)
throw new NullPointerException(name + "[" + i + "] can't be null");
}
/**
* Checks that the array reference isn't null and {@code items[i].toString().isEmpt ()} returns false
* for all items (if there are any).
*
* @param items The reference to an array to check
* @param name The reference name to construct better exception message
* @throws NullPointerException IllegalArgumentException
*/
static public void notEmptyItems(Object[] items, String name)
{
if (items == null)
throw new NullPointerException(name + " can't be null");
for(int i = 0;i < items.length;++i)
{
if (items[i] == null)
throw new NullPointerException(name + "[" + i + "] can't be null");
if (items[i].toString().isEmpty())
throw new IllegalArgumentException(name + "[" + i + "] can't be empty");
}
}
/**
* Checks that the array reference isn't null and the array has at least one item.
*
* @param items The reference to an array to check
* @param name The reference name to construct better exception message
* @throws NullPointerException IllegalArgumentException
*/
static public void notEmptyArray(Object[] items, String name)
{
if (items == null)
throw new NullPointerException(name + " can't be null");
if (items.length < 1)
throw new IllegalArgumentException(name + " can't be empty");
}
}