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

org.apache.tinkerpop.gremlin.util.function.Lambda Maven / Gradle / Ivy

There is a newer version: 4.15.102
Show newest version
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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 org.apache.tinkerpop.gremlin.util.function;

import java.io.Serializable;
import java.util.Comparator;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @author Marko A. Rodriguez (http://markorodriguez.com)
 */
public interface Lambda extends Serializable {

    public String getLambdaScript();

    public String getLambdaLanguage();

    public int getLambdaArguments();

    public abstract static class AbstractLambda implements Lambda {
        private final String lambdaSource;
        private final String lambdaLanguage;
        private final int lambdaArguments;

        private AbstractLambda(final String lambdaSource, final String lambdaLanguage, final int lambdaArguments) {
            this.lambdaSource = lambdaSource;
            this.lambdaLanguage = lambdaLanguage;
            this.lambdaArguments = lambdaArguments;
        }

        @Override
        public String getLambdaScript() {
            return this.lambdaSource;
        }

        @Override
        public String getLambdaLanguage() {
            return this.lambdaLanguage;
        }

        @Override
        public int getLambdaArguments() {
            return this.lambdaArguments;
        }

        @Override
        public String toString() {
            return "lambda[" + this.lambdaSource + "]";
        }

        @Override
        public int hashCode() {
            return this.lambdaSource.hashCode() + this.lambdaLanguage.hashCode() + this.lambdaArguments;
        }

        @Override
        public boolean equals(final Object object) {
            return object instanceof Lambda &&
                    ((Lambda) object).getLambdaArguments() == this.lambdaArguments &&
                    ((Lambda) object).getLambdaScript().equals(this.lambdaSource) &&
                    ((Lambda) object).getLambdaLanguage().equals(this.lambdaLanguage);
        }
    }

    public static class UnknownArgLambda extends AbstractLambda {

        public UnknownArgLambda(final String lambdaSource, final String lambdaLanguage, final int lambdaArguments) {
            super(lambdaSource, lambdaLanguage, lambdaArguments);
        }
    }

    public static class ZeroArgLambda extends AbstractLambda implements Supplier {

        public ZeroArgLambda(final String lambdaSource, final String lambdaLanguage) {
            super(lambdaSource, lambdaLanguage, 0);
        }

        @Override
        public A get() {
            return null;
        }

    }

    public static class OneArgLambda extends AbstractLambda implements Function, Predicate, Consumer {

        public OneArgLambda(final String lambdaSource, final String lambdaLanguage) {
            super(lambdaSource, lambdaLanguage, 1);
        }

        @Override
        public B apply(final A a) {
            return null;
        }

        @Override
        public boolean test(final A a) {
            return false;
        }

        @Override
        public void accept(final A a) {

        }
    }

    public static class TwoArgLambda extends AbstractLambda implements BiFunction, Comparator {

        public TwoArgLambda(final String lambdaSource, final String lambdaLanguage) {
            super(lambdaSource, lambdaLanguage, 2);
        }

        @Override
        public C apply(final A a, final B b) {
            return null;
        }


        @Override
        public int compare(final A first, final A second) {
            return 0;
        }
    }

    ///

    public static String DEFAULT_LAMBDA_LANGUAGE = "gremlin-groovy";

    public static  Function function(final String lambdaSource, final String lambdaLanguage) {
        return new OneArgLambda<>(lambdaSource, lambdaLanguage);
    }

    public static  Predicate predicate(final String lambdaSource, final String lambdaLanguage) {
        return new OneArgLambda<>(lambdaSource, lambdaLanguage);
    }

    public static  Consumer consumer(final String lambdaSource, final String lambdaLanguage) {
        return new OneArgLambda<>(lambdaSource, lambdaLanguage);
    }

    public static  Supplier supplier(final String lambdaSource, final String lambdaLanguage) {
        return new ZeroArgLambda<>(lambdaSource, lambdaLanguage);
    }

    public static  Comparator comparator(final String lambdaSource, final String lambdaLanguage) {
        return new TwoArgLambda<>(lambdaSource, lambdaLanguage);
    }

    public static  BiFunction biFunction(final String lambdaSource, final String lambdaLanguage) {
        return new TwoArgLambda<>(lambdaSource, lambdaLanguage);
    }

    //

    public static  Function function(final String lambdaSource) {
        return new OneArgLambda<>(lambdaSource, DEFAULT_LAMBDA_LANGUAGE);
    }

    public static  Predicate predicate(final String lambdaSource) {
        return new OneArgLambda<>(lambdaSource, DEFAULT_LAMBDA_LANGUAGE);
    }

    public static  Consumer consumer(final String lambdaSource) {
        return new OneArgLambda<>(lambdaSource, DEFAULT_LAMBDA_LANGUAGE);
    }

    public static  Supplier supplier(final String lambdaSource) {
        return new ZeroArgLambda<>(lambdaSource, DEFAULT_LAMBDA_LANGUAGE);
    }

    public static  Comparator comparator(final String lambdaSource) {
        return new TwoArgLambda<>(lambdaSource, DEFAULT_LAMBDA_LANGUAGE);
    }

    public static  BiFunction biFunction(final String lambdaSource) {
        return new TwoArgLambda<>(lambdaSource, DEFAULT_LAMBDA_LANGUAGE);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy