package textapp.client

import ccf.tree.indexing.TreeIndex
import ccf.tree.operation.{TreeOperation, InsertOperation, DeleteOperation}
import javax.swing.JFrame
import textapp.client.jgoodies.FormsPanel
import textapp.{TextDocument, Elem}

class MainFrame(document: TextDocument, sendToServer: TreeOperation => Unit) extends JFrame("libccf test application") {
  val textArea = new TextArea(document.text, onInsert, onDelete)
  val mainPanel = new MainPanel(textArea)
  getContentPane().add(mainPanel)

  def applyOp(op: TreeOperation) {
    textArea.applyOp(op)
  }

  private def onInsert(items: List[(Int, Char)]) {
    items.foreach { case (i, c) =>
      sendToServer(InsertOperation(TreeIndex(i), Elem(c)))
    }
  }

  private def onDelete(items: List[Int]) {
    items.foreach { i =>
      sendToServer(DeleteOperation(TreeIndex(i)))
    }
  }

  class MainPanel(textArea: TextArea) extends FormsPanel("pref", "pref") {
    add(textArea, xy(1,1))
  }
}

package textapp.client

import ccf.tree.indexing.TreeIndex
import ccf.tree.operation.{TreeOperation, InsertOperation, DeleteOperation}
import textapp.client.jgoodies.FormsPanel
import textapp.{TextDocument, Elem}
import javax.swing.{JLabel, JFrame}
import java.awt.Color

class MainFrame(document: TextDocument, sendToServer: TreeOperation => Unit) extends JFrame("libccf test application") {
  val textArea = new TextArea(document.text, onInsert, onDelete)
  val mainPanel = new MainPanel(textArea)
  getContentPane().add(mainPanel)

  def applyOp(op: TreeOperation) {
    textArea.applyOp(op)
  }

  private def onInsert(items: List[(Int, Char)]) {
    items.foreach { case (i, c) =>
      sendToServer(InsertOperation(TreeIndex(i), Elem(c)))
    }
  }

  private def onDelete(items: List[Int]) {
    items.foreach { i =>
      sendToServer(DeleteOperation(TreeIndex(i)))
    }
  }

  class MainPanel(textArea: TextArea) extends FormsPanel("pref", "pref, pref") {
    val infoPanel = new FormsPanel("pref", "20px, 14px, 14px, 8px") {
      val genericInfo = new JLabel("Collaborative text-editing using CCF")
      val ownTextInfo = new JLabel("* Own text is shown with green color")
      val otherTextInfo = new JLabel("* Others' text is shown with red color")
      ownTextInfo.setForeground(new Color(0, 150, 0))
      otherTextInfo.setForeground(new Color(255, 0, 0))

      add(genericInfo, xy(1,1))
      add(ownTextInfo, xy(1,2))
      add(otherTextInfo, xy(1,3))
    }
    add(infoPanel, xy(1,1))
    add(textArea, xy(1,2))
  }
}

app: Add info texts about coloring to the GUI

Signed-off-by: Aki Saarinen <2c8035bb80cf98a391872dcbe797b269d759f6d9@reaktor.fi>


package gitbucket.monitoring.utils

import java.util.concurrent.TimeUnit

object UnitConverter {
  def byteToKB(byte: BigDecimal, digit: Int = 2): BigDecimal = Rounding.ceil(byte / 1024)
  def byteToMB(byte: BigDecimal, digit: Int = 2): BigDecimal = Rounding.ceil(byte / (1024 * 1024))
  def byteToGB(byte: BigDecimal, digit: Int = 2): BigDecimal = Rounding.ceil(byte / (1024 * 1024 * 1024))
}

object Rounding {
  def ceil(num:BigDecimal, digit: Int = 2): BigDecimal = {
    num.setScale(digit, scala.math.BigDecimal.RoundingMode.CEILING)
  }
}

object Time {
  case class DateTime(
    days: Int,
    hours: Int,
    minutes: Int
  )

  def secondsToDateTime(seconds: Int): Either[String, DateTime] = {
    try {
      Right(DateTime(
        (seconds / (60 * 60 * 24)) % 60,
        ((seconds / (60 * 60)) % 24) % 60,
        (seconds / 60) % 60
      ))
    } catch {
      case e: Exception => Left("ERROR")
    }
  }
}

package gitbucket.monitoring.utils

import java.util.concurrent.TimeUnit

object UnitConverter {
  def byteToKB(byte: BigDecimal, digit: Int = 2): BigDecimal = Rounding.ceil(byte / 1024)
  def byteToMB(byte: BigDecimal, digit: Int = 2): BigDecimal = Rounding.ceil(byte / (1024 * 1024))
  def byteToGB(byte: BigDecimal, digit: Int = 2): BigDecimal = Rounding.ceil(byte / (1024 * 1024 * 1024))
}

object Rounding {
  def ceil(num:BigDecimal, digit: Int = 2): BigDecimal = {
    num.setScale(digit, scala.math.BigDecimal.RoundingMode.CEILING)
  }
}

object Time {
  case class DateTime(
    days: Int,
    hours: Int,
    minutes: Int
  )

  def secondsToDateTime(seconds: Int): Either[String, DateTime] = {
    try {
      Right(DateTime(
        seconds / (60 * 60 * 24),
        ((seconds / (60 * 60)) % 24) % 60,
        (seconds / 60) % 60
      ))
    } catch {
      case e: Exception => Left("ERROR")
    }
  }
}

Fix calculate day from seconds

Fix calculate day from seconds


package gitbucket.monitoring.utils

object UnitConverter {
  def byteToKB(byte: BigDecimal, digit: Int = 2): BigDecimal = Rounding.ceil(byte / 1024)
  def byteToMB(byte: BigDecimal, digit: Int = 2): BigDecimal = Rounding.ceil(byte / (1024 * 1024))
  def byteToGB(byte: BigDecimal, digit: Int = 2): BigDecimal = Rounding.ceil(byte / (1024 * 1024 * 1024))
}

object Rounding {
  def ceil(num:BigDecimal, digit: Int = 2): BigDecimal = {
    num.setScale(digit, scala.math.BigDecimal.RoundingMode.CEILING)
  }
}

package gitbucket.monitoring.utils

import java.util.concurrent.TimeUnit

object UnitConverter {
  def byteToKB(byte: BigDecimal, digit: Int = 2): BigDecimal = Rounding.ceil(byte / 1024)
  def byteToMB(byte: BigDecimal, digit: Int = 2): BigDecimal = Rounding.ceil(byte / (1024 * 1024))
  def byteToGB(byte: BigDecimal, digit: Int = 2): BigDecimal = Rounding.ceil(byte / (1024 * 1024 * 1024))
}

object Rounding {
  def ceil(num:BigDecimal, digit: Int = 2): BigDecimal = {
    num.setScale(digit, scala.math.BigDecimal.RoundingMode.CEILING)
  }
}

object Time {
  case class DateTime(
    days: Int,
    hours: Int,
    minutes: Int
  )

  def secondsToDateTime(seconds: Int): Either[String, DateTime] = {
    try {
      Right(DateTime(
        (seconds / (60 * 60 * 24)) % 60,
        ((seconds / (60 * 60)) % 24) % 60,
        (seconds / 60) % 60
      ))
    } catch {
      case e: Exception => Left("ERROR")
    }
  }
}

Create convert seconds to DateTime method


package io.continuum.bokeh

@model class Plot extends Widget {
    object title extends Field[String]("")

    title = include[TextProps]
    outline = include[LineProps]

    object x_range extends Field[Range]
    object y_range extends Field[Range]

    object extra_x_ranges extends Field[Map[String, Range]]
    object extra_y_ranges extends Field[Map[String, Range]]

    object x_mapper_type extends Field[String]("auto")
    object y_mapper_type extends Field[String]("auto")

    object renderers extends Field[List[Renderer]]
    object tools extends Field[List[Tool]] with ToolsField

    object left extends Field[List[PlotObject]]
    object right extends Field[List[PlotObject]]
    object above extends Field[List[PlotObject]]
    object below extends Field[List[PlotObject]]

    object toolbar_location extends Field[Location](Location.Above)
    object logo extends Field[Logo](Logo.Normal)

    object plot_width extends Field[Int](600)
    object plot_height extends Field[Int](600)

    object background_fill extends Field[Color](Color.White)
    object border_fill extends Field[Color](Color.White)

    object min_border_top extends Field[Int]
    object min_border_bottom extends Field[Int]
    object min_border_left extends Field[Int]
    object min_border_right extends Field[Int]
    object min_border extends Field[Int]

    object h_symmetry extends Field[Boolean](true)
    object v_symmetry extends Field[Boolean](false)
}

package io.continuum.bokeh

@model class Plot extends Widget {
    object title extends Field[String]("")

    title = include[TextProps]
    outline = include[LineProps]

    object x_range extends Field[Range]
    object y_range extends Field[Range]

    object extra_x_ranges extends Field[Map[String, Range]]
    object extra_y_ranges extends Field[Map[String, Range]]

    object x_mapper_type extends Field[String]("auto")
    object y_mapper_type extends Field[String]("auto")

    object renderers extends Field[List[Renderer]]
    object tools extends Field[List[Tool]] with ToolsField

    object left extends Field[List[PlotObject]]
    object right extends Field[List[PlotObject]]
    object above extends Field[List[PlotObject]]
    object below extends Field[List[PlotObject]]

    object toolbar_location extends Field[Location](Location.Above)
    object logo extends Field[Logo](Logo.Normal)

    object plot_width extends Field[Int](600)
    object plot_height extends Field[Int](600)

    def width = plot_width
    def height = plot_height

    object background_fill extends Field[Color](Color.White)
    object border_fill extends Field[Color](Color.White)

    object min_border_top extends Field[Int]
    object min_border_bottom extends Field[Int]
    object min_border_left extends Field[Int]
    object min_border_right extends Field[Int]
    object min_border extends Field[Int]

    object h_symmetry extends Field[Boolean](true)
    object v_symmetry extends Field[Boolean](false)
}

Add Plot.{width,height} aliases for plot_{width,height}


package com.twitter.util

import org.specs.Specification
import org.specs.mock.Mockito

object TimerSpec extends Specification with Mockito {
  "ReferenceCountedTimer" should {
    val underlying = mock[Timer]
    val factory = mock[() => Timer]
    factory() returns underlying

    val refcounted = new ReferenceCountedTimer(factory)
    there was no(factory)()

    "call the factory when it is first acquired" in {
      refcounted.acquire()
      there was one(factory)()
    }

    "stop the underlying timer when acquire count reaches 0" in {
      refcounted.acquire()
      refcounted.acquire()
      refcounted.acquire()
      there was one(factory)()

      refcounted.stop()
      there was no(underlying).stop()
      refcounted.stop()
      there was no(underlying).stop()
      refcounted.stop()
      there was one(underlying).stop()
    }
  }

  "ScheduledPoolTimer" should {
    "initialize and stop" in {
      val timer = new ScheduledPoolTimer(1)
      timer must notBeNull
      timer.stop()
    }
  }
}

package com.twitter.util

import java.util.concurrent.atomic.AtomicInteger
import com.twitter.conversions.time._
import org.specs.Specification
import org.specs.mock.Mockito

object TimerSpec extends Specification with Mockito {
  "ReferenceCountedTimer" should {
    val underlying = mock[Timer]
    val factory = mock[() => Timer]
    factory() returns underlying

    val refcounted = new ReferenceCountedTimer(factory)
    there was no(factory)()

    "call the factory when it is first acquired" in {
      refcounted.acquire()
      there was one(factory)()
    }

    "stop the underlying timer when acquire count reaches 0" in {
      refcounted.acquire()
      refcounted.acquire()
      refcounted.acquire()
      there was one(factory)()

      refcounted.stop()
      there was no(underlying).stop()
      refcounted.stop()
      there was no(underlying).stop()
      refcounted.stop()
      there was one(underlying).stop()
    }
  }

  "ScheduledPoolTimer" should {
    "initialize and stop" in {
      val timer = new ScheduledPoolTimer(1)
      timer must notBeNull
      timer.stop()
    }

    "increment a counter" in {
      val timer = new ScheduledPoolTimer(1)
      val counter = new AtomicInteger(0)
      timer.schedule(0.millis, 20.millis) {
        counter.incrementAndGet()
      }
      Thread.sleep(1.second.inMillis)
      timer.stop()
      counter.get() must beCloseTo(50, 1)
    }
  }
}

Implement a simple counter-incrementing test for the ScheduledPoolTimer


package org.openmole.gui.client.core

import org.openmole.gui.client.core.files.{ FileDisplayer, TreeNodePanel, TreeNodeTabs }

