MtasSpanAndQuery.java.html 10.1 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>MtasSpanAndQuery.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">MtasSpanAndQuery.java</span></div><h1>MtasSpanAndQuery.java</h1><pre class="source lang-java linenums">package mtas.search.spans;

import java.io.IOException;
import java.util.HashSet;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.spans.SpanNearQuery;
import org.apache.lucene.search.spans.SpanWeight;

import mtas.search.spans.util.MtasExtendedSpanAndQuery;
import mtas.search.spans.util.MtasSpanQuery;

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

  /** The base query. */
  private SpanNearQuery baseQuery;
  
  /** The clauses. */
  private HashSet&lt;MtasSpanQuery&gt; clauses;

  /**
   * Instantiates a new mtas span and query.
   *
   * @param initialClauses the initial clauses
   */
  public MtasSpanAndQuery(MtasSpanQuery... initialClauses) {
<span class="fc" id="L30">    super(null, null);</span>
<span class="fc" id="L31">    Integer minimum = null;</span>
<span class="fc" id="L32">    Integer maximum = null;</span>
<span class="fc" id="L33">    clauses = new HashSet&lt;&gt;();</span>
<span class="fc bfc" id="L34" title="All 2 branches covered.">    for (MtasSpanQuery item : initialClauses) {</span>
<span class="pc bpc" id="L35" title="1 of 2 branches missed.">      if (!clauses.contains(item)) {</span>
<span class="fc" id="L36">        clauses.add(item);</span>
<span class="pc bpc" id="L37" title="1 of 2 branches missed.">        if (item.getMinimumWidth() != null) {</span>
<span class="fc bfc" id="L38" title="All 2 branches covered.">          if(minimum != null) {</span>
<span class="fc" id="L39">            minimum = Math.max(minimum, item.getMinimumWidth());</span>
          } else {
<span class="fc" id="L41">            minimum = item.getMinimumWidth();</span>
          }
        }
<span class="pc bpc" id="L44" title="1 of 2 branches missed.">        if (item.getMaximumWidth() != null) {          </span>
<span class="fc bfc" id="L45" title="All 2 branches covered.">          if(maximum != null) {</span>
<span class="fc" id="L46">            maximum = Math.max(maximum, item.getMaximumWidth());</span>
          } else {
<span class="fc" id="L48">            maximum = item.getMaximumWidth();</span>
          }
        }
      }
    }
<span class="fc" id="L53">    setWidth(minimum, maximum);</span>
<span class="fc" id="L54">    baseQuery = new MtasExtendedSpanAndQuery(</span>
<span class="fc" id="L55">        clauses.toArray(new MtasSpanQuery[clauses.size()]));</span>
<span class="fc" id="L56">  }</span>

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

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.apache.lucene.search.spans.SpanQuery#createWeight(org.apache.lucene.
   * search.IndexSearcher, boolean)
   */
  @Override
  public SpanWeight createWeight(IndexSearcher searcher, boolean needsScores)
      throws IOException {
<span class="fc" id="L78">    return baseQuery.createWeight(searcher, needsScores);</span>
  }

  /* (non-Javadoc)
   * @see mtas.search.spans.util.MtasSpanQuery#rewrite(org.apache.lucene.index.IndexReader)
   */
  @Override
  public MtasSpanQuery rewrite(IndexReader reader) throws IOException {
<span class="pc bpc" id="L86" title="1 of 2 branches missed.">    if (clauses.size() &gt; 1) {</span>
      // rewrite, count MtasSpanMatchAllQuery and check for
      // MtasSpanMatchNoneQuery
<span class="fc" id="L89">      MtasSpanQuery[] newClauses = new MtasSpanQuery[clauses.size()];</span>
<span class="fc" id="L90">      MtasSpanQuery[] oldClauses = clauses</span>
<span class="fc" id="L91">          .toArray(new MtasSpanQuery[clauses.size()]);</span>
<span class="fc" id="L92">      int singlePositionQueries = 0;</span>
<span class="fc" id="L93">      int matchAllSinglePositionQueries = 0;</span>
<span class="fc" id="L94">      boolean actuallyRewritten = false;</span>
<span class="fc bfc" id="L95" title="All 2 branches covered.">      for (int i = 0; i &lt; oldClauses.length; i++) {</span>
<span class="fc" id="L96">        newClauses[i] = oldClauses[i].rewrite(reader);</span>
<span class="fc bfc" id="L97" title="All 2 branches covered.">        actuallyRewritten |= !oldClauses[i].equals(newClauses[i]);</span>
<span class="pc bpc" id="L98" title="1 of 2 branches missed.">        if (newClauses[i] instanceof MtasSpanMatchNoneQuery) {</span>
<span class="nc" id="L99">          return (new MtasSpanMatchNoneQuery(this.getField())).rewrite(reader);</span>
        } else {
<span class="pc bpc" id="L101" title="1 of 2 branches missed.">          if (newClauses[i].isSinglePositionQuery()) {</span>
<span class="fc" id="L102">            singlePositionQueries++;</span>
<span class="pc bpc" id="L103" title="1 of 2 branches missed.">            if (newClauses[i] instanceof MtasSpanMatchAllQuery) {</span>
<span class="nc" id="L104">              matchAllSinglePositionQueries++;</span>
            }
          }
        }
      }
      // filter clauses
<span class="pc bpc" id="L110" title="1 of 2 branches missed.">      if (matchAllSinglePositionQueries &gt; 0) {</span>
        // compute new number of clauses
<span class="nc" id="L112">        int newNumber = newClauses.length - matchAllSinglePositionQueries;</span>
<span class="nc bnc" id="L113" title="All 2 branches missed.">        if (matchAllSinglePositionQueries == singlePositionQueries) {</span>
<span class="nc" id="L114">          newNumber++;</span>
        }
<span class="nc" id="L116">        MtasSpanQuery[] newFilteredClauses = new MtasSpanQuery[newNumber];</span>
<span class="nc" id="L117">        int j = 0;</span>
<span class="nc bnc" id="L118" title="All 2 branches missed.">        for (int i = 0; i &lt; newClauses.length; i++) {</span>
<span class="nc bnc" id="L119" title="All 4 branches missed.">          if (!(newClauses[i].isSinglePositionQuery()</span>
              &amp;&amp; (newClauses[i] instanceof MtasSpanMatchAllQuery))) {
<span class="nc" id="L121">            newFilteredClauses[j] = newClauses[i];</span>
<span class="nc" id="L122">            j++;</span>
<span class="nc bnc" id="L123" title="All 2 branches missed.">          } else if (matchAllSinglePositionQueries == singlePositionQueries) {</span>
<span class="nc" id="L124">            newFilteredClauses[j] = newClauses[i];</span>
<span class="nc" id="L125">            j++;</span>
<span class="nc" id="L126">            singlePositionQueries++; // only match this condition once</span>
          }
        }
<span class="nc" id="L129">        newClauses = newFilteredClauses;</span>
      }
<span class="pc bpc" id="L131" title="1 of 2 branches missed.">      if (newClauses.length == 0) {</span>
<span class="nc" id="L132">        return (new MtasSpanMatchNoneQuery(this.getField())).rewrite(reader);</span>
<span class="pc bpc" id="L133" title="1 of 2 branches missed.">      } else if (newClauses.length == 1) {</span>
<span class="nc" id="L134">        return newClauses[0].rewrite(reader);</span>
<span class="pc bpc" id="L135" title="1 of 4 branches missed.">      } else if (actuallyRewritten || newClauses.length != clauses.size()) {</span>
<span class="fc" id="L136">        return new MtasSpanAndQuery(newClauses).rewrite(reader);</span>
      } else {
<span class="fc" id="L138">        return super.rewrite(reader);</span>
      }
<span class="nc bnc" id="L140" title="All 2 branches missed.">    } else if (clauses.size() == 1) {</span>
<span class="nc" id="L141">      return clauses.iterator().next().rewrite(reader);</span>
    } else {
<span class="nc" id="L143">      return (new MtasSpanMatchNoneQuery(this.getField())).rewrite(reader);</span>
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.apache.lucene.search.spans.SpanNearQuery#toString(java.lang.String)
   */
  @Override
  public String toString(String field) {
<span class="nc" id="L155">    return baseQuery.toString(field);</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.SpanNearQuery#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object obj) {
<span class="fc bfc" id="L165" title="All 2 branches covered.">    if (this == obj)</span>
<span class="fc" id="L166">      return true;</span>
<span class="pc bpc" id="L167" title="1 of 2 branches missed.">    if (obj == null)</span>
<span class="nc" id="L168">      return false;</span>
<span class="pc bpc" id="L169" title="1 of 2 branches missed.">    if (getClass() != obj.getClass())</span>
<span class="nc" id="L170">      return false;</span>
<span class="fc" id="L171">    final MtasSpanAndQuery that = (MtasSpanAndQuery) obj;</span>
<span class="fc" id="L172">    return baseQuery.equals(that.baseQuery);</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.SpanNearQuery#hashCode()
   */
  @Override
  public int hashCode() {
<span class="fc" id="L182">    int h = this.getClass().getSimpleName().hashCode();</span>
<span class="fc" id="L183">    h = (h * 7) ^ clauses.hashCode();</span>
<span class="fc" id="L184">    return h;</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>