com.speedment.runtime.core.internal.manager.sql.SqlStreamTerminator Maven / Gradle / Ivy
Show all versions of tool-deploy Show documentation
/*
*
* Copyright (c) 2006-2019, 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.runtime.core.internal.manager.sql;
import com.speedment.runtime.core.component.sql.SqlStreamOptimizer;
import com.speedment.runtime.core.component.sql.SqlStreamOptimizerComponent;
import com.speedment.runtime.core.component.sql.SqlStreamOptimizerInfo;
import com.speedment.runtime.core.component.sql.override.SqlStreamTerminatorComponent;
import com.speedment.runtime.core.db.AsynchronousQueryResult;
import com.speedment.runtime.core.internal.stream.builder.pipeline.DoublePipeline;
import com.speedment.runtime.core.internal.stream.builder.pipeline.IntPipeline;
import com.speedment.runtime.core.internal.stream.builder.pipeline.LongPipeline;
import com.speedment.runtime.core.internal.stream.builder.pipeline.ReferencePipeline;
import com.speedment.runtime.core.internal.stream.builder.streamterminator.StreamTerminator;
import com.speedment.runtime.core.stream.Pipeline;
import com.speedment.runtime.core.util.StreamComposition;
import java.util.Comparator;
import java.util.Iterator;
import static java.util.Objects.requireNonNull;
import java.util.Optional;
import java.util.PrimitiveIterator;
import java.util.Spliterator;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collector;
/**
* A class that will terminate ENTITY streams. ENTITY is the original type.
*
* Type T is the final type after stream mappings.
*
*
* @author pemi
* @param the entity type of the original stream, e.g. hares.stream()
* is of type Hare
*
*/
public final class SqlStreamTerminator implements StreamTerminator {
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."
+ "Note: If you want to enable this functionality, please use the .withAllowStreamIteratorAndSpliterator() application builder "
+ "method. Be aware though, you are then responsible for closing the stream implicitly after use, ALWAYS";
private final SqlStreamTerminatorComponent sqlStreamTerminatorComponent;
private final SqlStreamOptimizerComponent sqlStreamOptimizerComponent;
private final SqlStreamOptimizerInfo info;
private final AsynchronousQueryResult asynchronousQueryResult;
private final boolean allowIteratorAndSpliterator;
public SqlStreamTerminator(
final SqlStreamOptimizerInfo info,
final AsynchronousQueryResult asynchronousQueryResult,
final SqlStreamOptimizerComponent sqlStreamOptimizerComponent,
final SqlStreamTerminatorComponent sqlStreamTerminatorComponent,
final boolean allowIteratorAndSpliterator
) {
this.info = requireNonNull(info);
this.asynchronousQueryResult = requireNonNull(asynchronousQueryResult);
this.sqlStreamOptimizerComponent = requireNonNull(sqlStreamOptimizerComponent);
this.sqlStreamTerminatorComponent = requireNonNull(sqlStreamTerminatorComponent);
this.allowIteratorAndSpliterator = allowIteratorAndSpliterator;
}
//Todo: Remove this and split up responsibility
public AsynchronousQueryResult getAsynchronousQueryResult() {
return asynchronousQueryResult;
}
@Override
public P optimize(final P initialPipeline) {
requireNonNull(initialPipeline);
final SqlStreamOptimizer optimizer = sqlStreamOptimizerComponent.get(initialPipeline, info.getDbmsType());
return optimizer.optimize(initialPipeline, info, asynchronousQueryResult);
}
@Override
public void forEach(ReferencePipeline pipeline, Consumer action) {
sqlStreamTerminatorComponent.getForEachTerminator().apply(info, this, pipeline, action);
}
@Override
public void forEachOrdered(ReferencePipeline pipeline, Consumer action) {
sqlStreamTerminatorComponent.getForEachOrderedTerminator().apply(info, this, pipeline, action);
}
@Override
public Object[] toArray(ReferencePipeline pipeline) {
return sqlStreamTerminatorComponent.getToArrayTerminator().apply(info, this, pipeline);
}
@Override
public A[] toArray(ReferencePipeline pipeline, IntFunction generator) {
return sqlStreamTerminatorComponent.getToArrayGeneratorTerminator().apply(info, this, pipeline, generator);
}
@Override
public T reduce(ReferencePipeline pipeline, T identity, BinaryOperator accumulator) {
return sqlStreamTerminatorComponent.getReduceIdentityTerminator().apply(info, this, pipeline, identity, accumulator);
}
@Override
public U reduce(ReferencePipeline pipeline, U identity, BiFunction accumulator, BinaryOperator combiner) {
return sqlStreamTerminatorComponent.getReduceIdentityCombinerTerminator().apply(info, this, pipeline, identity, accumulator, combiner);
}
@Override
public Optional reduce(ReferencePipeline pipeline, BinaryOperator accumulator) {
return sqlStreamTerminatorComponent.getReduceTerminator().apply(info, this, pipeline, accumulator);
}
@Override
public R collect(ReferencePipeline pipeline, Supplier supplier, BiConsumer accumulator, BiConsumer combiner) {
return sqlStreamTerminatorComponent.getCollectSupplierAccumulatorCombinerTerminator().apply(info, this, pipeline, supplier, accumulator, combiner);
}
@Override
public R collect(ReferencePipeline pipeline, Collector collector) {
return sqlStreamTerminatorComponent.getCollectTerminator().apply(info, this, pipeline, collector);
}
@Override
public Optional min(ReferencePipeline pipeline, Comparator comparator) {
return sqlStreamTerminatorComponent.getMinTerminator().apply(info, this, pipeline, comparator);
}
@Override
public Optional max(ReferencePipeline pipeline, Comparator comparator) {
return sqlStreamTerminatorComponent.getMaxTerminator().apply(info, this, pipeline, comparator);
}
@Override
public long count(ReferencePipeline pipeline) {
return sqlStreamTerminatorComponent.getCountTerminator().apply(info, this, pipeline);
}
@Override
public boolean anyMatch(ReferencePipeline pipeline, Predicate predicate) {
return sqlStreamTerminatorComponent.getAnyMatchTerminator().apply(info, this, pipeline, predicate);
}
@Override
public boolean allMatch(ReferencePipeline pipeline, Predicate predicate) {
return sqlStreamTerminatorComponent.getAllMatchTerminator().apply(info, this, pipeline, predicate);
}
@Override
public boolean noneMatch(ReferencePipeline pipeline, Predicate predicate) {
return sqlStreamTerminatorComponent.getNoneMatchTerminator().apply(info, this, pipeline, predicate);
}
@Override
public Optional findAny(ReferencePipeline pipeline) {
return sqlStreamTerminatorComponent.getFindAnyTerminator().apply(info, this, pipeline);
}
@Override
public Optional findFirst(ReferencePipeline pipeline) {
return sqlStreamTerminatorComponent.getFindFirstTerminator().apply(info, this, pipeline);
}
@Override
public Iterator iterator(ReferencePipeline pipeline) {
if (allowIteratorAndSpliterator) {
return sqlStreamTerminatorComponent.getIteratorTerminator().apply(info, this, pipeline);
}
throw new UnsupportedOperationException(UNSUPPORTED_BECAUSE_OF_CLOSE_MAY_NOT_BE_CALLED);
}
@Override
public Spliterator spliterator(ReferencePipeline pipeline) {
if (allowIteratorAndSpliterator) {
return sqlStreamTerminatorComponent.getSpliteratorTerminator().apply(info, this, pipeline);
}
throw new UnsupportedOperationException(UNSUPPORTED_BECAUSE_OF_CLOSE_MAY_NOT_BE_CALLED);
}
///////////// double
@Override
public long count(DoublePipeline pipeline) {
return sqlStreamTerminatorComponent.getDoubleCountTerminator().apply(info, this, pipeline);
}
// Todo: Introduce delegator
@Override
public PrimitiveIterator.OfDouble iterator(DoublePipeline pipeline) {
if (allowIteratorAndSpliterator) {
return StreamTerminator.super.iterator(pipeline);
}
throw new UnsupportedOperationException(UNSUPPORTED_BECAUSE_OF_CLOSE_MAY_NOT_BE_CALLED);
}
// Todo: Introduce delegator
@Override
public Spliterator.OfDouble spliterator(DoublePipeline pipeline) {
if (allowIteratorAndSpliterator) {
return StreamTerminator.super.spliterator(pipeline);
}
throw new UnsupportedOperationException(UNSUPPORTED_BECAUSE_OF_CLOSE_MAY_NOT_BE_CALLED);
}
///////////// int
@Override
public long count(IntPipeline pipeline) {
return sqlStreamTerminatorComponent.getIntCountTerminator().apply(info, this, pipeline);
}
// Todo: Introduce delegator
@Override
public PrimitiveIterator.OfInt iterator(IntPipeline pipeline) {
if (allowIteratorAndSpliterator) {
return StreamTerminator.super.iterator(pipeline);
}
throw new UnsupportedOperationException(UNSUPPORTED_BECAUSE_OF_CLOSE_MAY_NOT_BE_CALLED);
}
// Todo: Introduce delegator
@Override
public Spliterator.OfInt spliterator(IntPipeline pipeline) {
if (allowIteratorAndSpliterator) {
return StreamTerminator.super.spliterator(pipeline);
}
throw new UnsupportedOperationException(UNSUPPORTED_BECAUSE_OF_CLOSE_MAY_NOT_BE_CALLED);
}
///////////// long
@Override
public long count(LongPipeline pipeline) {
return sqlStreamTerminatorComponent.getLongCountTerminator().apply(info, this, pipeline);
}
// Todo: Introduce delegator
@Override
public PrimitiveIterator.OfLong iterator(LongPipeline pipeline) {
if (allowIteratorAndSpliterator) {
return StreamTerminator.super.iterator(pipeline);
}
throw new UnsupportedOperationException(UNSUPPORTED_BECAUSE_OF_CLOSE_MAY_NOT_BE_CALLED);
}
// Todo: Introduce delegator
@Override
public Spliterator.OfLong spliterator(LongPipeline pipeline) {
if (allowIteratorAndSpliterator) {
return StreamTerminator.super.spliterator(pipeline);
}
throw new UnsupportedOperationException(UNSUPPORTED_BECAUSE_OF_CLOSE_MAY_NOT_BE_CALLED);
}
}