Package com.hubspot.immutables.utils
Class WireSafeEnum<T extends Enum<T>>
java.lang.Object
com.hubspot.immutables.utils.WireSafeEnum<T>
This utility is meant to help with the fragility introduced by
using an enum across API boundaries. The most common example is
adding a new enum constant causing clients to explode on
deserialization if they don't have the updated enum definition.
Instead of using the enum in your models directly, you could instead
wrap the field in a WireSafeEnum. This should be transparent from
a serialization perspective, but will allow you to more gracefully
handle the case of an unknown enum constant. It also stores the JSON
value when deserializing, and uses that for serialization. This means
that intermedaries preserve, rather than mangle, unknown enum values.
For the most part WireSafeEnum should be a drop-in replacement, but
there are some things to be aware of:
1. every enum constant must serialize to JSON as a non-null string
(serializing as a number or null is not supported)
2. T and WireSafeEnum are different types so migrating is a
breaking change from a code perspective and Java code usages
of the field will need to get updated
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic classstatic class -
Method Summary
Modifier and TypeMethodDescriptionasEnum()Deprecated.this method doesn't handle unknown enum values, and eliminates the benefits of WireSafeEnum.asEnumOrThrow(Supplier<? extends X> exceptionSupplier) Deprecated.this method doesn't handle unknown enum values, and eliminates the benefits of WireSafeEnum.asString()booleanbooleancontainsAnyOf(Collection<T> values) final booleancontainsAnyOf(T... values) enumType()booleanstatic <T extends Enum<T>>
WireSafeEnum<T> inthashCode()static <T extends Enum<T>>
WireSafeEnum<T> of(T value) toString()
-
Method Details
-
of
-
fromJson
-
enumType
-
asString
-
asEnum
-
asEnumOrThrow
@Nonnull @Deprecated public <X extends Throwable> T asEnumOrThrow(Supplier<? extends X> exceptionSupplier) throws X Deprecated.this method doesn't handle unknown enum values, and eliminates the benefits of WireSafeEnum. If you want to compare to a specific value, you can callcontains(Enum), which handles unknown values gracefully. If you do really need to coerce to an enum, you can replace this method with .asEnum().orElseThrow(exceptionSupplier)- Throws:
X
-
asEnumOrThrow
Deprecated.this method doesn't handle unknown enum values, and eliminates the benefits of WireSafeEnum. If you want to compare to a specific value, you can callcontains(Enum), which handles unknown values gracefully. If you do really need to coerce to an enum, you can replace this method with .asEnum().get() -
contains
-
containsAnyOf
-
containsAnyOf
-
equals
-
hashCode
public int hashCode() -
toString
-