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.recurrence;
025
026import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028import microsoft.exchange.webservices.data.core.EwsUtilities;
029import microsoft.exchange.webservices.data.core.XmlElementNames;
030import microsoft.exchange.webservices.data.core.enumeration.property.time.DayOfTheWeek;
031import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
032import microsoft.exchange.webservices.data.core.exception.misc.ArgumentOutOfRangeException;
033import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
034import microsoft.exchange.webservices.data.property.complex.ComplexProperty;
035import org.apache.commons.lang3.StringUtils;
036
037import javax.xml.stream.XMLStreamException;
038
039import java.util.ArrayList;
040import java.util.Iterator;
041import java.util.List;
042
043/**
044 * Represents a collection of DayOfTheWeek values.
045 */
046public final class DayOfTheWeekCollection extends ComplexProperty implements
047    Iterable<DayOfTheWeek> {
048
049  /**
050   * The item.
051   */
052  private List<DayOfTheWeek> items = new ArrayList<DayOfTheWeek>();
053
054  /**
055   * Initializes a new instance of the class.
056   */
057  public DayOfTheWeekCollection() {
058  }
059
060  /**
061   * Convert to string.
062   *
063   * @param separator the separator
064   * @return String representation of collection.
065   */
066  private String toString(String separator) {
067    if (this.getCount() == 0) {
068      return "";
069    } else {
070      // String[] daysOfTheWeekArray = new String[this.getCount()];
071      StringBuilder daysOfTheWeekstr = new StringBuilder();
072
073      for (int i = 0; i < this.getCount(); i++) {
074        // daysOfTheWeekArray[i] = item.get(i).toString();
075        if (daysOfTheWeekstr.length() == 0) {
076          daysOfTheWeekstr.append(items.get(i).toString());
077        } else {
078          daysOfTheWeekstr.append(separator);
079          daysOfTheWeekstr.append(items.get(i).toString());
080        }
081      }
082
083      return daysOfTheWeekstr.toString();
084    }
085  }
086
087  /**
088   * Loads from XML.
089   *
090   * @param reader         The reader.
091   * @param xmlElementName Name of the XML element.
092   * @throws Exception the exception
093   */
094  public void loadFromXml(EwsServiceXmlReader reader, String xmlElementName)
095      throws Exception {
096    reader.ensureCurrentNodeIsStartElement(XmlNamespace.Types,
097        xmlElementName);
098    EwsUtilities.parseEnumValueList(DayOfTheWeek.class, this.items, reader.readElementValue(), ' ');
099  }
100
101  /**
102   * Gets the request version.
103   *
104   * @param writer         the writer
105   * @param xmlElementName the xml element name
106   * @throws XMLStreamException the XML stream exception
107   * @throws ServiceXmlSerializationException the service xml serialization exception
108   */
109  @Override public void writeToXml(EwsServiceXmlWriter writer, String xmlElementName)
110      throws XMLStreamException, ServiceXmlSerializationException {
111    String daysOfWeekAsString = this.toString(" ");
112
113    if (!StringUtils.isEmpty(daysOfWeekAsString)) {
114      writer.writeElementValue(XmlNamespace.Types,
115          XmlElementNames.DaysOfWeek, daysOfWeekAsString);
116    }
117  }
118
119  /**
120   * Builds string representation of the collection.
121   *
122   * @return A comma-delimited string representing the collection.
123   */
124  @Override
125  public String toString() {
126    return this.toString(",");
127  }
128
129  /**
130   * Adds a day to the collection if it is not already present.
131   *
132   * @param dayOfTheWeek The day to add.
133   */
134  public void add(DayOfTheWeek dayOfTheWeek) {
135    if (!this.items.contains(dayOfTheWeek)) {
136      this.items.add(dayOfTheWeek);
137      this.changed();
138    }
139  }
140
141  /**
142   * Adds multiple days to the collection if they are not already present.
143   *
144   * @param daysOfTheWeek The days to add.
145   */
146  public void addRange(Iterator<DayOfTheWeek> daysOfTheWeek) {
147    while (daysOfTheWeek.hasNext()) {
148      this.add(daysOfTheWeek.next());
149    }
150  }
151
152  /**
153   * Clears the collection.
154   */
155  public void clear() {
156    if (this.getCount() > 0) {
157      this.items.clear();
158      this.changed();
159    }
160  }
161
162  /**
163   * Remove a specific day from the collection.
164   *
165   * @param dayOfTheWeek the day of the week
166   * @return True if the day was removed from the collection, false otherwise.
167   */
168  public boolean remove(DayOfTheWeek dayOfTheWeek) {
169    boolean result = this.items.remove(dayOfTheWeek);
170
171    if (result) {
172      this.changed();
173    }
174    return result;
175  }
176
177  /**
178   * Removes the day at a specific index.
179   *
180   * @param index the index
181   * @throws ArgumentOutOfRangeException the argument out of range exception
182   */
183  public void removeAt(int index) throws ArgumentOutOfRangeException {
184    if (index < 0 || index >= this.getCount()) {
185      throw new ArgumentOutOfRangeException("index", "index is out of range.");
186    }
187
188    this.items.remove(index);
189    this.changed();
190  }
191
192  /**
193   * Gets the DayOfTheWeek at a specific index in the collection.
194   *
195   * @param index the index
196   * @return DayOfTheWeek at index
197   */
198  public DayOfTheWeek getWeekCollectionAtIndex(int index) {
199    return this.items.get(index);
200  }
201
202  /**
203   * Gets the number of days in the collection.
204   *
205   * @return the count
206   */
207  public int getCount() {
208    return this.items.size();
209  }
210
211  /*
212   * (non-Javadoc)
213   *
214   * @see java.lang.Iterable#iterator()
215   */
216  @Override
217  public Iterator<DayOfTheWeek> iterator() {
218    return this.items.iterator();
219  }
220
221}