Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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 lightstep.lightstep.lightstep.com.google.common.base;
importstatic lightstep.lightstep.lightstep.com.google.common.base.Preconditions.checkArgument;
importstatic lightstep.lightstep.lightstep.com.google.common.base.Preconditions.checkNotNull;
import lightstep.lightstep.lightstep.com.google.common.annotations.GwtCompatible;
import java.io.Serializable;
import java.util.Map;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;
/**
* Static utility methods pertaining to {@code lightstep.lightstep.lightstep.com.google.common.base.Function} instances; see that
* class for information about migrating to {@code java.util.function}.
*
*
All methods return serializable functions as long as they're given serializable parameters.
*
*
See the Guava User Guide article on the use of {@code Function}.
*
* @author Mike Bostock
* @author Jared Levy
* @since 2.0
*/@GwtCompatiblepublicfinalclassFunctions{
privateFunctions(){}
/**
* A function equivalent to the method reference {@code Object::toString}, for users not yet using
* Java 8. The function simply invokes {@code toString} on its argument and returns the result. It
* throws a {@link NullPointerException} on null input.
*
*
Warning: The returned function may not be consistent with equals (as
* documented at {@link Function#apply}). For example, this function yields different results for
* the two equal instances {@code ImmutableSet.of(1, 2)} and {@code ImmutableSet.of(2, 1)}.
*
*
Warning: as with all function types in this package, avoid depending on the specific
* {@code equals}, {@code hashCode} or {@code toString} behavior of the returned function. A
* future migration to {@code java.util.function} will not preserve this behavior.
*
*
For Java 8 users: use the method reference {@code Object::toString} instead. In the
* future, when this class requires Java 8, this method will be deprecated. See {@link Function}
* for more important information about the Java 8 transition.
*/publicstatic Function
Note: if {@code map} is a {@link lightstep.lightstep.lightstep.com.google.common.collect.BiMap BiMap} (or can be one), you
* can use {@link lightstep.lightstep.lightstep.com.google.common.collect.Maps#asConverter Maps.asConverter} instead to get a
* function that also supports reverse conversion.
*
*
Java 8 users: if you are okay with {@code null} being returned for an unrecognized
* key (instead of an exception being thrown), you can use the method reference {@code map::get}
* instead.
*/publicstatic FunctionforMap(Map map){
returnnew FunctionForMapNoDefault<>(map);
}
/**
* 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. See also {@link #forMap(Map)}, which throws an exception in this case.
*
*
Java 8 users: you can just write the lambda expression {@code k ->
* map.getWithDefault(k, defaultValue)} instead.
*
* @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
*/publicstatic FunctionforMap(
Map map, @NullableDecl V defaultValue){
returnnew ForMapWithDefault<>(map, defaultValue);
}
privatestaticclassFunctionForMapNoDefaultimplementsFunction, Serializable{
final Map map;
FunctionForMapNoDefault(Map map) {
this.map = checkNotNull(map);
}
@Overridepublic V apply(@NullableDecl K key){
V result = map.get(key);
checkArgument(result != null || map.containsKey(key), "Key '%s' not present in map", key);
return result;
}
@Overridepublicbooleanequals(@NullableDecl Object o){
if (o instanceof FunctionForMapNoDefault) {
FunctionForMapNoDefault that = (FunctionForMapNoDefault) o;
return map.equals(that.map);
}
returnfalse;
}
@OverridepublicinthashCode(){
return map.hashCode();
}
@Overridepublic String toString(){
return"Functions.forMap(" + map + ")";
}
privatestaticfinallong serialVersionUID = 0;
}
privatestaticclassForMapWithDefaultimplementsFunction, Serializable{
final Map map;
@NullableDeclfinal V defaultValue;
ForMapWithDefault(Map map, @NullableDecl V defaultValue) {
this.map = checkNotNull(map);
this.defaultValue = defaultValue;
}
@Overridepublic V apply(@NullableDecl K key){
V result = map.get(key);
return (result != null || map.containsKey(key)) ? result : defaultValue;
}
@Overridepublicbooleanequals(@NullableDecl Object o){
if (o instanceof ForMapWithDefault) {
ForMapWithDefault that = (ForMapWithDefault) o;
return map.equals(that.map) && Objects.equal(defaultValue, that.defaultValue);
}
returnfalse;
}
@OverridepublicinthashCode(){
return Objects.hashCode(map, defaultValue);
}
@Overridepublic String toString(){
// TODO(cpovirk): maybe remove "defaultValue=" to make this look like the method call doesreturn"Functions.forMap(" + map + ", defaultValue=" + defaultValue + ")";
}
privatestaticfinallong 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}.
*
*
The returned function is consistent with equals (as documented at {@link
* Function#apply}) if and only if {@code predicate} is itself consistent with equals.
*
*
Java 8 users: use the method reference {@code predicate::test} instead.
*/publicstatic FunctionforPredicate(Predicate predicate){
returnnew PredicateFunction(predicate);
}
/** @see Functions#forPredicate */privatestaticclassPredicateFunctionimplementsFunction, Serializable{
privatefinal Predicate predicate;
privatePredicateFunction(Predicate predicate){
this.predicate = checkNotNull(predicate);
}
@Overridepublic Boolean apply(@NullableDecl T t){
return predicate.apply(t);
}
@Overridepublicbooleanequals(@NullableDecl Object obj){
if (obj instanceof PredicateFunction) {
PredicateFunction that = (PredicateFunction) obj;
return predicate.equals(that.predicate);
}
returnfalse;
}
@OverridepublicinthashCode(){
return predicate.hashCode();
}
@Overridepublic String toString(){
return"Functions.forPredicate(" + predicate + ")";
}
privatestaticfinallong serialVersionUID = 0;
}
/**
* Returns a function that ignores its input and always returns {@code value}.
*
*
Java 8 users: use the lambda expression {@code o -> value} instead.
*
* @param value the constant value for the function to return
* @return a function that always returns {@code value}
*/publicstatic Functionconstant(@NullableDecl E value){
returnnew ConstantFunction(value);
}
privatestaticclassConstantFunctionimplementsFunction, Serializable{
@NullableDeclprivatefinal E value;
publicConstantFunction(@NullableDecl E value){
this.value = value;
}
@Overridepublic E apply(@NullableDecl Object from){
return value;
}
@Overridepublicbooleanequals(@NullableDecl Object obj){
if (obj instanceof ConstantFunction) {
ConstantFunction that = (ConstantFunction) obj;
return Objects.equal(value, that.value);
}
returnfalse;
}
@OverridepublicinthashCode(){
return (value == null) ? 0 : value.hashCode();
}
@Overridepublic String toString(){
return"Functions.constant(" + value + ")";
}
privatestaticfinallong serialVersionUID = 0;
}
/**
* Returns a function that ignores its input and returns the result of {@code supplier.get()}.
*
*