Skip to content

Instantly share code, notes, and snippets.

View frgomes's full-sized avatar

Richard Gomes frgomes

View GitHub Profile
@frgomes
frgomes / PropertyChangeListenerAdapter.java
Last active August 29, 2015 14:22
Java - ChangeListener adapter to PropertyChangeListener
class PropertyChangeListenerAdapter implements PropertyChangeListener {
final private ChangeListener changeListener;
public PropertyChangeListenerAdapter(ChangeListener changeListener) {
this.changeListener = changeListener;
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
changeListener.stateChanged(new ChangeEvent(evt.getSource()));
@frgomes
frgomes / CatchSafely.scala
Last active June 28, 2018 17:59
Scala - Handling Throwables safely
/**
* Safely handles ControlThrowable
*
* See: [[https://www.sumologic.com/2014/05/05/why-you-should-never-catch-throwable-in-scala/ Why you should never catch Throwable in Scala ]]
*
* See: [[https://github.com/scala/scala/blob/2.12.x/src/library/scala/util/control/NonFatal.scala scala.util.control.NonFatal.scala]
*/
trait CatchSafely {
import scala.util.control.ControlThrowable
@frgomes
frgomes / ReportingValidationsLately.scala
Last active August 29, 2015 14:27
Scala - Reporting validations
def transmit[R: ClassTag](message: R, validations: (R => Seq[Option[Throwable]])*) = {
val conditions: Seq[Option[Throwable]] =
(Seq.empty[Option[Throwable]] /: (for { validation <- validations } yield { validation(message)} )) {
case (acc, item) => acc ++ item }
val lines =
(0 /: conditions.flatten) {
case (n, e) =>
val line = n + 1
log.error(s"VALIDATION #${line} :: " + e.getMessage + s"\n" + e.getStackTraceString)
line }
@frgomes
frgomes / ConvertDataStructures.scala
Last active August 29, 2015 14:28
Scala - Generic way to convert data structures
import collection.generic.CanBuildFrom
import collection.immutable.TreeMap
object test {
class TraversableW[A](t: Traversable[A]) {
def as[CC[X] <: Traversable[X]](implicit cbf: CanBuildFrom[Nothing, A, CC[A]]): CC[A] = t.map(identity)(collection.breakOut)
def to[Result](implicit cbf: CanBuildFrom[Nothing, A, Result]): Result = t.map(identity)(collection.breakOut)
}
implicit def ToTraverseableW[A](t: Traversable[A]): TraversableW[A] = new TraversableW[A](t)
@frgomes
frgomes / CustomOrdering.scala
Last active June 14, 2016 17:09
Scala - Custom ordering
case class Employee(id: Int, firstName: String, lastName: String)
object Employee {
// Note that because `Ordering[A]` is not contravariant, the declaration
// must be type-parametrized in the event that you want the implicit
// ordering to apply to subclasses of `Employee`.
implicit def orderingByName[A <: Employee]: Ordering[A] =
Ordering.by(e => (e.lastName, e.firstName))
val orderingById: Ordering[Employee] = Ordering.by(e => e.id)
@frgomes
frgomes / pom2deps.scala
Last active September 7, 2015 15:38
Scala - Converts a list of pom.xml files onto list of variables containing versions and dependencies
#!/bin/bash
#-*- mode: scala; -*-
######################################################################
# This Scala scripts extracts versions and dependencies from a list
# of pom.xml files passed as argument to the script.
######################################################################
exec scala "$0" "$@"
!#
@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!
}