Skip to content

Instantly share code, notes, and snippets.

@eribeiro
eribeiro / building-twitter-commons.sh
Last active October 8, 2015 20:17
Instructions how to build twitter commons
compile and run all tests
./pants tests/java/com/twitter/common:all!
find all java targets
$ ./pants list -d src/java tests/java
src/java/com/twitter/common/application/BUILD:action
src/java/com/twitter/common/application/BUILD:application
src/java/com/twitter/common/application/http/BUILD:http
src/java/com/twitter/common/application/modules/BUILD:applauncher
src/java/com/twitter/common/application/modules/BUILD:http
@eribeiro
eribeiro / Pair.java
Created October 29, 2012 22:16
Java Pair
public class Pair<U,T> {
public final U left;
public final T right;
public static <U,T> Pair<U,T> of(U u, T t) {
return new Pair<U,T>(u, t);
}
public Pair(U left, T right) {
@eribeiro
eribeiro / GenericsTest.java
Created December 13, 2012 19:01
How to retrieve the generics type of a class (even if subclassed). DISCLAIMER: it doesn't work, but for the most simple case [1]. If you need a bit more [2] then go with 3rd party lib (like classmate). * [1] class Bar extends Foo<String> {...} * [2] class Bar extends Foo<List<String>> {...}
public class GenericsTest {
public static void main(String[] args) {
Bar b = new Bar("Test");
System.out.println(b.getReferenceType().getType());
}
static class TypeReference<T> {
@eribeiro
eribeiro / log4j
Created February 20, 2013 17:47
Log4j properties example
#------------------------------------------------------------------------------
#
# The following properties set the logging levels and log appender. The
# log4j.rootCategory variable defines the default log level and one or more
# appenders. For the console, use 'S'. For the daily rolling file, use 'R'.
# For an HTML formatted log, use 'H'.
#
# To override the default (rootCategory) log level, define a property of the
# form (see below for available values):
#
FileSystem fs = FileSystems.getDefault();
for (FileStore e : fs.getFileStores()) {
System.out.println(e.name() + " " + e.getTotalSpace() + " " + e.type());
}
On my box returns:
/dev/sda1 488306122752 ext4
proc 0 proc
sysfs 0 sysfs
@eribeiro
eribeiro / scanTrace
Last active December 19, 2015 11:49
def scanTrace() {
import collection.mutable.{ HashMap, MultiMap, Set }
val source = scala.io.Source.fromFile("google-cluster-data-1.csv")
val map = new HashMap[String, Set[String]] with MultiMap[String, String]
for (i <- source.getLines()) {
val fields = i.split(" ")
val parent = fields(1)
// scala
(1 to 100).toList foreach { x => x match {
case x if (x % 15 == 0) => println("FizzBuzz")
case x if (x % 3 == 0) => println("Fizz")
case x if (x % 5 == 0) => println("Buzz")
case x => println(x)
}
}
@eribeiro
eribeiro / MapBasic.scala
Last active July 25, 2021 05:41
How to do a foldLeft with a Map in Scala, plus basic stuff.
val list = (1 to 10).toList
// imutable map
val map1 = list.foldLeft(Map.empty[Int,String])( (map, value) => map + (value -> value.toString) )
// this is equivalent to
val map1 = list.foldLeft(Map.empty[Int,String])( (map, value) => map + ((value, value.toString)) )
// imutable map with more complex value (note the extra parenthesis)
@eribeiro
eribeiro / ex1-ch1.scala
Created August 14, 2013 17:56
Exercise of the first chapter of Okasaki's Functional Data Structures.
def suffix(l: List[Int]): List[List[Int]] = l match {
case Nil => List()::Nil
case _ => l :: suffix(l.tail)
}
@eribeiro
eribeiro / WTF.java
Last active December 21, 2015 04:29
Can someone please explain why on earth is this guy synchronizing on Thread.sleep ? AFAIK, if you have, say, 4 threads, then one is sleeping while the other 3 are blocked, *waiting* to sleep (!!!) (detail: finished is not volatile). Your sincerely, Ed.
public class WTF extends Thread {
private boolean finished = false;
...
public void run() {
....
while (!finished) {