Skip to content

Instantly share code, notes, and snippets.

View voronaam's full-sized avatar

Lex Vorona voronaam

View GitHub Profile
@voronaam
voronaam / MutableComboBox.scala
Last active February 24, 2022 23:32
Mutable ComboBox for Scala Swing
import scala.swing.{ Component, Swing, event }
import javax.swing.JComboBox
/**
* Very basic Mutable ComboBox for Scala.
* <p>Sample usage:<p>
* <pre>
* val box = new MutableComboBox[String]
* box.items = List("1", "11", "222")
* listenTo(box)
@voronaam
voronaam / Page set parser
Last active December 27, 2015 02:58
A function to parse page set definitions with ranges. No negative numbers (it is for page numbers).
// A function to parse page set defintion, which supports [overlapping] ranges
def pageSet(definition: String) = {
def pageDefToSet(pageDef: String): Set[Int] = {
val rangeExtractor = """[^\d]*([\d]+).*-[^\d]*([\d])+[^\d]*""".r
val numberExtractor = """[^\d]*([\d]+)[^\d]*""".r
pageDef match {
case rangeExtractor(from, to) => (from.toInt to to.toInt).toSet
case numberExtractor(num) => Set(num.toInt)
case _ => Set()
}
@voronaam
voronaam / VaadinTableContainer.scala
Last active December 30, 2015 05:09
Vaadin to Squeryl Container implementation
import java.util.{ Collection => JCollection }
import java.lang.{ Long => JLong }
import java.sql.Timestamp
import scala.collection.JavaConversions._
import org.squeryl.Query
import com.vaadin.data._
import scala.reflect.ClassTag
import org.squeryl.dsl.ast.OrderByArg
import org.squeryl.dsl.ast.ExpressionNode
@voronaam
voronaam / jsonsqueryl.scala
Created January 29, 2014 20:22
JSON type mapper for Squeryl
trait XsnTypeMode extends PrimitiveTypeMode {
import com.codahale.jerkson.Json._
import scala.collection.mutable.HashMap
import scala.language.implicitConversions
implicit val jsonTEF = new NonPrimitiveJdbcMapper[String,HashMap[String, Any],TString](stringTEF, this) {
def convertFromJdbc(v: String) = if(v == null) null else parse[HashMap[String, Any]](v)
def convertToJdbc(v: HashMap[String, Any]) = if(v == null) null else generate(v)
override def sample = new HashMap[String, Any]()
}
/** Test results:
Benchmark Mode Samples Mean Mean error Units
c.t.MyBenchmark.testString thrpt 200 391517.835 3311.834 ops/ms
c.t.MyBenchmark.testSymbol thrpt 200 36618.384 193.733 ops/ms
*/
package com.test {
import org.openjdk.jmh.annotations.GenerateMicroBenchmark
/**
* This is a benchmark to gauge if there is any performance hit into using Option.apply() in Scala.
* Use JMH to build it: http://blog.vorona.ca/measuring-scala-performance-with-jmh.html
*/
package org.sample
import org.openjdk.jmh.annotations._
import java.util.concurrent.TimeUnit
@voronaam
voronaam / FindBenchmark.java
Created March 5, 2015 22:30
OlognBenchmark
package com.test;
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
@voronaam
voronaam / SaveObjectToFile.java
Created December 4, 2015 23:09
A quick implementation of the object dump query for Eclipse Memory Analizer (MAT)
package org.eclipse.mat.ui.snapshot.actions;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.HashSet;
@voronaam
voronaam / MyBenchmark.java
Last active July 6, 2016 22:04
JMHing difference between chained and unchained calls
package org.sample;
/**
* Results:
* Benchmark Mode Cnt Score Error Units
* MyBenchmark.testChain avgt 200 0.019 ± 0.001 us/op
* MyBenchmark.testNoChain avgt 200 0.019 ± 0.001 us/op
*/
import org.openjdk.jmh.annotations.Benchmark;