Skip to content

Instantly share code, notes, and snippets.

View msakamoto-sf's full-sized avatar

Masahiko Sakamoto msakamoto-sf

View GitHub Profile
@msakamoto-sf
msakamoto-sf / groovy_enum_metaClass_demo1.groovy
Last active December 20, 2015 18:19
Groovy and enum demo. Groovy 1.8.9 doesn't suport enum's abstract method, but static initializer and metaClass makes nealy likely enable it. This is a work arround for http://jira.codehaus.org/browse/GROOVY-4641
// tested on Groovy 1.8.9.
enum GreetEnum {
MORNING("Good Morning"),
AFTERNOON("Good Afternoon"),
EVENING("Good Evening");
static {
MORNING.metaClass.greeting = { String you ->
return delegate.emphasize() + " " + you + ", I'm sleeping..."
}
@msakamoto-sf
msakamoto-sf / hogan_ex1.groovy
Created March 17, 2013 15:57
Hogan.groovy examples
@Grab(group='com.github.plecong', module='hogan-groovy', version='3.0')
import com.github.plecong.hogan.Hogan
def expected = ''
def template = null
def data = [:]
def template_s = ''
// {{{ basic compile()
@msakamoto-sf
msakamoto-sf / Singleton.java
Created November 18, 2012 02:08
Java + Groovy + TestNG + Maven combination example3 (replace singleton field)
package gjt3;
public class Singleton {
private static Singleton instance = null;
private String myarg;
private Singleton(String arg) {
myarg = arg;
}
public synchronized static Singleton getInstance(String arg) {
if (Singleton.instance == null) {
@msakamoto-sf
msakamoto-sf / Calc1.groovy
Created November 11, 2012 07:50
Java + Groovy + TestNG + Maven combination example1
package gjt1;
public class Calc1 {
int v1;
int v2;
def Calc1(int a, int b) {
v1 = a;
v2 = b;
}
int calc() {
@msakamoto-sf
msakamoto-sf / Calc.java
Created November 11, 2012 07:47
Java + Groovy + TestNG + Maven combination example2
package gjt2;
public class Calc {
private int v1;
private int v2;
public Calc(int a, int b) {
v1 = a;
v2 = b;
}
public int calc() {
@msakamoto-sf
msakamoto-sf / t_networkaddress_cache_ttl.groovy
Last active August 29, 2015 14:14
get "networkaddress.cache.ttl" and "networkaddress.cache.negative.ttl" for your environment. see java.net.InetAddress javadoc.
import sun.net.InetAddressCachePolicy;
// see: https://docs.oracle.com/javase/jp/8/api/java/net/InetAddress.html
// see: https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/sun/net/InetAddressCachePolicy.java
// for (Open/Oralce) JRE 7-8
// "networkaddress.cache.ttl"
println InetAddressCachePolicy.cachePolicy
// "networkaddress.cache.negative.ttl"
println InetAddressCachePolicy.negativeCachePolicy
println InetAddressCachePolicy.propertySet
@msakamoto-sf
msakamoto-sf / history_api_ex1.html
Last active August 29, 2015 14:12
HTML5 hisotry API exercises
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>HTML5 history API exercise</title>
</head>
<script>
/*
HTML5 history API exercise 1.
javascript:void(function(){if (window.location.href.match(new RegExp("file/d/(\\w+)/edit"))) { var n = "http://drive.google.com/uc?export=view&id=" + RegExp.$1; prompt("url", n); } else { prompt("not google drive image edit url. see:", "http://googlesystem.blogspot.jp/2013/02/permalinks-for-google-drive-images.html"); }}())
@msakamoto-sf
msakamoto-sf / get256bytes.groovy
Created May 26, 2014 08:20
create 0x00 - 0xFF 256 bytes binary data to stdout.
for (int i in 0..0xFF) {
System.out.write(i);
System.out.flush();
}
@msakamoto-sf
msakamoto-sf / t_tcp_echo_client_1.groovy
Last active August 29, 2015 13:56
very simple TCP Echo server and client, with "strace"ing server thread : example #1.
/*
* (no copyright, license-free, AS-IS, for any commercial or oss or free source code)
*/
String host = args[0]
int port = args[1].toInteger()
int to_connect = 10 * 1000 // connect timeout in milli secs
int to_read = 10 * 1000 // read timeout in milli secs
SocketAddress sa_local = null // bind to system default address and port.
SocketAddress sa_remote = new InetSocketAddress(host, port)