IndexerEvent.java

  1. /*
  2.  * IndexerEvent
  3.  */
  4. package gov.usgs.earthquake.indexer;

  5. import gov.usgs.earthquake.product.Product;

  6. import java.util.EventObject;
  7. import java.util.HashMap;
  8. import java.util.Iterator;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.Vector;

  13. /**
  14.  * A description of a change to a ProductIndex.
  15.  *
  16.  * IndexerEvents are created by the Indexer, and sent to IndexerListeners.
  17.  */
  18. public class IndexerEvent extends EventObject {

  19.     /** Serialization ID. */
  20.     private static final long serialVersionUID = 1L;

  21.     /** The index that was changed. */
  22.     private ProductIndex index;

  23.     /** The product that triggered this change. */
  24.     private ProductSummary summary;

  25.     /**
  26.      * A Vector object is used here because it provides synchronized
  27.      * (thread-safe) access to its elements. This is important since this event
  28.      * will be sent asynchronously to many listeners (potentially).
  29.      */
  30.     private Vector<IndexerChange> indexerChanges = null;

  31.     /**
  32.      * Construct a new IndexerEvent.
  33.      *
  34.      * @param source
  35.      *            the indexer that made the change.
  36.      */
  37.     public IndexerEvent(final Indexer source) {
  38.         super(source);
  39.         // Initial capacity = 5, capacity increment = 5;
  40.         this.indexerChanges = new Vector<IndexerChange>(5, 5);
  41.     }

  42.     /** @return Indexer */
  43.     public Indexer getIndexer() {
  44.         return (Indexer) getSource();
  45.     }

  46.     /** @return Product Index */
  47.     public ProductIndex getIndex() {
  48.         return this.index;
  49.     }

  50.     /** @param index to set */
  51.     public void setIndex(ProductIndex index) {
  52.         this.index = index;
  53.     }

  54.     /** @return product summary */
  55.     public ProductSummary getSummary() {
  56.         return this.summary;
  57.     }

  58.     /** @param summary to add */
  59.     public void setSummary(ProductSummary summary) {
  60.         this.summary = summary;
  61.     }

  62.     /** @param change to add */
  63.     public void addIndexerChange(IndexerChange change) {
  64.         if (change != null) {
  65.             this.indexerChanges.add(change);
  66.         }
  67.     }

  68.     /** @param changes list of changes to add */
  69.     public void addIndexerChanges(List<IndexerChange> changes) {
  70.         if (changes == null) {
  71.             return;
  72.         }

  73.         Iterator<IndexerChange> iterator = changes.iterator();
  74.         while (iterator.hasNext()) {
  75.             addIndexerChange(iterator.next());
  76.         }
  77.     }

  78.     /** @return vector of Indexer Changes */
  79.     public Vector<IndexerChange> getIndexerChanges() {
  80.         return this.indexerChanges;
  81.     }

  82.     /**
  83.      * Convenience method to retrieve Product from Indexer storage.
  84.      *
  85.      * @return Product object corresponding to ProductSummary.
  86.      * @throws Exception if error occurs
  87.      */
  88.     public Product getProduct() throws Exception {
  89.         if (summary == null) {
  90.             return null;
  91.         }
  92.         return getIndexer().getProductStorage().getProduct(summary.getId());
  93.     }

  94.     /**
  95.      * Retrieve a distinct list of events that were changed as part of this
  96.      * IndexerEvent.
  97.      *
  98.      * @return list of events
  99.      */
  100.     public List<Event> getEvents() {
  101.         // map from eventid to event
  102.         Map<Long, Event> events = new HashMap<Long, Event>();

  103.         // iterate over all changes, and place new event into map.

  104.         // more recent changes occur later in list, and thus only the
  105.         // latest update to an event will appear (if the event was
  106.         // changed more than once).
  107.         Iterator<IndexerChange> iter = indexerChanges.iterator();
  108.         while (iter.hasNext()) {
  109.             IndexerChange change = iter.next();
  110.             Event event = change.getNewEvent();
  111.             if (event != null) {
  112.                 // not an UNASSOCIATED_PRODUCT, which doesn't have an associated
  113.                 // event
  114.                 events.put(event.getIndexId(), event);
  115.             }
  116.         }

  117.         // extract distinct events from map
  118.         return new LinkedList<Event>(events.values());
  119.     }

  120. }