All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.tcurrie.rest.factory.proxy.ProxyInvocationHandler Maven / Gradle / Ivy

There is a newer version: 0.2.70
Show newest version
package com.github.tcurrie.rest.factory.proxy;

import com.openpojo.business.BusinessIdentity;
import com.openpojo.business.annotation.BusinessKey;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

class ProxyInvocationHandler> implements InvocationHandler {
    @BusinessKey private final Map methodHandlers;

    public static > ProxyInvocationHandler create(final Stream methodHandlers) {
        return new ProxyInvocationHandler<>(methodHandlers.collect(Collectors.toMap(ProxyMethod::getMethod, m->m)));
    }

    private ProxyInvocationHandler(final Map methodHandlers) {
        this.methodHandlers = methodHandlers;
    }

    @Override
    public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
        if (ProxyMethodHandler.METHOD.equals(method)) {
            return methodHandlers.values().stream().collect(Collectors.toSet());
        } else if (Methods.EQUALS.equals(method)) {
            return equals(args[0]);
        } else if (Methods.HASH_CODE.equals(method)) {
            return hashCode();
        } else {
            return methodHandlers.get(method).invoke(args);
        }
    }

    @Override
    public int hashCode() {
        return BusinessIdentity.getHashCode(this);
    }

    @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
    @Override
    public boolean equals(final Object obj) {
        return BusinessIdentity.areEqual(this, obj);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy