Skip to content

Instantly share code, notes, and snippets.

class Machine {
def states, accept, initialState, sigma, blank, gamma
def transitions = [:].withDefault{[:]}
def validate() {
def actions = transitions.values()*.values()
def usedchars = transitions.values()*.keySet().sum() + actions.write.sum()
assert states.containsAll(transitions.keySet() + actions.newState.sum()),
"transitions should only contain known states"
@jpertino
jpertino / init.gradle
Created February 7, 2011 20:38
override gradle project repositories in init script
projectsEvaluated {
rootProject.allprojects {
buildscript.repositories {
resolverNames = []
mavenRepo name: 'plugins-repo', urls: 'http://repository.example.org/plugins'
}
repositories {
resolverNames = []
mavenRepo name: 'libs-repo', urls: 'http://repository.example.org/libs'
@jpertino
jpertino / databaseConnection.groovy
Created January 28, 2011 23:29
several database connectors and drivers
// PostgreSQL
@GrabConfig(systemClassLoader=true)
@Grab('postgresql:postgresql:9.0-801.jdbc4')
def sql = groovy.sql.Sql.newInstance(
"jdbc:postgresql://host.example.org/database",
"username", "password", "org.postgresql.Driver")
// MySQL
@GrabConfig(systemClassLoader=true)
@jpertino
jpertino / simpleJettyServer.groovy
Created January 20, 2011 04:29
a simple embedded jetty server
@Grab('org.mortbay.jetty:jetty-embedded:6.1.26')
import static org.mortbay.jetty.Handler.DEFAULT
import org.mortbay.jetty.Server
import org.mortbay.jetty.servlet.Context
import org.mortbay.jetty.servlet.DefaultServlet
import org.mortbay.servlet.MultiPartFilter
import groovy.servlet.GroovyServlet
import groovy.servlet.TemplateServlet
@jpertino
jpertino / preparePdf.groovy
Created January 8, 2011 04:30
sort pdf pages for book-like two-page print
@GrabResolver(name='itextpdf', root='http://maven.itextpdf.com/')
@Grab('com.itextpdf:itextpdf:5.0.5')
import static com.itextpdf.text.PageSize.A4
import com.itextpdf.text.Document
import com.itextpdf.text.pdf.PdfReader
import com.itextpdf.text.pdf.PdfWriter
def cli = new CliBuilder().with {
usage = 'preparePdf [options] <source file path>'
import static org.hamcrest.CoreMatchers.is
import static org.junit.Assert.assertArrayEquals
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertSame
import static org.junit.Assert.assertNot
import static org.junit.Assert.assertNull
import static org.junit.Assert.assertNotNull
import static org.junit.Assume.assumeNoException
@jpertino
jpertino / readClipboard.groovy
Created January 8, 2011 01:42
read string from clipboard
java.awt.Toolkit.defaultToolkit.systemClipboard.getData java.awt.datatransfer.DataFlavor.stringFlavor
@jpertino
jpertino / uselessTrustManager.groovy
Created January 6, 2011 19:42
HttpsURLConnection DefaultSSLSocketFactory TrustManager
javax.net.ssl.SSLContext.getInstance('SSL').with {
init null, [[checkServerTrusted:{chain,authType->}]] as javax.net.ssl.X509TrustManager[], null
javax.net.ssl.HttpsURLConnection.defaultSSLSocketFactory = socketFactory
}
@jpertino
jpertino / cliBuilderExample.groovy
Created December 30, 2010 14:32
groovy CliBuilder usage
def cli = new CliBuilder().with {
usage = 'program [options] <arguments>'
header = 'Options:'
footer = '-' * width
s 'simplest boolean option'
b longOpt: 'both', 'boolean option with both longop and shortop'
_ longOpt: 'no-shortop-1', 'boolean option without short version 1'
@jpertino
jpertino / missingMethodMultipleDispatch.groovy
Created December 29, 2010 23:54
groovy method dispatching with a little help
class Magic {
def methodMissing(String methodName, Object arguments) {
method(methodName, *arguments)
}
private method(String methodName, Object... arguments) {
if (!(arguments[-1] instanceof Closure))
throw new MissingMethodException(methodName, this.class, arguments)