4
4
import org .jetbrains .annotations .NotNull ;
5
5
6
6
import java .util .*;
7
+ import java .util .function .BinaryOperator ;
8
+ import java .util .function .Function ;
7
9
import java .util .function .Predicate ;
10
+ import java .util .stream .Stream ;
8
11
9
12
/*................................................................................................................................
10
13
. Copyright (c)
11
14
.
12
15
. The ArrayList8 Class was Coded by : Alexandre BOLOT
13
16
.
14
- . Last Modified : 20/01 /18 00:17
17
+ . Last Modified : 16/03 /18 08:28
15
18
.
16
19
17
20
...............................................................................................................................*/
18
21
19
- @ SuppressWarnings ({"UnusedReturnValue" , "ConstantConditions" })
22
+ @ SuppressWarnings ({"UnusedReturnValue" , "ConstantConditions" , "unchecked" })
20
23
public class ArrayList8 <E > extends ArrayList <E >
21
24
{
22
- //region --------------- Attributes ----------------------
23
- private Random random = new Random ();
24
- //endregion
25
-
26
25
//region --------------- Constructors --------------------
27
- public ArrayList8 ()
28
- {
29
- super ();
30
- }
26
+ public ArrayList8 () { super (); }
31
27
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 )); }
36
31
//endregion
37
32
38
33
//region --------------- Methods -------------------------
34
+ @ NotNull
39
35
public E getRandom ()
40
36
{
41
- //region --> Check params
42
37
if (this .isEmpty ()) throw new IndexOutOfBoundsException ("List is empty" );
43
- //endregion
44
38
45
- return get (random .nextInt (this .size ()));
39
+ return get (new Random () .nextInt (this .size ()));
46
40
}
47
41
42
+ @ NotNull
48
43
public E removeRandom ()
49
44
{
50
- //region --> Check params
51
45
if (this .isEmpty ()) throw new IndexOutOfBoundsException ("List is empty" );
52
- //endregion
53
46
54
- return remove (random .nextInt (this .size ()));
47
+ return remove (new Random () .nextInt (this .size ()));
55
48
}
56
49
57
- public boolean addIf (E value , @ NotNull Predicate <? super E > filter )
50
+ @ NotNull
51
+ public ArrayList8 <E > merge (@ NotNull Collection <? extends E > collection )
58
52
{
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
+ }
62
56
57
+ public boolean addIf (E value , @ NotNull Predicate <? super E > filter )
58
+ {
63
59
return filter .test (value ) && add (value );
64
60
}
65
61
66
62
public int addAllIf (@ NotNull Collection <? extends E > collection , @ NotNull Predicate <? super E > filter )
67
63
{
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
-
73
64
ArrayList8 <E > paramList = new ArrayList8 <>();
74
65
paramList .addAll (collection );
75
66
@@ -83,49 +74,42 @@ public int addAllIf (@NotNull Collection<? extends E> collection, @NotNull Predi
83
74
@ Contract (pure = true )
84
75
public boolean contains (@ NotNull Predicate <? super E > filter )
85
76
{
86
- //region --> Check params
87
- if (filter == null ) throw new IllegalArgumentException ("Filter param is null" );
88
- //endregion
89
-
90
77
return this .stream ().anyMatch (filter ::test );
91
78
}
92
79
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 )
94
87
{
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
+ }
98
90
91
+ public int countIf (@ NotNull Predicate <? super E > filter )
92
+ {
99
93
return (int ) this .stream ().filter (filter ::test ).count ();
100
94
}
101
95
96
+ @ NotNull
102
97
public ArrayList8 <E > subList (@ NotNull Predicate <? super E > filter )
103
98
{
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 ));
107
102
108
- return new ArrayList8 <E >()
109
- {{
110
- this .forEach (e -> addIf (e , filter ));
111
- }};
103
+ return res ;
112
104
}
113
105
114
106
public Optional <E > findAny (@ NotNull Predicate <? super E > filter )
115
107
{
116
- //region --> Check params
117
- if (filter == null ) throw new IllegalArgumentException ("Filter param is null" );
118
- //endregion
119
-
120
108
return this .isEmpty () ? Optional .empty () : subList (filter ).stream ().findAny ();
121
109
}
122
110
123
111
public Optional <E > findFirst (@ NotNull Predicate <? super E > filter )
124
112
{
125
- //region --> Check params
126
- if (filter == null ) throw new IllegalArgumentException ("Filter param is null" );
127
- //endregion
128
-
129
113
for (E e : this )
130
114
{
131
115
if (filter .test (e )) return Optional .of (e );
@@ -136,10 +120,6 @@ public Optional<E> findFirst (@NotNull Predicate<? super E> filter)
136
120
137
121
public Optional <E > max (@ NotNull Comparator <? super E > comparator )
138
122
{
139
- //region --> Check params
140
- if (comparator == null ) throw new IllegalArgumentException ("Comparator param is null" );
141
- //endregion
142
-
143
123
if (this .isEmpty ()) return Optional .empty ();
144
124
145
125
E max = this .getRandom ();
@@ -154,10 +134,6 @@ public Optional<E> max (@NotNull Comparator<? super E> comparator)
154
134
155
135
public Optional <E > min (@ NotNull Comparator <? super E > comparator )
156
136
{
157
- //region --> Check params
158
- if (comparator == null ) throw new IllegalArgumentException ("Comparator param is null" );
159
- //endregion
160
-
161
137
if (this .isEmpty ()) return Optional .empty ();
162
138
163
139
E min = this .get (0 );
@@ -169,5 +145,15 @@ public Optional<E> min (@NotNull Comparator<? super E> comparator)
169
145
170
146
return Optional .of (min );
171
147
}
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
+ }
172
158
//endregion
173
159
}
0 commit comments