import java.util.ArrayDeque;
import java.util.Iterator;
import java.lang.reflect.Method;
class HelloWorld
{
{
d.add(null, HelloWorld.class, "foo");
d.add(null, HelloWorld.class, "bar");
d.invoke();
}
public static void foo()
{
}
public static void bar()
{
}
{
ArrayDeque<Pair> methods = new ArrayDeque<Pair>();
Class<?>[] parameterTypes;
public Delegate(Class
<?>...
parameterTypes) {
this.parameterTypes = parameterTypes;
}
{
Method m
= c.
getDeclaredMethod(n, parameterTypes
); methods.add(new Pair(o, m));
}
{
Method m
= c.
getDeclaredMethod(n, parameterTypes
); methods.remove(new Pair(o, m));
}
{
for (Iterator<Pair> i = methods.iterator(); i.hasNext(); )
{
Pair p = i.next();
if (args.length == 0)
{
r = p.m.invoke(p.c);
}
else
{
r = p.m.invoke(p.c, args);
}
}
return r;
}
class Pair
{
{
this.c = c;
this.m = m;
}
public int hashCode()
{
int h = 0;
if (c != null) h ^= c.hashCode();
if (m != null) h ^= m.hashCode();
return h;
}
public boolean equals
(Object obj
) {
if (obj == null) return false; if (this == obj) return true;
if (!(obj instanceof Pair)) return false;
Pair p = (Pair)obj;
return (this.c == p.c) && this.m.equals(p.m);
}
}
}
}
CmltcG9ydCBqYXZhLnV0aWwuQXJyYXlEZXF1ZTsKaW1wb3J0IGphdmEudXRpbC5JdGVyYXRvcjsKaW1wb3J0IGphdmEubGFuZy5yZWZsZWN0Lk1ldGhvZDsKCmNsYXNzIEhlbGxvV29ybGQKewogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgdGhyb3dzIEV4Y2VwdGlvbgogICAgewogICAgICAgIERlbGVnYXRlIGQgPSBuZXcgRGVsZWdhdGUoKTsKICAgICAgICBkLmFkZChudWxsLCBIZWxsb1dvcmxkLmNsYXNzLCAiZm9vIik7CiAgICAgICAgZC5hZGQobnVsbCwgSGVsbG9Xb3JsZC5jbGFzcywgImJhciIpOwogICAgICAgIGQuaW52b2tlKCk7CiAgICB9CiAgICAKICAgIHB1YmxpYyBzd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AgICAgT2JqZWN0IHIgPSBudWxsOwoJICAgICAgICBmb3IgKEl0ZXJhdG9yPFBhaXI+IGkgPSBtZXRob2RzLml0ZXJhdG9yKCk7IGkuaGFzTmV4dCgpOyApCgkgICAgICAgIHsKCSAgICAgICAgICAgIFBhaXIgcCA9IGkubmV4dCgpOwoJICAgICAgICAgICAgaWYgKGFyZ3MubGVuZ3RoID09IDApCgkgICAgICAgICAgICB7CgkgICAgICAgICAgICAgICAgciA9IHAubS5pbnZva2UocC5jKTsKCSAgICAgICAgICAgIH0KCSAgICAgICAgICAgIGVsc2UgCgkgICAgICAgICAgICB7CgkgICAgICAgICAgICAgICAgciA9IHAubS5pbnZva2UocC5jLCBhcmdzKTsKCSAgICAgICAgICAgIH0KCSAgICAgICAgfQoJICAgICAgICByZXR1cm4gcjsKCSAgICB9CgkgICAgCgkgICAgY2xhc3MgUGFpcgoJICAgIHsKCSAgICAgICAgcHVibGljIGZpbmFsIE9iamVjdCBjOwoJICAgICAgICBwdWJsaWMgZmluYWwgTWV0aG9kIG07CgkgICAgICAgIFBhaXIoT2JqZWN0IGMsIE1ldGhvZCBtKQoJICAgICAgICB7CgkgICAgICAgICAgICB0aGlzLmMgPSBjOwoJICAgICAgICAgICAgdGhpcy5tID0gbTsKCSAgICAgICAgfQoJICAgICAgICBwdWJsaWMgaW50IGhhc2hDb2RlKCkKCSAgICAgICAgewoJICAgICAgICAgICAgaW50IGggPSAwOwoJICAgICAgICAgICAgaWYgKGMgIT0gbnVsbCkgaCBePSBjLmhhc2hDb2RlKCk7CgkgICAgICAgICAgICBpZiAobSAhPSBudWxsKSBoIF49IG0uaGFzaENvZGUoKTsKCSAgICAgICAgICAgIHJldHVybiBoOwoJICAgICAgICB9CgkgICAgICAgIHB1YmxpYyBib29sZWFuIGVxdWFscyhPYmplY3Qgb2JqKQoJICAgICAgICB7CgkgICAgICAgICAgICBpZiAob2JqID09IG51bGwpIHJldHVybiBmYWxzZTsgaWYgKHRoaXMgPT0gb2JqKSByZXR1cm4gdHJ1ZTsKCSAgICAgICAgICAgIGlmICghKG9iaiBpbnN0YW5jZW9mIFBhaXIpKSByZXR1cm4gZmFsc2U7CgkgICAgICAgICAgICBQYWlyIHAgPSAoUGFpcilvYmo7CgkgICAgICAgICAgICByZXR1cm4gKHRoaXMuYyA9PSBwLmMpICYmIHRoaXMubS5lcXVhbHMocC5tKTsKCSAgICAgICAgfQoJICAgIH0KCX0KfQ==