/*
 * Copyright (C) 24/07/15 // mathieu.leclaire@openmole.org
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package object panels {
  val executionPanel = new ExecutionPanel
  val treeNodeTabs = new TreeNodeTabs()
  val fileDisplayer = new FileDisplayer(treeNodeTabs)
  val treeNodePanel = TreeNodePanel()
  val modelWizardPanel = new ModelWizardPanel
  val marketPanel = new MarketPanel
  val pluginPanel = new PluginPanel
  val stackPanel = new TextPanel("Error stack")
}

package org.openmole.gui.client.core

import org.openmole.gui.client.core.files.{ FileDisplayer, TreeNodePanel, TreeNodeTabs }

/*
 * Copyright (C) 24/07/15 // mathieu.leclaire@openmole.org
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package object panels {
  val executionPanel = new ExecutionPanel
  val treeNodeTabs = new TreeNodeTabs()
  val fileDisplayer = new FileDisplayer(treeNodeTabs)
  val treeNodePanel = TreeNodePanel()
  def modelWizardPanel = new ModelWizardPanel
  val marketPanel = new MarketPanel
  val pluginPanel = new PluginPanel
  val stackPanel = new TextPanel("Error stack")
}

[GUI] Build new WizardPanel each time it is open


package enumeratum
import scala.language.experimental.macros

/**
  * Created by Lloyd on 1/4/17.
  *
  * Copyright 2017
  */
object FindValEnums {
  def apply[A] = macro ValueEnumMacros.findIntValueEntriesImpl[A]
}

sealed abstract class A private (val value: Int) {
  val text: String
}

object A {

  val values = FindValEnums[A]

  case object A1 extends A(1) {
    val text = identity("something")
  }

  def identity(str: String) = str

}

class C
object C {
  def build: C = new C
}

sealed abstract class B private (val value: Int, val other: C) {
  val text: String
}

object B {

  val values = FindValEnums[B]

  case object B1 extends B(1, new C) {
    val text = identity("something")
  }
  case object B2 extends B(2, C.build) {
    val text = identity("something")
  }

  def identity(str: String) = str

}

package enumeratum
import scala.language.experimental.macros

/**
  * Created by Lloyd on 1/4/17.
  *
  * Copyright 2017
  */
object FindValEnums {
  def apply[A] = macro ValueEnumMacros.findIntValueEntriesImpl[A]
}

sealed abstract class A private (val value: Int) {
  val text: String
}

object A {

  val values = FindValEnums[A]

  case object A1 extends A(1) {
    val text = identity("something")
  }

  def identity(str: String) = str

}

// Example with outside constructor call in constructor call
class C
object C {
  def build: C = new C
}

sealed abstract class B private (val value: Int, val other: C) {
  val text: String
}
object B {

  val values = FindValEnums[B]

  case object B1 extends B(1, new C) {
    val text = identity("something")
  }
  case object B2 extends B(2, C.build) {
    val text = identity("something")
  }

  def identity(str: String) = str

}

// Test case of traits with val member fulfilling the value contract
sealed abstract trait D {
  def value: Int
}

object D {
  val values = FindValEnums[D]

  case object D1 extends D {
    val value = 1
    val text  = identity("something")
  }

  // Out of order
  case object D2 extends D {
    val text  = identity("something")
    val value = 2
  }

  def identity(str: String) = str
}

Add extra compilation test to macros

Add extra compilation test to macros


package io.shaka.http

import io.shaka.http.Https._
import io.shaka.http.Request.GET

// TODO Convert into automated test once code dependency has been sorted out
object ManualTestingTool extends App {
  val response = Http.http(GET("https://127.0.0.1:7878/foo"))(httpsConfig = Some(HttpsConfig(
    TrustServersByTrustStore("client-truststore.jks", "password"),
    UseKeyStore("src/test/resources/certs/keystore-testing-client.jks", "password"))
  ))
  println(response)
}

package io.shaka.http

import io.shaka.http.Https._
import io.shaka.http.Request.GET

// TODO Convert into automated test once code dependency has been sorted out
object ManualTestingTool extends App {
  val response = Http.http(GET("https://127.0.0.1:7878/foo"))(httpsConfig = Some(HttpsConfig(
    TrustServersByTrustStore("../naive-http-server/client-truststore.jks", "password"),
    UseKeyStore("src/test/resources/certs/keystore-testing-client.jks", "password"))
  ))
  println(response)
}

Update to use client-truststore from naive-http-server


package protocols

import java.nio.charset.StandardCharsets

import akka.NotUsed
import akka.stream.scaladsl.Source
import models.cfs.Block._
import play.api.http._
import play.api.libs.MimeTypes
import play.api.mvc._
import play.utils.UriEncoding

/**
 * @author zepeng.li@gmail.com
 */
trait HttpDownloadable {

  def size: Long
  def name: String
  def whole: Source[BLK, NotUsed]
}

trait HttpDownloadResult extends HeaderNames with Status {

  def send(
    file: HttpDownloadable,
    name: HttpDownloadable => String = _.name,
    inline: Boolean = false
  ): Result = {
    val result = Result(
      ResponseHeader(OK),
      HttpEntity.Streamed(
        file.whole,
        Some(file.size),
        contentTypeOf(file)
      )
    )
    val encoded = UriEncoding.encodePathSegment(name(file), StandardCharsets.UTF_8)
    result.withHeaders(
      CONTENT_DISPOSITION -> {
        val dispositionType = if (inline) "inline" else "attachment"
        s"""$dispositionType; filename="$name"; filename*=utf-8''$encoded"""
      }
    )
  }

  def contentTypeOf(inode: HttpDownloadable): Option[String] = {
    MimeTypes.forFileName(inode.name).orElse(Some(ContentTypes.BINARY))
  }
}

object HttpDownloadResult extends HttpDownloadResult

package protocols

import java.nio.charset.StandardCharsets

import akka.NotUsed
import akka.stream.scaladsl.Source
import models.cfs.Block._
import play.api.http._
import play.api.libs.MimeTypes
import play.api.mvc._
import play.utils.UriEncoding

/**
 * @author zepeng.li@gmail.com
 */
trait HttpDownloadable {

  def size: Long
  def name: String
  def whole: Source[BLK, NotUsed]
}

trait HttpDownloadResult extends HeaderNames with Status {

  def send(
    file: HttpDownloadable,
    name: HttpDownloadable => String = _.name,
    inline: Boolean = false
  ): Result = {
    val result = Result(
      ResponseHeader(OK),
      HttpEntity.Streamed(
        file.whole,
        Some(file.size),
        contentTypeOf(file)
      )
    )
    val encoded = UriEncoding.encodePathSegment(name(file), StandardCharsets.UTF_8)
    result.withHeaders(
      CONTENT_DISPOSITION -> {
        val dispositionType = if (inline) "inline" else "attachment"
        s"""$dispositionType; filename="${name(file)}"; filename*=utf-8''$encoded"""
      }
    )
  }

  def contentTypeOf(inode: HttpDownloadable): Option[String] = {
    MimeTypes.forFileName(inode.name).orElse(Some(ContentTypes.BINARY))
  }
}

object HttpDownloadResult extends HttpDownloadResult

Fix incorrect file name in http headers

Fix incorrect file name in http headers


object Version {
  val Akka = "2.5.1"
  val Camel = "2.19.0"
  val Fs2 = "0.9.5"
  val Log4j = "2.5"
  val JUnitInterface = "0.11"
  val Scalatest = "3.0.1"
}

object Version {
  val Akka = "2.4.18"
  val Camel = "2.19.0"
  val Fs2 = "0.9.5"
  val Log4j = "2.5"
  val JUnitInterface = "0.11"
  val Scalatest = "3.0.1"
}

Revert back to Akka 2.4.18

Revert back to Akka 2.4.18

- re #30


package controllers

import play.api._
import play.api.mvc._
import play.api.libs.ws.WS
import play.api.Play.current
import play.api.libs.json._
import play.api.libs.functional.syntax._
import play.api.libs.concurrent.Execution.Implicits.defaultContext

import models.Repo

class Api extends Controller {

  implicit val writesRepo = new Writes[Repo] {
    def writes(repo:Repo) = Json.obj(
      "name" -> repo.name,
      "language" -> repo.language,
      "is_fork" -> repo.isFork,
      "size" -> repo.size
    )
  }

  implicit val readsRepoFromGithub:Reads[Repo] = (
    (JsPath \ "name").read[String] and
    (JsPath \ "language").read[String] and
    (JsPath \ "fork").read[Boolean] and
    (JsPath \ "size").read[Long]
  )(Repo.apply _)

  def repos(login:String) = Action.async {
    val url = s"https://api.github.com/users/$login/repos"
    val request = WS.url(url).withHeaders("Content-Type" -> "application/json").get()
    WS.url(url).get().map { r =>
      if (r.status == 200) {
        val reposOpt = Json.parse(r.body).validate[List[Repo]]
        reposOpt match {
          case JsSuccess(repos, _) => Ok(Json.toJson(repos))
          case _ => InternalServerError
        }
      }
      else {
        NotFound
      }
    }
  }

}

package controllers

import play.api._
import play.api.mvc._
import play.api.libs.ws.WS
import play.api.Play.current
import play.api.libs.json._
import play.api.libs.functional.syntax._
import play.api.libs.concurrent.Execution.Implicits.defaultContext

import models.Repo

class Api extends Controller {

  implicit val writesRepo = new Writes[Repo] {
    def writes(repo:Repo) = Json.obj(
      "name" -> repo.name,
      "language" -> repo.language,
      "is_fork" -> repo.isFork,
      "size" -> repo.size
    )
  }

  implicit val readsRepoFromGithub:Reads[Repo] = (
    (JsPath \ "name").read[String] and
    (JsPath \ "language").read[String] and
    (JsPath \ "fork").read[Boolean] and
    (JsPath \ "size").read[Long]
  )(Repo.apply _)

  def repos(login:String) = Action.async {
    val url = s"https://api.github.com/users/$login/repos"
    val request = WS.url(url).withHeaders("Content-Type" -> "application/json").get()
    request.map { r =>
      if (r.status == 200) {
        val reposOpt = Json.parse(r.body).validate[List[Repo]]
        reposOpt match {
          case JsSuccess(repos, _) => Ok(Json.toJson(repos))
          case _ => InternalServerError
        }
      }
      else {
        NotFound
      }
    }
  }

}

Remove unnecessary query to GitHub API.

CHAP13: Remove unnecessary query to GitHub API.


package com.github.xiaodongw.swagger.finatra

import io.swagger.annotations.{ApiModelProperty, ApiModel}
import org.joda.time.{LocalDate, DateTime}

@ApiModel(value="AddressModel", description="Sample address model for documentation")
case class Address(street: String, zip: String)

case class Student(firstName: String, lastName: String, gender: Gender, birthday: LocalDate, grade: Int, address: Option[Address])

//todo scala enumeration is not supported
object CourseType extends Enumeration {
  val LEC, LAB = Value
}

case class Course(time: DateTime,
                  name: String,
                  tags: Seq[String],
                  @ApiModelProperty(dataType = "string", allowableValues = "LEC,LAB") typ: CourseType.Value,
                  capacity: Int,
                  @ApiModelProperty(dataType = "double") cost: BigDecimal)

package com.github.xiaodongw.swagger.finatra

import io.swagger.annotations.{ApiModelProperty, ApiModel}
import org.joda.time.{LocalDate, DateTime}

@ApiModel(value="AddressModel", description="Sample address model for documentation")
case class Address(street: String, zip: String)

case class Student(firstName: String, lastName: String, gender: Gender, birthday: LocalDate, grade: Int, address: Option[Address])

object CourseType extends Enumeration {
  val LEC, LAB = Value
}

case class Course(time: DateTime,
                  name: String,
                  @ApiModelProperty(required = false, example = "[math,stem]")
                  tags: Seq[String],
                  @ApiModelProperty(dataType = "string", allowableValues = "LEC,LAB")
                  typ: CourseType.Value,
                  @ApiModelProperty(readOnly = true)
                  capacity: Int,
                  @ApiModelProperty(dataType = "double", required = true)
                  cost: BigDecimal)

Update the sample to demo usage of ApiModelProperty.


import sbt._

object Dependencies {
  val CaffeineVersion = "2.5.2"

  val Caffeine = "com.github.ben-manes.caffeine" % "caffeine" % CaffeineVersion
  val Java8Compat = "org.scala-lang.modules" %% "scala-java8-compat" % "0.8.0"
  val Jsr305 = "com.google.code.findbugs" % "jsr305" % "3.0.1"
  val Scalactic = "org.scalactic" %% "scalactic" % "3.0.1"
  val Scalatest = "org.scalatest" %% "scalatest" % "3.0.1"
}

import sbt._

object Dependencies {
  val CaffeineVersion = "2.5.6"

  val Caffeine = "com.github.ben-manes.caffeine" % "caffeine" % CaffeineVersion
  val Java8Compat = "org.scala-lang.modules" %% "scala-java8-compat" % "0.8.0"
  val Jsr305 = "com.google.code.findbugs" % "jsr305" % "3.0.1"
  val Scalactic = "org.scalactic" %% "scalactic" % "3.0.1"
  val Scalatest = "org.scalatest" %% "scalatest" % "3.0.1"
}

Bump caffeine version to 2.5.6

Bump caffeine version to 2.5.6


package org.talkingpuffin.ui.util

import java.awt.Desktop
import java.net.URI
import java.awt._
import javax.swing.ImageIcon
import org.talkingpuffin.Main

object DesktopUtil {
  val trayIcon: TrayIcon = SystemTray.isSupported match {
    case true =>
      val icon = new TrayIcon(new ImageIcon(getClass.getResource("/TalkingPuffin_16.png")).getImage,Main.title)
      SystemTray.getSystemTray.add(icon)
      icon
    case _ => null
  }

  def browse(uri: String) = if (Desktop.isDesktopSupported) Desktop.getDesktop.browse(new URI(uri))

  def notify(message: String, header: String) = if (SystemTray.isSupported)
    trayIcon.displayMessage(header, message, TrayIcon.MessageType.INFO)
}

package org.talkingpuffin.ui.util

import java.awt.Desktop
import java.net.URI
import java.awt._
import javax.swing.ImageIcon
import org.talkingpuffin.Main
import actors.Actor._

object DesktopUtil {
  private case class Browse(uri: String)
  val browser = actor {
    while(true) {
      receive {
        case browse: Browse => Desktop.getDesktop.browse(new URI(browse.uri))
      }
    }
  }

  val trayIcon: TrayIcon = SystemTray.isSupported match {
    case true =>
      val icon = new TrayIcon(new ImageIcon(getClass.getResource("/TalkingPuffin_16.png")).getImage,Main.title)
      SystemTray.getSystemTray.add(icon)
      icon
    case _ => null
  }

  def browse(uri: String) = if (Desktop.isDesktopSupported) browser ! Browse(uri)

  def notify(message: String, header: String) = if (SystemTray.isSupported)
    trayIcon.displayMessage(header, message, TrayIcon.MessageType.INFO)
}

Use an actor, so a delayed desktop.browse won’t block the UI


name := "apibuilder-validation"

organization := "io.flow"

scalaVersion in ThisBuild := "2.12.8"

crossScalaVersions := Seq("2.12.8")

lazy val root = project
  .in(file("."))
  .settings(
    libraryDependencies ++= Seq(
      "com.typesafe.play" %% "play-json" % "2.7.1",
      "org.apache.commons" % "commons-compress" % "1.18",
      "org.scalatest" %% "scalatest" % "3.0.5" % Test
    ),
    credentials += Credentials(
      "Artifactory Realm",
      "flow.jfrog.io",
      System.getenv("ARTIFACTORY_USERNAME"),
      System.getenv("ARTIFACTORY_PASSWORD")
    )
  )

publishTo := {
  val host = "https://flow.jfrog.io/flow"
  if (isSnapshot.value) {
    Some("Artifactory Realm" at s"$host/libs-snapshot-local;build.timestamp=" + new java.util.Date().getTime)
  } else {
    Some("Artifactory Realm" at s"$host/libs-release-local")
  }
}
version := "0.4.1"

name := "apibuilder-validation"

organization := "io.flow"

scalaVersion in ThisBuild := "2.12.8"

crossScalaVersions := Seq("2.12.8")

lazy val root = project
  .in(file("."))
  .settings(
    libraryDependencies ++= Seq(
      "com.typesafe.play" %% "play-json" % "2.7.1",
      "org.apache.commons" % "commons-compress" % "1.18",
      "org.scalatest" %% "scalatest" % "3.0.5" % Test
    ),
    credentials += Credentials(
      "Artifactory Realm",
      "flow.jfrog.io",
      System.getenv("ARTIFACTORY_USERNAME"),
      System.getenv("ARTIFACTORY_PASSWORD")
    )
  )

publishTo := {
  val host = "https://flow.jfrog.io/flow"
  if (isSnapshot.value) {
    Some("Artifactory Realm" at s"$host/libs-snapshot-local;build.timestamp=" + new java.util.Date().getTime)
  } else {
    Some("Artifactory Realm" at s"$host/libs-release-local")
  }
}
version := "0.4.2"

autocommit: Update configuration to record version 0.4.2


name := "lib-reference-scala"

organization := "io.flow"

scalaVersion in ThisBuild := "2.11.8"

lazy val root = project
  .in(file("."))
  .settings(
    libraryDependencies ++= Seq(
      "org.scalatest" %% "scalatest" % "2.2.6" % Test
    )
  )

publishTo := {
  val host = "https://flow.artifactoryonline.com/flow"
  if (isSnapshot.value) {
    Some("Artifactory Realm" at s"$host/libs-snapshot-local;build.timestamp=" + new java.util.Date().getTime)
  } else {
    Some("Artifactory Realm" at s"$host/libs-release-local")
  }
}
version := "0.1.0"

name := "lib-reference-scala"

organization := "io.flow"

scalaVersion in ThisBuild := "2.11.8"

lazy val root = project
  .in(file("."))
  .settings(
    libraryDependencies ++= Seq(
      "org.scalatest" %% "scalatest" % "2.2.6" % Test
    )
  )

publishTo := {
  val host = "https://flow.artifactoryonline.com/flow"
  if (isSnapshot.value) {
    Some("Artifactory Realm" at s"$host/libs-snapshot-local;build.timestamp=" + new java.util.Date().getTime)
  } else {
    Some("Artifactory Realm" at s"$host/libs-release-local")
  }
}
version := "0.1.1"

autocommit: Update configuration to record version 0.1.1


enablePlugins(ScalaJSPlugin)

organization := "com.payalabs"
name := "scalajs-react-bridge-example"
version := "0.1.1-SNAPSHOT"

scalaVersion := "2.11.6"

libraryDependencies ++= {
  val scalaJsDom = "0.8.0"
  val scalaJsReact = "0.9.0"
  Seq(
    "org.scala-js" %%% "scalajs-dom" % scalaJsDom,
    "com.github.japgolly.scalajs-react" %%% "core" % scalaJsReact,
    "com.github.japgolly.scalajs-react" %%% "extra" % scalaJsReact,
    "com.payalabs" %%% "scalajs-react-bridge" % "0.1.1-SNAPSHOT"
  )
}

enablePlugins(ScalaJSPlugin)

organization := "com.payalabs"
name := "scalajs-react-bridge-example"
version := "0.1.1-SNAPSHOT"

scalaVersion := "2.11.6"

libraryDependencies ++= {
  val scalaJsDom = "0.8.0"
  val scalaJsReact = "0.9.0"
  Seq(
    "org.scala-lang" % "scala-reflect" % scalaVersion.value,
    "org.scala-js" %%% "scalajs-dom" % scalaJsDom,
    "com.github.japgolly.scalajs-react" %%% "core" % scalaJsReact,
    "com.github.japgolly.scalajs-react" %%% "extra" % scalaJsReact,
    "com.payalabs" %%% "scalajs-react-bridge" % "0.1.1-SNAPSHOT"
  )
}

Revert "Remove unneeded scala-reflect dependency"

This reverts commit cb5da4a6d1704b8a120d8db1d36a9aabe9a36bda.


import play.PlayImport.PlayKeys._

name := "lib-play"

organization := "io.flow"

scalaVersion in ThisBuild := "2.11.7"

crossScalaVersions := Seq("2.11.7")

version := "0.0.18"

lazy val root = project
  .in(file("."))
  .enablePlugins(PlayScala)
  .settings(
    libraryDependencies ++= Seq(
      ws,
      "org.scalatest" %% "scalatest" % "2.2.6" % "test"
    )
)

publishTo := {
  val host = "https://flow.artifactoryonline.com/flow"
  if (isSnapshot.value) {
    Some("Artifactory Realm" at s"$host/libs-snapshot-local;build.timestamp=" + new java.util.Date().getTime)
  } else {
    Some("Artifactory Realm" at s"$host/libs-release-local")
  }
}

import play.PlayImport.PlayKeys._

name := "lib-play"

organization := "io.flow"

scalaVersion in ThisBuild := "2.11.7"

crossScalaVersions := Seq("2.11.7")

version := "0.0.19"

lazy val root = project
  .in(file("."))
  .enablePlugins(PlayScala)
  .settings(
    libraryDependencies ++= Seq(
      ws,
      "org.scalatest" %% "scalatest" % "2.2.6" % "test"
    )
)

publishTo := {
  val host = "https://flow.artifactoryonline.com/flow"
  if (isSnapshot.value) {
    Some("Artifactory Realm" at s"$host/libs-snapshot-local;build.timestamp=" + new java.util.Date().getTime)
  } else {
    Some("Artifactory Realm" at s"$host/libs-release-local")
  }
}

Increment version from 0.0.18 => 0.0.19

Increment version from 0.0.18 => 0.0.19


name := "funalgebra"

scalaVersion := "2.10.3"

val scalazVersion = "7.0.5"

libraryDependencies ++= Seq(
  "org.scalaz" %% "scalaz-core" % scalazVersion,
  "org.scalaz" %% "scalaz-effect" % scalazVersion,
  "org.scalaz" %% "scalaz-typelevel" % scalazVersion,
  "org.scalaz" %% "scalaz-xml" % scalazVersion,
  "org.scalaz" %% "scalaz-scalacheck-binding" % scalazVersion % "test"
)

scalacOptions += "-feature"

initialCommands in console := "import scalaz._, Scalaz._"

name := "funalgebra"

scalaVersion := "2.10.3"

val scalazVersion = "7.0.5"

libraryDependencies ++= Seq(
  "org.scalaz" %% "scalaz-core" % scalazVersion,
  "org.scalaz" %% "scalaz-effect" % scalazVersion,
  "org.scalaz" %% "scalaz-concurrent" % scalazVersion,
  "org.scalaz" %% "scalaz-typelevel" % scalazVersion,
  "org.scalaz" %% "scalaz-xml" % scalazVersion,
  "org.scalaz" %% "scalaz-scalacheck-binding" % scalazVersion % "test"
)

scalacOptions += "-feature"

initialCommands in console := "import scalaz._, Scalaz._"

Add scalaz-concurrent as dependency for experiment examples


name := "proxy"

organization := "io.flow"

scalaVersion in ThisBuild := "2.11.8"

lazy val root = project
  .in(file("."))
  .enablePlugins(PlayScala)
  .settings(commonSettings: _*)
  .settings(
    libraryDependencies ++= Seq(
      filters,
      ws,
      "com.jason-goodwin" %% "authentikat-jwt" % "0.4.1",
      "org.yaml" % "snakeyaml" % "1.17",
      "org.scalatestplus.play" %% "scalatestplus-play" % "1.5.0" % "test"
    )
  )

val credsToUse = Option(System.getenv("ARTIFACTORY_USERNAME")) match {
  case None => Credentials(Path.userHome / ".ivy2" / ".artifactory")
  case _ => Credentials("Artifactory Realm","flow.artifactoryonline.com",System.getenv("ARTIFACTORY_USERNAME"),System.getenv("ARTIFACTORY_PASSWORD"))
}

lazy val commonSettings: Seq[Setting[_]] = Seq(
  name <<= name("proxy-" + _),
  scalacOptions += "-feature",
  sources in (Compile,doc) := Seq.empty,
  publishArtifact in (Compile, packageDoc) := false,
  credentials += credsToUse,
  resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases",
  resolvers += "Artifactory" at "https://flow.artifactoryonline.com/flow/libs-release/"
)
version := "0.0.8"

name := "proxy"

organization := "io.flow"

scalaVersion in ThisBuild := "2.11.8"

lazy val root = project
  .in(file("."))
  .enablePlugins(PlayScala)
  .settings(commonSettings: _*)
  .settings(
    libraryDependencies ++= Seq(
      filters,
      ws,
      "com.jason-goodwin" %% "authentikat-jwt" % "0.4.1",
      "org.yaml" % "snakeyaml" % "1.17",
      "org.scalatestplus.play" %% "scalatestplus-play" % "1.5.0" % "test"
    )
  )

val credsToUse = Option(System.getenv("ARTIFACTORY_USERNAME")) match {
  case None => Credentials(Path.userHome / ".ivy2" / ".artifactory")
  case _ => Credentials("Artifactory Realm","flow.artifactoryonline.com",System.getenv("ARTIFACTORY_USERNAME"),System.getenv("ARTIFACTORY_PASSWORD"))
}

lazy val commonSettings: Seq[Setting[_]] = Seq(
  name <<= name("proxy-" + _),
  scalacOptions += "-feature",
  sources in (Compile,doc) := Seq.empty,
  publishArtifact in (Compile, packageDoc) := false,
  credentials += credsToUse,
  resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases",
  resolvers += "Artifactory" at "https://flow.artifactoryonline.com/flow/libs-release/"
)
version := "0.0.9"

autocommit: Update configuration to record version 0.0.9


import com.github.retronym.SbtOneJar._

oneJarSettings

name := "api-lint"

organization := "io.flow"

scalaVersion in ThisBuild := "2.11.7"

version := "0.0.6"

exportJars := true

lazy val root = project
  .in(file("."))
  .settings(
    libraryDependencies ++= Seq(
      "com.typesafe.play" %% "play-json" % "2.4.6",
      "com.ning" % "async-http-client" % "1.9.31",
      "org.scalatest" %% "scalatest" % "2.2.6" % Test
    )
  )

