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

com.syntaxphoenix.syntaxapi.service.ServiceContainer Maven / Gradle / Ivy

package com.syntaxphoenix.syntaxapi.service;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class ServiceContainer {

	private final ArrayList subscribed = new ArrayList<>();
	private final Object owner;

	ServiceContainer(Object owner) {
		this.owner = owner;
	}
	
	/*
	 * 
	 */

	public Object getOwner() {
		return owner;
	}
	
	/*
	 * 
	 */
	
	boolean isEmpty() {
		return subscribed.isEmpty();
	}
	
	boolean add(IServiceValue value) {
		return subscribed.add(value);
	}
	
	/*
	 * 
	 */

	public IServiceValue[] getValues(Class service) {
		return filter(value -> value.getService().equals(service)).toArray(size -> new IServiceValue[size]);
	}

	public ServiceFieldValue getValue(Field field) {
		Optional option = filter(value -> {
			if (!value.isField())
				return false;
			return value.asField().equals(field);
		}).findAny();
		return option.isPresent() ? (ServiceFieldValue) option.get() : null;
	}

	public ServiceMethodValue getValue(Method method) {
		Optional option = filter(value -> {
			if (!value.isMethod())
				return false;
			return value.asMethod().equals(method);
		}).findAny();
		return option.isPresent() ? (ServiceMethodValue) option.get() : null;
	}
	
	/*
	 * 
	 */

	private Stream filter(Predicate predicate) {
		return subscribed.stream().filter(predicate);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy