Skip to content

Instantly share code, notes, and snippets.

@fumokmm
fumokmm / executor_submit3.groovy
Created July 7, 2012 07:54 — forked from mike-neck/console.log
ExecutorServiceの拡張がうまくできん -> メソッド名のせいかな?
import java.util.concurrent.ExecutorService
import java.util.concurrent.BlockingQueue
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.Executors
import java.util.concurrent.Callable
def executor = Executors.newSingleThreadExecutor()
// Closure as Interfaceパターン!
def future = executor.submit( {
def list = []
@fumokmm
fumokmm / rand_date.groovy
Created June 9, 2011 09:57 — forked from xnrghzjh/rand_date.groovy
指定範囲内の日付をランダムに取得
@Grab(group='joda-time', module='joda-time', version='*')
import org.joda.time.*
//def rand_date(def fr, def to) { // java...
def rand_date = {fr,to-> // groovy!!
def d = new DateTime(fr)
def range = Days.daysBetween(d, new DateTime(to)).getDays() + 1
d.plusDays((Integer)Math.floor(Math.random() * range)).toString("yyyy-MM-dd")
}
def worker = { latch, num ->
println "ready: $num"
latch.countDown()
Thread.sleep(num * 10)
println num
}
def latch = new java.util.concurrent.CountDownLatch(args.size())
args.each {
Thread.start worker.curry(latch, it as int)
@fumokmm
fumokmm / TestAmountNegative.groovy
Created May 28, 2011 16:55 — forked from kijuky/TestAmountNegative.groovy
Amountの単項マイナス演算子のテスト(Grab版)
@GrabResolver(name='jsciencerepos', root='http://maven.obiba.org/maven2/')
@Grab('org.jscience:jscience:4.3.1')
import org.jscience.physics.amount.*
def a = Amount.valueOf("1 m")
def b = Amount.valueOf("2 m")
Amount.metaClass.negative {
opposite()
class Hello {
void hello(){ println "1" }
}
def a = new Hello()
def b = new Hello()
println '-' * 10
b.metaClass.hello = { println "2" }
print '[1] '; a.hello() // => 1
print '[2] '; b.hello() // => 2 (metaClass.hello が呼ばれる)
def array = []
def tmp = ""
array = ('0'..'9') + ('a'..'z') + ('A'..'Z') + '_'
(1..16).each {
tmp += array[Math.floor(Math.random() * array.size()) as int]
}
println tmp