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. {
  28.     private List<T> items;
  29.     private Date originalDate;
  30.    
  31.     /**
  32.      * Constructs an <tt>ItemPublishEvent</tt> with the provided list
  33.      * of {@link Item} that were published.
  34.      *
  35.      * @param nodeId The id of the node the event came from
  36.      * @param eventItems The list of {@link Item} that were published
  37.      */
  38.     public ItemPublishEvent(String nodeId, List<T> eventItems)
  39.     {
  40.         super(nodeId);
  41.         items = eventItems;
  42.     }
  43.    
  44.     /**
  45.      * Constructs an <tt>ItemPublishEvent</tt> with the provided list
  46.      * of {@link Item} that were published.  The list of subscription ids
  47.      * represents the subscriptions that matched the event, in the case
  48.      * of the user having multiple subscriptions.
  49.      *
  50.      * @param nodeId The id of the node the event came from
  51.      * @param eventItems The list of {@link Item} that were published
  52.      * @param subscriptionIds The list of subscriptionIds
  53.      */
  54.     public ItemPublishEvent(String nodeId, List<T> eventItems, List<String> subscriptionIds)
  55.     {
  56.         super(nodeId, subscriptionIds);
  57.         items = eventItems;
  58.     }
  59.    
  60.     /**
  61.      * Constructs an <tt>ItemPublishEvent</tt> with the provided list
  62.      * of {@link Item} that were published in the past.  The published
  63.      * date signifies that this is delayed event.  The list of subscription ids
  64.      * represents the subscriptions that matched the event, in the case
  65.      * of the user having multiple subscriptions.
  66.      *
  67.      * @param nodeId The id of the node the event came from
  68.      * @param eventItems The list of {@link Item} that were published
  69.      * @param subscriptionIds The list of subscriptionIds
  70.      */
  71.     public ItemPublishEvent(String nodeId, List<T> eventItems, List<String> subscriptionIds, Date publishedDate)
  72.     {
  73.         super(nodeId, subscriptionIds);
  74.         items = eventItems;
  75.        
  76.         if (publishedDate != null)
  77.             originalDate = publishedDate;
  78.     }
  79.    
  80.     /**
  81.      * Get the list of {@link Item} that were published.
  82.      *
  83.      * @return The list of published {@link Item}
  84.      */
  85.     public List<T> getItems()
  86.     {
  87.         return Collections.unmodifiableList(items);
  88.     }
  89.    
  90.     /**
  91.      * Indicates whether this event was delayed.  That is, the items
  92.      * were published to the node at some time in the past.  This will
  93.      * typically happen if there is an item already published to the node
  94.      * before a user subscribes to it.  In this case, when the user
  95.      * subscribes, the server may send the last item published to the node
  96.      * with a delay date showing its time of original publication.
  97.      *
  98.      * @return true if the items are delayed, false otherwise.
  99.      */
  100.     public boolean isDelayed()
  101.     {
  102.         return (originalDate != null);
  103.     }
  104.    
  105.     /**
  106.      * Gets the original date the items were published.  This is only
  107.      * valid if {@link #isDelayed()} is true.
  108.      *
  109.      */
  110.     public Date getPublishedDate()
  111.     {
  112.         return originalDate;
  113.     }

  114.     @Override
  115.     public String toString()
  116.     {
  117.         return getClass().getName() + "  [subscriptions: " + getSubscriptions() + "], [Delayed: " +
  118.             (isDelayed() ? originalDate.toString() : "false") + ']';
  119.     }
  120.    
  121. }