Skip to content

Instantly share code, notes, and snippets.

View frgomes's full-sized avatar

Richard Gomes frgomes

View GitHub Profile
@frgomes
frgomes / Resources.java
Last active September 10, 2015 17:06
Java - Copy resources from classpath onto a temporary directory
import java.io.File;
import java.io.IOException;
import java.net.URL;
import org.apache.commons.io.FileUtils;
public class Resource {
private static java.net.URLStreamHandler cp = new org.ops4j.pax.url.classpath.Handler();
private final URL url;
private final String paths[];
@frgomes
frgomes / RichConfig.scala
Last active September 25, 2015 09:26
Scala - Extracts a custom configuration object from a generic com.typesafe.config.Config
package util.implicits
object RichConfig extends RichConfig
trait RichConfig {
import com.typesafe.config.Config
import net.ceedubs.ficus.Ficus._
import util.types.Database
implicit class RichConfig(config: Config) {
@frgomes
frgomes / MultiMap.scala
Created October 12, 2015 10:48
Scala -- Using multimaps
import scala.collection.mutable.{ Map, HashMap, Set, MultiMap }
// val evictedModuleIDs: Seq[ModuleID] = ...
val evicteds: Map[String, Set[ModuleID]] =
(new HashMap[String, Set[ModuleID]] with MultiMap[String, ModuleID] /: evictedModuleIDs) {
case (acc, m) =>
val key = s"${m.organization}:${m.name}"
acc.addBinding(key, m)
}
@frgomes
frgomes / Ctx.scala
Last active October 16, 2015 12:21
Scala / SBT - Obtain BuildStructure and root ProjectRef
case class Ctx(_state: State) {
val state: State = _state
val xt: Extracted = Project.extract(state)
val bs: BuildStructure = xt.structure
val pr: ProjectRef = ProjectRef(bs.root, bs.rootProject(bs.root)) //i.e.: the root project!
}
@frgomes
frgomes / EvaluationSupport.scala
Created October 16, 2015 12:23
Scala / SBT - Evaluation and task execution helpers
trait EvaluationSupport {
import sbt._
protected def fail(errorMessage: String, state: State): Nothing = {
state.log.error(errorMessage)
throw new IllegalArgumentException()
}
protected def log(implicit state: State) = state.log
@frgomes
frgomes / SourceAsStream.scala
Last active October 30, 2015 13:12
Scala - Read file as Stream
def matches(text: String, is: InputStream = System.in): Int =
scala.io.Source.fromInputStream(is)
.getLines
.toStream
.map(line => if(line.contains(text)) 1 else 0)
.reduce(_ + _)
@frgomes
frgomes / MonadExtractor.scala
Last active November 27, 2015 23:35
Scala - For comprehension monad with extractor magic
for(row <- sheet.rows if (row.index == index); Cell(cindex, data) <- row.cells) yield { (cindex, data) }
@frgomes
frgomes / OrElse.scala
Last active December 17, 2015 11:16
Scala - Partial functions with orElse
import java.io._
val osURL: PartialFunction[Option[String], OutputStream] = {
case Some(name) => new java.io.FileOutputStream(new java.io.File(name))
if(name.contains(":"))
}
val osLocalFile: PartialFunction[Option[String], OutputStream] = {
case Some(name) => new java.io.FileOutputStream(new java.io.File(name))
}
@frgomes
frgomes / ScalaScriptTemplate.scala
Last active December 17, 2015 11:20
Scala script - template script
#!/bin/bash
#-*- mode: scala; -*-
#####
##### NOTE: save this script WITHOUT EXTENSION onto the file system.
#####
launcher=$(which sbt-launch.jar)
if [ -z "$launcher" ] ;then
pushd /tmp
@frgomes
frgomes / Cache.scala
Last active January 5, 2016 17:26
Scala - cache2k with underlying file storage
import net.jcip.annotations.NotThreadSafe
import org.cache2k.impl.ClockProPlusCache
type ImageType = Array[Byte]
class CacheSource(dir: java.io.File)
extends org.cache2k.CacheSource[String, ImageType] {
import java.nio.file.Files
import javax.imageio.ImageIO