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
024package microsoft.exchange.webservices.data.property.complex;
025
026import microsoft.exchange.webservices.data.attribute.EditorBrowsable;
027import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
028import microsoft.exchange.webservices.data.core.EwsUtilities;
029import microsoft.exchange.webservices.data.core.service.item.Item;
030import microsoft.exchange.webservices.data.core.enumeration.attribute.EditorBrowsableState;
031import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
032import microsoft.exchange.webservices.data.core.exception.service.local.ServiceObjectPropertyException;
033import microsoft.exchange.webservices.data.core.exception.service.local.ServiceVersionException;
034import microsoft.exchange.webservices.data.security.XmlNodeType;
035import org.apache.commons.logging.Log;
036import org.apache.commons.logging.LogFactory;
037
038import java.util.ArrayList;
039import java.util.Iterator;
040import java.util.List;
041
042/**
043 * Represents a collection of item.
044 *
045 * @param <TItem> the generic type. The type of item the collection contains.
046 */
047@EditorBrowsable(state = EditorBrowsableState.Never)
048public final class ItemCollection<TItem extends Item> extends ComplexProperty
049    implements Iterable<TItem> {
050
051  private static final Log LOG = LogFactory.getLog(ItemCollection.class);
052
053  /**
054   * The item.
055   */
056  private List<TItem> items = new ArrayList<TItem>();
057
058  /**
059   * Initializes a new instance of the "ItemCollection&lt;TItem&gt;" class.
060   */
061  public ItemCollection() {
062    super();
063  }
064
065  /**
066   * Loads from XML.
067   *
068   * @param reader           The reader.
069   * @param localElementName Name of the local element.
070   * @throws Exception the exception
071   */
072  @Override public void loadFromXml(EwsServiceXmlReader reader, String localElementName) throws Exception {
073    reader.ensureCurrentNodeIsStartElement(XmlNamespace.Types,
074        localElementName);
075    if (!reader.isEmptyElement()) {
076      do {
077        reader.read();
078
079        if (reader.getNodeType().getNodeType() == XmlNodeType.START_ELEMENT) {
080          TItem item = EwsUtilities
081              .createEwsObjectFromXmlElementName(Item.class, reader.getService(), reader.getLocalName());
082
083          if (item == null) {
084            reader.skipCurrentElement();
085          } else {
086            try {
087              item.loadFromXml(reader,
088                  true /* clearPropertyBag */);
089            } catch (ServiceObjectPropertyException e) {
090              LOG.error(e);
091            } catch (ServiceVersionException e) {
092              LOG.error(e);
093            }
094
095            this.items.add(item);
096          }
097        }
098      } while (!reader.isEndElement(XmlNamespace.Types,
099          localElementName));
100    } else {
101      reader.read();
102    }
103  }
104
105  /**
106   * Gets the total number of item in the collection.
107   *
108   * @return the count
109   */
110  public int getCount() {
111    return this.items.size();
112  }
113
114  /**
115   * Gets the item at the specified index.
116   *
117   * @param index The zero-based index of the item to get.
118   * @return The item at the specified index.
119   */
120  public TItem getItem(int index) {
121
122    if (index < 0 || index >= this.getCount()) {
123      throw new ArrayIndexOutOfBoundsException("index is out of range.");
124    }
125    return this.items.get(index);
126  }
127
128  /**
129   * Gets an iterator that iterates through the elements of the collection.
130   *
131   * @return An Iterator for the collection.
132   */
133  public Iterator<TItem> getIterator() {
134    return this.items.iterator();
135  }
136
137  /**
138   * Returns an iterator over a set of elements of type T.
139   *
140   * @return an Iterator.
141   */
142  @Override
143  public Iterator<TItem> iterator() {
144    return this.items.iterator();
145  }
146
147}