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.property.complex;
025    
026    import microsoft.exchange.webservices.data.attribute.EditorBrowsable;
027    import microsoft.exchange.webservices.data.core.EwsUtilities;
028    import microsoft.exchange.webservices.data.core.enumeration.attribute.EditorBrowsableState;
029    import microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName;
030    
031    /**
032     * Represents a collection of folder Ids.
033     */
034    @EditorBrowsable(state = EditorBrowsableState.Never)
035    public final class FolderIdCollection extends ComplexPropertyCollection<FolderId> {
036    
037      /**
038       * Initializes a new instance of the <see cref="FolderIdCollection"/> class.
039       */
040      protected FolderIdCollection() {
041        super();
042      }
043    
044      /**
045       * Creates the complex property.
046       *
047       * @param xmlElementName Name of the XML element.
048       * @return Complex property instance.
049       */
050      @Override
051      /**
052       * Creates the complex property.
053       * @param xmlElementName Name of the XML element.
054       * @return FolderId.
055       */
056      protected FolderId createComplexProperty(String xmlElementName) {
057        return new FolderId();
058      }
059    
060      /**
061       * Adds a folder Id to the collection.
062       *
063       * @param folderId The folder Id to add.
064       * @throws Exception the exception
065       */
066      public void add(FolderId folderId) throws Exception {
067        EwsUtilities.validateParam(folderId, "folderId");
068        if (this.contains(folderId)) {
069          throw new IllegalArgumentException("The ID is already in the list.");
070        }
071        this.internalAdd(folderId);
072      }
073    
074      /**
075       * Gets the name of the collection item XML element.
076       *
077       * @param complexProperty accepts FolderId
078       * @return XML element name.
079       */
080      @Override
081      protected String getCollectionItemXmlElementName(FolderId complexProperty) {
082        return complexProperty.getXmlElementName();
083      }
084    
085      /**
086       * Adds a well-known folder to the collection.
087       *
088       * @param folderName the folder name
089       * @return A FolderId encapsulating the specified Id.
090       */
091      public FolderId add(WellKnownFolderName folderName) {
092        FolderId folderId = new FolderId(folderName);
093        if (this.contains(folderId)) {
094          throw new IllegalArgumentException("The ID is already in the list.");
095        }
096        this.internalAdd(folderId);
097        return folderId;
098      }
099    
100      /**
101       * Clears the collection.
102       */
103      public void clear() {
104        this.internalClear();
105      }
106    
107      /**
108       * Removes the folder Id at the specified index.
109       *
110       * @param index The zero-based index of the folder Id to remove.
111       */
112      public void removeAt(int index) {
113        if (index < 0 || index >= this.getCount()) {
114          throw new IndexOutOfBoundsException("index is out of range.");
115        }
116        this.internalRemoveAt(index);
117      }
118    
119      /**
120       * Removes the specified folder Id from the collection.
121       *
122       * @param folderId The folder Id to remove from the collection.
123       * @return True if the folder id was successfully removed from the
124       * collection, false otherwise.
125       * @throws Exception the exception
126       */
127      public boolean remove(FolderId folderId) throws Exception {
128        EwsUtilities.validateParam(folderId, "folderId");
129        return this.internalRemove(folderId);
130      }
131    
132      /**
133       * Removes the specified well-known folder from the collection.
134       *
135       * @param folderName The well-knwon folder to remove from the collection.
136       * @return True if the well-known folder was successfully removed from the
137       * collection, false otherwise.
138       */
139      public boolean remove(WellKnownFolderName folderName) {
140        FolderId folderId = FolderId
141            .getFolderIdFromWellKnownFolderName(folderName);
142        return this.internalRemove(folderId);
143      }
144    
145    }