Skip to content

Instantly share code, notes, and snippets.

View taichi's full-sized avatar
😸
shaving...

taichi taichi

😸
shaving...
View GitHub Profile
/*
* Copyright 2015 SATO taichi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@taichi
taichi / Zipper.java
Created January 10, 2015 08:41
Zipper on Java8 Stream API.
/*
* Copyright 2015 SATO taichi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@taichi
taichi / ElementFilter.java
Last active August 29, 2015 14:13
rewrite javax.lang.model.util.ElementFilter with Stream API.
/*
* Copyright 2015 SATO taichi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@taichi
taichi / MemoizeSupplier.java
Created January 9, 2015 08:28
Lazy loading Supplier on Java8
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Supplier;
public class MemoizeSupplier<T> implements Supplier<T> {
final Supplier<T> delegate;
ConcurrentMap<Class<?>, T> map = new ConcurrentHashMap<>(1);
public MemoizeSupplier(Supplier<T> delegate) {
@taichi
taichi / HackCompiler.java
Last active August 29, 2015 14:12
JavaCompiler hacking.
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URI;
@taichi
taichi / Main.java
Last active August 29, 2015 14:12
1.8.0_25 javac can compile this code. eclipse 4.4.1 JDT cannot compile.
import java.util.Optional;
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
Optional.of("aaa")
.map(s -> new F<>(s)) // (1) no problem
.map(t -> // (2) <U> is Object. this is incorrect. JDK resolve <U> is Integer.
t.map(f -> 200)) // (3) <Z> is Integer. no problem.
.map(n -> n + 300) // (4) n is Object. but should be Integer.
/**
* @author taichi
*/
@FunctionalInterface
public interface ExceptionalFunction<T, R, EX extends Exception> {
R apply(T t) throws EX;
}
@taichi
taichi / Main.java
Created December 9, 2014 00:13
Nonblocking client socket
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
public class Main {
public static void main(String[] args) throws Exception {
@taichi
taichi / Js.java
Created October 17, 2014 06:26
Nashorn hack.
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Js {
public static void main(String[] args) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine se = manager.getEngineByExtension("js");
System.out.println(se.eval("Object.freeze"));
@taichi
taichi / Main.java
Last active August 29, 2015 14:07
javac bug. java8u20 compiler cannot compile this code. but eclipse4.4.0 compiler can compile.
import java.io.OutputStream;
import java.io.Writer;
public class Main {
public static String exec(Object value) { return null; }
public static void exec(Object value, Appendable appendable) {}
public static void main(String[] args) {