MtasSpanRecurrenceQuery.java.html 16.8 KB
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>MtasSpanRecurrenceQuery.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">MTAS</a> &gt; <a href="index.source.html" class="el_package">mtas.search.spans</a> &gt; <span class="el_source">MtasSpanRecurrenceQuery.java</span></div><h1>MtasSpanRecurrenceQuery.java</h1><pre class="source lang-java linenums">package mtas.search.spans;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermContext;
import org.apache.lucene.index.Terms;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.spans.SpanWeight;
import org.apache.lucene.search.spans.Spans;

import mtas.search.spans.util.MtasSpanQuery;
import mtas.search.spans.util.MtasSpanWeight;
import mtas.search.spans.util.MtasSpans;

/**
 * The Class MtasSpanRecurrenceQuery.
 */
public class MtasSpanRecurrenceQuery extends MtasSpanQuery {

  /** The query. */
  private MtasSpanQuery query;

  /** The minimum recurrence. */
  private int minimumRecurrence;

  /** The maximum recurrence. */
  private int maximumRecurrence;

  /** The ignore query. */
  private MtasSpanQuery ignoreQuery;

  /** The maximum ignore length. */
  private Integer maximumIgnoreLength;

  /** The field. */
  private String field;

  /**
   * Instantiates a new mtas span recurrence query.
   *
   * @param query the query
   * @param minimumRecurrence the minimum recurrence
   * @param maximumRecurrence the maximum recurrence
   * @param ignoreQuery the ignore query
   * @param maximumIgnoreLength the maximum ignore length
   */
  public MtasSpanRecurrenceQuery(MtasSpanQuery query, int minimumRecurrence,
      int maximumRecurrence, MtasSpanQuery ignoreQuery,
      Integer maximumIgnoreLength) {
<span class="fc" id="L55">    super(null, null);</span>
<span class="fc" id="L56">    field = query.getField();</span>
<span class="fc" id="L57">    this.query = query;</span>
<span class="pc bpc" id="L58" title="2 of 4 branches missed.">    if (field != null &amp;&amp; ignoreQuery != null) {</span>
<span class="nc bnc" id="L59" title="All 2 branches missed.">      if (ignoreQuery.getField() == null</span>
<span class="nc bnc" id="L60" title="All 2 branches missed.">          || field.equals(ignoreQuery.getField())) {</span>
<span class="nc" id="L61">        this.ignoreQuery = ignoreQuery;</span>
<span class="nc bnc" id="L62" title="All 2 branches missed.">        if (maximumIgnoreLength == null) {</span>
<span class="nc" id="L63">          this.maximumIgnoreLength = 1;</span>
        } else {
<span class="nc" id="L65">          this.maximumIgnoreLength = maximumIgnoreLength;</span>
        }
      } else {
<span class="nc" id="L68">        throw new IllegalArgumentException(</span>
            &quot;ignore must have same field as clauses&quot;);
      }
    } else {
<span class="fc" id="L72">      this.ignoreQuery = null;</span>
<span class="fc" id="L73">      this.maximumIgnoreLength = null;</span>
    }
<span class="fc" id="L75">    setRecurrence(minimumRecurrence, maximumRecurrence);</span>
<span class="fc" id="L76">  }</span>

  /**
   * Gets the query.
   *
   * @return the query
   */
  public MtasSpanQuery getQuery() {
<span class="fc" id="L84">    return query;</span>
  }

  /**
   * Gets the ignore query.
   *
   * @return the ignore query
   */
  public MtasSpanQuery getIgnoreQuery() {
<span class="fc" id="L93">    return ignoreQuery;</span>
  }

  /**
   * Gets the maximum ignore length.
   *
   * @return the maximum ignore length
   */
  public Integer getMaximumIgnoreLength() {
<span class="nc" id="L102">    return maximumIgnoreLength;</span>
  }

  /**
   * Gets the minimum recurrence.
   *
   * @return the minimum recurrence
   */
  public int getMinimumRecurrence() {
<span class="fc" id="L111">    return minimumRecurrence;</span>
  }

  /**
   * Gets the maximum recurrence.
   *
   * @return the maximum recurrence
   */
  public int getMaximumRecurrence() {
<span class="fc" id="L120">    return maximumRecurrence;</span>
  }

