All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.speedment.runtime.core.internal.stream.builder.streamterminator.StreamTerminatorUtil Maven / Gradle / Ivy

Go to download

A Speedment bundle that shades all dependencies into one jar. This is useful when deploying an application on a server.

There is a newer version: 3.1.18
Show newest version
/**
 *
 * Copyright (c) 2006-2017, 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.stream.builder.streamterminator;

import com.speedment.runtime.core.internal.stream.builder.action.reference.FilterAction;
import com.speedment.runtime.core.internal.util.Cast;
import com.speedment.runtime.core.stream.Pipeline;
import com.speedment.runtime.core.stream.action.Action;
import com.speedment.runtime.field.internal.predicate.AbstractCombinedPredicate;
import com.speedment.runtime.field.predicate.FieldPredicate;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

import static java.util.Objects.requireNonNull;

/**
 *
 * @author pemi
 */
public final class StreamTerminatorUtil {

    public static  List> topLevelAndPredicates(T initialPipeline) {
        final List> andPredicateBuilders = new ArrayList<>();

        for (final Action action : initialPipeline) {
            @SuppressWarnings("rawtypes")
            final Optional oFilterAction = Cast.cast(action, FilterAction.class);
            if (oFilterAction.isPresent()) {
                @SuppressWarnings("unchecked")
                final List> newAndPredicates = andPredicates(oFilterAction.get());
                andPredicateBuilders.addAll(newAndPredicates);
            } else {
                break; // We can only do initial consecutive FilterAction(s)
            }
        }
        return andPredicateBuilders;
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    public static  List> andPredicates(FilterAction action) {
        requireNonNull(action);
        final List> andPredicateBuilders = new ArrayList<>();
        final Predicate predicate = action.getPredicate();

        final Optional oPredicateBuilder = Cast.cast(predicate, FieldPredicate.class);
        if (oPredicateBuilder.isPresent()) {
            andPredicateBuilders.add(oPredicateBuilder.get()); // Just a top level predicate builder
        } else {

            final Optional oAndCombinedBasePredicate = Cast.cast(predicate, AbstractCombinedPredicate.AndCombinedBasePredicate.class);
            if (oAndCombinedBasePredicate.isPresent()) {

                final AbstractCombinedPredicate.AndCombinedBasePredicate andCombinedBasePredicate = (AbstractCombinedPredicate.AndCombinedBasePredicate) oAndCombinedBasePredicate.get();
                andCombinedBasePredicate.stream()
                    .map(p -> Cast.cast(p, FieldPredicate.class))
                    .filter(p -> p.isPresent())
                    .map(Optional::get)
                    .forEachOrdered(andPredicateBuilders::add);
            }
        }
        return andPredicateBuilders;
    }

    private StreamTerminatorUtil() {}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy