DefaultListenerObjectRetriever.java

/*
 * Copyright (C) 2012-2024 RRiBbit.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.rribbit.retrieval;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

import org.rribbit.ListenerObject;
import org.rribbit.creation.ListenerObjectCreator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This {@link ListenerObjectRetriever} provides common functionality that is required by typical implementations of {@link ListenerObjectRetriever}.
 * This includes methods for checking whether a {@link ListenerObject} matches a certain request.
 * <p />
 * This class uses a {@link List} of {@link ListenerObjectCreator}s for getting all {@link ListenerObject}s to search in. It does not keep a local copy of this collection, so implementations of
 * {@link ListenerObjectCreator} are responsible of caching it.
 * <p />
 * This implementation is quite naieve, checking each {@link ListenerObject} in the {@link Collection} for a match everytime a request is made. Subclasses may replace this behaviour with
 * more intelligent behaviour, such as caching of search results, or may implement {@link ListenerObjectRetriever} directly.
 *
 * @author G.J. Schouten
 *
 */
public class DefaultListenerObjectRetriever implements ListenerObjectRetriever {

	private static final Logger log = LoggerFactory.getLogger(DefaultListenerObjectRetriever.class);

	protected List<ListenerObjectCreator> listenerObjectCreators;

	/**
	 * Whenever you use this constructor, be sure to set the {@link ListenerObjectCreator} with the setter provided by this class.
	 * If you don't, runtime {@link NullPointerException}s will occur.
	 */
	public DefaultListenerObjectRetriever() {
		listenerObjectCreators = new CopyOnWriteArrayList<>();
	}

	/**
	 * This constructor is recommended, since it forces you to specify the {@link ListenerObjectCreator}. Passing a null value for this
	 * will result in a runtime {@link NullPointerException} whenever the {@link DefaultListenerObjectRetriever} is used.
	 *
	 * @param listenerObjectCreators
	 */
	public DefaultListenerObjectRetriever(ListenerObjectCreator... listenerObjectCreators) {
		this();
		for(ListenerObjectCreator listenerObjectCreator : listenerObjectCreators) {
			this.addListenerObjectCreator(listenerObjectCreator);
		}
	}

	@Override
	public Collection<ListenerObject> getListenerObjects() {

		return this.getListenerObjectsFromAllCreators();
	}

	@Override
	public Collection<ListenerObject> getListenerObjects(Class<?> returnType) {

		return this.getListenerObjectsFromAllCreators()
			.stream()
			.filter(listenerObject -> this.matchesReturnType(listenerObject, returnType))
			.collect(Collectors.toList());
	}

	@Override
	public Collection<ListenerObject> getListenerObjects(String hint) {

		return this.getListenerObjectsFromAllCreators()
			.stream()
			.filter(listenerObject -> this.matchesHint(listenerObject, hint))
			.collect(Collectors.toList());
	}

	@Override
	public Collection<ListenerObject> getListenerObjects(Class<?> returnType, String hint) {

		return this.getListenerObjectsFromAllCreators()
			.stream()
			.filter(listenerObject -> this.matchesHint(listenerObject, hint) && this.matchesReturnType(listenerObject, returnType))
			.collect(Collectors.toList());
	}

	protected Collection<ListenerObject> getListenerObjectsFromAllCreators() {

		log.debug("Getting all ListenerObjects from all ListenerObjectCreators");
		Collection<ListenerObject> listenerObjects = new ArrayList<>();
		for(ListenerObjectCreator listenerObjectCreator : listenerObjectCreators) {
			listenerObjects.addAll(listenerObjectCreator.getListenerObjects());
		}
		return listenerObjects;
	}

	/**
	 * Checks whether the {@link ListenerObject} matches the hint.
	 *
	 * @param listenerObject
	 * @param hint
	 * @return whether the {@link ListenerObject} matches the hint.
	 */
	protected boolean matchesHint(ListenerObject listenerObject, String hint) {

		if(hint == null) {
			throw new IllegalArgumentException("hint cannot be null!");
		}
		return listenerObject.getHints().contains(hint);
	}

	/**
	 * Checks whether the {@link ListenerObject} matches the returntype.
	 *
	 * @param listenerObject
	 * @param returnType
	 * @return whether the {@link ListenerObject} matches the returntype.
	 */
	protected boolean matchesReturnType(ListenerObject listenerObject, Class<?> returnType) {

		if(returnType == null) {
			throw new IllegalArgumentException("returnType cannot be null!");
		}
		return returnType.isAssignableFrom(listenerObject.getReturnType());
	}

	/**
	 * Returns all {@link ListenerObjectCreator}s that are used by this {@link DefaultListenerObjectRetriever}.
	 *
	 * @return  All {@link ListenerObjectCreator}s that are used by this {@link DefaultListenerObjectRetriever}
	 */
	public List<ListenerObjectCreator> getListenerObjectCreators() {
		return listenerObjectCreators;
	}

	/**
	 * Adds a {@link ListenerObjectCreator} to this {@link DefaultListenerObjectRetriever}.
	 *
	 * @param listenerObjectCreator The {@link ListenerObjectCreator} that needs to be added to this {@link DefaultListenerObjectRetriever}
	 */
	public void addListenerObjectCreator(ListenerObjectCreator listenerObjectCreator) {
		listenerObjectCreators.add(listenerObjectCreator);
	}

	/**
	 * Removes all {@link ListenerObjectCreator}s from this {@link DefaultListenerObjectRetriever} and adds only the given one. After invoking this method, only the given
	 * {@link ListenerObjectCreator} will be used by this {@link DefaultListenerObjectRetriever}.
	 *
	 * @param listenerObjectCreator The {@link ListenerObjectCreator} that must be the sole {@link ListenerObjectCreator} used by this {@link DefaultListenerObjectRetriever}
	 */
	public void setListenerObjectCreator(ListenerObjectCreator listenerObjectCreator) {
		listenerObjectCreators.clear();
		listenerObjectCreators.add(listenerObjectCreator);
	}
}