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.misc;
025    
026    import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027    import microsoft.exchange.webservices.data.core.EwsUtilities;
028    import microsoft.exchange.webservices.data.core.ExchangeService;
029    import microsoft.exchange.webservices.data.core.XmlAttributeNames;
030    import microsoft.exchange.webservices.data.core.XmlElementNames;
031    import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
032    import microsoft.exchange.webservices.data.core.exception.misc.ArgumentOutOfRangeException;
033    
034    import java.util.ArrayList;
035    import java.util.Iterator;
036    import java.util.List;
037    
038    /**
039     * Represents a list of suggested name resolutions.
040     */
041    public final class NameResolutionCollection implements
042        Iterable<NameResolution> {
043    
044      /**
045       * The service.
046       */
047      private ExchangeService service;
048    
049      /**
050       * The includes all resolutions.
051       */
052      private boolean includesAllResolutions;
053    
054      /**
055       * The item.
056       */
057      private List<NameResolution> items = new ArrayList<NameResolution>();
058    
059      /**
060       * Represents a list of suggested name resolutions.
061       *
062       * @param service the service
063       */
064      public NameResolutionCollection(ExchangeService service) {
065        EwsUtilities.ewsAssert(service != null, "NameResolutionSet.ctor", "service is null.");
066        this.service = service;
067      }
068    
069      /**
070       * Loads from XML.
071       *
072       * @param reader the reader
073       * @throws Exception the exception
074       */
075      public void loadFromXml(EwsServiceXmlReader reader) throws Exception {
076        reader.readStartElement(XmlNamespace.Messages,
077            XmlElementNames.ResolutionSet);
078        int totalItemsInView = reader.readAttributeValue(Integer.class,
079            XmlAttributeNames.TotalItemsInView);
080        this.includesAllResolutions = reader.readAttributeValue(Boolean.class,
081            XmlAttributeNames.IncludesLastItemInRange);
082    
083        for (int i = 0; i < totalItemsInView; i++) {
084          NameResolution nameResolution = new NameResolution(this);
085          nameResolution.loadFromXml(reader);
086          this.items.add(nameResolution);
087        }
088    
089        reader.readEndElement(XmlNamespace.Messages,
090            XmlElementNames.ResolutionSet);
091      }
092    
093      /**
094       * Gets the session. <value>The session.</value>
095       *
096       * @return the session
097       */
098      protected ExchangeService getSession() {
099        return this.service;
100      }
101    
102      /**
103       * Gets the total number of elements in the list.
104       *
105       * @return the count
106       */
107      public int getCount() {
108        return this.items.size();
109      }
110    
111      /**
112       * Gets a value indicating whether more suggested resolutions are available.
113       * ResolveName only returns a maximum of 100 name resolutions. When
114       * IncludesAllResolutions is false, there were more than 100 matching names
115       * on the server. To narrow the search, provide a more precise name to
116       * ResolveName.
117       *
118       * @return the includes all resolutions
119       */
120      public boolean getIncludesAllResolutions() {
121        return this.includesAllResolutions;
122      }
123    
124      /**
125       * Gets the name resolution at the specified index.
126       *
127       * @param index the index
128       * @return The name resolution at the speicfied index.
129       * @throws ArgumentOutOfRangeException the argument out of range exception
130       */
131      public NameResolution nameResolutionCollection(int index)
132          throws ArgumentOutOfRangeException {
133        if (index < 0 || index >= this.getCount()) {
134          throw new ArgumentOutOfRangeException("index", "index is out of range.");
135        }
136    
137        return this.items.get(index);
138      }
139    
140      /*
141       * (non-Javadoc)
142       *
143       * @see java.lang.Iterable#iterator()
144       */
145      @Override
146      public Iterator<NameResolution> iterator() {
147    
148        return items.iterator();
149      }
150    }