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

com.github.thorbenkuck.keller.repository.ConditionalSupplyingImpl Maven / Gradle / Ivy

The newest version!
package com.github.thorbenkuck.keller.repository;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Optional;

final class ConditionalSupplyingImpl implements ConditionalSupplying {

	private final RepositoryInternals repositoryInternals;
	private final Class clazz;
	private final ActionStack actionStack;

	ConditionalSupplyingImpl(final RepositoryInternals repositoryInternals, final Class clazz) {
		this(repositoryInternals, clazz, new ActionStack<>(repositoryInternals, clazz));
	}

	ConditionalSupplyingImpl(final RepositoryInternals repositoryInternals, final Class clazz, final ActionStack actionStack) {
		this.repositoryInternals = repositoryInternals;
		this.clazz = clazz;
		this.actionStack = actionStack;
	}

	@Override
	public T getFirst() {
		final Optional toReturn = repositoryInternals.stream()
				.filter(o -> o.getClass().equals(clazz))
				.findFirst();
		return (T) toReturn.orElse(null);
	}

	@Override
	public T getAny() {
		final Optional toReturn = repositoryInternals.stream()
				.filter(o -> o.getClass().equals(clazz))
				.findAny();
		return (T) toReturn.orElse(null);
	}

	@Override
	public Collection getAll() {
		final Collection toReturn = new ArrayList<>();
		repositoryInternals.stream()
				.filter(o -> o.getClass().equals(clazz))
				.forEach(toReturn::add);
		return (Collection) toReturn;
	}

	@Override
	public NotPresentHandler ifNotPresent() {
		return new NotPresentHandlerImpl<>(actionStack);
	}

	@Override
	public RepositoryCondition withRequirement() {
		return new RepositoryConditionImpl<>(actionStack);
	}
}