Home | History | Annotate | Download | only in pubsub
      1 /**
      2  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      3  * you may not use this file except in compliance with the License.
      4  * You may obtain a copy of the License at
      5  *
      6  *     http://www.apache.org/licenses/LICENSE-2.0
      7  *
      8  * Unless required by applicable law or agreed to in writing, software
      9  * distributed under the License is distributed on an "AS IS" BASIS,
     10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     11  * See the License for the specific language governing permissions and
     12  * limitations under the License.
     13  */
     14 package org.jivesoftware.smackx.pubsub;
     15 
     16 import java.util.Collections;
     17 import java.util.Date;
     18 import java.util.List;
     19 
     20 /**
     21  * Represents an event generated by an item(s) being published to a node.
     22  *
     23  * @author Robin Collier
     24  */
     25 public class ItemPublishEvent <T extends Item> extends SubscriptionEvent
     26 {
     27 	private List<T> items;
     28 	private Date originalDate;
     29 
     30 	/**
     31 	 * Constructs an <tt>ItemPublishEvent</tt> with the provided list
     32 	 * of {@link Item} that were published.
     33 	 *
     34 	 * @param nodeId The id of the node the event came from
     35 	 * @param eventItems The list of {@link Item} that were published
     36 	 */
     37 	public ItemPublishEvent(String nodeId, List<T> eventItems)
     38 	{
     39 		super(nodeId);
     40 		items = eventItems;
     41 	}
     42 
     43 	/**
     44 	 * Constructs an <tt>ItemPublishEvent</tt> with the provided list
     45 	 * of {@link Item} that were published.  The list of subscription ids
     46 	 * represents the subscriptions that matched the event, in the case
     47 	 * of the user having multiple subscriptions.
     48 	 *
     49 	 * @param nodeId The id of the node the event came from
     50 	 * @param eventItems The list of {@link Item} that were published
     51 	 * @param subscriptionIds The list of subscriptionIds
     52 	 */
     53 	public ItemPublishEvent(String nodeId, List<T> eventItems, List<String> subscriptionIds)
     54 	{
     55 		super(nodeId, subscriptionIds);
     56 		items = eventItems;
     57 	}
     58 
     59 	/**
     60 	 * Constructs an <tt>ItemPublishEvent</tt> with the provided list
     61 	 * of {@link Item} that were published in the past.  The published
     62 	 * date signifies that this is delayed event.  The list of subscription ids
     63 	 * represents the subscriptions that matched the event, in the case
     64 	 * of the user having multiple subscriptions.
     65 	 *
     66 	 * @param nodeId The id of the node the event came from
     67 	 * @param eventItems The list of {@link Item} that were published
     68 	 * @param subscriptionIds The list of subscriptionIds
     69 	 * @param publishedDate The original publishing date of the events
     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 	 * @return Date items were published if {@link #isDelayed()} is true, null otherwise.
    110 	 */
    111 	public Date getPublishedDate()
    112 	{
    113 		return originalDate;
    114 	}
    115 
    116 	@Override
    117 	public String toString()
    118 	{
    119 		return getClass().getName() + "  [subscriptions: " + getSubscriptions() + "], [Delayed: " +
    120 			(isDelayed() ? originalDate.toString() : "false") + ']';
    121 	}
    122 
    123 }
    124