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

io.carml.engine.sourceresolver.CompositeSourceResolver Maven / Gradle / Ivy

package io.carml.engine.sourceresolver;

import com.google.common.collect.ImmutableSet;
import java.util.Optional;
import java.util.Set;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;

@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class CompositeSourceResolver implements SourceResolver {

  private final Set resolvers;

  public static CompositeSourceResolver of(SourceResolver... sourceResolvers) {
    return of(ImmutableSet.copyOf(sourceResolvers));
  }

  public static CompositeSourceResolver of(Set sourceResolvers) {
    return new CompositeSourceResolver(sourceResolvers);
  }

  @Override
  public Optional apply(Object source) {
    return resolvers.stream()
        .map(resolver -> resolver.apply(source))
        .filter(Optional::isPresent)
        .map(Optional::get)
        .findFirst();
  }

}