package org.mit.jstreamit;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Jeffrey Bosboom <jeffreybosboom@gmail.com>
 * @since 11/8/2012
 */
public final class Portal<I extends Object> {
	private final Class<I> klass;
	private final List<I> listeners = new ArrayList<>();
	public Portal(Class<I> klass) {
		this.klass = klass;
	}

	public void addListener(I listener) {
		listeners.add(listener);
	}

	public I getHandle(int latency) {
		//When running in the JIT compiler, this is a JIT hook instead.
		return (I)Proxy.newProxyInstance(klass.getClassLoader(), new Class[]{klass}, new LatencyInvocationHandler(latency));
	}

	private class LatencyInvocationHandler implements InvocationHandler {
		private final int latency;
		LatencyInvocationHandler(int latency) {
			this.latency = latency;
		}
		@Override
		public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
			throw new UnsupportedOperationException("TODO");
		}
	}
}

@@ -496,16 +496,267 @@
 ener) %7B%0A
+%09%09//I'm pretty sure this can only happen via unchecked casts or%0A%09%09//incompatible class file changes, but we should check anyway.%0A%09%09if (!klass.isInstance(listener))%0A%09%09%09throw new IllegalArgumentException(%22Listener %22+listener+%22 not instance of %22+klass);%0A
 %09%09listen

check listener is instance of interface