001    /*
002     * The MIT License
003     * Copyright (c) 2012 Microsoft Corporation
004     *
005     * Permission is hereby granted, free of charge, to any person obtaining a copy
006     * of this software and associated documentation files (the "Software"), to deal
007     * in the Software without restriction, including without limitation the rights
008     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
009     * copies of the Software, and to permit persons to whom the Software is
010     * furnished to do so, subject to the following conditions:
011     *
012     * The above copyright notice and this permission notice shall be included in
013     * all copies or substantial portions of the Software.
014     *
015     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
021     * THE SOFTWARE.
022     */
023    
024    package microsoft.exchange.webservices.data.core.response;
025    
026    import microsoft.exchange.webservices.data.core.EwsUtilities;
027    import microsoft.exchange.webservices.data.core.enumeration.service.ServiceResult;
028    
029    import java.util.Enumeration;
030    import java.util.Iterator;
031    import java.util.Vector;
032    
033    /**
034     * Represents a strongly typed list of service response.
035     *
036     * @param <TResponse> The type of response stored in the list.
037     */
038    public final class ServiceResponseCollection<TResponse extends ServiceResponse>
039        implements Iterable<TResponse> {
040    
041      /**
042       * The response.
043       */
044      private Vector<TResponse> responses = new Vector<TResponse>();
045    
046      /**
047       * The overall result.
048       */
049      private ServiceResult overallResult = ServiceResult.Success;
050    
051      /**
052       * Initializes a new instance.
053       */
054      public ServiceResponseCollection() {
055    
056      }
057    
058      /**
059       * Adds specified response.
060       *
061       * @param response The response.
062       */
063      public void add(TResponse response) {
064        EwsUtilities.ewsAssert(response != null, "EwsResponseList.Add", "response is null");
065        if (response.getResult().ordinal() > this.overallResult.ordinal()) {
066          this.overallResult = response.getResult();
067        }
068        this.responses.add(response);
069      }
070    
071      /**
072       * Gets the total number of response in the list.
073       *
074       * @return total number of response in the list.
075       */
076      public int getCount() {
077        return this.responses.size();
078      }
079    
080      /**
081       * Gets the response at the specified index.
082       *
083       * @param index The zero-based index of the response to get.
084       * @return The response at the specified index.
085       * @throws IndexOutOfBoundsException the index out of bounds exception
086       */
087      public TResponse getResponseAtIndex(int index)
088          throws IndexOutOfBoundsException {
089        if (index < 0 || index >= this.getCount()) {
090          throw new IndexOutOfBoundsException("Index out of Range");
091        }
092        return this.responses.get(index);
093      }
094    
095      /**
096       * Gets a value indicating the overall result of the request that
097       * generated this response collection. If all of the response have their
098       * Result property set to Success, OverallResult returns Success. If at
099       * least one response has its Result property set to Warning and all other
100       * response have their Result property set to Success, OverallResult
101       * returns Warning. If at least one response has a its Result set to Error,
102       * OverallResult returns Error.
103       *
104       * @return the overall result
105       */
106      public ServiceResult getOverallResult() {
107        return this.overallResult;
108      }
109    
110      /**
111       * Returns an iterator over a set of elements of type T.
112       *
113       * @return an Iterator.
114       */
115      @Override
116      public Iterator<TResponse> iterator() {
117        return responses.iterator();
118      }
119    
120      /**
121       * Gets the enumerator.
122       *
123       * @return the enumerator
124       */
125      public Enumeration<TResponse> getEnumerator() {
126        return this.responses.elements();
127      }
128    }