/**
  * Initializes a new instance of the StreamingSubscriptionConnection class.
  *
  * @param service The ExchangeService instance this connection uses to connect to the server.
  * @param subscriptions Iterable subcriptions
  * @param lifetime The maximum time, in minutes, the connection will remain open. Lifetime must be
  *     between 1 and 30.
  * @throws Exception
  */
 public StreamingSubscriptionConnection(
     ExchangeService service, Iterable<StreamingSubscription> subscriptions, int lifetime)
     throws Exception {
   this(service, lifetime);
   EwsUtilities.validateParamCollection(subscriptions.iterator(), "subscriptions");
   for (StreamingSubscription subscription : subscriptions) {
     this.subscriptions.put(subscription.getId(), subscription);
   }
 }
  /**
   * Adds a subscription to this connection.
   *
   * @param subscription The subscription to add.
   * @throws Exception Thrown when AddSubscription is called while connected.
   */
  public void addSubscription(StreamingSubscription subscription) throws Exception {
    this.throwIfDisposed();
    EwsUtilities.validateParam(subscription, "subscription");
    this.validateConnectionState(false, "Subscriptions can't be added to an open connection.");

    synchronized (this) {
      if (this.subscriptions.containsKey(subscription.getId())) {
        return;
      }
      this.subscriptions.put(subscription.getId(), subscription);
    }
  }
  /**
   * Removes the specified streaming subscription from the connection.
   *
   * @param subscription The subscription to remove.
   * @throws Exception Thrown when RemoveSubscription is called while connected.
   */
  public void removeSubscription(StreamingSubscription subscription) throws Exception {
    this.throwIfDisposed();

    EwsUtilities.validateParam(subscription, "subscription");

    this.validateConnectionState(false, "Subscriptions can't be removed from an open connection.");

    synchronized (this) {
      this.subscriptions.remove(subscription.getId());
    }
  }
  /**
   * Removes the specified streaming subscription from the connection.
   *
   * @param subscription The subscription to remove.
   * @throws Exception
   * @exception Thrown when RemoveSubscription is called while connected.
   */
  public void removeSubscription(StreamingSubscription subscription) throws Exception {
    this.throwIfDisposed();

    EwsUtilities.validateParam(subscription, "subscription");

    this.validateConnectionState(false, Strings.CannotRemoveSubscriptionFromLiveConnection);

    synchronized (this) {
      this.subscriptions.remove(subscription.getId());
    }
  }