scala-common

Maven Central Maven Central

Coverage Status Licence Latest release Code quality Gitterchat

Scala library that provides common code part or templates

Features:

  • enumeration pattern
    • enumeration value trait EnumValue
      • enumeration value code
      • enumeration value name
      • common toString method implementation
    • enumeration object trait EnumObject
      • common values method
      • common valueOf method
  • serializable enumeration pattern
    • all features of enumeration pattern
    • standard Java serialization/deserialization
  • Scala 2.11 and 2.12

Usage

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

Basic enum implementation

To implement basic enum:

  1. create enum value class by extending EnumValue trait
    1. override code method
  2. create enum values objects by extending EnumObject trait
    1. create final case object for each enum value
    2. override values method with Seq of all enum value objects

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)
}

Java serializable Int- and String-based enum implementation

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.

Trait-based realization

To implement Java serializable enum:

  1. create enum value class by extending SerializableIntEnumValue or SerializableStringEnumValue abstract class.
    1. override code method
    2. override objectTypeTag() method
  2. create enum values objects by extending EnumObject trait
    1. create final case object for each enum value
    2. override values method with Seq of all enum value objects

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)
}

Abstract-based realization

To implement Java serializable enum:

  1. create enum value class by extending AbstractSerializableIntEnumValue or AbstractSerializableStringEnumValue abstract class.
    1. override code method
  2. create enum values objects by extending EnumObject trait
    1. create final case object for each enum value
    2. override values method with Seq of all enum value objects

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

Copyright 2018 Arsen Ibragimov (ars)