Skip to content

Commit 8764ebe

Browse files
committed
Changing verification of params + change bits of javaDoc + update ArrayList8
1 parent e4ee272 commit 8764ebe

File tree

8 files changed

+312
-190
lines changed

8 files changed

+312
-190
lines changed
Lines changed: 71 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package CodingUtils;
22

3+
import org.jetbrains.annotations.Contract;
4+
import org.jetbrains.annotations.NotNull;
5+
36
import java.util.*;
47
import java.util.function.Predicate;
58

@@ -8,38 +11,64 @@
811
.
912
. The ArrayList8 Class was Coded by : Alexandre BOLOT
1013
.
11-
. Last Modified : 17/12/17 22:47
14+
. Last Modified : 20/01/18 00:17
1215
.
1316
. Contact : [email protected]
1417
...............................................................................................................................*/
1518

19+
@SuppressWarnings ({"UnusedReturnValue", "ConstantConditions"})
1620
public class ArrayList8<E> extends ArrayList<E>
1721
{
22+
//region --------------- Attributes ----------------------
1823
private Random random = new Random();
24+
//endregion
25+
26+
//region --------------- Constructors --------------------
27+
public ArrayList8 ()
28+
{
29+
super();
30+
}
31+
32+
public ArrayList8 (Collection<? extends E> c)
33+
{
34+
super(c);
35+
}
36+
//endregion
1937

38+
//region --------------- Methods -------------------------
2039
public E getRandom ()
2140
{
41+
//region --> Check params
2242
if (this.isEmpty()) throw new IndexOutOfBoundsException("List is empty");
43+
//endregion
2344

2445
return get(random.nextInt(this.size()));
2546
}
2647

2748
public E removeRandom ()
2849
{
50+
//region --> Check params
2951
if (this.isEmpty()) throw new IndexOutOfBoundsException("List is empty");
52+
//endregion
3053

3154
return remove(random.nextInt(this.size()));
3255
}
3356

34-
public boolean addIf (E value, Predicate<? super E> filter)
57+
public boolean addIf (E value, @NotNull Predicate<? super E> filter)
3558
{
59+
//region --> Check params
60+
if (filter == null) throw new IllegalArgumentException("Filter param is null");
61+
//endregion
62+
3663
return filter.test(value) && add(value);
3764
}
3865

39-
public int addAllIf (Collection<? extends E> collection, Predicate<? super E> filter)
66+
public int addAllIf (@NotNull Collection<? extends E> collection, @NotNull Predicate<? super E> filter)
4067
{
41-
Objects.requireNonNull(collection);
42-
Objects.requireNonNull(filter);
68+
//region --> Check params
69+
if (collection == null) throw new IllegalArgumentException("Collection param is null");
70+
if (filter == null) throw new IllegalArgumentException("Filter param is null");
71+
//endregion
4372

4473
ArrayList8<E> paramList = new ArrayList8<>();
4574
paramList.addAll(collection);
@@ -51,53 +80,51 @@ public int addAllIf (Collection<? extends E> collection, Predicate<? super E> fi
5180
return sublist.size();
5281
}
5382

54-
public boolean contains (Predicate<? super E> filter)
83+
@Contract (pure = true)
84+
public boolean contains (@NotNull Predicate<? super E> filter)
5585
{
56-
Objects.requireNonNull(filter);
57-
58-
for (E e : this)
59-
{
60-
if (filter.test(e)) return true;
61-
}
86+
//region --> Check params
87+
if (filter == null) throw new IllegalArgumentException("Filter param is null");
88+
//endregion
6289

63-
return false;
90+
return this.stream().anyMatch(filter::test);
6491
}
6592

66-
public int countIf (Predicate<? super E> filter)
93+
public int countIf (@NotNull Predicate<? super E> filter)
6794
{
68-
Objects.requireNonNull(filter);
69-
70-
int count = 0;
71-
72-
for (E e : this)
73-
{
74-
if (filter.test(e)) count++;
75-
}
95+
//region --> Check params
96+
if (filter == null) throw new IllegalArgumentException("Filter param is null");
97+
//endregion
7698

77-
return count;
99+
return (int) this.stream().filter(filter::test).count();
78100
}
79101

80-
public ArrayList8<E> subList (Predicate<? super E> filter)
102+
public ArrayList8<E> subList (@NotNull Predicate<? super E> filter)
81103
{
82-
Objects.requireNonNull(filter);
83-
84-
ArrayList8<E> newList = new ArrayList8<>();
85-
86-
this.forEach(e -> newList.addIf(e, filter));
87-
88-
return newList;
104+
//region --> Check params
105+
if (filter == null) throw new IllegalArgumentException("Filter param is null");
106+
//endregion
107+
108+
return new ArrayList8<E>()
109+
{{
110+
this.forEach(e -> addIf(e, filter));
111+
}};
89112
}
90113

91-
public Optional<E> findAny (Predicate<? super E> filter)
114+
public Optional<E> findAny (@NotNull Predicate<? super E> filter)
92115
{
93-
Objects.requireNonNull(filter);
116+
//region --> Check params
117+
if (filter == null) throw new IllegalArgumentException("Filter param is null");
118+
//endregion
94119

95120
return this.isEmpty() ? Optional.empty() : subList(filter).stream().findAny();
96121
}
97122

98-
public Optional<E> findFirst (Predicate<? super E> filter)
123+
public Optional<E> findFirst (@NotNull Predicate<? super E> filter)
99124
{
100-
Objects.requireNonNull(filter);
125+
//region --> Check params
126+
if (filter == null) throw new IllegalArgumentException("Filter param is null");
127+
//endregion
101128

102129
for (E e : this)
103130
{
@@ -107,9 +134,11 @@ public Optional<E> findFirst (Predicate<? super E> filter)
107134
return Optional.empty();
108135
}
109136

110-
public Optional<E> max (Comparator<? super E> comparator)
137+
public Optional<E> max (@NotNull Comparator<? super E> comparator)
111138
{
112-
Objects.requireNonNull(comparator);
139+
//region --> Check params
140+
if (comparator == null) throw new IllegalArgumentException("Comparator param is null");
141+
//endregion
113142

114143
if (this.isEmpty()) return Optional.empty();
115144

@@ -123,9 +152,11 @@ public Optional<E> max (Comparator<? super E> comparator)
123152
return Optional.of(max);
124153
}
125154

126-
public Optional<E> min (Comparator<? super E> comparator)
155+
public Optional<E> min (@NotNull Comparator<? super E> comparator)
127156
{
128-
Objects.requireNonNull(comparator);
157+
//region --> Check params
158+
if (comparator == null) throw new IllegalArgumentException("Comparator param is null");
159+
//endregion
129160

130161
if (this.isEmpty()) return Optional.empty();
131162

@@ -138,4 +169,5 @@ public Optional<E> min (Comparator<? super E> comparator)
138169

139170
return Optional.of(min);
140171
}
172+
//endregion
141173
}

src/main/java/CodingUtils/AssertUtils.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,65 @@
11
package CodingUtils;
22

33
import org.jetbrains.annotations.Contract;
4+
import org.jetbrains.annotations.NotNull;
45

56
import java.util.Collection;
67
import java.util.Map;
7-
import java.util.Objects;
88

99
/*................................................................................................................................
1010
. Copyright (c)
1111
.
1212
. The AssertUtils Class was Coded by : Alexandre BOLOT
1313
.
14-
. Last Modified : 01/12/17 23:50
14+
. Last Modified : 20/01/18 00:24
1515
.
1616
. Contact : [email protected]
1717
...............................................................................................................................*/
1818

19-
@SuppressWarnings ({"WeakerAccess", "unused"})
19+
@SuppressWarnings ({"WeakerAccess", "unused", "ConstantConditions"})
2020
public class AssertUtils
2121
{
22-
//region ==================== assert not null (x2) =============================
22+
//region --------------- assert not null (x2) ------------------------
2323

2424
/**
2525
<hr>
2626
<h2>Tests if the parameters are null</h2>
27-
<h3>If one of them is —> throws NullPointerException, with index of first null object as message.</h3>
27+
<h3>If one of them is —> throws IllegalArgumentException, with index of first null object as message.</h3>
2828
<br>
2929
Note : asserts [array] isn't null.<br>
3030
<br>
3131
<hr>
3232
3333
@param array Array of T objects to test for null value
3434
*/
35+
@Contract ("null -> fail")
3536
@SafeVarargs
3637
public static <T> void assertNotNull (T... array)
3738
{
38-
Objects.requireNonNull(array, "List of Objects is null");
39+
if (array == null) throw new IllegalArgumentException("List of Objects is null");
3940

4041
for (int i = 0; i < array.length; i++)
4142
{
42-
Objects.requireNonNull(array[i], "Element at index " + i + " is null");
43+
if (array[i] == null) throw new IllegalArgumentException("Element at index " + i + " is null");
4344
}
4445
}
4546

4647
/**
4748
<hr>
4849
<h2>Tests if param is null</h2>
49-
<h3>If it is —> throws NullPointerException<br></h3>
50+
<h3>If it is —> throws IllegalArgumentException<br></h3>
5051
<hr>
5152
5253
@param t The ‹T› object to test for null value
5354
*/
5455
@Contract ("null-> fail")
5556
public static <T> void assertNotNull (T t)
5657
{
57-
Objects.requireNonNull(t, "Param is null");
58+
if (t == null) throw new IllegalArgumentException("Param is null");
5859
}
5960
//endregion
6061

61-
//region ==================== assert not empty (x3) ============================
62+
//region --------------- assert not empty (x3) -----------------------
6263

6364
/**
6465
<hr>
@@ -70,9 +71,9 @@ public static <T> void assertNotNull (T t)
7071
7172
@param string The String to test for empty value
7273
*/
73-
public static void assertNotEmpty (String string)
74+
public static void assertNotEmpty (@NotNull String string)
7475
{
75-
Objects.requireNonNull(string, "String is null");
76+
if (string == null) throw new IllegalArgumentException("String is null");
7677
if (string.isEmpty()) throw new IllegalArgumentException("String is empty");
7778
}
7879

@@ -84,11 +85,11 @@ public static void assertNotEmpty (String string)
8485
<br>
8586
<hr>
8687
87-
@param list The List to test for empty value
88+
@param collection The List to test for empty value
8889
*/
89-
public static void assertNotEmpty (Collection collection)
90+
public static void assertNotEmpty (@NotNull Collection collection)
9091
{
91-
Objects.requireNonNull(collection, "Collection is null");
92+
if (collection == null) throw new IllegalArgumentException("Collection is null");
9293
if (collection.isEmpty()) throw new IllegalArgumentException("List is empty");
9394
}
9495

@@ -102,14 +103,14 @@ public static void assertNotEmpty (Collection collection)
102103
103104
@param map The Map to test for empty value
104105
*/
105-
public static void assertNotEmpty (Map map)
106+
public static void assertNotEmpty (@NotNull Map map)
106107
{
107-
Objects.requireNonNull(map, "Map is null");
108+
if (map == null) throw new IllegalArgumentException("Map is null");
108109
if (map.isEmpty()) throw new IllegalArgumentException("Map is empty");
109110
}
110111
//endregion
111112

112-
//region ==================== assert strictly positive (x1) ====================
113+
//region --------------- assert strictly positive (x1) ---------------
113114

114115
/**
115116
<hr>

0 commit comments

Comments
 (0)