/* * Decompiled with CFR 0.152. */ package com.google.common.base; import com.google.common.annotations.Beta; import com.google.common.annotations.GwtCompatible; import com.google.common.base.Function; import com.google.common.base.Objects; import com.google.common.base.Preconditions; import com.google.common.base.Predicate; import com.google.common.base.Supplier; import java.io.Serializable; import java.util.Map; import javax.annotation.Nullable; @GwtCompatible public final class Functions { private Functions() { } public static Function toStringFunction() { return ToStringFunction.INSTANCE; } public static Function identity() { return IdentityFunction.INSTANCE; } public static Function forMap(Map map) { return new FunctionForMapNoDefault(map); } public static Function forMap(Map map, @Nullable V defaultValue) { return new ForMapWithDefault(map, defaultValue); } public static Function compose(Function g, Function f) { return new FunctionComposition(g, f); } public static Function forPredicate(Predicate predicate) { return new PredicateFunction(predicate); } public static Function constant(@Nullable E value) { return new ConstantFunction(value); } @Beta public static Function forSupplier(Supplier supplier) { return new SupplierFunction(supplier); } private static class SupplierFunction implements Function, Serializable { private final Supplier supplier; private static final long serialVersionUID = 0L; private SupplierFunction(Supplier supplier) { this.supplier = Preconditions.checkNotNull(supplier); } @Override public T apply(@Nullable Object input) { return this.supplier.get(); } @Override public boolean equals(@Nullable Object obj) { if (obj instanceof SupplierFunction) { SupplierFunction that = (SupplierFunction)obj; return this.supplier.equals(that.supplier); } return false; } public int hashCode() { return this.supplier.hashCode(); } public String toString() { String string = String.valueOf(String.valueOf(this.supplier)); return new StringBuilder(13 + string.length()).append("forSupplier(").append(string).append(")").toString(); } } private static class ConstantFunction implements Function, Serializable { private final E value; private static final long serialVersionUID = 0L; public ConstantFunction(@Nullable E value) { this.value = value; } @Override public E apply(@Nullable Object from) { return this.value; } @Override public boolean equals(@Nullable Object obj) { if (obj instanceof ConstantFunction) { ConstantFunction that = (ConstantFunction)obj; return Objects.equal(this.value, that.value); } return false; } public int hashCode() { return this.value == null ? 0 : this.value.hashCode(); } public String toString() { String string = String.valueOf(String.valueOf(this.value)); return new StringBuilder(10 + string.length()).append("constant(").append(string).append(")").toString(); } } private static class PredicateFunction implements Function, Serializable { private final Predicate predicate; private static final long serialVersionUID = 0L; private PredicateFunction(Predicate predicate) { this.predicate = Preconditions.checkNotNull(predicate); } @Override public Boolean apply(@Nullable T t) { return this.predicate.apply(t); } @Override public boolean equals(@Nullable Object obj) { if (obj instanceof PredicateFunction) { PredicateFunction that = (PredicateFunction)obj; return this.predicate.equals(that.predicate); } return false; } public int hashCode() { return this.predicate.hashCode(); } public String toString() { String string = String.valueOf(String.valueOf(this.predicate)); return new StringBuilder(14 + string.length()).append("forPredicate(").append(string).append(")").toString(); } } private static class FunctionComposition implements Function, Serializable { private final Function g; private final Function f; private static final long serialVersionUID = 0L; public FunctionComposition(Function g, Function f) { this.g = Preconditions.checkNotNull(g); this.f = Preconditions.checkNotNull(f); } @Override public C apply(@Nullable A a) { return this.g.apply(this.f.apply(a)); } @Override public boolean equals(@Nullable Object obj) { if (obj instanceof FunctionComposition) { FunctionComposition that = (FunctionComposition)obj; return this.f.equals(that.f) && this.g.equals(that.g); } return false; } public int hashCode() { return this.f.hashCode() ^ this.g.hashCode(); } public String toString() { String string = String.valueOf(String.valueOf(this.g)); String string2 = String.valueOf(String.valueOf(this.f)); return new StringBuilder(2 + string.length() + string2.length()).append(string).append("(").append(string2).append(")").toString(); } } private static class ForMapWithDefault implements Function, Serializable { final Map map; final V defaultValue; private static final long serialVersionUID = 0L; ForMapWithDefault(Map map, @Nullable V defaultValue) { this.map = Preconditions.checkNotNull(map); this.defaultValue = defaultValue; } @Override public V apply(@Nullable K key) { V result = this.map.get(key); return result != null || this.map.containsKey(key) ? result : this.defaultValue; } @Override public boolean equals(@Nullable Object o) { if (o instanceof ForMapWithDefault) { ForMapWithDefault that = (ForMapWithDefault)o; return this.map.equals(that.map) && Objects.equal(this.defaultValue, that.defaultValue); } return false; } public int hashCode() { return Objects.hashCode(this.map, this.defaultValue); } public String toString() { String string = String.valueOf(String.valueOf(this.map)); String string2 = String.valueOf(String.valueOf(this.defaultValue)); return new StringBuilder(23 + string.length() + string2.length()).append("forMap(").append(string).append(", defaultValue=").append(string2).append(")").toString(); } } private static class FunctionForMapNoDefault implements Function, Serializable { final Map map; private static final long serialVersionUID = 0L; FunctionForMapNoDefault(Map map) { this.map = Preconditions.checkNotNull(map); } @Override public V apply(@Nullable K key) { V result = this.map.get(key); Preconditions.checkArgument(result != null || this.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 this.map.equals(that.map); } return false; } public int hashCode() { return this.map.hashCode(); } public String toString() { String string = String.valueOf(String.valueOf(this.map)); return new StringBuilder(8 + string.length()).append("forMap(").append(string).append(")").toString(); } } private static enum IdentityFunction implements Function { INSTANCE; @Override @Nullable public Object apply(@Nullable Object o) { return o; } public String toString() { return "identity"; } } private static enum ToStringFunction implements Function { INSTANCE; @Override public String apply(Object o) { Preconditions.checkNotNull(o); return o.toString(); } public String toString() { return "toString"; } } }