/
FlatCollectIterable.java
155 lines (138 loc) · 5.14 KB
/
FlatCollectIterable.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright (c) 2016 Goldman Sachs.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.lazy;
import java.util.Iterator;
import java.util.Optional;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.block.procedure.AdaptObjectIntProcedureToProcedure;
import org.eclipse.collections.impl.lazy.iterator.FlatCollectIterator;
import org.eclipse.collections.impl.utility.Iterate;
public class FlatCollectIterable<T, V>
extends AbstractLazyIterable<V>
{
private final Iterable<T> adapted;
private final Function<? super T, ? extends Iterable<V>> function;
public FlatCollectIterable(Iterable<T> newAdapted, Function<? super T, ? extends Iterable<V>> function)
{
this.adapted = newAdapted;
this.function = function;
}
@Override
public void each(Procedure<? super V> procedure)
{
Iterate.forEach(this.adapted, each -> Iterate.forEach(this.function.valueOf(each), procedure));
}
@Override
public void forEachWithIndex(ObjectIntProcedure<? super V> objectIntProcedure)
{
Procedure<V> innerProcedure = new AdaptObjectIntProcedureToProcedure<>(objectIntProcedure);
Iterate.forEach(this.adapted, each ->
{
Iterable<V> iterable = this.function.valueOf(each);
Iterate.forEach(iterable, innerProcedure);
});
}
@Override
public <P> void forEachWith(Procedure2<? super V, ? super P> procedure, P parameter)
{
Iterate.forEach(this.adapted, each -> Iterate.forEachWith(this.function.valueOf(each), procedure, parameter));
}
@Override
public V detect(Predicate<? super V> predicate)
{
V[] result = (V[]) new Object[1];
Iterate.anySatisfy(this.adapted, each ->
{
Iterable<V> iterable = this.function.valueOf(each);
return Iterate.anySatisfy(iterable, each1 ->
{
if (predicate.accept(each1))
{
result[0] = each1;
return true;
}
return false;
});
});
return result[0];
}
@Override
public Optional<V> detectOptional(Predicate<? super V> predicate)
{
V[] result = (V[]) new Object[1];
Iterate.anySatisfy(this.adapted, each ->
{
if (each == null)
{
throw new NullPointerException();
}
Iterable<V> iterable = this.function.valueOf(each);
if (iterable == null)
{
throw new NullPointerException();
}
return Iterate.anySatisfy(iterable, each1 ->
{
if (predicate.accept(each1))
{
if (each1 == null)
{
throw new NullPointerException();
}
result[0] = each1;
return true;
}
return false;
});
});
return Optional.ofNullable(result[0]);
}
@Override
public boolean anySatisfy(Predicate<? super V> predicate)
{
return Iterate.anySatisfy(this.adapted, each -> Iterate.anySatisfy(this.function.valueOf(each), predicate));
}
@Override
public <P> boolean anySatisfyWith(Predicate2<? super V, ? super P> predicate, P parameter)
{
return this.anySatisfy(Predicates.bind(predicate, parameter));
}
@Override
public boolean allSatisfy(Predicate<? super V> predicate)
{
return Iterate.allSatisfy(this.adapted, each -> Iterate.allSatisfy(this.function.valueOf(each), predicate));
}
@Override
public <P> boolean allSatisfyWith(Predicate2<? super V, ? super P> predicate, P parameter)
{
return this.allSatisfy(Predicates.bind(predicate, parameter));
}
@Override
public boolean noneSatisfy(Predicate<? super V> predicate)
{
return Iterate.noneSatisfy(this.adapted, each -> Iterate.anySatisfy(this.function.valueOf(each), predicate));
}
@Override
public <P> boolean noneSatisfyWith(Predicate2<? super V, ? super P> predicate, P parameter)
{
return this.noneSatisfy(Predicates.bind(predicate, parameter));
}
@Override
public Iterator<V> iterator()
{
return new FlatCollectIterator<>(this.adapted, this.function);
}
}