001/*
002 * Copyright 2015-2024 Ping Identity Corporation
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.scim2.client.requests;
019
020import com.fasterxml.jackson.databind.node.ObjectNode;
021import com.unboundid.scim2.client.SearchResultHandler;
022import com.unboundid.scim2.common.annotations.NotNull;
023import com.unboundid.scim2.common.annotations.Nullable;
024import com.unboundid.scim2.common.messages.ListResponse;
025
026import java.util.LinkedHashMap;
027import java.util.LinkedList;
028import java.util.List;
029import java.util.Map;
030
031/**
032 * A builder for ListResponses that is also a SearchResultHandler
033 * implementation.
034 */
035public class ListResponseBuilder<T>
036    implements SearchResultHandler<T>
037{
038  @Nullable
039  private Integer totalResults;
040
041  @NotNull
042  private List<T> resources = new LinkedList<T>();
043
044  @Nullable
045  private Integer startIndex;
046
047  @Nullable
048  private Integer itemsPerPage;
049
050  /**
051   * {@inheritDoc}
052   */
053  public void startIndex(final int startIndex)
054  {
055    this.startIndex = startIndex;
056  }
057
058  /**
059   * {@inheritDoc}
060   */
061  public void itemsPerPage(final int itemsPerPage)
062  {
063    this.itemsPerPage = itemsPerPage;
064  }
065
066  /**
067   * {@inheritDoc}
068   */
069  public void totalResults(final int totalResults)
070  {
071    this.totalResults = totalResults;
072  }
073
074  /**
075   * {@inheritDoc}
076   */
077  public boolean resource(@NotNull final T scimResource)
078  {
079    this.resources.add(scimResource);
080    return true;
081  }
082
083  /**
084   * {@inheritDoc}
085   * <p>
086   * This method currently does not perform any action and should not be used.
087   */
088  public void extension(@NotNull final String urn,
089                        @NotNull final ObjectNode extensionObjectNode)
090  {
091    // TODO: do nothing for now
092  }
093
094  /**
095   * Builds a List Response.
096   *
097   * @return generated ListResponse.
098   */
099  @NotNull
100  public ListResponse<T> build()
101  {
102    final Map<String,Object> properties = new LinkedHashMap<String,Object>();
103    properties.put("totalResults", totalResults == null ?
104      resources.size() : totalResults);
105    properties.put("resources", resources);
106    properties.put("startIndex", startIndex);
107    properties.put("itemsPerPage", itemsPerPage);
108    return new ListResponse<T>(properties);
109  }
110}