forked from nusCS2113-AY1819S1/addressbook-level3
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Utils.java
60 lines (53 loc) · 1.39 KB
/
Utils.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
package seedu.addressbook.common;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
/**
* Utility methods
*/
public class Utils {
/**
* Checks whether any of the given items are null.
*/
public static boolean isAnyNull(Object... items) {
for (Object item : items) {
if (item == null) {
return true;
}
}
return false;
}
/**
* Checks if every element in a collection are unique by {@link Object#equals(Object)}.
*/
public static boolean elementsAreUnique(Collection<?> items) {
final Set<Object> testSet = new HashSet<>();
for (Object item : items) {
final boolean itemAlreadyExists = !testSet.add(item); // see Set documentation
if (itemAlreadyExists) {
return false;
}
}
return true;
}
public static boolean isStringInteger(String str) {
if (str == null || str.isEmpty()) {
return false;
}
int i = 0;
if (str.charAt(0) == '-') {
if (str.length() == 1) {
return false;
}
i = 1;
}
while (i < str.length()) {
char c = str.charAt(i);
if (c < '0' || c > '9') {
return false;
}
i++;
}
return true;
}
}