001/*
002 * Copyright 2011-2016 UnboundID Corp.
003 *
004 * This program is free software; you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License (GPLv2 only)
006 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
007 * as published by the Free Software Foundation.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program; if not, see <http://www.gnu.org/licenses>.
016 */
017
018package com.unboundid.scim.sdk;
019
020import com.unboundid.scim.data.BaseResource;
021import com.unboundid.scim.marshal.Marshaller;
022
023import java.io.OutputStream;
024import java.util.Iterator;
025import java.util.List;
026
027/**
028 * Represents a list of SCIM resources returned by the service provider from
029 * a query/listing request.
030 */
031public class Resources<R extends BaseResource> implements Iterable<R>,
032    SCIMResponse
033{
034  private final int totalResults;
035  private final int startIndex;
036  private final List<R> resourceList;
037
038  /**
039   * Create a new resources response from the provided list. The number of
040   * total results will be set to the size of the list and the default value of
041   * 1 for start index.
042   *
043   * @param resourceList The list of resources to create the response from.
044   */
045  public Resources(final List<R> resourceList)
046  {
047    this(resourceList, resourceList.size(), 1);
048  }
049
050  /**
051   * Create a new resources response from the provided list.
052   *
053   * @param resourceList The list of resources to create the response from.
054   * @param totalResults The total number of results matching the Consumer query
055   * @param startIndex The 1-based index of the first result in the current set
056   *                   of search results
057   */
058  public Resources(final List<R> resourceList, final int totalResults,
059                   final int startIndex)
060  {
061    this.totalResults = totalResults;
062    this.startIndex = startIndex;
063    this.resourceList = resourceList;
064  }
065
066  /**
067   * Retrieves the 1-based index of the first result in the current set of
068   * search results.
069   *
070   * @return The 1-based index of the first result in the current set of
071   *         search results.
072   */
073  public long getStartIndex()
074  {
075    return startIndex;
076  }
077
078  /**
079   * Retrieves the total number of results matching the Consumer query.
080   *
081   * @return The total number of results matching the Consumer query.
082   */
083  public long getTotalResults()
084  {
085    return totalResults;
086  }
087
088  /**
089   * Retrieves the number of search results returned in this query response.
090   *
091   * @return The number of search results returned in this query response.
092   */
093  public int getItemsPerPage()
094  {
095    return resourceList.size();
096  }
097
098  /**
099   * {@inheritDoc}
100   */
101  public Iterator<R> iterator() {
102    return resourceList.iterator();
103  }
104
105  /**
106   * {@inheritDoc}
107   */
108  public void marshal(final Marshaller marshaller,
109                      final OutputStream outputStream)
110    throws Exception
111  {
112    marshaller.marshal(this, outputStream);
113  }
114}