SearchResponse.java

  1. package gov.usgs.earthquake.indexer;

  2. import java.util.Iterator;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.Objects;

  6. /**
  7.  * Results from a SearchRequest.
  8.  */
  9. public class SearchResponse {

  10.     /** The queries with results. */
  11.     private final List<SearchQuery> results = new LinkedList<SearchQuery>();

  12.     /**
  13.      * Construct a new Search response.
  14.      */
  15.     public SearchResponse() {
  16.     }

  17.     /**
  18.      * Add a search result to this response.
  19.      *
  20.      * @param result a searchQuery result
  21.      */
  22.     public void addResult(final SearchQuery result) {
  23.         results.add(result);
  24.     }

  25.     /**
  26.      * @return The search results.
  27.      */
  28.     public List<SearchQuery> getResults() {
  29.         return results;
  30.     }

  31.     /**
  32.      * Test by comparing each result.
  33.      */
  34.     @Override
  35.     public boolean equals(final Object obj) {
  36.         if (!(obj instanceof SearchResponse)) {
  37.             return false;
  38.         }

  39.         SearchResponse that = (SearchResponse) obj;
  40.         if (this.getResults().size() != that.getResults().size()) {
  41.             return false;
  42.         }

  43.         Iterator<SearchQuery> thisIter = this.getResults().iterator();
  44.         Iterator<SearchQuery> thatIter = that.getResults().iterator();
  45.         while (thisIter.hasNext() && thatIter.hasNext()) {
  46.             if (!thisIter.next().equals(thatIter.next())) {
  47.                 return false;
  48.             }
  49.         }

  50.         return true;
  51.     }

  52.     /**
  53.      * Also override hashCode, using hash of result objects.
  54.      */
  55.     @Override
  56.     public int hashCode() {
  57.         return Objects.hash(this.getResults().toArray());
  58.     }

  59.     /**
  60.      * Get a distinct list of events from EventDetailQuery results.
  61.      *
  62.      * @return List of found events. List will be empty if there were no
  63.      *         EventDetailQueries, or no matching events were found.
  64.      */
  65.     public List<Event> getEvents() {
  66.         List<Event> events = new LinkedList<Event>();

  67.         // Get the results out of the SearchRequest and into a list
  68.         Iterator<SearchQuery> iter = getResults().iterator();
  69.         while (iter.hasNext()) {
  70.             SearchQuery query = iter.next();

  71.             if (query instanceof EventDetailQuery) {
  72.                 List<Event> queryEvents = ((EventDetailQuery) query)
  73.                         .getResult();
  74.                 Iterator<Event> queryIter = queryEvents.iterator();
  75.                 while (queryIter.hasNext()) {
  76.                     Event event = queryIter.next();
  77.                     if (!events.contains(event)) {
  78.                         // since events may be returned multiple times by
  79.                         // search query (one per query).
  80.                         events.add(event);
  81.                     }
  82.                 }
  83.             }
  84.         }

  85.         return events;
  86.     }

  87. }