package moe.banana.jsonapi2;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.Iterator;

public final class HasMany<T extends Resource> implements Relationship, Iterable<T>, Serializable {

    public final ResourceLinkage[] linkages;

    private final Class<T> type;
    private final Resource resource;

    HasMany(Class<T> type, Resource resource, ResourceLinkage[] linkages) {
        this.type = type;
        this.resource = resource;
        this.linkages = linkages;
    }

    @SuppressWarnings("unchecked")
    public T[] getAll() throws ResourceNotFoundException {
        T[] array = (T[]) Array.newInstance(type, linkages.length);
        for (int i = 0; i != linkages.length; i++) {
            array[i] = (T) resource._doc.find(linkages[i]);
        }
        return array;
    }

    @Override
    public Iterator<T> iterator() {
        return new Iterator<T>() {
            int i = 0;

            @Override
            public boolean hasNext() {
                return linkages != null && i != linkages.length;
            }

            @Override
            @SuppressWarnings("unchecked")
            public T next() {
                return (T) resource._doc.find(linkages[i++]);
            }
        };
    }

    @SuppressWarnings("unchecked")
    public static <T extends Resource> HasMany<T> create(Resource resource, T... linked) {
        ResourceLinkage[] linkages = new ResourceLinkage[linked.length];
        for (int i = 0; i != linkages.length; i++) {
            linkages[i] = ResourceLinkage.of(linked[i]);
        }
        return create(resource, (Class<T>) linked.getClass().getComponentType(), linkages);
    }

    public static HasMany<? extends Resource> create(Resource resource, ResourceLinkage... linkage) {
        return create(resource, Resource.class, linkage);
    }

    public static <T extends Resource> HasMany<T> create(Resource resource, Class<T> componentType, ResourceLinkage... linkage) {
        return new HasMany<>(componentType, resource, linkage);
    }
}

@@ -817,24 +817,210 @@
 ray;%0A    %7D%0A%0A
+    /**%0A     * Iterates over linked resources.%0A     * @return iterator whose %7B@link Iterator#next()%7D returns linked Resource or null if linkage cannot be resolved with document.%0A     */%0A
     @Overrid
@@ -1017,24 +1017,24 @@
   @Override%0A
-
     public I
@@ -1344,32 +1344,58 @@
 blic T next() %7B%0A
+                try %7B%0A    
                 
@@ -1436,24 +1436,131 @@
 ages%5Bi++%5D);%0A
+                %7D catch (ResourceNotFoundException e) %7B%0A                    return null;%0A                %7D%0A
             

Make iteration over HasMany safe to dangling linkage