package org.atlasapi.content;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.util.concurrent.Futures;
import org.atlasapi.application.ApplicationSources;
import org.atlasapi.entity.Id;
import org.atlasapi.equivalence.MergingEquivalentsResolver;
import org.atlasapi.equivalence.ResolvedEquivalents;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Set;
import java.util.concurrent.TimeUnit;

public class MergingEquivalentsResolverBackedContainerSummaryResolver implements ContainerSummaryResolver {

    private static final Logger log = LoggerFactory.getLogger(MergingEquivalentsResolverBackedContainerSummaryResolver.class);
    private final MergingEquivalentsResolver<Content> contentResolver;

    public MergingEquivalentsResolverBackedContainerSummaryResolver(MergingEquivalentsResolver<Content> contentResolver) {
        this.contentResolver = contentResolver;
    }

    @Override
    public Optional<ContainerSummary> resolveContainerSummary(Id id, ApplicationSources applicationSources) {
        ResolvedEquivalents<Content> contentResolved = null;
        try {
            contentResolved = Futures.get(
                    contentResolver.resolveIds(ImmutableSet.of(id), applicationSources, null),
                    1, TimeUnit.MINUTES,
                    Exception.class
            );
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Set<Content> equivalentContent = contentResolved.get(id);
        if(equivalentContent.isEmpty()) {
            log.warn("Reference to non-existent container with ID {}", id);
            return Optional.absent();
        }

        Content content = Iterables.getFirst(equivalentContent, null);
        if(content instanceof Container) {
            return Optional.of((((Container) content).toSummary()));
        }

        log.warn(
                "Incorrect type of content with ID {}, expected Container, got {}",
                id,
                content.getClass().getName()
        );
        return Optional.absent();
    }
}

@@ -64,16 +64,58 @@
 tional;%0A
+import com.google.common.base.Throwables;%0A
 import c
@@ -149,24 +149,24 @@
 mutableSet;%0A
-
 import com.g
@@ -562,16 +562,82 @@
 eUnit;%0A%0A
+import static com.google.common.base.Preconditions.checkNotNull;%0A%0A
 public c
@@ -1090,16 +1090,29 @@
 olver = 
+checkNotNull(
 contentR
@@ -1118,16 +1118,17 @@
 Resolver
+)
 ;%0A    %7D%0A
@@ -1605,34 +1605,28 @@
     
-t
+T
 hrow
- new RuntimeException
+ables.propagate
 (e);

Check for null objects in ContainerSummaryResolver.