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.sync;
025    
026    import microsoft.exchange.webservices.data.core.EwsUtilities;
027    
028    import java.util.ArrayList;
029    import java.util.Iterator;
030    import java.util.List;
031    
032    /**
033     * Represents a collection of changes as returned by a synchronization
034     * operation.
035     *
036     * @param <TChange> the generic type
037     */
038    public final class ChangeCollection<TChange extends Change> implements
039        Iterable<TChange> {
040    
041      /**
042       * The changes.
043       */
044      private List<TChange> changes = new ArrayList<TChange>();
045    
046      /**
047       * The sync state.
048       */
049      private String syncState;
050    
051      /**
052       * The more changes available.
053       */
054      private boolean moreChangesAvailable;
055    
056      /**
057       * Initializes a new instance of the class.
058       */
059      public ChangeCollection() {
060      }
061    
062      /**
063       * Adds the specified change.
064       *
065       * @param change the change
066       */
067      public void add(TChange change) {
068        EwsUtilities.ewsAssert(change != null, "ChangeList.Add", "change is null");
069        this.changes.add(change);
070      }
071    
072      /**
073       * Gets the number of changes in the collection.
074       *
075       * @return the count
076       */
077      public int getCount() {
078        return this.changes.size();
079      }
080    
081      /**
082       * Gets an individual change from the change collection.
083       *
084       * @param index the index
085       * @return An single change
086       */
087      public TChange getChangeAtIndex(int index) {
088        if (index < 0 || index >= this.getCount()) {
089          throw new IndexOutOfBoundsException(
090              String.format("index %d is out of range [0..%d[.", index, this.getCount()));
091        }
092        return this.changes.get(index);
093      }
094    
095      /**
096       * Gets the SyncState blob returned by a synchronization operation.
097       *
098       * @return the sync state
099       */
100      public String getSyncState() {
101        return this.syncState;
102      }
103    
104      /**
105       * Sets the sync state.
106       *
107       * @param syncState the new sync state
108       */
109      public void setSyncState(String syncState) {
110        this.syncState = syncState;
111      }
112    
113      /**
114       * Gets the SyncState blob returned by a synchronization operation.
115       *
116       * @return the more changes available
117       */
118      public boolean getMoreChangesAvailable() {
119        return this.moreChangesAvailable;
120      }
121    
122      /**
123       * Sets the more changes available.
124       *
125       * @param moreChangesAvailable the new more changes available
126       */
127      public void setMoreChangesAvailable(boolean moreChangesAvailable) {
128        this.moreChangesAvailable = moreChangesAvailable;
129      }
130    
131      /**
132       * Returns an iterator over a set of elements of type T.
133       *
134       * @return an Iterator.
135       */
136      @Override
137      public Iterator<TChange> iterator() {
138        return this.changes.iterator();
139      }
140    
141    }