/* * 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.Preconditions; import java.io.Serializable; import java.util.Iterator; import javax.annotation.Nullable; @Beta @GwtCompatible public abstract class Converter implements Function { private final boolean handleNullAutomatically; private transient Converter reverse; protected Converter() { this(true); } Converter(boolean handleNullAutomatically) { this.handleNullAutomatically = handleNullAutomatically; } protected abstract B doForward(A var1); protected abstract A doBackward(B var1); @Nullable public final B convert(@Nullable A a) { return this.correctedDoForward(a); } @Nullable B correctedDoForward(@Nullable A a) { if (this.handleNullAutomatically) { return a == null ? null : (B)Preconditions.checkNotNull(this.doForward(a)); } return this.doForward(a); } @Nullable A correctedDoBackward(@Nullable B b) { if (this.handleNullAutomatically) { return b == null ? null : (A)Preconditions.checkNotNull(this.doBackward(b)); } return this.doBackward(b); } public Iterable convertAll(final Iterable fromIterable) { Preconditions.checkNotNull(fromIterable, "fromIterable"); return new Iterable(){ @Override public Iterator iterator() { return new Iterator(){ private final Iterator fromIterator; { this.fromIterator = fromIterable.iterator(); } @Override public boolean hasNext() { return this.fromIterator.hasNext(); } @Override public B next() { return Converter.this.convert(this.fromIterator.next()); } @Override public void remove() { this.fromIterator.remove(); } }; } }; } public Converter reverse() { Converter result = this.reverse; return result == null ? (this.reverse = new ReverseConverter(this)) : result; } public final Converter andThen(Converter secondConverter) { return this.doAndThen(secondConverter); } Converter doAndThen(Converter secondConverter) { return new ConverterComposition(this, Preconditions.checkNotNull(secondConverter)); } @Override @Deprecated @Nullable public final B apply(@Nullable A a) { return this.convert(a); } @Override public boolean equals(@Nullable Object object) { return super.equals(object); } public static Converter from(Function forwardFunction, Function backwardFunction) { return new FunctionBasedConverter(forwardFunction, backwardFunction); } public static Converter identity() { return IdentityConverter.INSTANCE; } private static final class IdentityConverter extends Converter implements Serializable { static final IdentityConverter INSTANCE = new IdentityConverter(); private static final long serialVersionUID = 0L; private IdentityConverter() { } @Override protected T doForward(T t) { return t; } @Override protected T doBackward(T t) { return t; } public IdentityConverter reverse() { return this; } @Override Converter doAndThen(Converter otherConverter) { return Preconditions.checkNotNull(otherConverter, "otherConverter"); } public String toString() { return "Converter.identity()"; } private Object readResolve() { return INSTANCE; } } private static final class FunctionBasedConverter extends Converter implements Serializable { private final Function forwardFunction; private final Function backwardFunction; private FunctionBasedConverter(Function forwardFunction, Function backwardFunction) { this.forwardFunction = Preconditions.checkNotNull(forwardFunction); this.backwardFunction = Preconditions.checkNotNull(backwardFunction); } @Override protected B doForward(A a) { return this.forwardFunction.apply(a); } @Override protected A doBackward(B b) { return this.backwardFunction.apply(b); } @Override public boolean equals(@Nullable Object object) { if (object instanceof FunctionBasedConverter) { FunctionBasedConverter that = (FunctionBasedConverter)object; return this.forwardFunction.equals(that.forwardFunction) && this.backwardFunction.equals(that.backwardFunction); } return false; } public int hashCode() { return this.forwardFunction.hashCode() * 31 + this.backwardFunction.hashCode(); } public String toString() { String string = String.valueOf(String.valueOf(this.forwardFunction)); String string2 = String.valueOf(String.valueOf(this.backwardFunction)); return new StringBuilder(18 + string.length() + string2.length()).append("Converter.from(").append(string).append(", ").append(string2).append(")").toString(); } } private static final class ConverterComposition extends Converter implements Serializable { final Converter first; final Converter second; private static final long serialVersionUID = 0L; ConverterComposition(Converter first, Converter second) { this.first = first; this.second = second; } @Override protected C doForward(A a) { throw new AssertionError(); } @Override protected A doBackward(C c) { throw new AssertionError(); } @Override @Nullable C correctedDoForward(@Nullable A a) { return this.second.correctedDoForward(this.first.correctedDoForward(a)); } @Override @Nullable A correctedDoBackward(@Nullable C c) { return this.first.correctedDoBackward(this.second.correctedDoBackward(c)); } @Override public boolean equals(@Nullable Object object) { if (object instanceof ConverterComposition) { ConverterComposition that = (ConverterComposition)object; return this.first.equals(that.first) && this.second.equals(that.second); } return false; } public int hashCode() { return 31 * this.first.hashCode() + this.second.hashCode(); } public String toString() { String string = String.valueOf(String.valueOf(this.first)); String string2 = String.valueOf(String.valueOf(this.second)); return new StringBuilder(10 + string.length() + string2.length()).append(string).append(".andThen(").append(string2).append(")").toString(); } } private static final class ReverseConverter extends Converter implements Serializable { final Converter original; private static final long serialVersionUID = 0L; ReverseConverter(Converter original) { this.original = original; } @Override protected A doForward(B b) { throw new AssertionError(); } @Override protected B doBackward(A a) { throw new AssertionError(); } @Override @Nullable A correctedDoForward(@Nullable B b) { return this.original.correctedDoBackward(b); } @Override @Nullable B correctedDoBackward(@Nullable A a) { return this.original.correctedDoForward(a); } @Override public Converter reverse() { return this.original; } @Override public boolean equals(@Nullable Object object) { if (object instanceof ReverseConverter) { ReverseConverter that = (ReverseConverter)object; return this.original.equals(that.original); } return false; } public int hashCode() { return ~this.original.hashCode(); } public String toString() { String string = String.valueOf(String.valueOf(this.original)); return new StringBuilder(10 + string.length()).append(string).append(".reverse()").toString(); } } }