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

uk.gov.gchq.koryphe.tuple.TupleInputAdapter Maven / Gradle / Ivy

There is a newer version: 2.5.2
Show newest version
/*
 * Copyright 2017 Crown Copyright
 *
 * 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 uk.gov.gchq.koryphe.tuple;

import java.util.Arrays;
import java.util.function.Function;

/**
 * @param   The type of reference used by tuples.
 * @param  The adapted input type.
 */
public class TupleInputAdapter implements Function, FI> {
    private R[] selection;

    /**
     * Create a new TupleMask.
     */
    public TupleInputAdapter() {
        selection = (R[]) new Object[0];
    }

    /**
     * Create a new TupleMask with the given field references.
     *
     * @param selection Field references.
     */
    @SafeVarargs
    public TupleInputAdapter(final R... selection) {
        setSelection(selection);
    }

    @Override
    public FI apply(final Tuple input) {
        if (null == selection) {
            throw new IllegalArgumentException("Selection is required");
        }

        if (null != input) {
            if (1 == selection.length) {
                return (FI) input.get(selection[0]);
            }
        }

        return (FI) new ReferenceArrayTuple<>(input, selection);
    }

    /**
     * @return Field references.
     */
    public R[] getSelection() {
        return Arrays.copyOf(selection, selection.length);
    }

    /**
     * Set this TupleMask to refer to a tuple of field references.
     *
     * @param selection Field references.
     */
    public void setSelection(final R... selection) {
        if (null == selection) {
            this.selection = (R[]) new Object[0];
        } else {
            this.selection = selection;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy