Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2007 The Guava Authors
   *
   * 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 static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 
 import java.util.Map;
 
 import  javax.annotation.Nullable;

Static utility methods pertaining to Function instances.

All methods return serializable functions as long as they're given serializable parameters.

See the Guava User Guide article on the use of Function.

Author(s):
Mike Bostock
Jared Levy
Since:
2.0 (imported from Google Collections Library)
 
 public final class Functions {
   private Functions() {}

  
Returns a function that calls toString() on its argument. The function does not accept nulls; it will throw a NullPointerException when applied to null.

Warning: The returned function may not be consistent with equals (as documented at Function.apply). For example, this function yields different results for the two equal instances ImmutableSet.of(1, 2) and ImmutableSet.of(2, 1).

 
   public static Function<ObjectStringtoStringFunction() {
     return .;
   }
 
   // enum singleton pattern
   private enum ToStringFunction implements Function<ObjectString> {
     INSTANCE;
 
     @Override
     public String apply(Object o) {
       checkNotNull(o);  // eager for GWT.
       return o.toString();
     }
 
     @Override public String toString() {
       return "toString";
     }
   }

  
Returns the identity function.
 
   @SuppressWarnings("unchecked")
   public static <E> Function<E, E> identity() {
     return (Function<E, E>) .;
   }
 
   // enum singleton pattern
   private enum IdentityFunction implements Function<ObjectObject> {
     INSTANCE;
 
     @Override
     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 IllegalArgumentException if given a key that does not exist in the map.
 
  public static <K, V> Function<K, V> forMap(Map<K, V> map) {
    return new FunctionForMapNoDefault<K, V>(map);
  }
  private static class FunctionForMapNoDefault<K, V> implements Function<K, V>, Serializable {
    final Map<K, V> map;
    FunctionForMapNoDefault(Map<K, V> map) {
      this. = checkNotNull(map);
    }
    @Override
    public V apply(K key) {
      V result = .get(key);
      checkArgument(result != null || .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 .equals(that.map);
      }
      return false;
    }
    @Override public int hashCode() {
      return .hashCode();
    }
    @Override public String toString() {
      return "forMap(" +  + ")";
    }
    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 defaultValue for all inputs that do not belong to the map's key set.

Parameters:
map source map that determines the function behavior
defaultValue the value to return for inputs that aren't map keys
Returns:
function that returns map.get(a) when a is a key, or defaultValue otherwise
  public static <K, V> Function<K, V> forMap(Map<K, ? extends V> map, @Nullable V defaultValue) {
    return new ForMapWithDefault<K, V>(mapdefaultValue);
  }
  private static class ForMapWithDefault<K, V> implements Function<K, V>, Serializable {
    final Map<K, ? extends V> map;
    final V defaultValue;
    ForMapWithDefault(Map<K, ? extends V> map, @Nullable V defaultValue) {
      this. = checkNotNull(map);
      this. = defaultValue;
    }
    @Override
    public V apply(K key) {
      V result = .get(key);
      return (result != null || .containsKey(key)) ? result : ;
    }
    @Override public boolean equals(@Nullable Object o) {
      if (o instanceof ForMapWithDefault) {
        ForMapWithDefault<?, ?> that = (ForMapWithDefault<?, ?>) o;
        return .equals(that.map) && Objects.equal(that.defaultValue);
      }
      return false;
    }
    @Override public int hashCode() {
      return Objects.hashCode();
    }
    @Override public String toString() {
      return "forMap(" +  + ", defaultValue=" +  + ")";
    }
    private static final long serialVersionUID = 0;
  }

  
Returns the composition of two functions. For f: A->B and g: B->C, composition is defined as the function h such that h(a) == g(f(a)) for each a.

Parameters:
g the second function to apply
f the first function to apply
Returns:
the composition of f and g
See also:
function composition
  public static <A, B, C> Function<A, C> compose(Function<B, C> gFunction<A, ? extends B> f) {
    return new FunctionComposition<A, B, C>(gf);
  }
  private static class FunctionComposition<A, B, C> implements Function<A, C>, Serializable {
    private final Function<B, C> g;
    private final Function<A, ? extends B> f;
    public FunctionComposition(Function<B, C> gFunction<A, ? extends B> f) {
      this. = checkNotNull(g);
      this. = checkNotNull(f);
    }
    @Override
    public C apply(A a) {
      return .apply(.apply(a));
    }
    @Override public boolean equals(@Nullable Object obj) {
      if (obj instanceof FunctionComposition) {
        FunctionComposition<?, ?, ?> that = (FunctionComposition<?, ?, ?>) obj;
        return .equals(that.f) && .equals(that.g);
      }
      return false;
    }
    @Override public int hashCode() {
      return .hashCode() ^ .hashCode();
    }
    @Override public String toString() {
      return .toString() + "(" + .toString() + ")";
    }
    private static final long serialVersionUID = 0;
  }

  
Creates a function that returns the same boolean output as the given predicate for all inputs.

The returned function is consistent with equals (as documented at Function.apply) if and only if predicate is itself consistent with equals.

  public static <T> Function<T, BooleanforPredicate(Predicate<T> predicate) {
    return new PredicateFunction<T>(predicate);
  }

  

See also:
Functions.forPredicate
  private static class PredicateFunction<T> implements Function<T, Boolean>, Serializable {
    private final Predicate<T> predicate;
    private PredicateFunction(Predicate<T> predicate) {
      this. = checkNotNull(predicate);
    }
    @Override
    public Boolean apply(T t) {
      return .apply(t);
    }
    @Override public boolean equals(@Nullable Object obj) {
      if (obj instanceof PredicateFunction) {
        PredicateFunction<?> that = (PredicateFunction<?>) obj;
        return .equals(that.predicate);
      }
      return false;
    }
    @Override public int hashCode() {
      return .hashCode();
    }
    @Override public String toString() {
      return "forPredicate(" +  + ")";
    }
    private static final long serialVersionUID = 0;
  }

  
Creates a function that returns value for any input.

Parameters:
value the constant value for the function to return
Returns:
a function that always returns value
  public static <E> Function<Object, E> constant(@Nullable E value) {
    return new ConstantFunction<E>(value);
  }
  private static class ConstantFunction<E> implements Function<Object, E>, Serializable {
    private final E value;
    public ConstantFunction(@Nullable E value) {
      this. = value;
    }
    @Override
    public E apply(@Nullable Object from) {
      return ;
    }
    @Override public boolean equals(@Nullable Object obj) {
      if (obj instanceof ConstantFunction) {
        ConstantFunction<?> that = (ConstantFunction<?>) obj;
        return Objects.equal(that.value);
      }
      return false;
    }
    @Override public int hashCode() {
      return ( == null) ? 0 : .hashCode();
    }
    @Override public String toString() {
      return "constant(" +  + ")";
    }
    private static final long serialVersionUID = 0;
  }

  
Returns a function that always returns the result of invoking Supplier.get on supplier, regardless of its input.

Since:
10.0
  @Beta
  public static <T> Function<Object, T> forSupplier(Supplier<T> supplier) {
    return new SupplierFunction<T>(supplier);
  }

  

See also:
Functions.forSupplie
  private static class SupplierFunction<T> implements Function<Object, T>, Serializable {
    
    private final Supplier<T> supplier;
    private SupplierFunction(Supplier<T> supplier) {
      this. = checkNotNull(supplier);
    }
    @Override public T apply(@Nullable Object input) {
      return .get();
    }
    
    @Override public boolean equals(@Nullable Object obj) {
      if (obj instanceof SupplierFunction) {
        SupplierFunction<?> that = (SupplierFunction<?>) obj;
        return this..equals(that.supplier);
      }
      return false;
    }
    
    @Override public int hashCode() {
      return .hashCode();
    }
    
    @Override public String toString() {
      return "forSupplier(" +  + ")";
    }
    
    private static final long serialVersionUID = 0;
  }
New to GrepCode? Check out our FAQ X