  /**
   * Sets the recurrence.
   *
   * @param minimumRecurrence the minimum recurrence
   * @param maximumRecurrence the maximum recurrence
   */
  public void setRecurrence(int minimumRecurrence, int maximumRecurrence) {
<span class="pc bpc" id="L130" title="1 of 2 branches missed.">    if (minimumRecurrence &gt; maximumRecurrence) {</span>
<span class="nc" id="L131">      throw new IllegalArgumentException(</span>
          &quot;minimumRecurrence &gt; maximumRecurrence&quot;);
<span class="pc bpc" id="L133" title="1 of 2 branches missed.">    } else if (minimumRecurrence &lt; 1) {</span>
<span class="nc" id="L134">      throw new IllegalArgumentException(&quot;minimumRecurrence &lt; 1 not supported&quot;);</span>
<span class="pc bpc" id="L135" title="1 of 2 branches missed.">    } else if (query == null) {</span>
<span class="nc" id="L136">      throw new IllegalArgumentException(&quot;no clause&quot;);</span>
    }
<span class="fc" id="L138">    this.minimumRecurrence = minimumRecurrence;</span>
<span class="fc" id="L139">    this.maximumRecurrence = maximumRecurrence;</span>
    // set minimum/maximum
<span class="fc" id="L141">    Integer minimum = null;</span>
<span class="fc" id="L142">    Integer maximum = null;</span>
<span class="fc bfc" id="L143" title="All 2 branches covered.">    if (query.getMinimumWidth() != null) {</span>
<span class="fc" id="L144">      minimum = minimumRecurrence * query.getMinimumWidth();</span>
    }
<span class="fc bfc" id="L146" title="All 2 branches covered.">    if (query.getMaximumWidth() != null) {</span>
<span class="fc" id="L147">      maximum = maximumRecurrence * query.getMaximumWidth();</span>
<span class="pc bpc" id="L148" title="3 of 4 branches missed.">      if (ignoreQuery != null &amp;&amp; maximumIgnoreLength != null) {</span>
<span class="nc bnc" id="L149" title="All 2 branches missed.">        if (ignoreQuery.getMaximumWidth() != null) {</span>
<span class="nc" id="L150">          maximum += (maximumRecurrence - 1) * maximumIgnoreLength</span>
<span class="nc" id="L151">              * ignoreQuery.getMaximumWidth();</span>
        } else {
<span class="nc" id="L153">          maximum = null;</span>
        }
      }
    }
<span class="fc" id="L157">    setWidth(minimum, maximum);</span>
<span class="fc" id="L158">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.SpanQuery#getField()
   */
  @Override
  public String getField() {
<span class="fc" id="L167">    return field;</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.apache.lucene.search.Query#rewrite(org.apache.lucene.index.IndexReader)
   */
  @Override
  public MtasSpanQuery rewrite(IndexReader reader) throws IOException {
<span class="fc" id="L178">    MtasSpanQuery newQuery = query.rewrite(reader);</span>
<span class="pc bpc" id="L179" title="1 of 2 branches missed.">    if (maximumRecurrence == 1) {</span>
<span class="nc" id="L180">      return newQuery;</span>
    } else {
<span class="pc bpc" id="L182" title="1 of 2 branches missed.">      MtasSpanQuery newIgnoreQuery = (ignoreQuery != null)</span>
<span class="pc" id="L183">          ? ignoreQuery.rewrite(reader) : null;</span>
<span class="pc bpc" id="L184" title="1 of 2 branches missed.">      if (newQuery instanceof MtasSpanRecurrenceQuery) {</span>
        // for now too difficult, possibly merge later
      }
<span class="pc bpc" id="L187" title="1 of 4 branches missed.">      if (!newQuery.equals(query)</span>
<span class="nc bnc" id="L188" title="All 2 branches missed.">          || (newIgnoreQuery != null &amp;&amp; !newIgnoreQuery.equals(ignoreQuery))) {</span>
<span class="fc" id="L189">        return new MtasSpanRecurrenceQuery(newQuery, minimumRecurrence,</span>
            maximumRecurrence, newIgnoreQuery, maximumIgnoreLength)
<span class="fc" id="L191">                .rewrite(reader);</span>
      } else {
<span class="fc" id="L193">        return super.rewrite(reader);</span>
      }
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.Query#toString(java.lang.String)
   */
  @Override
  public String toString(String field) {
<span class="nc" id="L205">    StringBuilder buffer = new StringBuilder();</span>
<span class="nc" id="L206">    buffer.append(this.getClass().getSimpleName() + &quot;([&quot;);</span>
<span class="nc" id="L207">    buffer.append(query.toString(query.getField()));</span>
<span class="nc" id="L208">    buffer.append(&quot;,&quot; + minimumRecurrence + &quot;,&quot; + maximumRecurrence);</span>
<span class="nc" id="L209">    buffer.append(&quot;, &quot;);</span>
<span class="nc" id="L210">    buffer.append(ignoreQuery);</span>
<span class="nc" id="L211">    buffer.append(&quot;])&quot;);</span>
<span class="nc" id="L212">    return buffer.toString();</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.Query#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object obj) {
<span class="fc bfc" id="L222" title="All 2 branches covered.">    if (this == obj)</span>
<span class="fc" id="L223">      return true;</span>
<span class="pc bpc" id="L224" title="1 of 2 branches missed.">    if (obj == null)</span>
<span class="nc" id="L225">      return false;</span>
<span class="fc bfc" id="L226" title="All 2 branches covered.">    if (getClass() != obj.getClass())</span>
<span class="fc" id="L227">      return false;</span>
<span class="fc" id="L228">    final MtasSpanRecurrenceQuery other = (MtasSpanRecurrenceQuery) obj;</span>
    boolean result;
<span class="fc" id="L230">    result = query.equals(other.query);</span>
<span class="fc bfc" id="L231" title="All 2 branches covered.">    result &amp;= minimumRecurrence == other.minimumRecurrence;</span>
<span class="fc bfc" id="L232" title="All 2 branches covered.">    result &amp;= maximumRecurrence == other.maximumRecurrence;</span>
<span class="fc bfc" id="L233" title="All 2 branches covered.">    if (result) {</span>
      boolean subResult;
<span class="pc bpc" id="L235" title="2 of 4 branches missed.">      subResult = ignoreQuery == null &amp;&amp; other.ignoreQuery == null;</span>
<span class="pc bpc" id="L236" title="3 of 4 branches missed.">      subResult |= ignoreQuery != null &amp;&amp; other.ignoreQuery != null</span>
<span class="pc bnc" id="L237" title="All 2 branches missed.">          &amp;&amp; ignoreQuery.equals(other.ignoreQuery);</span>
<span class="fc" id="L238">      return subResult;</span>
    } else {
<span class="fc" id="L240">      return false;</span>
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.Query#hashCode()
   */
  @Override
  public int hashCode() {
<span class="fc" id="L251">    int h = this.getClass().getSimpleName().hashCode();</span>
<span class="fc" id="L252">    h = (h * 7) ^ query.hashCode();</span>
<span class="fc" id="L253">    h = (h * 11) ^ minimumRecurrence;</span>
<span class="fc" id="L254">    h = (h * 13) ^ maximumRecurrence;</span>
<span class="fc" id="L255">    return h;</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.apache.lucene.search.spans.SpanQuery#createWeight(org.apache.lucene.
   * search.IndexSearcher, boolean)
   */
  @Override
  public MtasSpanWeight createWeight(IndexSearcher searcher,
      boolean needsScores, float boost) throws IOException {
<span class="fc" id="L268">    SpanWeight subWeight = query.createWeight(searcher, false, boost);</span>
<span class="fc" id="L269">    SpanWeight ignoreWeight = null;</span>
<span class="pc bpc" id="L270" title="1 of 2 branches missed.">    if (ignoreQuery != null) {</span>
<span class="nc" id="L271">      ignoreWeight = ignoreQuery.createWeight(searcher, false, boost);</span>
    }
<span class="pc bpc" id="L273" title="1 of 2 branches missed.">    return new SpanRecurrenceWeight(subWeight, ignoreWeight,</span>
        maximumIgnoreLength, searcher,
<span class="pc" id="L275">        needsScores ? getTermContexts(subWeight) : null, boost);</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see mtas.search.spans.util.MtasSpanQuery#disableTwoPhaseIterator()
   */
  @Override
  public void disableTwoPhaseIterator() {
<span class="fc" id="L285">    super.disableTwoPhaseIterator();</span>
<span class="fc" id="L286">    query.disableTwoPhaseIterator();</span>
<span class="pc bpc" id="L287" title="1 of 2 branches missed.">    if (ignoreQuery != null) {</span>
<span class="nc" id="L288">      ignoreQuery.disableTwoPhaseIterator();</span>
    }
<span class="fc" id="L290">  }</span>

  /**
   * The Class SpanRecurrenceWeight.
   */
  protected class SpanRecurrenceWeight extends MtasSpanWeight {

    /** The sub weight. */
    final SpanWeight subWeight;

    /** The ignore weight. */
    final SpanWeight ignoreWeight;

    /** The maximum ignore length. */
    final Integer maximumIgnoreLength;

    /**
     * Instantiates a new span recurrence weight.
     *
     * @param subWeight the sub weight
     * @param ignoreWeight the ignore weight
     * @param maximumIgnoreLength the maximum ignore length
     * @param searcher the searcher
     * @param terms the terms
     * @throws IOException Signals that an I/O exception has occurred.
     */
    public SpanRecurrenceWeight(SpanWeight subWeight, SpanWeight ignoreWeight,
        Integer maximumIgnoreLength, IndexSearcher searcher,
<span class="fc" id="L318">        Map&lt;Term, TermContext&gt; terms, float boost) throws IOException {</span>
<span class="fc" id="L319">      super(MtasSpanRecurrenceQuery.this, searcher, terms, boost);</span>
<span class="fc" id="L320">      this.subWeight = subWeight;</span>
<span class="fc" id="L321">      this.ignoreWeight = ignoreWeight;</span>
<span class="fc" id="L322">      this.maximumIgnoreLength = maximumIgnoreLength;</span>
<span class="fc" id="L323">    }</span>

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.apache.lucene.search.spans.SpanWeight#extractTermContexts(java.util.
     * Map)
     */
    @Override
    public void extractTermContexts(Map&lt;Term, TermContext&gt; contexts) {
<span class="fc" id="L334">      subWeight.extractTermContexts(contexts);</span>
<span class="fc" id="L335">    }</span>

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.apache.lucene.search.spans.SpanWeight#getSpans(org.apache.lucene.
     * index.LeafReaderContext,
     * org.apache.lucene.search.spans.SpanWeight.Postings)
     */
    @Override
    public MtasSpans getSpans(LeafReaderContext context,
        Postings requiredPostings) throws IOException {
<span class="pc bpc" id="L348" title="1 of 2 branches missed.">      if (field == null) {</span>
<span class="nc" id="L349">        return null;</span>
      } else {
<span class="fc" id="L351">        Terms terms = context.reader().terms(field);</span>
<span class="pc bpc" id="L352" title="1 of 2 branches missed.">        if (terms == null) {</span>
<span class="nc" id="L353">          return null; // field does not exist</span>
        }
<span class="fc" id="L355">        Spans subSpans = subWeight.getSpans(context, requiredPostings);</span>
<span class="pc bpc" id="L356" title="1 of 2 branches missed.">        if (subSpans == null) {</span>
<span class="nc" id="L357">          return null;</span>
        } else {
<span class="fc" id="L359">          Spans ignoreSpans = null;</span>
<span class="pc bpc" id="L360" title="1 of 2 branches missed.">          if (ignoreWeight != null) {</span>
<span class="nc" id="L361">            ignoreSpans = ignoreWeight.getSpans(context, requiredPostings);</span>
          }
<span class="fc" id="L363">          return new MtasSpanRecurrenceSpans(MtasSpanRecurrenceQuery.this,</span>
<span class="fc" id="L364">              subSpans, minimumRecurrence, maximumRecurrence, ignoreSpans,</span>
              maximumIgnoreLength);
        }
      }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.apache.lucene.search.Weight#extractTerms(java.util.Set)
     */
    @Override
    public void extractTerms(Set&lt;Term&gt; terms) {
<span class="nc" id="L377">      subWeight.extractTerms(terms);</span>
<span class="nc" id="L378">    }</span>

  }

}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.7.9.201702052155</span></div></body></html>