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.misc;
025
026import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
027import microsoft.exchange.webservices.data.core.service.item.Item;
028import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
029import microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException;
030import microsoft.exchange.webservices.data.property.complex.ItemId;
031
032import java.util.ArrayList;
033import java.util.Iterator;
034import java.util.List;
035
036/**
037 * Represents a list a abstracted item Ids.
038 */
039public class ItemIdWrapperList implements Iterable<AbstractItemIdWrapper> {
040
041  /**
042   * The item ids.
043   */
044  private List<AbstractItemIdWrapper> itemIds =
045      new ArrayList<AbstractItemIdWrapper>();
046
047  /**
048   * Initializes a new instance of the class.
049   */
050  public ItemIdWrapperList() {
051  }
052
053  /**
054   * Adds the specified item.
055   *
056   * @param item the item
057   * @throws ServiceLocalException the service local exception
058   */
059  protected void add(Item item) throws ServiceLocalException {
060    this.itemIds.add(new ItemWrapper(item));
061
062  }
063
064  /**
065   * Adds the specified item.
066   *
067   * @param items the item
068   * @throws ServiceLocalException the service local exception
069   */
070  public void addRangeItem(Iterable<Item> items)
071      throws ServiceLocalException {
072    for (Item item : items) {
073      this.add(item);
074    }
075  }
076
077  /**
078   * Adds the range.
079   *
080   * @param itemIds the item ids
081   */
082  public void addRange(Iterable<ItemId> itemIds) {
083    for (ItemId itemId : itemIds) {
084      this.add(itemId);
085    }
086  }
087
088  /**
089   * Adds the specified item id.
090   *
091   * @param itemId the item id
092   */
093  protected void add(ItemId itemId) {
094    this.itemIds.add(new ItemIdWrapper(itemId));
095
096  }
097
098  /**
099   * Writes to XML.
100   *
101   * @param writer         the writer
102   * @param ewsNamesapce   the ews namesapce
103   * @param xmlElementName the xml element name
104   * @throws Exception the exception
105   */
106  public void writeToXml(EwsServiceXmlWriter writer, XmlNamespace ewsNamesapce, String xmlElementName) throws Exception {
107    if (this.getCount() > 0) {
108      writer.writeStartElement(ewsNamesapce, xmlElementName);
109
110      for (AbstractItemIdWrapper itemIdWrapper : this.itemIds) {
111        itemIdWrapper.writeToXml(writer);
112      }
113
114      writer.writeEndElement();
115    }
116  }
117
118  /**
119   * Gets the count.
120   *
121   * @return the count
122   */
123  public int getCount() {
124    return this.itemIds.size();
125  }
126
127  /**
128   * Gets the item at the specified index.
129   *
130   * @param i the i
131   * @return the item id wrapper list
132   */
133  public Item getItemIdWrapperList(int i) {
134    return this.itemIds.get(i).getItem();
135  }
136
137  /**
138   * Gets an Iterator that iterates through the elements of the collection.
139   *
140   * @return An IEnumerator for the collection
141   */
142  @Override
143  public Iterator<AbstractItemIdWrapper> iterator() {
144
145    return itemIds.iterator();
146  }
147}