Scala library that provides common code part or templates
- enumeration pattern
- enumeration value trait
EnumValue
- enumeration value
code
- enumeration value
name
- common
toString
method implementation
- enumeration value
- enumeration object trait
EnumObject
- common
values
method - common
valueOf
method
- common
- enumeration value trait
- serializable enumeration pattern
- all features of enumeration pattern
- standard Java serialization/deserialization
- Scala 2.11 and 2.12
Add dependency to build.sbt
:
libraryDependencies += "ru.ars-co" %% "scala-common" % currentVersion
Implement enumeration:
sealed abstract class MyIntEnumValue(override val code: Int) extends EnumValue[Int]
object MyIntEnumValues extends EnumObject[MyIntEnumValue, Int] {
final case object FirstValue extends MyIntEnumValue(1)
final case object SecondValue extends MyIntEnumValue(2)
final case object ThirdValue extends MyIntEnumValue(3)
override def values: Seq[MyIntEnumValue] = Seq(FirstValue, SecondValue, ThirdValue)
}
Use enumeration:
MyIntEnumValues.ThirdValue
MyIntEnumValues.FirstValue.code
To implement basic enum:
- create enum value class by extending
EnumValue
trait- override
code
method
- override
- create enum values objects by extending
EnumObject
trait- create
final case object
for each enum value - override
values
method withSeq
of all enum value objects
- create
Examples:
sealed abstract class MyIntEnumValue(override val code: Int) extends EnumValue[Int]
object MyIntEnumValues extends EnumObject[MyIntEnumValue, Int] {
final case object FirstValue extends MyIntEnumValue(1)
final case object SecondValue extends MyIntEnumValue(2)
final case object ThirdValue extends MyIntEnumValue(3)
override def values: Seq[MyIntEnumValue] = Seq(FirstValue, SecondValue, ThirdValue)
}
sealed abstract class MyStringEnumValue(override val code: String) extends EnumValue[String]
object MyStringEnumValues extends EnumObject[MyStringEnumValue, String] {
final case object FirstValue extends MyStringEnumValue("first")
final case object SecondValue extends MyStringEnumValue("second")
final case object ThirdValue extends MyStringEnumValue("third")
override def values: Seq[MyStringEnumValue] = Seq(FirstValue, SecondValue, ThirdValue)
}
There are 2 ways to implement enumeration: trait-based and abstract class-based.
The first is better when you want to subclass enum class from another class but need more code to write.
The last one provides more concise code but you need to implement objectTypeTag()
method manually.
To implement Java serializable enum:
- create enum value class by extending
SerializableIntEnumValue
orSerializableStringEnumValue
abstract class.- override
code
method - override
objectTypeTag()
method
- override
- create enum values objects by extending
EnumObject
trait- create
final case object
for each enum value - override
values
method withSeq
of all enum value objects
- create
Examples:
sealed abstract class MyIntEnumValue(override val code: Int)
extends SerializableIntEnumValue[MyEnumValue, MyEnumValues.type] {
override protected[this] val objectTypeTag: TypeTag[MyIntEnumValues.type] = typeTag[MyIntEnumValues.type]
}
object MyIntEnumValues extends EnumObject[MyIntEnumValue, Int] {
final case object FirstValue extends MyIntEnumValue(1)
final case object SecondValue extends MyIntEnumValue(2)
final case object ThirdValue extends MyIntEnumValue(3)
override def values: Seq[MyIntEnumValue] = Seq(FirstValue, SecondValue, ThirdValue)
}
sealed abstract class MyStringEnumValue(override val code: String)
extends SerializableStringEnumValue[MyEnumValue, MyEnumValues.type] {
override protected[this] val objectTypeTag: TypeTag[MyStringEnumValues.type] = typeTag[MyStringEnumValues.type]
}
object MyStringEnumValues extends EnumObject[MyStringEnumValue, String] {
final case object FirstValue extends MyStringEnumValue("first")
final case object SecondValue extends MyStringEnumValue("second")
final case object ThirdValue extends MyStringEnumValue("third")
override def values: Seq[MyStringEnumValue] = Seq(FirstValue, SecondValue, ThirdValue)
}
To implement Java serializable enum:
- create enum value class by extending
AbstractSerializableIntEnumValue
orAbstractSerializableStringEnumValue
abstract class.- override
code
method
- override
- create enum values objects by extending
EnumObject
trait- create
final case object
for each enum value - override
values
method withSeq
of all enum value objects
- create
Examples:
sealed abstract class MyIntEnumValue(override val code: Int)
extends AbstractSerializableIntEnumValue[MyEnumValue, MyEnumValues.type]
object MyIntEnumValues extends EnumObject[MyIntEnumValue, Int] {
final case object FirstValue extends MyIntEnumValue(1)
final case object SecondValue extends MyIntEnumValue(2)
final case object ThirdValue extends MyIntEnumValue(3)
override def values: Seq[MyIntEnumValue] = Seq(FirstValue, SecondValue, ThirdValue)
}
sealed abstract class MyStringEnumValue(override val code: String)
extends AbstractSerializableStringEnumValue[MyEnumValue, MyEnumValues.type]
object MyStringEnumValues extends EnumObject[MyStringEnumValue, String] {
final case object FirstValue extends MyStringEnumValue("first")
final case object SecondValue extends MyStringEnumValue("second")
final case object ThirdValue extends MyStringEnumValue("third")
override def values: Seq[MyStringEnumValue] = Seq(FirstValue, SecondValue, ThirdValue)
}
Copyright 2018 Arsen Ibragimov (ars)