publishTo := {
  val host = "https://flow.artifactoryonline.com/flow"
  if (isSnapshot.value) {
    Some("Artifactory Realm" at s"$host/libs-snapshot-local;build.timestamp=" + new java.util.Date().getTime)
  } else {
    Some("Artifactory Realm" at s"$host/libs-release-local")
  }
}

import com.github.retronym.SbtOneJar._

oneJarSettings

name := "api-lint"

organization := "io.flow"

scalaVersion in ThisBuild := "2.11.7"

version := "0.0.7"

exportJars := true

lazy val root = project
  .in(file("."))
  .settings(
    libraryDependencies ++= Seq(
      "com.typesafe.play" %% "play-json" % "2.4.6",
      "com.ning" % "async-http-client" % "1.9.31",
      "org.scalatest" %% "scalatest" % "2.2.6" % Test
    )
  )

publishTo := {
  val host = "https://flow.artifactoryonline.com/flow"
  if (isSnapshot.value) {
    Some("Artifactory Realm" at s"$host/libs-snapshot-local;build.timestamp=" + new java.util.Date().getTime)
  } else {
    Some("Artifactory Realm" at s"$host/libs-release-local")
  }
}

Increment version from 0.0.6 => 0.0.7

Increment version from 0.0.6 => 0.0.7


name := "Shnappy"

organization := "com.roundeights"

version := "0.1"

scalaVersion := "2.10.1"

// Support for the web plugin
seq(webSettings :_*)

// append -deprecation to the options passed to the Scala compiler
scalacOptions ++= Seq("-deprecation", "-feature")

// Application dependencies
libraryDependencies ++= Seq(
    "com.roundeights" %% "attempt" % "0.1",
    "com.roundeights" %% "scalon" % "0.1",
    "com.roundeights" %% "foldout" % "0.1",
    "com.roundeights" %% "skene" % "0.1",
    "com.roundeights" %% "vfunk" % "0.1",
    "com.roundeights" %% "hasher" % "1.0.0",
    "org.pegdown" % "pegdown" % "1.3.0",
    "com.github.jknack" % "handlebars" % "1.0.0",
    "org.slf4j" % "slf4j-simple" % "1.7.2",
    "org.apache.commons" % "commons-io" % "1.3.2",
    "org.eclipse.jetty" % "jetty-webapp" % "8.0.1.v20110908" % "container",
    "javax.servlet" % "javax.servlet-api" % "3.0.1" % "provided"
)

name := "Shnappy"

organization := "com.roundeights"

version := "0.1"

scalaVersion := "2.10.1"

// Support for the web plugin
seq(webSettings :_*)

// append -deprecation to the options passed to the Scala compiler
scalacOptions ++= Seq("-deprecation", "-feature")

// Application dependencies
libraryDependencies ++= Seq(
    "com.roundeights" %% "attempt" % "0.1",
    "com.roundeights" %% "scalon" % "0.1",
    "com.roundeights" %% "foldout" % "0.1",
    "com.roundeights" %% "skene" % "0.1",
    "com.roundeights" %% "vfunk" % "0.1",
    "com.roundeights" %% "hasher" % "1.0.0",
    "org.pegdown" % "pegdown" % "1.3.0",
    "com.github.jknack" % "handlebars" % "1.0.0",
    "org.slf4j" % "slf4j-simple" % "1.7.2",
    "org.eclipse.jetty" % "jetty-webapp" % "8.0.1.v20110908" % "container",
    "javax.servlet" % "javax.servlet-api" % "3.0.1" % "provided"
)

Remove Apache Commons IO dependency

Remove Apache Commons IO dependency


package toolplugin

import scala.scalajs.js.Dynamic.{ global ⇒ jsg }
import org.scalajs.dom
import org.scalajs.jquery.jQuery
import scala.scalajs.js.JSApp

object Main extends JSApp {

  private val $ = jQuery

  override def main(): Unit = {
    import scalatags.JsDom.all._

    val par = h1("Page matches ruleset").render
    $("body").replaceAll(par)
  }
}

package toolplugin

import scala.scalajs.js
import scala.scalajs.js.Dynamic.{ global ⇒ jsg }
import org.scalajs.dom
import org.scalajs.jquery.jQuery
import scala.scalajs.js.JSApp
import org.scalajs.dom.raw.Element

object Main extends JSApp {

  implicit class Regex(sc: StringContext) {
    def r = new util.matching.Regex(sc.parts.mkString, sc.parts.tail.map(_ => "x"): _*)
  }
  implicit class AsDynamic[A](private val a: A) extends AnyVal {
    def jsg: js.Dynamic = a.asInstanceOf[js.Dynamic]
  }

  private val $ = jQuery

  override def main(): Unit = {
    import scalatags.JsDom.all._

    dom.window.location.pathname match {
      // for directories
      // case r"""/([^/]+)$username/([^/]+)$reponame/tree/([^/]+)$branch/(.+)$filename""" ⇒

      // for files
      case r"""/([^/]+)$username/([^/]+)$reponame/blob/([^/]+)$branch/(.+)$filename""" ⇒
        println(s"username: $username")
        println(s"reponame: $reponame")
        println(s"branch: $branch")
        println(s"filename: $filename")

        val h = dom.document.getElementsByClassName("highlight")(0)
        val tbody = h.asInstanceOf[Element].children(0)
        val trs = tbody.children
        val lines = for (i ← 0 until trs.length) yield {
          val code = trs(i).getElementsByClassName("blob-code")(0)
          val text = code.jsg.innerText
          (i+1) → text
        }
        lines foreach println

      case path ⇒
        println(s"path `$path` didn't match pattern")
    }
    val par = h1(s"Page matches ruleset").render
    $("body").prepend(par)

  }
}

Print out name and content of file hosted on Github


package org.http4s
package server

import cats.Monoid
import fs2.Task

package object syntax {

  @deprecated("Import `scalaz.syntax.semigroup._` and use `service1 |+| service2` instead", "0.16")
  final implicit class ServiceOps[A, B](val service: Service[A, B])(implicit B: Monoid[Task[B]]) {
    def ||    (fallback: Service[A, B]) = orElse(fallback)
    def orElse(fallback: Service[A, B]) = Service.withFallback(fallback)(service)
  }

}

package org.http4s
package server

import cats.Monoid
import fs2.Task

package object syntax {

  @deprecated("Import `cats.syntax.semigroup._` and use `service1 |+| service2` instead", "0.16")
  final implicit class ServiceOps[A, B](val service: Service[A, B])(implicit B: Monoid[Task[B]]) {
    def ||    (fallback: Service[A, B]) = orElse(fallback)
    def orElse(fallback: Service[A, B]) = Service.withFallback(fallback)(service)
  }

}

Fix deprecation warning to refer to cats instead of scalaz


package org.talkingpuffin

import javax.swing.{UIManager, JFrame}
import mac.MacInit
import twitter.{Credentials, CredentialsRepository, AuthenticatedSession}
import ui.{TopFrame}

/**
 * TalkingPuffin main object
 */
object Main {
  val title = "TalkingPuffin"

  def main(args: Array[String]): Unit = {
    MacInit init Main.title
    UIManager setLookAndFeel UIManager.getSystemLookAndFeelClassName
    JFrame setDefaultLookAndFeelDecorated true
    if ( 0 == CredentialsRepository.getAll.size )
      launchNewSession()
    else
      launchAllSessions
  }

  def launchNewSession(credentials: Option[Credentials] = None) =
      new TopFrame(AuthenticatedSession.logIn(credentials))

  private def launchAllSessions = CredentialsRepository.getAll.foreach(cred =>
      launchNewSession(Some(cred)))
}

package org.talkingpuffin

import javax.swing.{UIManager, JFrame}
import mac.MacInit
import twitter.{Credentials, CredentialsRepository, AuthenticatedSession}
import ui.{TopFrame}

/**
 * TalkingPuffin main object
 */
object Main {
  val title = "TalkingPuffin"

  def main(args: Array[String]): Unit = {
    MacInit init Main.title
    UIManager setLookAndFeel UIManager.getSystemLookAndFeelClassName
    JFrame setDefaultLookAndFeelDecorated true
    launchAllSessions
  }

  def launchNewSession(credentials: Option[Credentials] = None) = new TopFrame(AuthenticatedSession.logIn(credentials))

  private def launchAllSessions = {
    val credentials = CredentialsRepository.getAll
    if (credentials.isEmpty)
      launchNewSession(None)
    else
      credentials.foreach(cred => launchNewSession(Some(cred)))
  }
}

Rework Leif’s essential fix slightly to avoid calling CredentialsRepository.getAll twice.


package org.thp.cortex.controllers

import javax.inject.{ Inject, Singleton }

import scala.concurrent.{ ExecutionContext, Future }

import play.api.mvc._

import org.thp.cortex.services.UserSrv

import org.elastic4play.controllers.{ Authenticated, Fields, FieldsBodyParser, Renderer }
import org.elastic4play.database.DBIndex
import org.elastic4play.services.AuthSrv
import org.elastic4play.{ MissingAttributeError, Timed }

@Singleton
class AuthenticationCtrl @Inject() (
    authSrv: AuthSrv,
    userSrv: UserSrv,
    authenticated: Authenticated,
    dbIndex: DBIndex,
    renderer: Renderer,
    components: ControllerComponents,
    fieldsBodyParser: FieldsBodyParser,
    implicit val ec: ExecutionContext) extends AbstractController(components) {

  @Timed
  def login: Action[Fields] = Action.async(fieldsBodyParser) { implicit request ⇒
    dbIndex.getIndexStatus.flatMap {
      case false ⇒ Future.successful(Results.Status(520))
      case _ ⇒
        for {
          user ← request.body.getString("user").fold[Future[String]](Future.failed(MissingAttributeError("user")))(Future.successful)
          password ← request.body.getString("password").fold[Future[String]](Future.failed(MissingAttributeError("password")))(Future.successful)
          authContext ← authSrv.authenticate(user, password)
        } yield authenticated.setSessingUser(Ok, authContext)
    }
  }

  @Timed
  def logout = Action {
    Ok.withNewSession
  }
}

package org.thp.cortex.controllers

import javax.inject.{ Inject, Singleton }

import scala.concurrent.{ ExecutionContext, Future }

import play.api.mvc._

import org.thp.cortex.services.UserSrv

import org.elastic4play.controllers.{ Authenticated, Fields, FieldsBodyParser, Renderer }
import org.elastic4play.database.DBIndex
import org.elastic4play.services.AuthSrv
import org.elastic4play.{ MissingAttributeError, Timed }

@Singleton
class AuthenticationCtrl @Inject() (
    authSrv: AuthSrv,
    userSrv: UserSrv,
    authenticated: Authenticated,
    dbIndex: DBIndex,
    renderer: Renderer,
    components: ControllerComponents,
    fieldsBodyParser: FieldsBodyParser,
    implicit val ec: ExecutionContext) extends AbstractController(components) {

  @Timed
  def login: Action[Fields] = Action.async(fieldsBodyParser) { implicit request ⇒
    dbIndex.getIndexStatus.flatMap {
      case false ⇒ Future.successful(Results.Status(520))
      case _ ⇒
        for {
          user ← request.body.getString("user").fold[Future[String]](Future.failed(MissingAttributeError("user")))(Future.successful)
          password ← request.body.getString("password").fold[Future[String]](Future.failed(MissingAttributeError("password")))(Future.successful)
          authContext ← authSrv.authenticate(user, password)
        } yield authenticated.setSessingUser(Ok(authContext), authContext)
    }
  }

  @Timed
  def logout = Action {
    Ok.withNewSession
  }
}

Add user information in login output

Add user information in login output


package scala.collection.immutable

import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.Test
import org.junit.Assert._

@RunWith(classOf[JUnit4])
class PagedSeqTest {
  // should not NPE, and should equal the given Seq
  @Test
  def test_SI6615(): Unit = {
    assertEquals(Seq('a'), PagedSeq.fromStrings(List.fill(5000)("a")).slice(4096, 4097))
  }

  // should not NPE, and should be empty
  @Test
  def test_SI9480(): Unit = {
    assertEquals(Seq(), PagedSeq.fromStrings(List("a")).slice(1))
  }

  // Slices shouldn't read outside where they belong
  @Test
  def test_SI6519 {
    var readAttempt = 0
    val sideEffectingIterator = new Iterator[Int] {
      def hasNext = readAttempt < 65536
      def next = { readAttempt += 1; readAttempt }
    }
    val s = PagedSeq.fromIterator(sideEffectingIterator).slice(0,2).mkString
    assertEquals(s, "12")
    assert(readAttempt <= 4096)
  }
}

package scala.collection.immutable

import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.{Ignore, Test}
import org.junit.Assert._

@RunWith(classOf[JUnit4])
class PagedSeqTest {
  // should not NPE, and should equal the given Seq
  @Test
  @Ignore("This tests a non-stack safe method in a deprecated class that requires ~1.5M stack, disabling")
  def test_SI6615(): Unit = {
    assertEquals(Seq('a'), PagedSeq.fromStrings(List.fill(5000)("a")).slice(4096, 4097))
  }

