/scalax

Extension for default scala library

Primary LanguageScalaOtherNOASSERTION

ScalaX Build Status Coverage Status Version License

Extension for Scala library

Examples

StringOption

import com.github.t3hnar.scalax.StringOption

StringOption(null) // None
StringOption("") // None
StringOption("\t\n\r ") // None
StringOption("nonempty") // Some("nonempty")
StringOption("\t\nnonempty\r ") // Some("nonempty")

RichString

import com.github.t3hnar.scalax.RichString

"123".toByteOpt // Some(123)
"1-2-3".toByteOpt // None

"123".toIntOpt // Some(123)
"1-2-3".toIntOpt // None

"123".toLongOpt // Some(123)
"1-2-3".toLongOpt // None

"123".toFloatOpt // Some(123.0)
"1-2-3".toFloatOpt // None

"123".toDoubleOpt // Some(123.0)
"1-2-3".toDoubleOpt // None

"true".toBooleanOpt // Some(true)
"false".toBooleanOpt // Some(false)

"+".toBooleanOpt // Some(true)
"-".toBooleanOpt // Some(false)

"ON".toBooleanOpt // Some(true)
"OFF".toBooleanOpt // Some(false)

"yes".toBooleanOpt // Some(true)
"no".toBooleanOpt // Some(false)

asInstanceOfOpt

import com.github.t3hnar.scalax.RichAny

(null: Any).asInstanceOfOpt[String] // None
("string": Any).asInstanceOfOpt[String] // Some

RichEnum

Adds withNameOpt method additionally to default withName

  object Color extends Enumeration {
    val Green, Blue, Red = Value
  }

  import com.github.t3hnar.scalax.RichEnum

  Color.withNameOpt("Green") // Some(Green)
  Color.withNameOpt("Black") // None

RichSet

Adds collate method to set. collate tries to compare sets of items in detail and returns items which were added, updated (key relative) and deleted in s2 regarding s1

  import com.github.t3hnar.scalax.RichSet

  case class Entity(id: Int, name: String)

  val s1 = Set(Entity(1, "1"), Entity(2, "2"), Entity(3, "3"))
  val s2 = Set(Entity(1, "1"), Entity(2, "u"), Entity(4, "4"))

  s1.collate(s2)(_.id) // Some(Set(Entity(4, "4")), Set(Entity(2, "u")), Set(3))

RichDuration

  import com.github.t3hnar.scalax.RichDuration
  import concurrent.duration._

  60.seconds.toCoarsest // 1.minute
  60.minutes.toCoarsest // 1.hour
  180.minutes.toCoarsest // 3.hours
  24.hours.toCoarsest // 1.day
  Duration.Inf.toCoarsest // Duration.Inf
  Duration.MinusInf.toCoarsest // Duration.MinusInf

RichPartialFunction

  import com.github.t3hnar.scalax.RichPartialFunction

  val pf1: PartialFunction[Int, String] = {
    case 0 => "0"
    case 1 => "1"
  }

  val pf2 = pf1.filter(_ == 0)

  pf2.isDefinedAt(0) // true
  pf2.isDefinedAt(1) // false

RichTry

  import com.github.t3hnar.scalax.RichTry
  
  Try("10".toInt).fold("NumberFormatException for " + _)("Valid int: " + _)
  
  Try("10".toInt).toEither // Right(10)
  Try("abc".toInt).toEither // Left(java.lang.NumberFormatException: For input string: "abc") 

RichEither

  import com.github.t3hnar.scalax.RichEither

  val either: Either[Throwable, Int] = Left(new RuntimeException)
  either.toTry // Failure(java.lang.RuntimeException)

  val either: Either[Throwable, Int] = Right(1)
  either.toTry // Success(1)  

RichSetMap

  import com.github.t3hnar.scalax.RichSetMap

  val map = Map(1 -> Set(1))
  map.getOrEmpty(1) // Set()
  map.updatedSet(1, _ - 1) // Map()
  map.updatedSet(1, _ + 2) // Map(1 -> Set(1, 2))
  map.updatedSet(2, _ + 2) // Map(1 -> Set(1), 2 -> Set(2))
  map.updatedSet(1, _ => Set(1, 2, 3)) // Map(1 -> Set(1, 2, 3))

Primitive

  import com.github.t3hnar.scalax.Primitive

  Primitive.unapply(()) // None
  Primitive.unapply(true) // Some(true)
  Primitive.unapply(' ') // Some( )
  Primitive.unapply(1) // Some(1)
  Primitive.unapply("") // None

StandardAnyVal

  import com.github.t3hnar.scalax.StandardAnyVal

  StandardAnyVal.unapply(()) // Some(())
  StandardAnyVal.unapply(true) // Some(true)
  StandardAnyVal.unapply(' ') // Some( )
  StandardAnyVal.unapply(1) // Some(1)
  StandardAnyVal.unapply("") // None

RichClass

  import com.github.t3hnar.scalax.RichClass
  
  classOf[Class1].simpleName // Class1 
  classOf[Class1.Class2].simpleName // Class2
  classOf[Class1.Class2.Class3].simpleName // Class3

  class Class1
  object Class1 {
    class Class2
    object Class2 {
      class Class3
    }
  }

RichOption

  import com.github.t3hnar.scalax.RichOption

  Option(1) orError "option is empty" // 1
  Option.empty[Int] orError "option is empty" // java.lang.RuntimeException

ExpiringCache

  import com.github.t3hnar.scalax.util.ExpiringCache

  val cache = new ExpiringCache[Int, Int](duration = 5,
                                        unit = TimeUnit.SECONDS,
                                        queryOverflow = 3)
  cache.put(0,0)
  cache.get(0) // Some(0)

  // after 5 seconds
  cache.get(0) // None, however it is not cleaned up yet, need one more query to go

  cache.get(0) // None, now it's cleaned up as we reached `queryOverflow` limit