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

com.google.common.base.Functions Maven / Gradle / Ivy

There is a newer version: 11.0.1
Show newest version
/*
 * Copyright (C) 2007 Google Inc.
 *
 * 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.google.common.base;

import com.google.common.annotations.GwtCompatible;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import java.io.Serializable;
import java.util.Map;

import javax.annotation.Nullable;

/**
 * Useful functions.
 *
 * 

All methods returns serializable functions as long as they're given * serializable parameters. * * @author Mike Bostock * @author Vlad Patryshev * @author Jared Levy * @since 2 (imported from Google Collections Library) */ @GwtCompatible public final class Functions { private Functions() {} /** * Returns a function that calls {@code toString()} on its argument. The * function does not accept nulls; it will throw a * {@link NullPointerException} when applied to {@code null}. */ public static Function toStringFunction() { return ToStringFunction.INSTANCE; } // enum singleton pattern private enum ToStringFunction implements Function { INSTANCE; public String apply(Object o) { return o.toString(); } @Override public String toString() { return "toString"; } } /** * Returns the identity function. */ @SuppressWarnings("unchecked") public static Function identity() { return (Function) IdentityFunction.INSTANCE; } // enum singleton pattern private enum IdentityFunction implements Function { INSTANCE; public Object apply(Object o) { return o; } @Override public String toString() { return "identity"; } } /** * Returns a function which performs a map lookup. The returned function * throws an {@link IllegalArgumentException} if given a key that does not * exist in the map. */ public static Function forMap(Map map) { return new FunctionForMapNoDefault(map); } private static class FunctionForMapNoDefault implements Function, Serializable { final Map map; FunctionForMapNoDefault(Map map) { this.map = checkNotNull(map); } public V apply(K key) { V result = map.get(key); checkArgument(result != null || map.containsKey(key), "Key '%s' not present in map", key); return result; } @Override public boolean equals(@Nullable Object o) { if (o instanceof FunctionForMapNoDefault) { FunctionForMapNoDefault that = (FunctionForMapNoDefault) o; return map.equals(that.map); } return false; } @Override public int hashCode() { return map.hashCode(); } @Override public String toString() { return "forMap(" + map + ")"; } private static final long serialVersionUID = 0; } /** * Returns a function which performs a map lookup with a default value. The * function created by this method returns {@code defaultValue} for all * inputs that do not belong to the map's key set. * * @param map source map that determines the function behavior * @param defaultValue the value to return for inputs that aren't map keys * @return function that returns {@code map.get(a)} when {@code a} is a key, * or {@code defaultValue} otherwise */ public static Function forMap( Map map, @Nullable V defaultValue) { return new ForMapWithDefault(map, defaultValue); } private static class ForMapWithDefault implements Function, Serializable { final Map map; final V defaultValue; ForMapWithDefault(Map map, V defaultValue) { this.map = checkNotNull(map); this.defaultValue = defaultValue; } public V apply(K key) { return map.containsKey(key) ? map.get(key) : defaultValue; } @Override public boolean equals(@Nullable Object o) { if (o instanceof ForMapWithDefault) { ForMapWithDefault that = (ForMapWithDefault) o; return map.equals(that.map) && Objects.equal(defaultValue, that.defaultValue); } return false; } @Override public int hashCode() { return Objects.hashCode(map, defaultValue); } @Override public String toString() { return "forMap(" + map + ", defaultValue=" + defaultValue + ")"; } private static final long serialVersionUID = 0; } /** * Returns the composition of two functions. For {@code f: A->B} and * {@code g: B->C}, composition is defined as the function h such that * {@code h(a) == g(f(a))} for each {@code a}. * * @see * function composition * * @param g the second function to apply * @param f the first function to apply * @return the composition of {@code f} and {@code g} */ public static Function compose( Function g, Function f) { return new FunctionComposition(g, f); } private static class FunctionComposition implements Function, Serializable { private final Function g; private final Function f; public FunctionComposition(Function g, Function f) { this.g = checkNotNull(g); this.f = checkNotNull(f); } public C apply(A a) { return g.apply(f.apply(a)); } @Override public boolean equals(@Nullable Object obj) { if (obj instanceof FunctionComposition) { FunctionComposition that = (FunctionComposition) obj; return f.equals(that.f) && g.equals(that.g); } return false; } @Override public int hashCode() { return f.hashCode() ^ g.hashCode(); } @Override public String toString() { return g.toString() + "(" + f.toString() + ")"; } private static final long serialVersionUID = 0; } /** * Creates a function that returns the same boolean output as the given * predicate for all inputs. */ public static Function forPredicate(Predicate predicate) { return new PredicateFunction(predicate); } /** @see Functions#forPredicate */ private static class PredicateFunction implements Function, Serializable { private final Predicate predicate; private PredicateFunction(Predicate predicate) { this.predicate = checkNotNull(predicate); } public Boolean apply(T t) { return predicate.apply(t); } @Override public boolean equals(@Nullable Object obj) { if (obj instanceof PredicateFunction) { PredicateFunction that = (PredicateFunction) obj; return predicate.equals(that.predicate); } return false; } @Override public int hashCode() { return predicate.hashCode(); } @Override public String toString() { return "forPredicate(" + predicate + ")"; } private static final long serialVersionUID = 0; } /** * Creates a function that returns {@code value} for any input. * * @param value the constant value for the function to return * @return a function that always returns {@code value} */ public static Function constant(@Nullable E value) { return new ConstantFunction(value); } private static class ConstantFunction implements Function, Serializable { private final E value; public ConstantFunction(@Nullable E value) { this.value = value; } public E apply(@Nullable Object from) { return value; } @Override public boolean equals(@Nullable Object obj) { if (obj instanceof ConstantFunction) { ConstantFunction that = (ConstantFunction) obj; return Objects.equal(value, that.value); } return false; } @Override public int hashCode() { return (value == null) ? 0 : value.hashCode(); } @Override public String toString() { return "constant(" + value + ")"; } private static final long serialVersionUID = 0; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy