package io.jenkins.blueocean.rest.model;

import com.google.common.collect.Iterables;
import io.jenkins.blueocean.commons.stapler.TreeResponse;
import io.jenkins.blueocean.rest.pageable.Pageable;
import io.jenkins.blueocean.rest.pageable.Pageables;
import org.kohsuke.stapler.WebMethod;
import org.kohsuke.stapler.verb.GET;

import java.util.AbstractCollection;
import java.util.Iterator;

/**
 * Stapler-bound REST endpoint for a collection of objects.
 *
 * @author Kohsuke Kawaguchi
 */
//TODO: extending AbstractCollection is workaround and should be removed once Stapler starts serializing Iterable
public abstract class Container<T> extends AbstractCollection<T> implements Pageable<T> {
    /**
     * Gets the individual member by its name
     *
     * @param name identifying the member
     *
     * @return individual member
     */
    public abstract T get(String name);

    // public abstract T create(..)

    // for stapler
    public final T getDynamic(String name) {
        return get(name);
    }

    @Override
    public int size() {
        return 0;
    }

    /**
     * Base implementation of pagination that is dumb.
     */
    @Override
    public Iterator<T> iterator(int start, int limit) {
        return Pageables.slice(iterator(),start,limit);
    }

    /**
     * When GET is requested on '/', serve the collection
     * @return collection in this container
     */
    @WebMethod(name="") @GET @TreeResponse
    // if we wanted collection listing to take filtering parameters, we can do that with one additional parameter
    public Object[] list(/*@QueryParameter Query q*/) {
        // TODO: pagenation
        return Iterables.toArray(this,Object.class);
    }
}

@@ -322,45 +322,8 @@
 T;%0A%0A
-import java.util.AbstractCollection;%0A
 impo
@@ -345,16 +345,16 @@
 erator;%0A
+
 %0A/**%0A * 
@@ -450,122 +450,8 @@
  */%0A
-//TODO: extending AbstractCollection is workaround and should be removed once Stapler starts serializing Iterable%0A
 publ
@@ -484,38 +484,8 @@
 r%3CT%3E
- extends AbstractCollection%3CT%3E
  imp
@@ -795,24 +795,24 @@
 ing name) %7B%0A
+
         retu
@@ -836,71 +836,8 @@
  %7D%0A%0A
-    @Override%0A    public int size() %7B%0A        return 0;%0A    %7D%0A%0A
     

Work around no longer needed with Stapler 1.240