/
AbstractStreamBuilder.java
184 lines (161 loc) · 5.4 KB
/
AbstractStreamBuilder.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
*
* Copyright (c) 2006-2015, Speedment, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); You may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.speedment.internal.core.stream.builder;
import com.speedment.exception.SpeedmentException;
import com.speedment.internal.core.stream.autoclose.AbstractAutoClosingStream;
import com.speedment.internal.core.stream.builder.pipeline.PipelineImpl;
import com.speedment.internal.core.stream.builder.streamterminator.StreamTerminator;
import com.speedment.internal.core.stream.builder.action.Action;
import com.speedment.util.StreamComposition;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import static java.util.Objects.requireNonNull;
import java.util.Set;
import java.util.function.BooleanSupplier;
import java.util.function.DoubleSupplier;
import java.util.function.IntSupplier;
import java.util.function.LongSupplier;
import java.util.function.Supplier;
import java.util.stream.BaseStream;
/**
*
* @author pemi
* @param <T> The type of the super class
* @param <P> Pipeline type
*/
public abstract class AbstractStreamBuilder<T extends AbstractStreamBuilder<T, P>, P> {
protected final String UNSUPPORTED_BECAUSE_OF_CLOSE_MAY_NOT_BE_CALLED = "This method has been disabled for this Stream type because improper use will "
+ "lead to resources not being freed up. "
+ "We regret any inconvenience caused by this. "
+ "If you want to concatenate two or more stream, please use the " + StreamComposition.class.getName() + "#concatAndAutoClose() method instead.";
protected final PipelineImpl<?> pipeline;
protected final StreamTerminator streamTerminator;
protected final Set<BaseStream<?, ?>> streamSet; // Keeps track of the chain of streams so that we can auto-close them all
private final List<Runnable> closeHandlers; // The close handlers for this particular stream
private boolean parallel;
private boolean ordered;
private boolean closed;
protected AbstractStreamBuilder(PipelineImpl<?> pipeline, StreamTerminator streamTerminator, Set<BaseStream<?, ?>> streamSet) {
this.pipeline = requireNonNull(pipeline);
this.streamTerminator = requireNonNull(streamTerminator);
this.closeHandlers = new ArrayList<>();
this.ordered = true;
this.streamSet = streamSet;
}
protected T append(Action<?, ?> newAction) {
requireNonNull(newAction);
pipeline.add(newAction);
return self();
}
public T sequential() {
parallel = false;
return self();
}
public T parallel() {
parallel = true;
return self();
}
public boolean isParallel() {
return parallel;
}
public T unordered() {
ordered = false;
return self();
}
public T onClose(Runnable closeHandler) {
requireNonNull(closeHandler);
closeHandlers.add(closeHandler);
return self();
}
public void close() {
if (!closed) {
closed = true;
try {
if (!closeHandlers.isEmpty()) {
AbstractAutoClosingStream.composedRunnable(closeHandlers); // Run this stream's close handlers
}
} catch (Exception e) {
throw new SpeedmentException(e);
} finally {
try {
AbstractAutoClosingStream.composedClose(streamSet.toArray(new AutoCloseable[0])); // Close the other streams
} catch (Exception e) {
throw new SpeedmentException(e);
}
}
}
}
public boolean isOrdered() {
return ordered;
}
protected P pipeline() {
@SuppressWarnings("unchecked")
final P result = (P) pipeline;
return result;
}
private T self() {
@SuppressWarnings("unchecked")
final T thizz = (T) this;
return thizz;
}
protected <T> boolean finallyClose(BooleanSupplier bs) {
try {
return bs.getAsBoolean();
} finally {
close();
}
}
protected <T> long finallyClose(LongSupplier lp) {
try {
return lp.getAsLong();
} finally {
close();
}
}
protected <T> int finallyClose(IntSupplier is) {
try {
return is.getAsInt();
} finally {
close();
}
}
protected <T> double finallyClose(DoubleSupplier ds) {
try {
return ds.getAsDouble();
} finally {
close();
}
}
protected <T> void finallyClose(Runnable r) {
try {
r.run();
} finally {
close();
}
}
protected <T> T finallyClose(Supplier<T> s) {
try {
return s.get();
} finally {
close();
}
}
protected static Set<BaseStream<?, ?>> newStreamSet() {
return new HashSet<>();
}
}