  // should not NPE, and should be empty
  @Test
  def test_SI9480(): Unit = {
    assertEquals(Seq(), PagedSeq.fromStrings(List("a")).slice(1))
  }

  // Slices shouldn't read outside where they belong
  @Test
  def test_SI6519 {
    var readAttempt = 0
    val sideEffectingIterator = new Iterator[Int] {
      def hasNext = readAttempt < 65536
      def next = { readAttempt += 1; readAttempt }
    }
    val s = PagedSeq.fromIterator(sideEffectingIterator).slice(0,2).mkString
    assertEquals(s, "12")
    assert(readAttempt <= 4096)
  }
}

[backport] Disable stack hungry test of deprecated PagedSeq

(cherry picked from commit 241fb9fe204d2974e0e1b2a60c2b71298e88f3b6)


package tests

package classModifiers

sealed abstract class B

abstract case class C(s: String)

sealed case class D(c: String)

final case class E(c: String)

open class F

implicit class Foo(i: Int)

Add test for implicit class

Add test for implicit class


package ducttape.versioner
import ducttape.util.Files
import java.io.File

class WorkflowVersionHistory(val history: Seq[WorkflowVersionInfo]) {
  lazy val prevVersion: Int = (Seq(0) ++ history.map(_.version)).max
  lazy val nextVersion: Int = prevVersion + 1
}

object WorkflowVersionHistory {
  def load(versionHistoryDir: File) = new WorkflowVersionHistory(
    Files.ls(versionHistoryDir).filter(_.isDirectory).map(dir => WorkflowVersionInfo.load(dir)))
}

package ducttape.versioner

import ducttape.util.Files
import java.io.File

class WorkflowVersionHistory(val history: Seq[WorkflowVersionInfo]) {
  lazy val prevVersion: Int = (Seq(0) ++ history.map(_.version)).max
  lazy val nextVersion: Int = prevVersion + 1
}

object WorkflowVersionHistory {
  def load(versionHistoryDir: File) = new WorkflowVersionHistory(
    Files.ls(versionHistoryDir).filter {
      _.isDirectory
    }.map { dir =>
      try {
        Some(WorkflowVersionInfo.load(dir))
      } catch {
        case ex => {
          System.err.println("Version is corrupt or incomplete, DELETING: %s: %s".format(dir, ex.getMessage))
          val DELAY_SECS = 3
          Thread.sleep(DELAY_SECS)
          Files.deleteDir(dir)
          None
        }
      }
    }.collect {
      // only keep versions that are non-broken
      case Some(info) => info
    }
  )
}

Fix bug when previous run of the workflow terminated very early, resulting in an invalide entry in the workflow version history


/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package spark

import java.io._

import org.apache.hadoop.io.ObjectWritable
import org.apache.hadoop.io.Writable
import org.apache.hadoop.mapred.JobConf

class SerializableWritable[T <: Writable](@transient var t: T) extends Serializable {
  def value = t
  override def toString = t.toString

  private def writeObject(out: ObjectOutputStream) {
    out.defaultWriteObject()
    new ObjectWritable(t).write(out)
  }

  private def readObject(in: ObjectInputStream) {
    in.defaultReadObject()
    val ow = new ObjectWritable()
    ow.setConf(new JobConf())
    ow.readFields(in)
    t = ow.get().asInstanceOf[T]
  }
}

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package spark

import java.io._

import org.apache.hadoop.io.ObjectWritable
import org.apache.hadoop.io.Writable
import org.apache.hadoop.conf.Configuration

class SerializableWritable[T <: Writable](@transient var t: T) extends Serializable {
  def value = t
  override def toString = t.toString

  private def writeObject(out: ObjectOutputStream) {
    out.defaultWriteObject()
    new ObjectWritable(t).write(out)
  }

  private def readObject(in: ObjectInputStream) {
    in.defaultReadObject()
    val ow = new ObjectWritable()
    ow.setConf(new Configuration())
    ow.readFields(in)
    t = ow.get().asInstanceOf[T]
  }
}

Use new Configuration() instead of new JobConf() for ObjectWritable.

JobConf's constructor loads default config files in some verisons of
Hadoop, which is quite slow, and we only need the Configuration object
to pass the correct ClassLoader.


package io.prediction.engines.stock

import io.prediction.{ Algorithm, BaseParams }

import scala.util.Random

//class EmptyModel() extends BaseModel {}

class RandomAlgoParams(
  val seed: Long = 709394,
  val scale: Double = 0.01,
  val drift: Double = 0.0) extends BaseParams {}

class RandomAlgorithm
    extends Algorithm[TrainingData, Feature, Target, Null, RandomAlgoParams] {
  var _scale: Double = 0.0
  var _drift: Double = 0.0
  var _seed: Long = 0
  // Notice that parallization may mess-up reproduceability of a fixed seed.
  @transient lazy val _random: Random = new Random(_seed)
  override def init(algoParams: RandomAlgoParams): Unit = {
    println("RandomAlgorithm.init")
    _seed = algoParams.seed
    _scale = algoParams.scale
    _drift = algoParams.drift
  }

  def train(trainingData: TrainingData): Null = {
    println("RandomAlgorithm.train")
    null
  }

  def predict(model: Null, feature: Feature): Target = {
    println(s"RandomAlgorithm.predict ${_seed} ${_scale} ${_drift}")
    val tickers = feature.data.colIx.toVec.contents
    //val tickers = feature.boxedData.get.colIx.toVec.contents
    val prediction = tickers.map {
      ticker => (ticker, _drift + _random.nextGaussian() * _scale)
    }.toMap
    new Target(data = prediction)
  }

  override def toString() = "RandomAlgorithm"
}

package io.prediction.engines.stock

import io.prediction.{ Algorithm, BaseParams }

import scala.util.Random

//class EmptyModel() extends BaseModel {}

class RandomAlgoParams(
  val seed: Long = 709394,
  val scale: Double = 0.01,
  val drift: Double = 0.0) extends BaseParams {}

class RandomAlgorithm
    extends Algorithm[TrainingData, Feature, Target, Unit, RandomAlgoParams] {
  var _scale: Double = 0.0
  var _drift: Double = 0.0
  var _seed: Long = 0
  // Notice that parallization may mess-up reproduceability of a fixed seed.
  @transient lazy val _random: Random = new Random(_seed)
  override def init(algoParams: RandomAlgoParams): Unit = {
    println("RandomAlgorithm.init")
    _seed = algoParams.seed
    _scale = algoParams.scale
    _drift = algoParams.drift
  }

  def train(trainingData: TrainingData): Unit = {
    println("RandomAlgorithm.train")
    null
  }

  def predict(model: Unit, feature: Feature): Target = {
    println(s"RandomAlgorithm.predict ${_seed} ${_scale} ${_drift}")
    val tickers = feature.data.colIx.toVec.contents
    //val tickers = feature.boxedData.get.colIx.toVec.contents
    val prediction = tickers.map {
      ticker => (ticker, _drift + _random.nextGaussian() * _scale)
    }.toMap
    new Target(data = prediction)
  }

  override def toString() = "RandomAlgorithm"
}

Fix Spark error with collecting RDD[Null]


package org.continuumio.bokeh

sealed abstract class Marker extends BaseGlyph with FillProps with LineProps {
    object x extends DataSpec[Double]
    object y extends DataSpec[Double]
    object size extends DataSpec[Double] {
        // override val units = Units.Screen
        // override val default = 4
        // override val min_value = 0
    }
}

class Circle extends Marker {
    // object radius extends DataSpec[Double](units="data", default=4, min_value=0)
}

class Square extends Marker {
    object angle extends DataSpec[Double]
}

class Triangle extends Marker

class Cross extends Marker

class Xmarker extends Marker {
    override val viewModel: String = "x"
}

class Diamond extends Marker

class InvertedTriangle extends Marker

class SquareX extends Marker

class Asterisk extends Marker

class DiamondCross extends Marker

class CircleCross extends Marker

class HexStar extends Marker

class SquareCross extends Marker

class CircleX extends Marker

package org.continuumio.bokeh

sealed abstract class Marker extends BaseGlyph with FillProps with LineProps {
    object x extends DataSpec[Double]
    object y extends DataSpec[Double]
    object size extends DataSpec[Double] { // with Radius
        units = Some(Units.Screen)
        default = Some(4.0)
    }
}

class Circle extends Marker {
    object radius extends DataSpec[Double] { // with Radius
        units = Some(Units.Data)
        default = Some(4.0)
    }
}

class Square extends Marker {
    object angle extends DataSpec[Double]
}

class Triangle extends Marker

class Cross extends Marker

class Xmarker extends Marker {
    override val viewModel: String = "x"
}

class Diamond extends Marker

class InvertedTriangle extends Marker

class SquareX extends Marker

class Asterisk extends Marker

class DiamondCross extends Marker

class CircleCross extends Marker

class HexStar extends Marker

class SquareCross extends Marker

class CircleX extends Marker

Add Circle.radius field and configure Marker.size


package coursier.util

import utest._

object TreeTests extends TestSuite {
  case class Node(label: String, children: Node*)

  val roots = Array(
    Node("p1",
      Node("c1"),
      Node("c2")),
    Node("p2",
      Node("c3"),
      Node("c4"))
  )

  val tests = TestSuite {
    'apply {
      val str = Tree[Node](roots)(_.children, _.label)
      assert(str == """├─ p1
        ││  ├─ c1
        ││  └─ c2
        │└─ p2
        │   ├─ c3
        │   └─ c4""".stripMargin)
    }
  }
}

package coursier.util

import utest._

object TreeTests extends TestSuite {
  case class Node(label: String, children: Node*)

  val roots = Array(
    Node("p1",
      Node("c1"),
      Node("c2")),
    Node("p2",
      Node("c3"),
      Node("c4"))
  )

  val tests = TestSuite {
    'apply {
      val str = Tree[Node](roots)(_.children, _.label)
      assert(str == """├─ p1
        #│  ├─ c1
        #│  └─ c2
        #└─ p2
        #   ├─ c3
        #   └─ c4""".stripMargin('#'))
    }
  }
}

Tweak margin character in test

Tweak margin character in test


package spark.examples

import spark._
import spark.rdd.NewHadoopRDD
import org.apache.hadoop.hbase.{HBaseConfiguration, HTableDescriptor}
import org.apache.hadoop.hbase.client.HBaseAdmin
import org.apache.hadoop.hbase.mapreduce.TableInputFormat

object HBaseTest {
  def main(args: Array[String]) {
    val sc = new SparkContext(args(0), "HBaseTest",
      System.getenv("SPARK_HOME"), Seq(System.getenv("SPARK_EXAMPLES_JAR")))

    val conf = HBaseConfiguration.create()
    conf.set(TableInputFormat.INPUT_TABLE, args(1))

    // Initialize hBase table if necessary
    val admin = new HBaseAdmin(conf)
    if(!admin.isTableAvailable(args(1))) {
      val tableDesc = new HTableDescriptor(args(1))
      admin.createTable(tableDesc)
    }

    val hBaseRDD = new NewHadoopRDD(sc, classOf[TableInputFormat],
                        classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable],
                        classOf[org.apache.hadoop.hbase.client.Result], conf)

    hBaseRDD.count()

    System.exit(0)
  }
}

package spark.examples

import spark._
import spark.rdd.NewHadoopRDD
import org.apache.hadoop.hbase.{HBaseConfiguration, HTableDescriptor}
import org.apache.hadoop.hbase.client.HBaseAdmin
import org.apache.hadoop.hbase.mapreduce.TableInputFormat

object HBaseTest {
  def main(args: Array[String]) {
    val sc = new SparkContext(args(0), "HBaseTest",
      System.getenv("SPARK_HOME"), Seq(System.getenv("SPARK_EXAMPLES_JAR")))

    val conf = HBaseConfiguration.create()

    // Other options for configuring scan behavior are available. More information available at
    // http://hbase.apache.org/apidocs/org/apache/hadoop/hbase/mapreduce/TableInputFormat.html
    conf.set(TableInputFormat.INPUT_TABLE, args(1))

    // Initialize hBase table if necessary
    val admin = new HBaseAdmin(conf)
    if(!admin.isTableAvailable(args(1))) {
      val tableDesc = new HTableDescriptor(args(1))
      admin.createTable(tableDesc)
    }

    val hBaseRDD = new NewHadoopRDD(sc, classOf[TableInputFormat],
      classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable],
      classOf[org.apache.hadoop.hbase.client.Result], conf)

    hBaseRDD.count()

    System.exit(0)
  }
}

Fix indents and mention other configuration options


import sbt._
import Keys._
import org.portablescala.sbtplatformdeps.PlatformDepsPlugin.autoImport._

object Dependencies {
  lazy val jaxbApi = "javax.xml.bind" % "jaxb-api" % "2.3.1" % "test"

