1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id:AndQuery.java 984 2006-01-23 14:18:33 -0500 (Mon, 23 Jan 2006) dmsmith $
21   */
22  package org.crosswire.jsword.index.query;
23  
24  import org.crosswire.jsword.book.BookException;
25  import org.crosswire.jsword.index.Index;
26  import org.crosswire.jsword.passage.Key;
27  import org.crosswire.jsword.passage.PassageTally;
28  
29  /**
30   * An AND query specifies that a result needs to be in both the left and the
31   * right query results.
32   * 
33   * @see gnu.lgpl.License for license details.<br>
34   *      The copyright to this program is held by it's authors.
35   * @author DM Smith [dmsmith555 at yahoo dot com]
36   */
37  public class AndQuery extends AbstractBinaryQuery {
38  
39      /**
40       * Create a query where the result is the intersection of two queries.
41       * 
42       * @param theLeftQuery
43       * @param theRightQuery
44       */
45      public AndQuery(Query theLeftQuery, Query theRightQuery) {
46          super(theLeftQuery, theRightQuery);
47      }
48  
49      /*
50       * (non-Javadoc)
51       * 
52       * @see
53       * org.crosswire.jsword.index.search.parse.Query#find(org.crosswire.jsword
54       * .index.search.Index)
55       */
56      public Key find(Index index) throws BookException {
57          Key left = getLeftQuery().find(index);
58  
59          if (left.isEmpty()) {
60              return left;
61          }
62  
63          Key right = getRightQuery().find(index);
64  
65          if (right.isEmpty()) {
66              return right;
67          }
68  
69          // If ranking was requested then prioritize it.
70          if (right instanceof PassageTally) {
71              right.retainAll(left);
72              return right;
73          }
74  
75          left.retainAll(right);
76          return left;
77      }
78  }
79