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

org.apache.brooklyn.util.guava.IfFunctions 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.LinkedHashMap;
import java.util.Map;

import com.google.common.annotations.Beta;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Supplier;

/** Utilities for building {@link Function} instances which return specific values
 * (or {@link Supplier} or {@link Function} instances) when certain predicates are satisfied,
 * tested in order and returning the first matching,
 * with support for an "else" default value if none are satisfied (null by default). */
public class IfFunctions {

    public static  IfFunctionBuilder newInstance(Class testType, Class returnType) {
        return new IfFunctionBuilder();
    }
    
    public static  IfFunctionBuilderApplyingFirst ifPredicate(Predicate test) {
        return new IfFunctionBuilderApplyingFirst(test);
    }
    public static  IfFunctionBuilderApplyingFirst ifEquals(I test) {
        return ifPredicate(Predicates.equalTo(test));
    }
    public static  IfFunctionBuilderApplyingFirst ifNotEquals(I test) {
        return ifPredicate(Predicates.not(Predicates.equalTo(test)));
    }
    
    @Beta
    public static class IfFunction implements Function {
        protected final Map,Function> tests = new LinkedHashMap,Function>();
        protected Function defaultFunction = null;
        
        protected IfFunction(IfFunction input) {
            this.tests.putAll(input.tests);
            this.defaultFunction = input.defaultFunction;
        }

        protected IfFunction() {
        }
        
        @Override
        public O apply(I input) {
            for (Map.Entry,Function> test: tests.entrySet()) {
                if (test.getKey().apply(input)) 
                    return test.getValue().apply(input);
            }
            return defaultFunction==null ? null : defaultFunction.apply(input);
        }
        
        @Override
        public String toString() {
            return "if["+tests+"]"+(defaultFunction!=null ? "-else["+defaultFunction+"]" : "");
        }
    }
    
    @Beta
    public static class IfFunctionBuilder extends IfFunction {
        protected IfFunctionBuilder() { super(); }
        protected IfFunctionBuilder(IfFunction input) { super(input); }
        
        public IfFunction build() {
            return new IfFunction(this);
        }
        
        public IfFunctionBuilderApplying ifPredicate(Predicate test) {
            return new IfFunctionBuilderApplying(this, test);
        }
        public IfFunctionBuilderApplying ifEquals(I test) {
            return ifPredicate(Predicates.equalTo(test));
        }
        public IfFunctionBuilderApplying ifNotEquals(I test) {
            return ifPredicate(Predicates.not(Predicates.equalTo(test)));
        }

        public IfFunctionBuilder defaultValue(O defaultValue) {
            return defaultApply(new Functionals.ConstantFunction(defaultValue, defaultValue));
        }
        @SuppressWarnings("unchecked")
        public IfFunctionBuilder defaultGet(Supplier defaultSupplier) {
            return defaultApply(Functions.forSupplier(defaultSupplier));
        }
        public IfFunctionBuilder defaultApply(Function defaultFunction) {
            IfFunctionBuilder result = new IfFunctionBuilder(this);
            result.defaultFunction = defaultFunction;
            return result;
        }
    }

    @Beta
    public static class IfFunctionBuilderApplying {
        private IfFunction input;
        private Predicate test;
        
        private IfFunctionBuilderApplying(IfFunction input, Predicate test) {
            this.input = input;
            this.test = test;
        }
        
        public IfFunctionBuilder value(O value) {
            return apply(new Functionals.ConstantFunction(value, value));
        }
        @SuppressWarnings("unchecked")
        public IfFunctionBuilder get(Supplier supplier) {
            return apply(Functions.forSupplier(supplier));
        }
        public IfFunctionBuilder apply(Function function) {
            IfFunctionBuilder result = new IfFunctionBuilder(input);
            result.tests.put(test, function);
            return result;
        }
    }

    @Beta
    public static class IfFunctionBuilderApplyingFirst {
        private Predicate test;
        
        private IfFunctionBuilderApplyingFirst(Predicate test) {
            this.test = test;
        }
        
        public  IfFunctionBuilder value(O value) {
            return apply(new Functionals.ConstantFunction(value, value));
        }
        @SuppressWarnings("unchecked")
        public  IfFunctionBuilder get(Supplier supplier) {
            return apply(Functions.forSupplier(supplier));
        }
        public  IfFunctionBuilder apply(Function function) {
            IfFunctionBuilder result = new IfFunctionBuilder();
            result.tests.put(test, function);
            return result;
        }
    }
    
}