  lazy val jodaTime = Seq(
    "joda-time" % "joda-time" % "2.10.13",
    "org.joda" % "joda-convert" % "2.2.2"
  )
  lazy val jackson = Seq(
    "com.fasterxml.jackson.core" % "jackson-databind" % "2.13.1"
  )
  lazy val scalaz_core = Def.setting(
    "org.scalaz" %%% "scalaz-core" % "7.3.5" cross CrossVersion.for3Use2_13
  )
  lazy val paranamer = "com.thoughtworks.paranamer" % "paranamer" % "2.8"
  lazy val scalatest = Def.setting(
    Seq("org.scalatest" %%% "scalatest-wordspec" % "3.2.10" % "test")
  )
  lazy val scalatestScalacheck = Def.setting(
    Seq("org.scalatestplus" %%% "scalacheck-1-15" % "3.2.10.0" % "test")
  )

  lazy val scalaXml = Def.setting {
    "org.scala-lang.modules" %%% "scala-xml" % "2.0.1"
  }
}

import sbt._
import Keys._
import org.portablescala.sbtplatformdeps.PlatformDepsPlugin.autoImport._

object Dependencies {
  lazy val jaxbApi = "javax.xml.bind" % "jaxb-api" % "2.3.1" % "test"

  lazy val jodaTime = Seq(
    "joda-time" % "joda-time" % "2.10.13",
    "org.joda" % "joda-convert" % "2.2.2"
  )
  lazy val jackson = Seq(
    "com.fasterxml.jackson.core" % "jackson-databind" % "2.13.1"
  )
  lazy val scalaz_core = Def.setting(
    "org.scalaz" %%% "scalaz-core" % "7.3.6" cross CrossVersion.for3Use2_13
  )
  lazy val paranamer = "com.thoughtworks.paranamer" % "paranamer" % "2.8"
  lazy val scalatest = Def.setting(
    Seq("org.scalatest" %%% "scalatest-wordspec" % "3.2.10" % "test")
  )
  lazy val scalatestScalacheck = Def.setting(
    Seq("org.scalatestplus" %%% "scalacheck-1-15" % "3.2.10.0" % "test")
  )

  lazy val scalaXml = Def.setting {
    "org.scala-lang.modules" %%% "scala-xml" % "2.0.1"
  }
}

Update scalaz-core to 7.3.6 in master

Update scalaz-core to 7.3.6 in master


package net.fehmicansaglam.tepkin.api

import net.fehmicansaglam.tepkin

class MongoDatabase(proxy: tepkin.MongoDatabase) {

  def collection(collectionName: String): MongoCollection = {
    new MongoCollection(proxy.apply(collectionName))
  }

  def gridFs(): GridFs = {
    new GridFs(proxy.gridFs())
  }

  def gridFs(prefix: String): GridFs = {
    new GridFs(proxy.gridFs(prefix))
  }
}

package net.fehmicansaglam.tepkin.api

import java.util.concurrent.CompletableFuture
import java.util.{List => JavaList}

import akka.actor.ActorRef
import akka.stream.javadsl.Source
import net.fehmicansaglam.bson.BsonDocument
import net.fehmicansaglam.tepkin
import net.fehmicansaglam.tepkin.api.JavaConverters._

import scala.concurrent.ExecutionContext
import scala.concurrent.duration.FiniteDuration

class MongoDatabase(proxy: tepkin.MongoDatabase) {

  def collection(collectionName: String): MongoCollection = {
    new MongoCollection(proxy.apply(collectionName))
  }

  def gridFs(): GridFs = {
    new GridFs(proxy.gridFs())
  }

  def gridFs(prefix: String): GridFs = {
    new GridFs(proxy.gridFs(prefix))
  }

  def listCollections(ec: ExecutionContext,
                      timeout: FiniteDuration): CompletableFuture[Source[JavaList[BsonDocument], ActorRef]] = {
    toCompletableFuture {
      import scala.collection.JavaConverters._
      proxy.listCollections()(ec, timeout).map(source => Source.adapt(source.map(_.asJava)))(ec)
    }(ec)
  }

  def listCollections(filter: BsonDocument,
                      ec: ExecutionContext,
                      timeout: FiniteDuration): CompletableFuture[Source[JavaList[BsonDocument], ActorRef]] = {
    toCompletableFuture {
      import scala.collection.JavaConverters._
      proxy.listCollections(Some(filter))(ec, timeout).map(source => Source.adapt(source.map(_.asJava)))(ec)
    }(ec)
  }
}

Add listCollections to Java API

Add listCollections to Java API


package com.olegych.scastie
package sbt

import akka.actor.Actor
import com.olegych.scastie.api.{FormatRequest, FormatResponse, ScalaTarget}
import org.scalafmt.config.ScalafmtRunner.Dialect
import org.scalafmt.config.{ScalafmtConfig, ScalafmtRunner}
import org.scalafmt.{Formatted, Scalafmt}
import org.slf4j.LoggerFactory

class FormatActor() extends Actor {
  private val log = LoggerFactory.getLogger(getClass)

  private def format(code: String, isWorksheetMode: Boolean, scalaTarget: ScalaTarget): Either[String, String] = {
    log.info(s"format (isWorksheetMode: $isWorksheetMode)")
    log.info(code)

    val config = scalaTarget match {
      case scalaTarget if isWorksheetMode && scalaTarget.hasWorksheetMode =>
        ScalafmtConfig.default.copy(runner = ScalafmtRunner.sbt)
      case dotty: ScalaTarget.Scala3 =>
        ScalafmtConfig.default.withDialect(scala.meta.dialects.Scala3)
      case _ =>
        ScalafmtConfig.default
    }

    Scalafmt.format(code, style = config) match {
      case Formatted.Success(formattedCode) => Right(formattedCode)
      case Formatted.Failure(failure) =>
        Left(failure.toString)
    }
  }

  override def receive: Receive = {
    case FormatRequest(code, isWorksheetMode, scalaTarget) =>
      sender() ! FormatResponse(format(code, isWorksheetMode, scalaTarget))
  }
}

package com.olegych.scastie
package sbt

import akka.actor.Actor
import com.olegych.scastie.api.{FormatRequest, FormatResponse, ScalaTarget}
import org.scalafmt.config.ScalafmtRunner.Dialect
import org.scalafmt.config.{ScalafmtConfig, ScalafmtRunner}
import org.scalafmt.{Formatted, Scalafmt}
import org.slf4j.LoggerFactory

class FormatActor() extends Actor {
  private val log = LoggerFactory.getLogger(getClass)

  private def format(code: String, isWorksheetMode: Boolean, scalaTarget: ScalaTarget): Either[String, String] = {
    log.info(s"format (isWorksheetMode: $isWorksheetMode)")
    log.info(code)

    val config: ScalafmtConfig = {
      val withDialect =  scalaTarget match {
        case dotty: ScalaTarget.Scala3 => ScalafmtConfig.default.withDialect(scala.meta.dialects.Scala3)
        case _ =>  ScalafmtConfig.default
      }

      if (isWorksheetMode && scalaTarget.hasWorksheetMode)
        withDialect.copy(runner = ScalafmtRunner.sbt.copy(dialect=withDialect.runner.dialect))
      else withDialect
    }

    Scalafmt.format(code, style = config) match {
      case Formatted.Success(formattedCode) => Right(formattedCode)
      case Formatted.Failure(failure) => Left(failure.toString)
    }
  }

  override def receive: Receive = {
    case FormatRequest(code, isWorksheetMode, scalaTarget) =>
      sender() ! FormatResponse(format(code, isWorksheetMode, scalaTarget))
  }
}

Support formatting of Scala3 worksheet

Support formatting of Scala3 worksheet


package dotty.tools.scaladoc

import org.scalajs.dom._
import org.scalajs.dom.ext._

class CodeSnippets:
  def toggleHide(e: html.Element | html.Document) = e.querySelectorAll("code span.hideable").foreach {
    case e: html.Element if e.style.getPropertyValue("display").isEmpty => e.style.setProperty("display", "none")
    case e: html.Element => e.style.removeProperty("display")
  }

  toggleHide(document)

  document.querySelectorAll("pre").foreach {
    case e: html.Element if e.querySelectorAll("code span.hideable").nonEmpty =>
      val a = document.createElement("a")
      a.addEventListener("click", { (_: MouseEvent) =>
        if(a.classList.contains("hide-snippet-comments-button")) {
          a.classList.remove("hide-snippet-comments-button")
        } else {
          a.classList.add("hide-snippet-comments-button")
        }
        toggleHide(e)
      })
      a.classList.add("snippet-comment-button")
      e.insertBefore(a, e.firstChild)
  }

package dotty.tools.scaladoc

import org.scalajs.dom._
import org.scalajs.dom.ext._

class CodeSnippets:
  def toggleHide(e: html.Element | html.Document) = e.querySelectorAll("code span.hideable").foreach {
    case e: html.Element if e.style.getPropertyValue("display").isEmpty => e.style.setProperty("display", "none")
    case e: html.Element => e.style.removeProperty("display")
  }

  toggleHide(document)

  document.querySelectorAll("pre").foreach {
    case e: html.Element if e.querySelectorAll("code span.hideable").nonEmpty =>
      val a = document.createElement("a")
      a.addEventListener("click", { (_: MouseEvent) =>
        if(a.classList.contains("hide-snippet-comments-button")) {
          a.classList.remove("hide-snippet-comments-button")
        } else {
          a.classList.add("hide-snippet-comments-button")
        }
        toggleHide(e)
      })
      a.classList.add("snippet-comment-button")
      e.insertBefore(a, e.firstChild)
    case e => // skip
  }

Fix MatchError in Scaladoc Scalajs script


package la.dp.ingestion3

import java.net.URL

import org.apache.http.client.utils.URIBuilder

/**
  * Creates a fully formed and valid URL for OAI request
  */
class OaiQueryUrlBuilder extends QueryUrlBuilder {
  /**
    * Builds an OAI request
    *
    * @param params Map of parameters needed to construct the URL
    *               OAI request verbs
    *               See https://www.openarchives.org/OAI/openarchivesprotocol.html#ProtocolMessages
    * @return URL
    */
  def buildQueryUrl(params: Map[String, String]): URL = {
    // Required properties, not sure if this is the right style
    assume(params.get("endpoint").get.nonEmpty)
    assume(params.get("verb").get.nonEmpty)
    assume(params.get("metadataPrefix").get.nonEmpty)

    val verb = params.get("verb").get
    val metadataPrefix = params.get("metadataPrefix").get
    val url = new URL(params.get("endpoint").get)

    // Build the URL
    val urlParams = new URIBuilder()
      .setScheme(url.getProtocol)
      .setHost(url.getHost)
      .setPath(url.getPath)
      .setParameter("verb", verb)

    params.get("resumptionToken") match {
      case Some(value) => urlParams.setParameter("resumptionToken", value)
      case None  => urlParams.setParameter("metadataPrefix", metadataPrefix)
    }

    urlParams.build.toURL
  }
}

trait QueryUrlBuilder {
  def buildQueryUrl (params: Map[String, String]): URL

}

package la.dp.ingestion3

import java.net.URL

import org.apache.http.client.utils.URIBuilder

/**
  * Creates a fully formed and valid URL for OAI request
  */
class OaiQueryUrlBuilder extends QueryUrlBuilder {
  /**
    * Builds an OAI request
    *
    * @param params Map of parameters needed to construct the URL
    *               OAI request verbs
    *               See https://www.openarchives.org/OAI/openarchivesprotocol.html#ProtocolMessages
    * @return URL
    */
  def buildQueryUrl(params: Map[String, String]): URL = {
    // Required properties, not sure if this is the right style
    assume(params.get("endpoint").get.nonEmpty)
    assume(params.get("verb").get.nonEmpty)
    assume(params.get("metadataPrefix").get.nonEmpty)

    val verb = params.get("verb").get
    val metadataPrefix = params.get("metadataPrefix").get
    val url = new URL(params.get("endpoint").get)

    // Build the URL
    val urlParams = new URIBuilder()
      .setScheme(url.getProtocol)
      .setHost(url.getHost)
      .setPath(url.getPath)
      .setParameter("verb", verb)

    params.get("resumptionToken") match {
      case Some(v) if v.nonEmpty => urlParams.setParameter("resumptionToken", v)
      case None => urlParams.setParameter("metadataPrefix", metadataPrefix)
    }

    urlParams.build.toURL
  }
}

trait QueryUrlBuilder {
  def buildQueryUrl (params: Map[String, String]): URL

}

Change logic for checking the presence of resumptionToken. Previous method was failing on match because if resumptionToken was not present the second .get method was called on a None object


package bloomfilter.mutable

import scala.concurrent.util.Unsafe.{instance => unsafe}

class UnsafeBitArray(numberOfBits: Long) {
  private val indices = math.ceil(numberOfBits.toDouble / 64).toLong
  private val ptr = unsafe.allocateMemory(indices)
  unsafe.setMemory(ptr, indices, 0.toByte)

  def get(index: Long): Boolean = {
    (unsafe.getLong(ptr + (index >>> 6)) & (1L << index)) != 0
  }

