-
Notifications
You must be signed in to change notification settings - Fork 587
/
ParallelDistinctListIterable.java
167 lines (143 loc) · 5.5 KB
/
ParallelDistinctListIterable.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
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright (c) 2022 Goldman Sachs and others.
* 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.parallel.list;
import java.util.concurrent.ExecutorService;
import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.annotation.Beta;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.map.ConcurrentMutableMap;
import org.eclipse.collections.api.map.MapIterable;
import org.eclipse.collections.api.multimap.set.UnsortedSetMultimap;
import org.eclipse.collections.api.set.ParallelUnsortedSetIterable;
import org.eclipse.collections.impl.lazy.parallel.set.AbstractParallelUnsortedSetIterable;
import org.eclipse.collections.impl.lazy.parallel.set.UnsortedSetBatch;
import org.eclipse.collections.impl.map.mutable.ConcurrentHashMap;
import org.eclipse.collections.impl.multimap.set.UnifiedSetMultimap;
@Beta
class ParallelDistinctListIterable<T> extends AbstractParallelUnsortedSetIterable<T, UnsortedSetBatch<T>>
{
private final AbstractParallelListIterable<T, ? extends ListBatch<T>> delegate;
ParallelDistinctListIterable(AbstractParallelListIterable<T, ? extends ListBatch<T>> delegate)
{
this.delegate = delegate;
}
@Override
public ExecutorService getExecutorService()
{
return this.delegate.getExecutorService();
}
@Override
public int getBatchSize()
{
return this.delegate.getBatchSize();
}
@Override
public LazyIterable<UnsortedSetBatch<T>> split()
{
// TODO: Replace the map with a concurrent set once it's implemented
ConcurrentHashMap<T, Boolean> distinct = new ConcurrentHashMap<>();
return this.delegate.split().collect(listBatch -> listBatch.distinct(distinct));
}
@Override
public ParallelUnsortedSetIterable<T> asUnique()
{
return this;
}
@Override
public void forEach(Procedure<? super T> procedure)
{
// TODO: Replace the map with a concurrent set once it's implemented
ConcurrentMutableMap<T, Boolean> distinct = new ConcurrentHashMap<>();
this.delegate.forEach(each -> {
if (distinct.put(each, true) == null)
{
procedure.value(each);
}
});
}
@Override
public boolean anySatisfy(Predicate<? super T> predicate)
{
return this.delegate.anySatisfy(new DistinctAndPredicate<>(predicate));
}
@Override
public boolean allSatisfy(Predicate<? super T> predicate)
{
return this.delegate.allSatisfy(new DistinctOrPredicate<>(predicate));
}
@Override
public T detect(Predicate<? super T> predicate)
{
return this.delegate.detect(new DistinctAndPredicate<>(predicate));
}
@Override
public Object[] toArray()
{
// TODO: Implement in parallel
return this.delegate.toList().distinct().toArray();
}
@Override
public <E> E[] toArray(E[] array)
{
// TODO: Implement in parallel
return this.delegate.toList().distinct().toArray(array);
}
@Override
public <V> UnsortedSetMultimap<V, T> groupBy(Function<? super T, ? extends V> function)
{
// TODO: Implement in parallel
return this.delegate.toSet().groupBy(function, new UnifiedSetMultimap<>());
}
@Override
public <V> UnsortedSetMultimap<V, T> groupByEach(Function<? super T, ? extends Iterable<V>> function)
{
// TODO: Implement in parallel
return this.delegate.toSet().groupByEach(function, new UnifiedSetMultimap<>());
}
@Override
public <V> MapIterable<V, T> groupByUniqueKey(Function<? super T, ? extends V> function)
{
// TODO: Implement in parallel
return this.delegate.toSet().groupByUniqueKey(function);
}
private static final class DistinctAndPredicate<T> implements Predicate<T>
{
// TODO: Replace the map with a concurrent set once it's implemented
private final ConcurrentMutableMap<T, Boolean> distinct = new ConcurrentHashMap<>();
private final Predicate<? super T> predicate;
private DistinctAndPredicate(Predicate<? super T> predicate)
{
this.predicate = predicate;
}
@Override
public boolean accept(T each)
{
return this.distinct.put(each, true) == null && this.predicate.accept(each);
}
}
private static final class DistinctOrPredicate<T> implements Predicate<T>
{
// TODO: Replace the map with a concurrent set once it's implemented
private final ConcurrentMutableMap<T, Boolean> distinct = new ConcurrentHashMap<>();
private final Predicate<? super T> predicate;
private DistinctOrPredicate(Predicate<? super T> predicate)
{
this.predicate = predicate;
}
@Override
public boolean accept(T each)
{
boolean distinct = this.distinct.put(each, true) == null;
return distinct && this.predicate.accept(each) || !distinct;
}
}
}