Search in sources :

Example 1 with IFactory

use of com.minecolonies.api.colony.requestsystem.factory.IFactory in project minecolonies by Minecolonies.

the class StandardFactoryController method getFactoryForInput.

@Override
public <INPUT> IFactory<INPUT, ?> getFactoryForInput(@NotNull final TypeToken<? extends INPUT> inputClass) throws IllegalArgumentException {
    final ITypeOverrideHandler<?> inputOverrideHandler = typeOverrideHandlers.stream().filter(h -> h.matches(inputClass)).findFirst().orElse(null);
    final TypeToken input = inputOverrideHandler != null ? inputOverrideHandler.getOutputType() : inputClass;
    final Set<TypeToken> secondaryInputSet = ReflectionUtils.getSuperClasses(input);
    for (final TypeToken secondaryInputClass : secondaryInputSet) {
        final Set<IFactory> factories = primaryInputMappings.get(secondaryInputClass);
        if (factories != null && !factories.isEmpty()) {
            return factories.stream().findFirst().get();
        }
    }
    throw new IllegalArgumentException("The given input type is not a input of a factory.");
}
Also used : TypeToken(com.google.common.reflect.TypeToken) IFactory(com.minecolonies.api.colony.requestsystem.factory.IFactory)

Example 2 with IFactory

use of com.minecolonies.api.colony.requestsystem.factory.IFactory in project minecolonies by Minecolonies.

the class StandardFactoryController method getFactoryForIO.

@SuppressWarnings(Suppression.UNCHECKED)
@Override
public <INPUT, OUTPUT> IFactory<INPUT, OUTPUT> getFactoryForIO(@NotNull final TypeToken<? extends INPUT> inputClass, @NotNull final TypeToken<? extends OUTPUT> outputClass) throws IllegalArgumentException {
    final ITypeOverrideHandler<?> inputOverrideHandler = typeOverrideHandlers.stream().filter(h -> h.matches(inputClass)).findFirst().orElse(null);
    final ITypeOverrideHandler<OUTPUT> outputOverrideHandler = typeOverrideHandlers.stream().filter(h -> h.matches(outputClass)).findFirst().orElse(null);
    final TypeToken input = inputOverrideHandler != null ? inputOverrideHandler.getOutputType() : inputClass;
    final TypeToken output = outputOverrideHandler != null ? outputOverrideHandler.getOutputType() : outputClass;
    try {
        // Request from cache or search.
        return secondaryMappingsCache.get(new Tuple<>(input, output), () -> {
            final Set<TypeToken> secondaryInputSet = ReflectionUtils.getSuperClasses(input);
            for (final TypeToken secondaryInputClass : secondaryInputSet) {
                final Set<IFactory> factories = primaryInputMappings.get(secondaryInputClass);
                if (factories == null || factories.isEmpty()) {
                    continue;
                }
                for (final IFactory factory : factories) {
                    final Set<TypeToken> secondaryOutputSet = ReflectionUtils.getSuperClasses(factory.getFactoryOutputType());
                    if (secondaryOutputSet.contains(output)) {
                        return factory;
                    }
                }
            }
            throw new IllegalArgumentException("No factory found with the given IO types: " + input + " ->" + output);
        });
    } catch (final ExecutionException e) {
        throw (IllegalArgumentException) new IllegalArgumentException("No factory found with the given IO types: " + input + " ->" + output).initCause(e);
    }
}
Also used : TypeToken(com.google.common.reflect.TypeToken) IFactory(com.minecolonies.api.colony.requestsystem.factory.IFactory) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

TypeToken (com.google.common.reflect.TypeToken)2 IFactory (com.minecolonies.api.colony.requestsystem.factory.IFactory)2 ExecutionException (java.util.concurrent.ExecutionException)1