  def set(index: Long): Unit = {
    val offset = ptr + (index >>> 6)
    val long = unsafe.getLong(offset)
    unsafe.putLong(offset, long | (1L << index))
  }

  def getBitCount: Long = 0

  def dispose(): Unit = unsafe.freeMemory(ptr)
}

package bloomfilter.mutable

import scala.concurrent.util.Unsafe.{instance => unsafe}

class UnsafeBitArray(numberOfBits: Long) {
  private val indices = math.ceil(numberOfBits.toDouble / 64).toLong
  private val ptr = unsafe.allocateMemory(indices)
  unsafe.setMemory(ptr, indices, 0.toByte)

  def get(index: Long): Boolean = {
    (unsafe.getLong(ptr + (index >>> 6)) & (1L << index)) != 0
  }

  def set(index: Long): Unit = {
    val offset = ptr + (index >>> 6)
    val long = unsafe.getLong(offset)
    unsafe.putLong(offset, long | (1L << index))
  }

  def getBitCount: Long = {
    throw new NotImplementedError("Not implemented yet")
  }

  def dispose(): Unit = unsafe.freeMemory(ptr)
}

Throw an exception for NIE

Throw an exception for NIE


package se.gigurra.glasciia.impl

import com.badlogic.gdx.Gdx
import com.badlogic.gdx.files.FileHandle

import scala.util.Try

/**
  * Created by johan on 2016-10-02.
  */
object LoadFile {
  def apply(location: String): Option[FileHandle] = {
    Try(Gdx.files.internal(location)).filter(_.exists())
      .orElse(Try(Gdx.files.external(location)).filter(_.exists()))
      .orElse(Try(Gdx.files.local(location)).filter(_.exists()))
      .orElse(Try(Gdx.files.absolute(location)).filter(_.exists()))
      .toOption
  }
}

package se.gigurra.glasciia.impl

import com.badlogic.gdx.Gdx
import com.badlogic.gdx.files.FileHandle

import scala.util.Try

/**
  * Created by johan on 2016-10-02.
  */
object LoadFile {
  def apply(location: String): Option[FileHandle] = {
    require(Gdx.files != null, s"LibGDX not yet loaded! Need to create e.g. an LWJGLApplication before using this.")
    Try(Gdx.files.internal(location)).filter(_.exists())
      .orElse(Try(Gdx.files.external(location)).filter(_.exists()))
      .orElse(Try(Gdx.files.local(location)).filter(_.exists()))
      .orElse(Try(Gdx.files.absolute(location)).filter(_.exists()))
      .toOption
  }
}

Add explicit error when trying to use Gdx.files before LIBGdx is loaded


package sampler.maths

import sampler._

import org.scalatest.Matchers
import org.scalatest.FreeSpec
import org.scalatest.prop.GeneratorDrivenPropertyChecks
import org.scalacheck.Gen

class RangeChecksTest extends FreeSpec
    with Matchers
    with GeneratorDrivenPropertyChecks {

  "Individual probabilities" in {
    assert(0.0.isProbability)
    assert(1.0.isProbability)
    assert(!(1 + 1e-10).isProbability)
    assert(!(0 - 1e-10).isProbability)

    forAll{p: Double =>
      whenever(p >= 0 && p <= 1){
        assert(p.isProbability)
      }
    }

    forAll{p: Double =>
      whenever(p < 0 || p > 1){
        assert(!p.isProbability)
      }
    }
  }

  "Traversable of probabilities" in {
     assert(Seq(0, 0.1, 0.2, 0.3, 0.4, 0.9, 0.9999, 1).areProbabilities)
     assert(!Seq(0, 0.1, 0.2, 0.3, 1 + 1e-10, 0.9, 0.9999, 1).areProbabilities)
  }
}

package sampler.maths

import org.scalatest.{FreeSpec, Matchers}
import org.scalatest.prop.GeneratorDrivenPropertyChecks
import sampler._

class RangeChecksTest extends FreeSpec
    with Matchers
    with GeneratorDrivenPropertyChecks {

  "Individual probabilities" in {
    assert(0.0.isProbability)
    assert(1.0.isProbability)
    assert(!(1 + 1e-10).isProbability)
    assert(!(0 - 1e-10).isProbability)

    forAll{p: Double =>
      whenever(p >= 0 && p <= 1){
        assert(p.isProbability)
      }
    }

    forAll{p: Double =>
      whenever(p < 0 || p > 1){
        assert(!p.isProbability)
      }
    }
  }

  "Traversable of probabilities" in {
    assert(Seq(0.0, 0.1, 0.2, 0.3, 0.4, 0.9, 0.9999, 1.0).areProbabilities)
    assert(!Seq(0.0, 0.1, 0.2, 0.3, 1 + 1e-10, 0.9, 0.9999, 1.0).areProbabilities)
  }
}

Clean and help Intellij presentation compiler


/* From ESOP 2014, Kuwahara et al */

import stainless.lang._
import stainless.util._
import stainless.util.Random._

object IndirectIntro {
  def app(i: BigInt, h: BigInt ~> Unit, v: BigInt): Unit = {
    require(h.pre(v))
    if (i > 0) app(i - 1, h, v) else h(v)
  }

  def abs(i: BigInt): BigInt = if (i < 0) -i else i

  def f(x: BigInt)(implicit state: State): Unit =
    if (x > 0) {
      app(
        abs(Random.nextBigInt),
        PartialFunction { (x2: BigInt) => require(0 <= x2 && x2 < x); f(x2) },
        x - 1)
    } else {
      ()
    }
}

/* From ESOP 2014, Kuwahara et al */

import stainless.lang._
import stainless.util._

object IndirectIntro {
  case class RandomSequence(f: BigInt => BigInt) {
    def apply(index: BigInt): BigInt = f(index)
  }

  def app(i: BigInt, h: BigInt ~> Unit, v: BigInt): Unit = {
    require(h.pre(v))
    if (i > 0) app(i - 1, h, v) else h(v)
  }

  def abs(i: BigInt): BigInt = if (i < 0) -i else i

  def f(x: BigInt)(implicit random: RandomSequence): Unit =
    if (x > 0) {
      app(
        abs(random(x)),
        PartialFunction { (x2: BigInt) => require(0 <= x2 && x2 < x); f(x2) },
        x - 1)
    } else {
      ()
    }
}

Fix benchmark with invalid lambda effect


scalaHome := Some(file("../../build/pack"))
scalaVersion := "2.11.8"
scalacOptions += "-feature"

lazy val root = (project in file(".")).
  enablePlugins(JmhPlugin).
  settings(
    name := "test-benchmarks",
    version := "0.0.1",
	libraryDependencies += "org.openjdk.jol" % "jol-core" % "0.4"
  )

scalaHome := Some(file("../../build/pack"))
scalaVersion := "2.11.8"
scalacOptions ++= Seq("-feature", "-Yopt:l:classpath")

lazy val root = (project in file(".")).
  enablePlugins(JmhPlugin).
  settings(
    name := "test-benchmarks",
    version := "0.0.1",
	libraryDependencies += "org.openjdk.jol" % "jol-core" % "0.4"
  )

Enable full compiler optimizations in JMH benchmarking.


addSbtPlugin("com.typesafe.sbt" % "sbt-pgp" % "0.8")

addSbtPlugin("com.typesafe.sbt" % "sbt-osgi" % "0.7.0")

addSbtPlugin("com.typesafe.sbt" % "sbt-pgp" % "0.8.1")

addSbtPlugin("com.typesafe.sbt" % "sbt-osgi" % "0.7.0")

Upgrade to sbt-pgp 0.8.1 release.

Upgrade to sbt-pgp 0.8.1 release.

The 0.8 had broken dependencies (on artifacts compiled against Scala 2.9)
that caused our dbuild process to fail. The 0.8.1 release is the latest
stable release and has fixed dependencies.


addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "1.1.0")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.11.0")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.13")
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.1.2")
addSbtPlugin("com.github.sbt" % "sbt-release" % "1.1.0")
addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.2.0")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.2.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.11.0")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.7")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6")

scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature")

addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "1.1.1")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.11.0")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.13")
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.1.2")
addSbtPlugin("com.github.sbt" % "sbt-release" % "1.1.0")
addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.2.0")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.2.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.11.0")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.7")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6")

scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature")

Update sbt-mima-plugin to 1.1.1 in master


import java.util.concurrent.atomic.AtomicInteger
import sbt._, Keys._

object A extends AutoPlugin {
  object autoImport {
    lazy val foo = settingKey[String]("Foo.")
  }
  import autoImport._
  override def trigger = allRequirements

  override def buildSettings: Seq[Setting[_]] =
    (foo := s"build ${buildCount.getAndIncrement}") ::
    Nil

  override def globalSettings: Seq[Setting[_]] =
    (foo := s"global ${globalCount.getAndIncrement}") ::
    (commands += setUpScripted) ::
    Nil

  def setUpScripted = Command.command("setUpScripted") { (state0: State) =>
    Project.extract(state0).append(name := "foo", state0)
  }

  // used to ensure the build-level and global settings are only added once
  private[this] val buildCount = new AtomicInteger(0)
  private[this] val globalCount = new AtomicInteger(0)
}

import java.util.concurrent.atomic.AtomicInteger
import sbt._, Keys._

object A extends AutoPlugin {
  object autoImport {
    lazy val foo = settingKey[String]("Foo.")
  }
  import autoImport._
  override def trigger = allRequirements

  override def buildSettings: Seq[Setting[_]] =
    (foo := s"build ${buildCount.getAndIncrement}") ::
    Nil

  override def globalSettings: Seq[Setting[_]] =
    (foo := s"global ${globalCount.getAndIncrement}") ::
    (commands += setUpScripted) ::
    Nil

  def setUpScripted = Command.command("setUpScripted") { (state0: State) =>
    Project.extract(state0).appendWithoutSession(name := "foo", state0)
  }

  // used to ensure the build-level and global settings are only added once
  private[this] val buildCount = new AtomicInteger(0)
  private[this] val globalCount = new AtomicInteger(0)
}

Update call to deprecated api in scripted


package org.http4s.headers

import org.http4s.MediaRange

class LinkSpec extends HeaderLaws {

  val link = """</feed>; rel="alternate"; type="text/*"; title="main"""""

  "parse" should {
    "accept format RFC 5988" in {
      val parsedLink = Link.parse(link).right
      parsedLink.map(_.uri) must beRight(uri("/feed"))
      parsedLink.map(_.rel) must beRight(Option("alternate"))
      parsedLink.map(_.title) must beRight(Option("main"))
      parsedLink.map(_.`type`) must beRight(Option(MediaRange.`text/*`))
    }
  }

  "render" should {
    "properly format link according to RFC 5988" in {
      Link(
        uri("/feed"),
        rel = Some("alternate"),
        title = Some("main"),
        `type` = Some(MediaRange.`text/*`)).renderString must_==
        "Link: </feed>; rel=alternate; title=main; type=text/*"
    }
  }
}

package org.http4s.headers

import org.http4s.MediaRange

class LinkSpec extends HeaderLaws {

  val link = """</feed>; rel="alternate"; type="text/*"; title="main"; rev="previous""""

  "parse" should {
    "accept format RFC 5988" in {
      val parsedLink = Link.parse(link).right
      parsedLink.map(_.uri) must beRight(uri("/feed"))
      parsedLink.map(_.rel) must beRight(Option("alternate"))
      parsedLink.map(_.title) must beRight(Option("main"))
      parsedLink.map(_.`type`) must beRight(Option(MediaRange.`text/*`))
      parsedLink.map(_.rev) must beRight(Option("previous"))
    }
  }

  "render" should {
    "properly format link according to RFC 5988" in {
      Link(
        uri("/feed"),
        rel = Some("alternate"),
        title = Some("main"),
        `type` = Some(MediaRange.`text/*`)).renderString must_==
        "Link: </feed>; rel=alternate; title=main; type=text/*"
    }
  }
}

Add rev param in the test

Add rev param in the test


package bootstrap.liftweb

import net.liftweb.sitemap.{Menu, SiteMap}
import net.liftweb.sitemap.Loc._
import net.liftweb.widgets.flot._
import net.liftweb.common.{Loggable, Full}
import net.liftweb.http.{ResourceServer, LiftRules}
import net.liftweb.widgets.tablesorter.TableSorter
import org.talkingpuffin.snippet.{SessionState, Auth}

class Boot extends Loggable {

  def boot {
    LiftRules.addToPackages("org.talkingpuffin")

    val LoggedIn    = If(()     => loggedIn_?, "Not logged in")
    val NotLoggedIn = Unless(() => loggedIn_?, "Logged in")

    LiftRules.setSiteMap(SiteMap(
      Menu("Home"   ) / "index"   >> Hidden,
      Menu("Log In" ) / "login2"  >> Hidden,
      Menu("Analyze") / "analyze",
      Menu("People" ) / "people"  >> LoggedIn))

    LiftRules.ajaxStart = Full(() => LiftRules.jsArtifacts.show("ajax-loader").cmd)
    LiftRules.ajaxEnd = Full(() => LiftRules.jsArtifacts.hide("ajax-loader").cmd)
    LiftRules.ajaxPostTimeout = 60000

    LiftRules.early.append {_.setCharacterEncoding("UTF-8")}

    LiftRules.loggedInTest = Full(() => loggedIn_?)

    ResourceServer.allow {
      case "css" :: _ => true
      case "js" :: _ => true
      case "media" :: _ => true
    }

    Flot.init
    TableSorter.init
  }

