Skip to content

Commit 1c77e3f

Browse files
committed
Initial commit
0 parents  commit 1c77e3f

6 files changed

Lines changed: 2217 additions & 0 deletions

File tree

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
target/
2+
**/*.iml
3+
*.iml
4+
.attached_pid*
5+
.idea/

README.md

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
# Validation
2+
3+
A simple and easy-to-use validation utility library.
4+
5+
By Tom Ansill
6+
7+
## Motivation
8+
9+
I got tired of manually including null checks, empty array checks, and various kinds of checks against bad input parameters.
10+
So I decided to build a library to make things a bit easier for me.
11+
12+
### Why not use `Objects.requireNonNull(T)`?
13+
14+
`Objects.requireNonNull(T)` will throw `NullPointerException` and I disagree with this choice of exception because `NullPointerException` is supposed to be thrown in event of null object being de-referenced. Like:
15+
```
16+
StringBuilder sb = null;
17+
sb.append("something"); // <-- This will throw NullPointerException because you are trying to de-reference a null object
18+
```
19+
20+
So, hence the name `NullPointerException`, you get the exception because you are attempting to de-reference a null value and it blows up on you.
21+
I don't believe this is the case here where I want to ensure that parameters that are passed in is not null. I'm not necessarily trying to dereference it.
22+
I think `IllegalArgumentException` is more appropriate exception to best describe the problem.
23+
24+
## Prerequisites
25+
26+
* Java 8 or better
27+
* Maven
28+
29+
## Download
30+
31+
**No Maven Repository available yet ):**
32+
33+
For now, you need to build and install it on your machine.
34+
35+
```bash
36+
$ git clone https://github.com/tomansill/java-validation
37+
$ cd java-validation
38+
$ mvn install
39+
```
40+
41+
Then include the dependency in your project's `pom.xml`:
42+
43+
```xml
44+
<dependency>
45+
<groupId>com.ansill.validation</groupId>
46+
<artifactId>validation</artifactId>
47+
<version>0.1.0</version>
48+
</dependency>
49+
```
50+
51+
## How to use
52+
53+
### Null Checks
54+
55+
Use `Validation.assertNonnull(Object)` to assert that object is not null:
56+
57+
```java
58+
import com.ansill.validation.Validation;
59+
60+
public class Application{
61+
public static void main(String[] args){
62+
String message = null;
63+
Application application = new Application();
64+
application.print(message);
65+
}
66+
public void print(String message){
67+
Validation.assertNonnull(message);
68+
System.out.println(message);
69+
}
70+
}
71+
```
72+
73+
When you run the code, it will yield this message:
74+
```
75+
Exception in thread "main" java.lang.IllegalArgumentException: Value is expected to be non-null but is found to be null
76+
at Application.print(Application.java:10)
77+
at Application.main(Application.java:7)
78+
```
79+
80+
### Natural Number Checks
81+
82+
Use `Validation.assertNaturalNumber(long)` to assert that number is a natural number (no negative number or zero):
83+
84+
```java
85+
import com.ansill.validation.Validation;
86+
87+
public class Application{
88+
public static void main(String[] args){
89+
Application application = new Application();
90+
application.setPort(0);
91+
}
92+
private int port = 80;
93+
public void setPort(int port){
94+
Validation.assertNaturalNumber(port, "port");
95+
this.port = port;
96+
}
97+
}
98+
```
99+
100+
When you run the code, it will yield this message:
101+
```
102+
Exception in thread "main" java.lang.IllegalArgumentException: Value in variable 'port' is expected to be a natural number (1, 2, ..., N-1, N) but it is actually not a natural number
103+
at Application.setPort(Application.java:10)
104+
at Application.main(Application.java:6)
105+
```
106+
107+
### Non-Negative Checks
108+
109+
Use `Validation.assertNonnegative(long)` to assert that number is a positive number:
110+
111+
```java
112+
import com.ansill.validation.Validation;
113+
114+
public class Application{
115+
public static void main(String[] args){
116+
Application application = new Application();
117+
application.add((short) 0, -1);
118+
}
119+
public void add(short one, long two){
120+
Validation.assertNonnegative(one);
121+
Validation.assertNonnegative(two);
122+
System.out.println(one + two);
123+
}
124+
}
125+
```
126+
127+
When you run the code, it will yield this message:
128+
```
129+
Exception in thread "main" java.lang.IllegalArgumentException: Value is expected to be non-negative but value is actually a negative number
130+
at Application.add(Application.java:10)
131+
at Application.main(Application.java:6)
132+
```
133+
134+
### Non-Empty String Checks
135+
136+
Use `Validation.assertNonemptyString(String)` to assert that String is non-empty:
137+
138+
```java
139+
import com.ansill.validation.Validation;
140+
141+
public class Application{
142+
public static void main(String[] args){
143+
Application application = new Application("");
144+
}
145+
public final String name;
146+
public Application(String name){
147+
Validation.assertNonemptyString(name, "name");
148+
this.name = name;
149+
}
150+
}
151+
```
152+
153+
When you run the code, it will yield this message:
154+
```
155+
Exception in thread "main" java.lang.IllegalArgumentException: Value in variable 'name' is expected to be non-empty but value is actually a empty string
156+
at Application.<init>(Application.java:9)
157+
at Application.main(Application.java:5)
158+
```
159+
160+
### Non-Empty Array/Collection
161+
162+
Use `Validation.assertNonempty(Object[])` or `Validation.assertNonempty(Collection)` to assert that Array/Collection is non-empty:
163+
164+
```java
165+
import com.ansill.validation.Validation;
166+
167+
import java.util.*;
168+
169+
public class Application{
170+
public static void main(String[] args){
171+
Application application = new Application(Collections.emptyList());
172+
}
173+
public final Collection hostnames;
174+
public Application(Collection hostnames){
175+
Validation.assertNonempty(hostnames);
176+
this.hostnames = hostnames;
177+
}
178+
}
179+
```
180+
181+
When you run the code, it will yield this message:
182+
```
183+
Exception in thread "main" java.lang.IllegalArgumentException: Value is expected to be non-empty but value is actually empty
184+
at Application.<init>(Application.java:11)
185+
at Application.main(Application.java:7)
186+
```
187+
188+
### Array/Collection Member Null Check
189+
190+
Use `Validation.assertNonnullElements(Object[])` or `Validation.assertNonnullElements(Collection)` to assert that Array/Collection does not have null elements:
191+
192+
```java
193+
import com.ansill.validation.Validation;
194+
195+
import java.util.*;
196+
197+
public class Application{
198+
public static void main(String[] args){
199+
Application application = new Application(Arrays.asList("google.com", null, "github.com", null, null, "reddit.com"));
200+
}
201+
public final Collection hostnames;
202+
public Application(Collection hostnames){
203+
Validation.assertNonnullElements(hostnames, false);
204+
this.hostnames = hostnames;
205+
}
206+
}
207+
```
208+
209+
When you run the code, it will yield this message:
210+
```
211+
Exception in thread "main" java.lang.IllegalArgumentException: Value is expected to have all of its list members to be non-null but the list contains null members. Invalid members are located at indices [1, 3, 4]
212+
at Application.<init>(Application.java:11)
213+
at Application.main(Application.java:7)
214+
```

pom.xml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.ansill.validation</groupId>
8+
<artifactId>validation</artifactId>
9+
<version>0.1.0</version>
10+
11+
<packaging>jar</packaging>
12+
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>org.apache.maven.plugins</groupId>
17+
<artifactId>maven-compiler-plugin</artifactId>
18+
<configuration>
19+
<source>8</source>
20+
<target>8</target>
21+
</configuration>
22+
</plugin>
23+
<plugin>
24+
<groupId>org.apache.maven.plugins</groupId>
25+
<artifactId>maven-surefire-plugin</artifactId>
26+
<version>2.22.1</version>
27+
</plugin>
28+
</plugins>
29+
</build>
30+
31+
<properties>
32+
<junit.jupiter.version>5.4.2</junit.jupiter.version>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
35+
</properties>
36+
37+
<dependencies>
38+
<dependency>
39+
<groupId>com.google.code.findbugs</groupId>
40+
<artifactId>jsr305</artifactId>
41+
<version>3.0.0</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.junit.jupiter</groupId>
45+
<artifactId>junit-jupiter-api</artifactId>
46+
<version>${junit.jupiter.version}</version>
47+
<scope>test</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.junit.jupiter</groupId>
51+
<artifactId>junit-jupiter-engine</artifactId>
52+
<version>${junit.jupiter.version}</version>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.junit.jupiter</groupId>
57+
<artifactId>junit-jupiter-params</artifactId>
58+
<version>5.4.2</version>
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>com.google.code.gson</groupId>
63+
<artifactId>gson</artifactId>
64+
<version>2.8.5</version>
65+
<scope>test</scope>
66+
</dependency>
67+
</dependencies>
68+
</project>

0 commit comments

Comments
 (0)