Base trait for reading Strings into our com.paypal.cascade.common.enumeration.Enumeration.
Allows pattern matching on String values that correspond to com.paypal.cascade.common.enumeration.Enumeration subtypes.
Allows pattern matching on String values that correspond to com.paypal.cascade.common.enumeration.Enumeration
subtypes. Note that the type returned by the extractor is the general sealed trait T
, not an Enumeration instance.
scala> "SOMETYPE" match { case AnEnumeration(a) => a; case _ => throw new Exception("fail!") } res0: com.project.AnEnumeration = SOMETYPE scala> "OTHERTYPE" match { case AnEnumeration(a) => a; case _ => throw new Exception("fail!") } res1: com.project.AnEnumeration = OTHERTYPE scala> "not a type" match { case AnEnumeration(a) => a; case _ => throw new Exception("fail!") } java.lang.Exception: fail!
Our custom Enumeration type.
Exception type for failed String-to-Enumeration reading
Implicit wrapper for Strings to perform Enumeration reading, using an implicit com.paypal.cascade.common.enumeration.EnumReader
Creates an com.paypal.cascade.common.enumeration.EnumReader
the Enumeration type
the method to convert a String to an Enumeration
an EnumReader that knows how to read a String to an Enumeration
Creates an com.paypal.cascade.common.enumeration.EnumReader that converts a String to an Enumeration if the lowercase version of that String matches the lowercase of the Enumeration's stringVal
Creates an com.paypal.cascade.common.enumeration.EnumReader that converts a String to an Enumeration if the lowercase version of that String matches the lowercase of the Enumeration's stringVal
the Enumeration type
the Enumeration values that are candidates to convert
an EnumReader that has the aforementioned properties
Creates an com.paypal.cascade.common.enumeration.EnumReader that converts a String to an Enumeration if the uppercase version of that String matches the uppercase of the Enumeration's stringVal
Creates an com.paypal.cascade.common.enumeration.EnumReader that converts a String to an Enumeration if the uppercase version of that String matches the uppercase of the Enumeration's stringVal
the Enumeration type
the Enumeration values that are candidates to convert
an EnumReader that has the aforementioned properties
Contains a fully type-safe enumeration framework. Takes more setup & code than the built-in scala.Enumeration, but has two distinct advantages:
1. Configurable outcomes when a conversion from a String to Enumeration value fails 2. Non-exhaustive match warnings when one or more Enumeration values are not included in a
match
By comparison, scala.Enumeration will allow a
match
over as few as one Enumeration type, which runs the risk of a scala.MatchError. This will alert at compile time as to the possibility of amatch
miss.As of Jackson 2.x, a method exists to directly serialize scala.Enumeration, which involves writing less up-front boilerplate. One distinct downside, however, is the need for a special Jackson annotation on case classes that use Enumerations in this way. The annotation must be applied directly to each case class member that is an Enumeration. In this way, serializing scala.Enumeration in Jackson involves substantially more boilerplate effort than the custom Enumeration presented here. See https://github.com/FasterXML/jackson-module-scala/wiki/Enumeration for more information.
For these reasons, our custom Enumeration type should be preferred instead of scala.Enumeration or java.util.Enumeration. See the examples subproject for a sample implementation.