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

io.yawp.repository.EndpointFeatures Maven / Gradle / Ivy

There is a newer version: 2.08alpha
Show newest version
package io.yawp.repository;

import io.yawp.repository.actions.ActionKey;
import io.yawp.repository.annotations.Endpoint;
import io.yawp.repository.hooks.Hook;
import io.yawp.repository.shields.Shield;
import io.yawp.repository.shields.ShieldInfo;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EndpointFeatures {

	private Class clazz;

	private Map actions;

	private Map transformers;

	private List>> hooks;

	private ShieldInfo shieldInfo;

	public EndpointFeatures(Class clazz) {
		this.clazz = clazz;
		this.actions = new HashMap<>();
		this.transformers = new HashMap<>();
		this.hooks = new ArrayList<>();
	}

	public Class getClazz() {
		return this.clazz;
	}

	private  void assertInexistence(V key, Method method, Map map, String type) {
		if (map.get(key) != null) {
			throw new RuntimeException("Trying to add two " + type + " with the same name '" + key + "' to io.yawp "
					+ clazz.getSimpleName() + ": one at " + map.get(key).getDeclaringClass().getSimpleName() + " and the other at "
					+ method.getDeclaringClass().getSimpleName());
		}
	}

	public void addAction(ActionKey actionRef, Method method) {
		assertInexistence(actionRef, method, actions, "Actions");
		actions.put(actionRef, method);
	}

	public void addTransformer(String name, Method method) {
		assertInexistence(name, method, transformers, "Transformers");
		transformers.put(name, method);
	}

	public void addHook(Class> hook) {
		hooks.add(hook);
	}

	public void setShield(Class> shield) {
	}

	public void setShieldInfo(ShieldInfo shieldInfo) {
		this.shieldInfo = shieldInfo;
	}

	public List>> getHooks() {
		return hooks;
	}

	public Method getAction(ActionKey ref) {
		return actions.get(ref);
	}

	public Class getActionClazz(ActionKey ref) {
		return getAction(ref).getDeclaringClass();
	}

	public Method getTransformer(String name) {
		return transformers.get(name);
	}

	public Endpoint getEndpointAnnotation() {
		return clazz.getAnnotation(Endpoint.class);
	}

	public String getEndpointPath() {
		Endpoint endpoint = clazz.getAnnotation(Endpoint.class);
		if (endpoint == null) {
			throw new RuntimeException("The class " + clazz + " was used as an entity but was not annotated with @Endpoint.");
		}
		return endpoint.path();
	}

	public boolean hasCustomAction(ActionKey actionKey) {
		return actions.containsKey(actionKey);
	}

	public boolean hasTranformer(String transformerName) {
		return transformers.containsKey(transformerName);
	}

	public boolean hasShield() {
		return shieldInfo != null;
	}

	public ShieldInfo getShieldInfo() {
		return shieldInfo;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy