You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by dn...@apache.org on 2004/11/24 00:13:34 UTC

cvs commit: jakarta-lucene/src/java/org/apache/lucene/queryParser QueryParser.java QueryParser.jj

dnaber      2004/11/23 15:13:34

  Modified:    src/java/org/apache/lucene/queryParser QueryParser.java
                        QueryParser.jj
  Log:
  get/setOperator becomes get/setDefaultOperator and its parameter is now typsafe
  
  Revision  Changes    Path
  1.22      +53 -5     jakarta-lucene/src/java/org/apache/lucene/queryParser/QueryParser.java
  
  Index: QueryParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/queryParser/QueryParser.java,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- QueryParser.java	13 Nov 2004 15:11:26 -0000	1.21
  +++ QueryParser.java	23 Nov 2004 23:13:34 -0000	1.22
  @@ -9,6 +9,7 @@
   import org.apache.lucene.analysis.*;
   import org.apache.lucene.document.*;
   import org.apache.lucene.search.*;
  +import org.apache.lucene.util.Parameter;
   
   /**
    * This class is generated by JavaCC.  The only method that clients should need
  @@ -59,11 +60,18 @@
     private static final int MOD_NOT     = 10;
     private static final int MOD_REQ     = 11;
   
  +  /** @deprecated use {@link #OR_OPERATOR} instead */
     public static final int DEFAULT_OPERATOR_OR  = 0;
  +  /** @deprecated use {@link #AND_OPERATOR} instead */
     public static final int DEFAULT_OPERATOR_AND = 1;
   
  +  // make it possible to call setDefaultOperator() without accessing 
  +  // the nested class:
  +  public static final Operator AND_OPERATOR = Operator.AND;
  +  public static final Operator OR_OPERATOR = Operator.OR;
  +
     /** The actual operator that parser uses to combine query terms */
  -  private int operator = DEFAULT_OPERATOR_OR;
  +  private Operator operator = OR_OPERATOR;
   
     boolean lowercaseWildcardTerms = true;
   
  @@ -74,6 +82,14 @@
     int fuzzyPrefixLength = FuzzyQuery.defaultPrefixLength;
     Locale locale = Locale.getDefault();
   
  +  static final class Operator extends Parameter {
  +    private Operator(String name) {
  +      super(name);
  +    }
  +    static final Operator OR = new Operator("OR");
  +    static final Operator AND = new Operator("AND");
  +  }
  +
     /** Parses a query string, returning a {@link org.apache.lucene.search.Query}.
      *  @param query  the query string to be parsed.
      *  @param field  the default field for query terms.
  @@ -180,16 +196,48 @@
      * <code>capital OR of OR Hungary</code>.<br/>
      * In <code>DEFAULT_OPERATOR_AND</code> terms are considered to be in conjuction: the
      * above mentioned query is parsed as <code>capital AND of AND Hungary</code>
  +   * @deprecated use {@link #setDefaultOperator(QueryParser.Operator)} instead
  +   */
  +  public void setOperator(int op) {
  +    if (op == DEFAULT_OPERATOR_AND)
  +      this.operator = AND_OPERATOR;
  +    else if (op == DEFAULT_OPERATOR_OR)
  +      this.operator = OR_OPERATOR;
  +    else
  +      throw new IllegalArgumentException("Unknown operator " + op);
  +  }
  +
  +  /**
  +   * Sets the boolean operator of the QueryParser.
  +   * In default mode (<code>OR_OPERATOR</code>) terms without any modifiers
  +   * are considered optional: for example <code>capital of Hungary</code> is equal to
  +   * <code>capital OR of OR Hungary</code>.<br/>
  +   * In <code>AND_OPERATOR</code> mode terms are considered to be in conjuction: the
  +   * above mentioned query is parsed as <code>capital AND of AND Hungary</code>
      */
  -  public void setOperator(int operator) {
  -    this.operator = operator;
  +  public void setDefaultOperator(Operator op) {
  +    this.operator = op;
     }
   
     /**
      * Gets implicit operator setting, which will be either DEFAULT_OPERATOR_AND
      * or DEFAULT_OPERATOR_OR.
  +   * @deprecated use {@link #getDefaultOperator()} instead
      */
     public int getOperator() {
  +    if(operator == AND_OPERATOR)
  +      return DEFAULT_OPERATOR_AND;
  +    else if(operator == OR_OPERATOR)
  +      return DEFAULT_OPERATOR_OR;
  +    else
  +      throw new IllegalStateException("Unknown operator " + operator);
  +  }
  +
  +  /**
  +   * Gets implicit operator setting, which will be either AND_OPERATOR
  +   * or OR_OPERATOR.
  +   */
  +  public Operator getDefaultOperator() {
       return operator;
     }
   
  @@ -233,7 +281,7 @@
           c.setOccur(BooleanClause.Occur.MUST);
       }
   
  -    if (clauses.size() > 0 && operator == DEFAULT_OPERATOR_AND && conj == CONJ_OR) {
  +    if (clauses.size() > 0 && operator == AND_OPERATOR && conj == CONJ_OR) {
         // If this term is introduced by OR, make the preceding term optional,
         // unless it's prohibited (that means we leave -a OR b but +a OR b-->a OR b)
         // notice if the input is a OR b, first term is parsed as required; without
  @@ -248,7 +296,7 @@
       if (q == null)
         return;
   
  -    if (operator == DEFAULT_OPERATOR_OR) {
  +    if (operator == OR_OPERATOR) {
         // We set REQUIRED if we're introduced by AND or +; PROHIBITED if
         // introduced by NOT or -; make sure not to set both.
         prohibited = (mods == MOD_NOT);
  
  
  
  1.56      +53 -5     jakarta-lucene/src/java/org/apache/lucene/queryParser/QueryParser.jj
  
  Index: QueryParser.jj
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/queryParser/QueryParser.jj,v
  retrieving revision 1.55
  retrieving revision 1.56
  diff -u -r1.55 -r1.56
  --- QueryParser.jj	13 Nov 2004 15:11:26 -0000	1.55
  +++ QueryParser.jj	23 Nov 2004 23:13:34 -0000	1.56
  @@ -32,6 +32,7 @@
   import org.apache.lucene.analysis.*;
   import org.apache.lucene.document.*;
   import org.apache.lucene.search.*;
  +import org.apache.lucene.util.Parameter;
   
   /**
    * This class is generated by JavaCC.  The only method that clients should need
  @@ -82,11 +83,18 @@
     private static final int MOD_NOT     = 10;
     private static final int MOD_REQ     = 11;
   
  +  /** @deprecated use {@link #OR_OPERATOR} instead */
     public static final int DEFAULT_OPERATOR_OR  = 0;
  +  /** @deprecated use {@link #AND_OPERATOR} instead */
     public static final int DEFAULT_OPERATOR_AND = 1;
   
  +  // make it possible to call setDefaultOperator() without accessing 
  +  // the nested class:
  +  public static final Operator AND_OPERATOR = Operator.AND;
  +  public static final Operator OR_OPERATOR = Operator.OR;
  +
     /** The actual operator that parser uses to combine query terms */
  -  private int operator = DEFAULT_OPERATOR_OR;
  +  private Operator operator = OR_OPERATOR;
   
     boolean lowercaseWildcardTerms = true;
   
  @@ -97,6 +105,14 @@
     int fuzzyPrefixLength = FuzzyQuery.defaultPrefixLength;
     Locale locale = Locale.getDefault();
   
  +  static final class Operator extends Parameter {
  +    private Operator(String name) {
  +      super(name);
  +    }
  +    static final Operator OR = new Operator("OR");
  +    static final Operator AND = new Operator("AND");
  +  }
  +
     /** Parses a query string, returning a {@link org.apache.lucene.search.Query}.
      *  @param query  the query string to be parsed.
      *  @param field  the default field for query terms.
  @@ -203,16 +219,48 @@
      * <code>capital OR of OR Hungary</code>.<br/>
      * In <code>DEFAULT_OPERATOR_AND</code> terms are considered to be in conjuction: the
      * above mentioned query is parsed as <code>capital AND of AND Hungary</code>
  +   * @deprecated use {@link #setDefaultOperator(QueryParser.Operator)} instead
  +   */
  +  public void setOperator(int op) {
  +    if (op == DEFAULT_OPERATOR_AND)
  +      this.operator = AND_OPERATOR;
  +    else if (op == DEFAULT_OPERATOR_OR)
  +      this.operator = OR_OPERATOR;
  +    else
  +      throw new IllegalArgumentException("Unknown operator " + op);
  +  }
  +
  +  /**
  +   * Sets the boolean operator of the QueryParser.
  +   * In default mode (<code>OR_OPERATOR</code>) terms without any modifiers
  +   * are considered optional: for example <code>capital of Hungary</code> is equal to
  +   * <code>capital OR of OR Hungary</code>.<br/>
  +   * In <code>AND_OPERATOR</code> mode terms are considered to be in conjuction: the
  +   * above mentioned query is parsed as <code>capital AND of AND Hungary</code>
      */
  -  public void setOperator(int operator) {
  -    this.operator = operator;
  +  public void setDefaultOperator(Operator op) {
  +    this.operator = op;
     }
   
     /**
      * Gets implicit operator setting, which will be either DEFAULT_OPERATOR_AND
      * or DEFAULT_OPERATOR_OR.
  +   * @deprecated use {@link #getDefaultOperator()} instead
      */
     public int getOperator() {
  +    if(operator == AND_OPERATOR)
  +      return DEFAULT_OPERATOR_AND;
  +    else if(operator == OR_OPERATOR)
  +      return DEFAULT_OPERATOR_OR;
  +    else
  +      throw new IllegalStateException("Unknown operator " + operator);
  +  }
  +
  +  /**
  +   * Gets implicit operator setting, which will be either AND_OPERATOR
  +   * or OR_OPERATOR.
  +   */
  +  public Operator getDefaultOperator() {
       return operator;
     }
   
  @@ -256,7 +304,7 @@
           c.setOccur(BooleanClause.Occur.MUST);
       }
   
  -    if (clauses.size() > 0 && operator == DEFAULT_OPERATOR_AND && conj == CONJ_OR) {
  +    if (clauses.size() > 0 && operator == AND_OPERATOR && conj == CONJ_OR) {
         // If this term is introduced by OR, make the preceding term optional,
         // unless it's prohibited (that means we leave -a OR b but +a OR b-->a OR b)
         // notice if the input is a OR b, first term is parsed as required; without
  @@ -271,7 +319,7 @@
       if (q == null)
         return;
   
  -    if (operator == DEFAULT_OPERATOR_OR) {
  +    if (operator == OR_OPERATOR) {
         // We set REQUIRED if we're introduced by AND or +; PROHIBITED if
         // introduced by NOT or -; make sure not to set both.
         prohibited = (mods == MOD_NOT);
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-dev-help@jakarta.apache.org