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

org.apache.brooklyn.util.guava.Functionals Maven / Gradle / Ivy

Go to download

Utility classes and methods developed for Brooklyn but not dependendent on Brooklyn or much else

There is a newer version: 1.1.0
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.brooklyn.util.guava;

import java.util.concurrent.Callable;

import org.apache.brooklyn.util.guava.IfFunctions.IfFunctionBuilderApplyingFirst;

import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;

public class Functionals {

    /** applies f1 to the input, then the result of that is passed to f2 (note opposite semantics to {@link Functions#compose(Function, Function)} */ 
    public static  Function chain(final Function f1, final Function f2) {
        return Functions.compose(f2, f1);
    }
    
    /** applies f1 to the input, then f2 to that result, then f3 to that result */
    public static  Function chain(final Function f1, final Function f2, final Function f3) {
        return chain(f1, chain(f2, f3));
    }
    
    /** applies f1 to the input, then f2 to that result, then f3 to that result, then f4 to that result */
    public static  Function chain(final Function f1, final Function f2, final Function f3, final Function f4) {
        return chain(f1, chain(f2, chain(f3, f4)));
    }

    /** @see IfFunctions */
    public static  IfFunctionBuilderApplyingFirst ifEquals(I test) {
        return IfFunctions.ifEquals(test);
    }

    /** @see IfFunctions */
    public static  IfFunctionBuilderApplyingFirst ifNotEquals(I test) {
        return IfFunctions.ifNotEquals(test);
    }
    
    /** @see IfFunctions */
    public static  IfFunctionBuilderApplyingFirst ifPredicate(Predicate test) {
        return IfFunctions.ifPredicate(test);
    }

    /** like guava equivalent but parametrises the input generic type, and allows tostring to be customised */
    public static final class ConstantFunction implements Function {
        private final O constant;
        private Object toStringDescription;

        public ConstantFunction(O constant) {
            this(constant, null);
        }
        public ConstantFunction(O constant, Object toStringDescription) {
            this.constant = constant;
            this.toStringDescription = toStringDescription;
        }

        @Override
        public O apply(I input) {
            return constant;
        }
        
        @Override
        public String toString() {
            return toStringDescription==null ? "constant("+constant+")" : toStringDescription.toString();
        }
    }

    /** like guava {@link Functions#forSupplier(Supplier)} but parametrises the input generic type */
    public static  Function function(final Supplier supplier) {
        class SupplierAsFunction implements Function {
            @Override public O apply(I input) {
                return supplier.get();
            }
            @Override public String toString() {
                return "function("+supplier+")";
            }
        }
        return new SupplierAsFunction();
    }

    public static  Function function(final Runnable runnable) {
        class RunnableAsFunction implements Function {
            @Override public Void apply(I input) {
                runnable.run();
                return null;
            }
        }
        return new RunnableAsFunction();
    }

    public static Runnable runnable(final Supplier supplier) {
        class SupplierAsRunnable implements Runnable {
            @Override
            public void run() {
                supplier.get();
            }
        }
        return new SupplierAsRunnable();
    }

    public static  Callable callable(final Supplier supplier) {
        class SupplierAsCallable implements Callable {
            @Override
            public T call() {
                return supplier.get();
            }
            @Override
            public String toString() {
                return "callable("+supplier+")";
            }
        }
        return new SupplierAsCallable();
    }
    public static  Callable callable(Function f, T x) {
        return callable(Suppliers.compose(f, Suppliers.ofInstance(x)));
    }

    public static  Predicate predicate(final Function f) {
        // Deprecated use of anonymous class (even though it's got a name, it's in a method).
        // Kept for rebinding to historic persisted state only.
        @SuppressWarnings({ "unused", "hiding" })
        class FunctionAsPredicate implements Predicate {
            @Override
            public boolean apply(T input) {
                return f.apply(input);
            }
            @Override
            public String toString() {
                return "predicate("+f+")";
            }
        }
        return new Functionals.FunctionAsPredicate(f);
    }

    static class FunctionAsPredicate implements Predicate {
        private final Function f;
        
        FunctionAsPredicate(final Function f) {
            this.f = f;
        }
        @Override
        public boolean apply(T input) {
            return f.apply(input);
        }
        @Override
        public String toString() {
            return "predicate("+f+")";
        }
    }

    /**
     * Simple adapter from {@link Predicate} to {@link Callable} by currying the passed subject parameter.
     */
    public static  Callable isSatisfied(final T subject, final Predicate predicate) {
        return new Callable() {
            @Override
            public Boolean call() {
                return predicate.apply(subject);
            }
        };
    }

}