Skip to content

Commit d9163cc

Browse files
committed
Change test of params system + add some methods (tested)
1 parent 8764ebe commit d9163cc

File tree

2 files changed

+327
-90
lines changed

2 files changed

+327
-90
lines changed

src/main/java/CodingUtils/ArrayList8.java

Lines changed: 46 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,63 @@
44
import org.jetbrains.annotations.NotNull;
55

66
import java.util.*;
7+
import java.util.function.BinaryOperator;
8+
import java.util.function.Function;
79
import java.util.function.Predicate;
10+
import java.util.stream.Stream;
811

912
/*................................................................................................................................
1013
. Copyright (c)
1114
.
1215
. The ArrayList8 Class was Coded by : Alexandre BOLOT
1316
.
14-
. Last Modified : 20/01/18 00:17
17+
. Last Modified : 16/03/18 08:28
1518
.
1619
. Contact : [email protected]
1720
...............................................................................................................................*/
1821

19-
@SuppressWarnings ({"UnusedReturnValue", "ConstantConditions"})
22+
@SuppressWarnings ({"UnusedReturnValue", "ConstantConditions", "unchecked"})
2023
public class ArrayList8<E> extends ArrayList<E>
2124
{
22-
//region --------------- Attributes ----------------------
23-
private Random random = new Random();
24-
//endregion
25-
2625
//region --------------- Constructors --------------------
27-
public ArrayList8 ()
28-
{
29-
super();
30-
}
26+
public ArrayList8 () { super(); }
3127

32-
public ArrayList8 (Collection<? extends E> c)
33-
{
34-
super(c);
35-
}
28+
public ArrayList8 (@NotNull Collection<? extends E> c) { super(c); }
29+
30+
public ArrayList8 (@NotNull E[] array) { this(Arrays.asList(array)); }
3631
//endregion
3732

3833
//region --------------- Methods -------------------------
34+
@NotNull
3935
public E getRandom ()
4036
{
41-
//region --> Check params
4237
if (this.isEmpty()) throw new IndexOutOfBoundsException("List is empty");
43-
//endregion
4438

45-
return get(random.nextInt(this.size()));
39+
return get(new Random().nextInt(this.size()));
4640
}
4741

42+
@NotNull
4843
public E removeRandom ()
4944
{
50-
//region --> Check params
5145
if (this.isEmpty()) throw new IndexOutOfBoundsException("List is empty");
52-
//endregion
5346

54-
return remove(random.nextInt(this.size()));
47+
return remove(new Random().nextInt(this.size()));
5548
}
5649

57-
public boolean addIf (E value, @NotNull Predicate<? super E> filter)
50+
@NotNull
51+
public ArrayList8<E> merge (@NotNull Collection<? extends E> collection)
5852
{
59-
//region --> Check params
60-
if (filter == null) throw new IllegalArgumentException("Filter param is null");
61-
//endregion
53+
this.addAll(collection);
54+
return this;
55+
}
6256

57+
public boolean addIf (E value, @NotNull Predicate<? super E> filter)
58+
{
6359
return filter.test(value) && add(value);
6460
}
6561

6662
public int addAllIf (@NotNull Collection<? extends E> collection, @NotNull Predicate<? super E> filter)
6763
{
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
72-
7364
ArrayList8<E> paramList = new ArrayList8<>();
7465
paramList.addAll(collection);
7566

@@ -83,49 +74,42 @@ public int addAllIf (@NotNull Collection<? extends E> collection, @NotNull Predi
8374
@Contract (pure = true)
8475
public boolean contains (@NotNull Predicate<? super E> filter)
8576
{
86-
//region --> Check params
87-
if (filter == null) throw new IllegalArgumentException("Filter param is null");
88-
//endregion
89-
9077
return this.stream().anyMatch(filter::test);
9178
}
9279

93-
public int countIf (@NotNull Predicate<? super E> filter)
80+
@Contract (pure = true)
81+
public boolean containsAny (@NotNull E... items)
82+
{
83+
return Arrays.stream(items).anyMatch(this::contains);
84+
}
85+
86+
public boolean containsAll (@NotNull E... items)
9487
{
95-
//region --> Check params
96-
if (filter == null) throw new IllegalArgumentException("Filter param is null");
97-
//endregion
88+
return Arrays.stream(items).allMatch(this::contains);
89+
}
9890

91+
public int countIf (@NotNull Predicate<? super E> filter)
92+
{
9993
return (int) this.stream().filter(filter::test).count();
10094
}
10195

96+
@NotNull
10297
public ArrayList8<E> subList (@NotNull Predicate<? super E> filter)
10398
{
104-
//region --> Check params
105-
if (filter == null) throw new IllegalArgumentException("Filter param is null");
106-
//endregion
99+
ArrayList8<E> res = new ArrayList8<>();
100+
101+
this.forEach(e -> res.addIf(e, filter));
107102

108-
return new ArrayList8<E>()
109-
{{
110-
this.forEach(e -> addIf(e, filter));
111-
}};
103+
return res;
112104
}
113105

114106
public Optional<E> findAny (@NotNull Predicate<? super E> filter)
115107
{
116-
//region --> Check params
117-
if (filter == null) throw new IllegalArgumentException("Filter param is null");
118-
//endregion
119-
120108
return this.isEmpty() ? Optional.empty() : subList(filter).stream().findAny();
121109
}
122110

123111
public Optional<E> findFirst (@NotNull Predicate<? super E> filter)
124112
{
125-
//region --> Check params
126-
if (filter == null) throw new IllegalArgumentException("Filter param is null");
127-
//endregion
128-
129113
for (E e : this)
130114
{
131115
if (filter.test(e)) return Optional.of(e);
@@ -136,10 +120,6 @@ public Optional<E> findFirst (@NotNull Predicate<? super E> filter)
136120

137121
public Optional<E> max (@NotNull Comparator<? super E> comparator)
138122
{
139-
//region --> Check params
140-
if (comparator == null) throw new IllegalArgumentException("Comparator param is null");
141-
//endregion
142-
143123
if (this.isEmpty()) return Optional.empty();
144124

145125
E max = this.getRandom();
@@ -154,10 +134,6 @@ public Optional<E> max (@NotNull Comparator<? super E> comparator)
154134

155135
public Optional<E> min (@NotNull Comparator<? super E> comparator)
156136
{
157-
//region --> Check params
158-
if (comparator == null) throw new IllegalArgumentException("Comparator param is null");
159-
//endregion
160-
161137
if (this.isEmpty()) return Optional.empty();
162138

163139
E min = this.get(0);
@@ -169,5 +145,15 @@ public Optional<E> min (@NotNull Comparator<? super E> comparator)
169145

170146
return Optional.of(min);
171147
}
148+
149+
public Optional<E> reduce (@NotNull BinaryOperator<E> accumulator)
150+
{
151+
return this.stream().reduce(accumulator);
152+
}
153+
154+
public <R> Stream<R> map (@NotNull Function<? super E, ? extends R> mapper)
155+
{
156+
return this.stream().map(mapper);
157+
}
172158
//endregion
173159
}

0 commit comments

Comments
 (0)