Skip to content

Instantly share code, notes, and snippets.

View bdkosher's full-sized avatar

Joe Wolf bdkosher

View GitHub Profile
@bdkosher
bdkosher / timeIt.groovy
Created September 18, 2013 12:23
Closure which can be used to time how long operations took.
def timeIt = { resultFormatter = { obj -> obj?.toString() }, closure ->
def start = System.nanoTime()
def result = null
try {
result = closure()
} catch (Exception e) {
result = e
}
def end = System.nanoTime()
def ms = (end - start) / 1e6
@bdkosher
bdkosher / IntAsByteArray.java
Created September 18, 2013 13:53
Represent an int as its array of bytes. (taken from http://java.dzone.com/articles/top-10-methods-java-arrays)
import java.nio.ByteBuffer;
int integer = 8;
byte[] bytes = ByteBuffer.allocate(4).putInt(integer).array();
for (byte t : bytes) {
System.out.format("0x%x ", t);
}
// We can loop from today to next week and invoke a closure for each day.
def today = new Date().clearTime()
def nextWeek = today + 7
today.upto(nextWeek) {
// Print day of the week.
println it.format('EEEE')
}
println()
def printClassPath(classLoader) {
println "$classLoader"
classLoader.getURLs().each { url->
println "- ${url.toString()}"
}
if (classLoader.parent) {
printClassPath(classLoader.parent)
}
}
printClassPath this.class.classLoader
@bdkosher
bdkosher / parseAccessLog.groovy
Created January 16, 2014 23:01
Command line script for parsing an access log and outputting some subset of data, e.g. groovy parseAccessLog.groovy "$path,$status,$elapsedTime" lists all the request paths, response codes, and elapsed time in a CSV format.
/**
* Breaks apart a line localhost_access_logs and provides properties to access the various components of that line
*
* Assumes that the format is
* Time Taken: 0.006 10.113.110.28 - - [12/Jan/2014:00:02:29 -0500] GET /path/ HTTP/1.1 http-bsd-dfw-1.xyz.com%2F10.113.110.25-8443-7 302 - E1518C5CABA9A32394BE209727824D3D.profile
* 0 1 2=et 3=ip 4 5 6=timestamp 7=to 8=h 9=up 10=pr 11=hostname%2Fhostip-port-thread 12=rc 13 14=???
*/
class Entry {
def elapsedTimeIdx = 2
def ipIdx = 3
@bdkosher
bdkosher / GenerateMathProblems.groovy
Last active August 29, 2015 13:55
Generates addition and subtraction math problems.
import groovy.transform.*
/*
* Configuration:
*
* @twoDigit - if false, problems will be single digit operands and answers; if true, one or two digits
* @independentProblems - if false, the answer to the previous problem is the first operand of the
* subsequent problem, and a finalResult value must be specified
* @additionOnly - if true, all problems generated will be addition; if false, it'll be a mix of
* addition and subtraction
*
// store Groovy script in directory where ZIP was extracted
// run vertx run FlappyBird.groovy
def root = /FlappyBirdRL-master/
vertx.createHttpServer().requestHandler { req ->
def file = java.net.URLDecoder.decode(req.uri == "/" ? "index.html" : req.uri, 'UTF-8')
req.response.sendFile "$root/$file"
}.listen(8080)
@bdkosher
bdkosher / EasyDateCategory.groovy
Created February 25, 2014 22:46
Extends groovy.time.TimeCategory with the ability to cast Strings into Dates.
class EasyDateCategory extends groovy.time.TimeCategory {
static asType(String str, Class clazz) {
if (clazz == Date) {
new java.text.SimpleDateFormat('yyyy-MM-dd').parse(str)
} else {
str.asType(clazz)
}
}
}
@bdkosher
bdkosher / File_eachCsvRow.groovy
Created February 27, 2014 17:24
java.io.File extension method for parsing CSV files row-by-row.
/*
* Processes file as a CSV, row by row, using the provided closure. An optional starting
* row number may be provided to this method, defaulting to 1 (the first row).
*
* The provided closure can accept one or two arguments, either:
* - the row values (String[])
* - the row values (String[]), the current row number (int)
*
* Currently, only commas are recognized as the separator. Quoted values are handled
@bdkosher
bdkosher / FederalHolidays.java
Created March 7, 2014 16:30
Java class for determining the dates of U.S. Federal Holidays, along with Spock test.
package bdkosher.datetime;
import java.util.Calendar;
import static java.util.Calendar.*;
import java.util.Date;
import java.util.Locale;
import java.util.SortedSet;
import java.util.TimeZone;
import java.util.TreeSet;