  private def loggedIn_? = SessionState.loggedIn.get
}

package bootstrap.liftweb

import net.liftweb.sitemap.{Menu, SiteMap}
import net.liftweb.sitemap.Loc._
import net.liftweb.widgets.flot._
import net.liftweb.common.{Loggable, Full}
import net.liftweb.http.{ResourceServer, LiftRules}
import net.liftweb.widgets.tablesorter.TableSorter
import org.talkingpuffin.snippet.{SessionState}

class Boot extends Loggable {

  def boot() {
    LiftRules.addToPackages("org.talkingpuffin")

    val LoggedIn = If(() => loggedIn_?, "Not logged in")

    LiftRules.setSiteMap(SiteMap(
      Menu("Home"   ) / "index"   >> Hidden,
      Menu("Log In" ) / "login2"  >> Hidden,
      Menu("Analyze") / "analyze",
      Menu("People" ) / "people"  >> LoggedIn))

    LiftRules.ajaxStart = Full(() => LiftRules.jsArtifacts.show("ajax-loader").cmd)
    LiftRules.ajaxEnd   = Full(() => LiftRules.jsArtifacts.hide("ajax-loader").cmd)
    LiftRules.ajaxPostTimeout = 60000

    LiftRules.early.append {_.setCharacterEncoding("UTF-8")}

    LiftRules.loggedInTest = Full(() => loggedIn_?)

    ResourceServer.allow {
      case "css" :: _ => true
      case "js" :: _ => true
      case "media" :: _ => true
    }

    Flot.init()
    TableSorter.init()
  }

  private def loggedIn_? = SessionState.loggedIn.get
}

Clean up code a bit

Clean up code a bit


package com.softwaremill.react.kafka

import kafka.consumer.KafkaConsumer
import org.reactivestreams.{Subscription, Subscriber}

import scala.util.control.NonFatal

private[kafka] class KafkaTopicSubscription(consumer: KafkaConsumer,
                                            subscriber: Subscriber[_ >: String]) extends Subscription {

  override def request(n: Long): Unit = {
    require(n > 0, "n <= 0")

    (1L to n.toInt) foreach { _ =>
      consumer.read(bytes => {
        val msgAsStr = new String(bytes)
        subscriber.onNext(msgAsStr)
      })
    }
  }

  override def cancel() = try {
    consumer.close()
  } catch {
    case NonFatal(exception) =>
      subscriber.onError(new IllegalStateException("onComplete threw an exception", exception))
  }
}

package com.softwaremill.react.kafka

import com.softwaremill.react.kafka.RichKafkaConsumer._
import kafka.consumer.KafkaConsumer
import org.reactivestreams.{Subscriber, Subscription}

import scala.util.control.NonFatal
private[kafka] class KafkaTopicSubscription(consumer: KafkaConsumer,
                                            subscriber: Subscriber[_ >: String]) extends Subscription {

  override def request(n: Long) {
    require(n > 0, "Rule 3.9: n <= 0")
    if (consumer.connected()) {
      (1L to n.toInt) foreach { _ =>
        consumer.read(bytes => {
          val msgAsStr = new String(bytes)
          subscriber.onNext(msgAsStr)
        })
      }
    } // else 3.6 NOP
  }

  override def cancel() = try {
    consumer.close()
  } catch {
    case NonFatal(exception) =>
      subscriber.onError(new IllegalStateException("onComplete threw an exception", exception))
  }
}

Implement rule 3.6 - After the Subscription is cancelled, additional Subscription.request(long n) MUST be NOPs


name := "service-manager"

libraryDependencies ++= Seq(
  "ch.qos.logback" % "logback-classic" % "1.0.7",
  "com.jcraft" % "jsch" % "0.1.49",
  "com.typesafe" % "config" % "1.0.0",
  "com.typesafe" %% "scalalogging-slf4j" % "1.0.1",
  "com.typesafe.akka" %% "akka-actor" % "2.2.1",
  "net.databinder.dispatch" %% "dispatch-core" % "0.10.0",
  "net.liftweb" %% "lift-json" % "2.5-RC6"
)

name := "service-manager"

libraryDependencies ++= Seq(
  "ch.qos.logback" % "logback-classic" % "1.0.7",
  "com.jcraft" % "jsch" % "0.1.49",
  "com.typesafe" % "config" % "1.0.0",
  "com.typesafe" %% "scalalogging-slf4j" % "1.0.1",
  "com.typesafe.akka" %% "akka-actor" % "2.2.1",
  "net.databinder.dispatch" %% "dispatch-core" % "0.10.0",
  "net.liftweb" %% "lift-json" % "2.5.1"
)

Update dependency to stable version

Update dependency to stable version


// code-examples/Rounding/days-enumeration-script.scala

object WeekDay extends Enumeration {
  type WeekDay = Value
  val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}
import WeekDay._

def isWorkingDay(d: WeekDay) = ! (d == Sat || d == Sun)

WeekDay filter isWorkingDay foreach println

// code-examples/Rounding/days-enumeration-script.scala

object WeekDay extends Enumeration {
  type WeekDay = Value
  val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}
import WeekDay._

def isWorkingDay(d: WeekDay) = ! (d == Sat || d == Sun)

WeekDay.values filter isWorkingDay foreach println

Fix a buf for scala 2.9

Fix a buf for scala 2.9


package eu.timepit.refined

import eu.timepit.refined.TestUtils.wellTyped
import eu.timepit.refined.api.Refined
import eu.timepit.refined.collection.NonEmpty
import org.scalacheck.Prop._
import org.scalacheck.Properties
import shapeless.test.illTyped

class RefinedSpec extends Properties("Refined") {
  type NonEmptyString = String Refined NonEmpty

  property("apply") = wellTyped {
    illTyped(
      """ val x: NonEmptyString = Refined("") """,
      "eu.timepit.refined.api.Refined.type does not take parameters"
    )
  }

  property("copy") = wellTyped {
    val nonEmptyString1: NonEmptyString = refineMV[NonEmpty]("abc")
    illTyped(
      """ nonEmptyString1.copy("") """,
      "value copy is not a member of RefinedSpec\\.this\\.NonEmptyString"
    )
  }

  property("equals") = secure {
    (Refined.unsafeApply(1) ?= Refined.unsafeApply(1)) &&
    !Refined.unsafeApply(1).equals(1)
  }

  property("hashCode") = forAll { i: Int =>
    Refined.unsafeApply(i).hashCode() ?= i.hashCode
  }

  property("unapply") = secure {
    val x: NonEmptyString = refineMV("Hi")
    val Refined(s) = x
    s ?= x.value
  }
}

package eu.timepit.refined

import eu.timepit.refined.TestUtils.wellTyped
import eu.timepit.refined.api.Refined
import eu.timepit.refined.collection.NonEmpty
import eu.timepit.refined.types.string.NonEmptyString
import org.scalacheck.Prop._
import org.scalacheck.Properties
import shapeless.test.illTyped

class RefinedSpec extends Properties("Refined") {

  property("apply") = wellTyped {
    illTyped(
      """ val x: NonEmptyString = Refined("") """,
      "eu.timepit.refined.api.Refined.type does not take parameters"
    )
  }

  property("copy") = wellTyped {
    val x: NonEmptyString = refineMV[NonEmpty]("abc")
    illTyped(
      """ x.copy("") """,
      "value copy is not a member of eu.timepit.refined.types.string.NonEmptyString"
    )
  }

  property("equals") = secure {
    (Refined.unsafeApply(1) ?= Refined.unsafeApply(1)) &&
    !Refined.unsafeApply(1).equals(1)
  }

  property("hashCode") = forAll { i: Int =>
    Refined.unsafeApply(i).hashCode() ?= i.hashCode
  }

  property("unapply") = secure {
    val x: NonEmptyString = refineMV("Hi")
    val Refined(s) = x
    s ?= x.value
  }

  property("unapply in pattern matching") = secure {
    val x: NonEmptyString = refineMV("abc")
    x match {
      case Refined("abc") => true
      case _ => false
    }
  }
}

Test Refined.unapply in pattern matching


package benchmarks

import co.technius.scalajs.mithril._
import japgolly.scalajs.benchmark._
import japgolly.scalajs.benchmark.gui._
import scala.scalajs.js
import org.scalajs.dom.document

object Benchmarks extends js.JSApp {
  val suite = GuiSuite(
    Suite("Simple VNode construction")(
      Benchmark("mithril only") {
        m("div", js.Array[VNode](
          m("p", "Hi")
        ))
      },
      Benchmark("with scalatags") {
        import VNodeScalatags.all._
        div(
          p("Hi")
        ).render
      }
    )
  )

  def main(): Unit = {
    val body = document.getElementsByTagName("body")(0)
    BenchmarkGUI.renderSuite(body)(suite)
  }
}

package benchmarks

import co.technius.scalajs.mithril._
import japgolly.scalajs.benchmark._
import japgolly.scalajs.benchmark.gui._
import scala.scalajs.js
import scala.scalajs.js.JSConverters._
import org.scalajs.dom.document

object Benchmarks extends js.JSApp {
  val sampleData = ('a' to 'g').permutations.map(_.mkString)

  def vnodeMithril() =
    m("div", js.Array[VNode](
      m("div.header", js.Array[VNode](
        m("h1", "Sample Data")
      )),
      m("ul.data-list", sampleData.map { data =>
        m("li", data)
      }.toJSArray)
    ))

  def vnodeScalatagsExt() = {
    import VNodeScalatags.all._
    div(
      div(cls := "header")(
        h1("Sample Data")
      ),
      ul(cls := "data-list")(sampleData.map { data =>
        m("li", data)
      }.toSeq)
    ).render
  }

  val vnodeSuite = GuiSuite(
    Suite("Simple VNode construction")(
      Benchmark("scalajs-mithril only") {
        vnodeMithril()
      },
      Benchmark("with scalatags extension") {
        vnodeScalatagsExt()
      }
    )
  )

  val renderSuite = GuiSuite(
    Suite("Rendering")(
      Benchmark("scalajs-mithril only") {
        val parent = document.createElement("div")
        m.render(parent, vnodeMithril())
      },
      Benchmark("with scalatags extension") {
        val parent = document.createElement("div")
        m.render(parent, vnodeScalatagsExt())
      }
    )
  )

  def main(): Unit = {
    val body = document.getElementsByTagName("body")(0)
    BenchmarkGUI.renderMenu(body)(
      vnodeSuite,
      renderSuite
    )
  }
}

Add render benchmark and use better sample vnode


package eventscale.input.twitter

import akka.stream.actor.ActorConsumer
import akka.stream.actor.ActorConsumer.{ OnNext, MaxInFlightRequestStrategy, RequestStrategy }
import akka.actor.Actor.Receive
import twitter4j.Status
import akka.actor.Props

object TwitterConsumer {
  def props(): Props =
    Props(new TwitterConsumer())
}

class TwitterConsumer extends ActorConsumer {

  private var inFlight = 0

  override protected def requestStrategy: RequestStrategy = new MaxInFlightRequestStrategy(10) {
    override def inFlightInternally: Int = inFlight
  }

  override def receive: Receive = {
    case OnNext(status: Status) =>
      inFlight += 1
      processTweet(status)
    case _ => println("I don't know what this is")
  }

  private def processTweet(status: Status) = {
    println(status.getUser.getName)
    inFlight -= 1
  }
}

package eventscale.input.twitter

import akka.stream.actor.ActorConsumer
import akka.stream.actor.ActorConsumer.{ OnNext, MaxInFlightRequestStrategy, RequestStrategy }
import akka.actor.Actor.Receive
import twitter4j.Status
import akka.actor.Props

object TwitterConsumer {
  def props(): Props =
    Props(new TwitterConsumer())
}

class TwitterConsumer extends ActorConsumer {

  private var inFlight = 0

  override protected def requestStrategy: RequestStrategy = new MaxInFlightRequestStrategy(10) {
    override def inFlightInternally: Int = inFlight
  }

  override def receive: Receive = {
    case OnNext(status: Status) =>
      inFlight += 1
      processTweet(status)
    case _ => println("I don't know what this is")
  }

  private def processTweet(status: Status) = {
    println(status.getText)
    inFlight -= 1
  }

  private def processGeoTweet(status: Status) = {
    if (status.getGeoLocation != null) {
      println(status.getGeoLocation.getLatitude + ", "
        + status.getGeoLocation.getLongitude)
    }
    inFlight -= 1
  }
}

Add a couple of Twitter example filters

Add a couple of Twitter example filters