A to B with an associated reverse function from B
to A; used for converting back and forth between different representations of the same
information.
The reverse operation may be a strict inverse (meaning that converter.reverse().convert(converter.convert(a)).equals(a) is always true). However, it is very
common (perhaps more common) for round-trip conversion to be lossy. Consider an
example round-trip using com.:
stringConverter().convert("1.00") returns the Double value 1.0
stringConverter().reverse().convert(1.0) returns the string "1.0" --
not the same string ("1.00") we started with
Note that it should still be the case that the round-tripped and original objects are similar.
A converter always converts null to null and non-null references to non-null
references. It would not make sense to consider null and a non-null reference to be
"different representations of the same information", since one is distinguishable from
missing information and the other is not. The convert method handles this null
behavior for all converters; implementations of doForward and doBackward are
guaranteed to never be passed null, and must never return null.
Getting a converter:
Enums#stringConverter, Ints.stringConverter or the reverse views of these.
Maps.asConverter . For example, use this to
create a "fake" converter for a unit test. It is unnecessary (and confusing) to mock
the Converter type using a mocking framework.
doForward and doBackward methods.
from factory method.
Using a converter:
converter.convert(a).
converter.convertAll(as).
converter.reverse().convert(b) or converter.reverse().convertAll(bs).
converter or converter.reverse() anywhere a java.util.function.Function is accepted (for example Stream.map ).
doForward or doBackward directly; these exist only to
be overridden.
return new Converter<Integer, String>() {
protected String doForward(Integer i) {
return Integer.toHexString(i);
}
protected Integer doBackward(String s) {
return parseUnsignedInt(s, 16);
}
};
An alternative using Java 8:
return Converter.from(
Integer::toHexString,
s -> parseUnsignedInt(s, 16));
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| private static class | |
| private static class | |
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private final boolean | |
| private transient Converter |
| Access | Constructor and Description |
|---|---|
| protected | |
| pack-priv | Converter(boolean handleNullAutomatically)
Constructor used only by |
| Modifier and Type | Method and Description |
|---|---|
| public final <C> Converter | |
| public final B | apply(A
the function argument a)Implements com.
Deprecated
Provided to satisfy the
Applies this function to the given argument.
Function interface; use convert instead.
|
| public final B | Returns: the converted value; is null if and only ifa is nullReturns a representation of |
| public Iterable | convertAll(Iterable<? extends A> fromIterable)
Returns an iterable that applies |
| pack-priv A | |
| pack-priv B | |
| pack-priv <C> Converter | |
| protected abstract A | Returns: the converted instance; must not be nullthe instance to convert; will never be null b)Returns a representation of |
| protected abstract B | Returns: the converted instance; must not be nullthe instance to convert; will never be null a)Returns a representation of |
| public boolean | equals(Object
the reference object with which to compare. object)Overrides java. Implements com. |
| public static <A, B> Converter | |
| public static <T> Converter | identity()
Returns a serializable converter that always converts or reverses an object to itself.
|
| public Converter | reverse()
Returns the reversed view of this converter, which converts |
| private A | |
| private B |
| handleNullAutomatically | back to summary |
|---|---|
| private final boolean handleNullAutomatically | |
| reverse | back to summary |
|---|---|
| private transient Converter<B, A> reverse
| |
| Converter | back to summary |
|---|---|
| protected Converter() Constructor for use by subclasses. | |
| Converter | back to summary |
|---|---|
| pack-priv Converter(boolean handleNullAutomatically) Constructor used only by | |
| andThen | back to summary |
|---|---|
| public final <C> Converter Returns a converter whose The returned converter is serializable if | |
| apply | back to summary |
|---|---|
| public final B apply(A a) Implements com. Doc from java. Deprecated Provided to satisfy the Applies this function to the given argument.
| |
| convert | back to summary |
|---|---|
| public final B convert(A a) Returns a representation of
| |
| convertAll | back to summary |
|---|---|
| public Iterable Returns an iterable that applies The returned iterable's iterator supports | |
| correctedDoBackward | back to summary |
|---|---|
pack-priv A correctedDoBackward(B b)
| |
| correctedDoForward | back to summary |
|---|---|
pack-priv B correctedDoForward(A a)
| |
| doAndThen | back to summary |
|---|---|
| pack-priv <C> Converter Package-private non-final implementation of andThen() so only we can override it. | |
| doBackward | back to summary |
|---|---|
| protected abstract A doBackward(B b) Returns a representation of
| |
| doForward | back to summary |
|---|---|
| protected abstract B doForward(A a) Returns a representation of
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object object) Overrides java. Implements com. Indicates whether another object is equal to this converter. Most implementations will have no reason to override the behavior of
| |
| from | back to summary |
|---|---|
| public static <A, B> Converter Returns a converter based on separate forward and backward functions. This is useful if the
function instances already exist, or so that you can supply lambda expressions. If those
circumstances don't apply, you probably don't need to use this; subclass These functions will never be passed The returned converter is serializable if both provided functions are.
| |
| identity | back to summary |
|---|---|
| public static <T> Converter Hides java. Returns a serializable converter that always converts or reverses an object to itself.
| |
| reverse | back to summary |
|---|---|
| public Converter Returns the reversed view of this converter, which converts The returned converter is serializable if Note you should not override this method. It is non-final for legacy reasons.
| |
| unsafeDoBackward | back to summary |
|---|---|
private A unsafeDoBackward(B b)
| |
| unsafeDoForward | back to summary |
|---|---|
private B unsafeDoForward(A a)
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final Converter | |
| pack-priv final Converter | |
| private static final long |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv A | |
| pack-priv C | |
| protected A | doBackward(C
the instance to convert; will never be null c)Implements abstract com. b as an instance of type A.
|
| protected C | doForward(A
the instance to convert; will never be null a)Implements abstract com. a as an instance of type B.
|
| public boolean | equals(Object
the reference object with which to compare. object)Overrides com. Implements com. |
| public int | |
| public String |
| first | back to summary |
|---|---|
| pack-priv final Converter<A, B> first | |
| second | back to summary |
|---|---|
| pack-priv final Converter<B, C> second | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| ConverterComposition | back to summary |
|---|---|
| pack-priv ConverterComposition(Converter<A, B> first, Converter<B, C> second) | |
| correctedDoBackward | back to summary |
|---|---|
| pack-priv A correctedDoBackward(C c) Overrides com.
| |
| correctedDoForward | back to summary |
|---|---|
| pack-priv C correctedDoForward(A a) Overrides com.
| |
| doBackward | back to summary |
|---|---|
| protected A doBackward(C c) Implements abstract com. Doc from com. Returns a representation of
| |
| doForward | back to summary |
|---|---|
| protected C doForward(A a) Implements abstract com. Doc from com. Returns a representation of
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object object) Overrides com. Implements com. Doc from com. Indicates whether another object is equal to this converter. Most implementations will have no reason to override the behavior of
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for the object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| Modifier and Type | Field and Description |
|---|---|
| private final Function | |
| private final Function |
| Access | Constructor and Description |
|---|---|
| private | FunctionBasedConverter(Function<? super A, ? extends B> forwardFunction, Function<? super B, ? extends A> backwardFunction)
|
| Modifier and Type | Method and Description |
|---|---|
| protected A | doBackward(B
the instance to convert; will never be null b)Implements abstract com. b as an instance of type A.
|
| protected B | doForward(A
the instance to convert; will never be null a)Implements abstract com. a as an instance of type B.
|
| public boolean | equals(Object
the reference object with which to compare. object)Overrides com. Implements com. |
| public int | |
| public String |
| backwardFunction | back to summary |
|---|---|
| private final Function<? super B, ? extends A> backwardFunction | |
| forwardFunction | back to summary |
|---|---|
| private final Function<? super A, ? extends B> forwardFunction | |
| FunctionBasedConverter | back to summary |
|---|---|
| private FunctionBasedConverter(Function<? super A, ? extends B> forwardFunction, Function<? super B, ? extends A> backwardFunction) | |
| doBackward | back to summary |
|---|---|
| protected A doBackward(B b) Implements abstract com. Doc from com. Returns a representation of
| |
| doForward | back to summary |
|---|---|
| protected B doForward(A a) Implements abstract com. Doc from com. Returns a representation of
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object object) Overrides com. Implements com. Doc from com. Indicates whether another object is equal to this converter. Most implementations will have no reason to override the behavior of
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for the object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv static final Converter. | |
| private static final long |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv <S> Converter | doAndThen(Converter<T, S> otherConverter)
Overrides com. |
| protected T | doBackward(T
the instance to convert; will never be null t)Implements abstract com. b as an instance of type A.
|
| protected T | doForward(T
the instance to convert; will never be null t)Implements abstract com. a as an instance of type B.
|
| private Object | |
| public Converter. | reverse()
Overrides com. this.convert(a) back to a
value roughly equivalent to a.
|
| public String |
| INSTANCE | back to summary |
|---|---|
| pack-priv static final Converter. | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| IdentityConverter | back to summary |
|---|---|
| private IdentityConverter() | |
| doAndThen | back to summary |
|---|---|
| pack-priv <S> Converter Overrides com. Doc from com. Package-private non-final implementation of andThen() so only we can override it.
| |
| doBackward | back to summary |
|---|---|
| protected T doBackward(T t) Implements abstract com. Doc from com. Returns a representation of
| |
| doForward | back to summary |
|---|---|
| protected T doForward(T t) Implements abstract com. Doc from com. Returns a representation of
| |
| readResolve | back to summary |
|---|---|
| private Object readResolve() | |
| reverse | back to summary |
|---|---|
| public Converter. Overrides com. Doc from com. Returns the reversed view of this converter, which converts The returned converter is serializable if Note you should not override this method. It is non-final for legacy reasons.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final Converter | |
| private static final long |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv B | |
| pack-priv A | |
| protected B | doBackward(A
the instance to convert; will never be null a)Implements abstract com. b as an instance of type A.
|
| protected A | doForward(B
the instance to convert; will never be null b)Implements abstract com. a as an instance of type B.
|
| public boolean | equals(Object
the reference object with which to compare. object)Overrides com. Implements com. |
| public int | |
| public Converter | reverse()
Overrides com. this.convert(a) back to a
value roughly equivalent to a.
|
| public String |
| original | back to summary |
|---|---|
| pack-priv final Converter<A, B> original | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| ReverseConverter | back to summary |
|---|---|
| pack-priv ReverseConverter(Converter<A, B> original) | |
| correctedDoBackward | back to summary |
|---|---|
| pack-priv B correctedDoBackward(A a) Overrides com.
| |
| correctedDoForward | back to summary |
|---|---|
| pack-priv A correctedDoForward(B b) Overrides com.
| |
| doBackward | back to summary |
|---|---|
| protected B doBackward(A a) Implements abstract com. Doc from com. Returns a representation of
| |
| doForward | back to summary |
|---|---|
| protected A doForward(B b) Implements abstract com. Doc from com. Returns a representation of
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object object) Overrides com. Implements com. Doc from com. Indicates whether another object is equal to this converter. Most implementations will have no reason to override the behavior of
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for the object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| reverse | back to summary |
|---|---|
| public Converter Overrides com. Doc from com. Returns the reversed view of this converter, which converts The returned converter is serializable if Note you should not override this method. It is non-final for legacy reasons.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object. | |