ItemPublishEvent.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.pubsub;

  18. import java.util.Collections;
  19. import java.util.Date;
  20. import java.util.List;

  21. /**
  22.  * Represents an event generated by an item(s) being published to a node.
  23.  *
  24.  * @author Robin Collier
  25.  */
  26. public class ItemPublishEvent<T extends Item> extends SubscriptionEvent {
  27.     private List<T> items;
  28.     private Date originalDate;

  29.     /**
  30.      * Constructs an <code>ItemPublishEvent</code> with the provided list
  31.      * of {@link Item} that were published.
  32.      *
  33.      * @param nodeId The id of the node the event came from
  34.      * @param eventItems The list of {@link Item} that were published
  35.      */
  36.     public ItemPublishEvent(String nodeId, List<T> eventItems) {
  37.         super(nodeId);
  38.         items = eventItems;
  39.     }

  40.     /**
  41.      * Constructs an <code>ItemPublishEvent</code> with the provided list
  42.      * of {@link Item} that were published.  The list of subscription ids
  43.      * represents the subscriptions that matched the event, in the case
  44.      * of the user having multiple subscriptions.
  45.      *
  46.      * @param nodeId The id of the node the event came from
  47.      * @param eventItems The list of {@link Item} that were published
  48.      * @param subscriptionIds The list of subscriptionIds
  49.      */
  50.     public ItemPublishEvent(String nodeId, List<T> eventItems, List<String> subscriptionIds) {
  51.         super(nodeId, subscriptionIds);
  52.         items = eventItems;
  53.     }

  54.     /**
  55.      * Constructs an <code>ItemPublishEvent</code> with the provided list
  56.      * of {@link Item} that were published in the past.  The published
  57.      * date signifies that this is delayed event.  The list of subscription ids
  58.      * represents the subscriptions that matched the event, in the case
  59.      * of the user having multiple subscriptions.
  60.      *
  61.      * @param nodeId The id of the node the event came from
  62.      * @param eventItems The list of {@link Item} that were published
  63.      * @param subscriptionIds The list of subscriptionIds
  64.      * @param publishedDate date of publication.
  65.      */
  66.     public ItemPublishEvent(String nodeId, List<T> eventItems, List<String> subscriptionIds, Date publishedDate) {
  67.         super(nodeId, subscriptionIds);
  68.         items = eventItems;

  69.         if (publishedDate != null)
  70.             originalDate = publishedDate;
  71.     }

  72.     /**
  73.      * Get the list of {@link Item} that were published.
  74.      *
  75.      * @return The list of published {@link Item}
  76.      */
  77.     public List<T> getItems() {
  78.         return Collections.unmodifiableList(items);
  79.     }

  80.     /**
  81.      * Indicates whether this event was delayed.  That is, the items
  82.      * were published to the node at some time in the past.  This will
  83.      * typically happen if there is an item already published to the node
  84.      * before a user subscribes to it.  In this case, when the user
  85.      * subscribes, the server may send the last item published to the node
  86.      * with a delay date showing its time of original publication.
  87.      *
  88.      * @return true if the items are delayed, false otherwise.
  89.      */
  90.     public boolean isDelayed() {
  91.         return originalDate != null;
  92.     }

  93.     /**
  94.      * Gets the original date the items were published.  This is only
  95.      * valid if {@link #isDelayed()} is true.
  96.      *
  97.      * @return date of publication.
  98.      */
  99.     public Date getPublishedDate() {
  100.         return originalDate;
  101.     }

  102.     @Override
  103.     public String toString() {
  104.         return getClass().getName() + "  [subscriptions: " + getSubscriptions() + "], [Delayed: " +
  105.             (isDelayed() ? originalDate.toString() : "false") + ']';
  106.     }

  107. }