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

com.blazebit.persistence.impl.builder.object.MultisetTransformingObjectBuilder Maven / Gradle / Ivy

/*
 * Copyright 2014 - 2020 Blazebit.
 *
 * 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.blazebit.persistence.impl.builder.object;

import com.blazebit.persistence.ObjectBuilder;
import com.blazebit.persistence.SelectBuilder;
import com.blazebit.persistence.impl.SelectInfo;
import com.blazebit.persistence.parser.expression.Expression;
import com.blazebit.persistence.parser.expression.FunctionExpression;
import com.blazebit.persistence.spi.JpqlFunctionProcessor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Christian Beikov
 * @since 1.5.0
 */
public class MultisetTransformingObjectBuilder implements ObjectBuilder {

    private final ProcessorEntry[] processorEntries;

    public MultisetTransformingObjectBuilder(Map> jpqlFunctionProcessors, List arguments) {
        List processorEntries = new ArrayList<>(jpqlFunctionProcessors.size());
        for (Map.Entry> entry : jpqlFunctionProcessors.entrySet()) {
            processorEntries.add(new ProcessorEntry(entry.getValue(), entry.getKey(), ((FunctionExpression) arguments.get(entry.getKey()).getExpression()).getExpressions()));
        }
        this.processorEntries = processorEntries.toArray(new ProcessorEntry[0]);
    }

    public MultisetTransformingObjectBuilder(List arguments, Map> jpqlFunctionProcessors) {
        List processorEntries = new ArrayList<>(jpqlFunctionProcessors.size());
        for (Map.Entry> entry : jpqlFunctionProcessors.entrySet()) {
            processorEntries.add(new ProcessorEntry(entry.getValue(), entry.getKey(), ((FunctionExpression) arguments.get(entry.getKey())).getExpressions()));
        }
        this.processorEntries = processorEntries.toArray(new ProcessorEntry[0]);
    }

    @Override
    public > void applySelects(X selectBuilder) {
    }

    @Override
    public Object[] build(Object[] tuple) {
        for (int i = 0; i < processorEntries.length; i++) {
            ProcessorEntry processorEntry = processorEntries[i];
            int index = processorEntry.index;
            tuple[index] = processorEntry.processor.process(tuple[index], processorEntry.arguments);
        }

        return tuple;
    }

    @Override
    public List buildList(List list) {
        return list;
    }

    /**
     *
     * @author Christian Beikov
     * @since 1.5.0
     */
    private static final class ProcessorEntry {
        private final JpqlFunctionProcessor processor;
        private final int index;
        private final List arguments;

        public ProcessorEntry(JpqlFunctionProcessor processor, int index, List arguments) {
            this.processor = (JpqlFunctionProcessor) processor;
            this.index = index;
            this.arguments = (List) arguments;
        }
    }
}