MtasSpanSequenceQuery.java.html 32.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
<?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>MtasSpanSequenceQuery.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">MtasSpanSequenceQuery.java</span></div><h1>MtasSpanSequenceQuery.java</h1><pre class="source lang-java linenums">package mtas.search.spans;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
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.MtasExpandSpanQuery;
import mtas.search.spans.util.MtasIgnoreItem;
import mtas.search.spans.util.MtasSpanQuery;
import mtas.search.spans.util.MtasSpanWeight;
import mtas.search.spans.util.MtasSpans;

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

  /** The items. */
  private List&lt;MtasSpanSequenceItem&gt; items;

  /** The left minimum. */
  private int leftMinimum;

  /** The left maximum. */
  private int leftMaximum;

  /** The right minimum. */
  private int rightMinimum;

  /** The right maximum. */
  private int rightMaximum;

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

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

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

  /**
   * Instantiates a new mtas span sequence query.
   *
   * @param items the items
   * @param ignoreQuery the ignore query
   * @param maximumIgnoreLength the maximum ignore length
   */
  public MtasSpanSequenceQuery(List&lt;MtasSpanSequenceItem&gt; items,
      MtasSpanQuery ignoreQuery, Integer maximumIgnoreLength) {
<span class="fc" id="L63">    this(items, 0, 0, 0, 0, ignoreQuery, maximumIgnoreLength);</span>
<span class="fc" id="L64">  }</span>

  /**
   * Instantiates a new mtas span sequence query.
   *
   * @param items the items
   * @param leftMinimum the left minimum
   * @param leftMaximum the left maximum
   * @param rightMinimum the right minimum
   * @param rightMaximum the right maximum
   * @param ignoreQuery the ignore query
   * @param maximumIgnoreLength the maximum ignore length
   */
  public MtasSpanSequenceQuery(List&lt;MtasSpanSequenceItem&gt; items,
      int leftMinimum, int leftMaximum, int rightMinimum, int rightMaximum,
      MtasSpanQuery ignoreQuery, Integer maximumIgnoreLength) {
<span class="fc" id="L80">    super(null, null);</span>
<span class="fc" id="L81">    this.items = items;</span>
<span class="fc" id="L82">    this.leftMinimum = leftMinimum;</span>
<span class="fc" id="L83">    this.leftMaximum = leftMaximum;</span>
<span class="fc" id="L84">    this.rightMinimum = rightMinimum;</span>
<span class="fc" id="L85">    this.rightMaximum = rightMaximum;</span>
    // get field and do checks
<span class="fc" id="L87">    Integer minimum = leftMinimum + rightMinimum;</span>
<span class="fc" id="L88">    Integer maximum = leftMaximum + rightMaximum;</span>
<span class="fc bfc" id="L89" title="All 2 branches covered.">    for (MtasSpanSequenceItem item : items) {</span>
<span class="fc bfc" id="L90" title="All 2 branches covered.">      if (field == null) {</span>
<span class="fc" id="L91">        field = item.getQuery().getField();</span>
<span class="pc bpc" id="L92" title="1 of 2 branches missed.">      } else if (item.getQuery().getField() != null</span>
<span class="pc bpc" id="L93" title="1 of 2 branches missed.">          &amp;&amp; !item.getQuery().getField().equals(field)) {</span>
<span class="nc" id="L94">        throw new IllegalArgumentException(&quot;Clauses must have same field.&quot;);</span>
      }
<span class="fc bfc" id="L96" title="All 4 branches covered.">      if (minimum != null &amp;&amp; !item.isOptional()) {</span>
<span class="fc bfc" id="L97" title="All 2 branches covered.">        minimum = item.getQuery().getMinimumWidth() != null</span>
<span class="fc" id="L98">            ? minimum + item.getQuery().getMinimumWidth() : null;</span>
      }
<span class="fc bfc" id="L100" title="All 2 branches covered.">      if (maximum != null) {</span>
<span class="fc bfc" id="L101" title="All 2 branches covered.">        maximum = item.getQuery().getMaximumWidth() != null</span>
<span class="fc" id="L102">            ? maximum + item.getQuery().getMaximumWidth() : null;</span>
      }
<span class="fc" id="L104">    }</span>
    // check ignore
<span class="pc bpc" id="L106" title="1 of 4 branches missed.">    if (field != null &amp;&amp; ignoreQuery != null) {</span>
<span class="pc bpc" id="L107" title="1 of 2 branches missed.">      if (ignoreQuery.getField() == null</span>
<span class="pc bpc" id="L108" title="1 of 2 branches missed.">          || field.equals(ignoreQuery.getField())) {</span>
<span class="fc" id="L109">        this.ignoreQuery = ignoreQuery;</span>
<span class="pc bpc" id="L110" title="1 of 2 branches missed.">        if (maximumIgnoreLength == null) {</span>
<span class="nc" id="L111">          this.maximumIgnoreLength = MtasIgnoreItem.DEFAULT_MAXIMUM_IGNORE_LENGTH;</span>
        } else {
<span class="fc" id="L113">          this.maximumIgnoreLength = maximumIgnoreLength;</span>
        }
      } else {
<span class="nc" id="L116">        throw new IllegalArgumentException(</span>
            &quot;ignore must have same field as clauses&quot;);
      }
<span class="pc bpc" id="L119" title="2 of 4 branches missed.">      if (maximum != null &amp;&amp; items.size() &gt; 1) {</span>
<span class="pc bpc" id="L120" title="1 of 2 branches missed.">        if (this.ignoreQuery.getMaximumWidth() != null) {</span>
<span class="fc" id="L121">          maximum += (items.size() - 1) * this.maximumIgnoreLength</span>
<span class="fc" id="L122">              * this.ignoreQuery.getMaximumWidth();</span>
        } else {
<span class="nc" id="L124">          maximum = null;</span>
        }
      }
    } else {
<span class="fc" id="L128">      this.ignoreQuery = null;</span>
<span class="fc" id="L129">      this.maximumIgnoreLength = null;</span>
    }
<span class="fc" id="L131">    setWidth(minimum, maximum);</span>
<span class="fc" id="L132">  }</span>

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

  /**
   * Gets the items.
   *
   * @return the items
   */
  public List&lt;MtasSpanSequenceItem&gt; getItems() {
<span class="fc" id="L150">    return items;</span>
  }

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

  /**
   * Gets the maximum ignore length.
   *
   * @return the maximum ignore length
   */
  public Integer getMaximumIgnoreLength() {
<span class="nc" id="L168">    return maximumIgnoreLength;</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 bfc" id="L179" title="All 2 branches covered.">    if (items.size() == 1) {</span>
<span class="fc" id="L180">      MtasSpanQuery singleQuery = items.get(0).getQuery();</span>
<span class="fc bfc" id="L181" title="All 4 branches covered.">      if (leftMaximum != 0 || rightMaximum != 0) {</span>
<span class="fc" id="L182">        singleQuery = new MtasExpandSpanQuery(singleQuery, leftMinimum,</span>
            leftMaximum, rightMinimum, rightMaximum);
      }
<span class="fc" id="L185">      return singleQuery.rewrite(reader);</span>
    } else {
      MtasSpanSequenceItem newItem;
<span class="fc" id="L188">      MtasSpanSequenceItem previousNewItem = null;</span>
<span class="fc" id="L189">      ArrayList&lt;MtasSpanSequenceItem&gt; newItems = new ArrayList&lt;&gt;(items.size());</span>
<span class="fc" id="L190">      int newLeftMinimum = leftMinimum;</span>
<span class="fc" id="L191">      int newLeftMaximum = leftMaximum;</span>
<span class="fc" id="L192">      int newRightMinimum = rightMinimum;</span>
<span class="fc" id="L193">      int newRightMaximum = rightMaximum;</span>
<span class="fc bfc" id="L194" title="All 2 branches covered.">      MtasSpanQuery newIgnoreClause = ignoreQuery != null</span>
<span class="fc" id="L195">          ? ignoreQuery.rewrite(reader) : null;</span>
<span class="fc bfc" id="L196" title="All 2 branches covered.">      boolean actuallyRewritten = ignoreQuery != null</span>
<span class="fc bfc" id="L197" title="All 2 branches covered.">          ? !newIgnoreClause.equals(ignoreQuery) : false;</span>
<span class="fc bfc" id="L198" title="All 2 branches covered.">      for (int i = 0; i &lt; items.size(); i++) {</span>
<span class="fc" id="L199">        newItem = items.get(i).rewrite(reader);</span>
<span class="pc bpc" id="L200" title="1 of 2 branches missed.">        if (newItem.getQuery() instanceof MtasSpanMatchNoneQuery) {</span>
<span class="nc bnc" id="L201" title="All 2 branches missed.">          if (!newItem.isOptional()) {</span>
<span class="nc" id="L202">            return new MtasSpanMatchNoneQuery(field);</span>
          } else {
<span class="nc" id="L204">            actuallyRewritten = true;</span>
          }
        } else {
<span class="fc bfc" id="L207" title="All 2 branches covered.">          actuallyRewritten |= !items.get(i).equals(newItem);</span>
<span class="fc" id="L208">          MtasSpanSequenceItem previousMergedItem = MtasSpanSequenceItem.merge(</span>
              previousNewItem, newItem, ignoreQuery, maximumIgnoreLength);
<span class="pc bpc" id="L210" title="1 of 2 branches missed.">          if (previousMergedItem != null) {</span>
<span class="nc" id="L211">            newItems.set((newItems.size() - 1), previousMergedItem);</span>
<span class="nc" id="L212">            actuallyRewritten = true;</span>
          } else {
<span class="fc" id="L214">            newItems.add(newItem);</span>
          }
<span class="fc" id="L216">          previousNewItem = newItem;</span>
        }
      }
      // check first and last
<span class="fc bfc" id="L220" title="All 2 branches covered.">      if (ignoreQuery == null) {</span>
<span class="fc" id="L221">        ArrayList&lt;MtasSpanSequenceItem&gt; possibleTrimmedItems = new ArrayList&lt;&gt;(</span>
<span class="fc" id="L222">            newItems.size());</span>
<span class="fc" id="L223">        MtasSpanSequenceItem firstItem = newItems.get(0);</span>
<span class="fc" id="L224">        MtasSpanQuery firstQuery = firstItem.getQuery();</span>
<span class="fc bfc" id="L225" title="All 2 branches covered.">        if (firstQuery instanceof MtasSpanMatchAllQuery) {</span>
<span class="fc" id="L226">          newLeftMaximum++;</span>
<span class="fc bfc" id="L227" title="All 2 branches covered.">          if (!firstItem.isOptional()) {</span>
<span class="fc" id="L228">            newLeftMinimum++;</span>
          }
<span class="fc bfc" id="L230" title="All 2 branches covered.">        } else if (firstQuery instanceof MtasSpanRecurrenceQuery) {</span>
<span class="fc" id="L231">          MtasSpanRecurrenceQuery firstRecurrenceQuery = (MtasSpanRecurrenceQuery) firstQuery;</span>
<span class="fc bfc" id="L232" title="All 2 branches covered.">          if (firstRecurrenceQuery.getQuery() instanceof MtasSpanMatchAllQuery</span>
<span class="pc bpc" id="L233" title="1 of 2 branches missed.">              &amp;&amp; firstRecurrenceQuery.getIgnoreQuery() == null) {</span>
<span class="pc bpc" id="L234" title="1 of 2 branches missed.">            if (!firstItem.isOptional()) {</span>
<span class="nc" id="L235">              newLeftMinimum += firstRecurrenceQuery.getMinimumRecurrence();</span>
<span class="nc" id="L236">              newLeftMaximum += firstRecurrenceQuery.getMaximumRecurrence();</span>
            } else {
<span class="pc bpc" id="L238" title="1 of 2 branches missed.">              if (firstRecurrenceQuery.getMinimumRecurrence() == 1</span>
                  || firstRecurrenceQuery
<span class="nc bnc" id="L240" title="All 2 branches missed.">                      .getMinimumRecurrence() &lt;= newLeftMinimum) {</span>
<span class="fc" id="L241">                newLeftMinimum += 0;</span>
<span class="fc" id="L242">                newLeftMaximum += firstRecurrenceQuery.getMaximumRecurrence();</span>
              } else {
<span class="nc" id="L244">                possibleTrimmedItems.add(firstItem);</span>
              }
            }
          } else {
<span class="fc" id="L248">            possibleTrimmedItems.add(firstItem);</span>
          }
<span class="fc" id="L250">        } else {</span>
<span class="fc" id="L251">          possibleTrimmedItems.add(firstItem);</span>
        }
<span class="fc bfc" id="L253" title="All 2 branches covered.">        for (int i = 1; i &lt; (newItems.size() - 1); i++) {</span>
<span class="fc" id="L254">          possibleTrimmedItems.add(newItems.get(i));</span>
        }
<span class="pc bpc" id="L256" title="1 of 2 branches missed.">        if (newItems.size() &gt; 1) {</span>
<span class="fc" id="L257">          MtasSpanSequenceItem lastItem = newItems.get((newItems.size() - 1));</span>
<span class="fc" id="L258">          MtasSpanQuery lastQuery = lastItem.getQuery();</span>
<span class="fc bfc" id="L259" title="All 2 branches covered.">          if (lastQuery instanceof MtasSpanMatchAllQuery) {</span>
<span class="fc" id="L260">            newRightMaximum++;</span>
<span class="fc bfc" id="L261" title="All 2 branches covered.">            if (!lastItem.isOptional()) {</span>
<span class="fc" id="L262">              newRightMinimum++;</span>
            }
<span class="fc bfc" id="L264" title="All 2 branches covered.">          } else if (lastQuery instanceof MtasSpanRecurrenceQuery) {</span>
<span class="fc" id="L265">            MtasSpanRecurrenceQuery lastRecurrenceQuery = (MtasSpanRecurrenceQuery) lastQuery;</span>
<span class="fc bfc" id="L266" title="All 2 branches covered.">            if (lastRecurrenceQuery.getQuery() instanceof MtasSpanMatchAllQuery</span>
<span class="pc bpc" id="L267" title="1 of 2 branches missed.">                &amp;&amp; lastRecurrenceQuery.getIgnoreQuery() == null) {</span>
<span class="fc bfc" id="L268" title="All 2 branches covered.">              if (!lastItem.isOptional()) {</span>
<span class="fc" id="L269">                newRightMinimum += lastRecurrenceQuery.getMinimumRecurrence();</span>
<span class="fc" id="L270">                newRightMaximum += lastRecurrenceQuery.getMaximumRecurrence();</span>
<span class="pc bpc" id="L271" title="1 of 2 branches missed.">              } else if (lastRecurrenceQuery.getMinimumRecurrence() == 1</span>
                  || lastRecurrenceQuery
<span class="nc bnc" id="L273" title="All 2 branches missed.">                      .getMinimumRecurrence() &lt;= newRightMinimum) {</span>
<span class="fc" id="L274">                newRightMinimum += 0;</span>
<span class="fc" id="L275">                newRightMaximum += lastRecurrenceQuery.getMaximumRecurrence();</span>
              } else {
<span class="nc" id="L277">                possibleTrimmedItems.add(lastItem);</span>
              }
            } else {
<span class="fc" id="L280">              possibleTrimmedItems.add(lastItem);</span>
            }
<span class="fc" id="L282">          } else {</span>
<span class="fc" id="L283">            possibleTrimmedItems.add(lastItem);</span>
          }
        }
<span class="fc bfc" id="L286" title="All 2 branches covered.">        if (possibleTrimmedItems.size() &lt; newItems.size()) {</span>
<span class="fc" id="L287">          actuallyRewritten = true;</span>
<span class="fc" id="L288">          newItems = possibleTrimmedItems;</span>
        }
      }
<span class="fc bfc" id="L291" title="All 2 branches covered.">      if (!actuallyRewritten) {</span>
<span class="fc bfc" id="L292" title="All 4 branches covered.">        if (leftMaximum != 0 || rightMaximum != 0) {</span>
<span class="fc" id="L293">          newLeftMinimum = leftMinimum;</span>
<span class="fc" id="L294">          newLeftMaximum = leftMaximum;</span>
<span class="fc" id="L295">          newRightMinimum = rightMinimum;</span>
<span class="fc" id="L296">          newRightMaximum = rightMaximum;</span>
<span class="fc" id="L297">          leftMinimum = 0;</span>
<span class="fc" id="L298">          leftMaximum = 0;</span>
<span class="fc" id="L299">          rightMinimum = 0;</span>
<span class="fc" id="L300">          rightMaximum = 0;</span>
<span class="fc" id="L301">          MtasSpanQuery finalQuery = new MtasExpandSpanQuery(this,</span>
              newLeftMinimum, newLeftMaximum, newRightMinimum, newRightMaximum);
<span class="fc" id="L303">          return finalQuery.rewrite(reader);</span>
        } else {
<span class="fc" id="L305">          return super.rewrite(reader);</span>
        }
      } else {
<span class="pc bpc" id="L308" title="1 of 2 branches missed.">        if (!newItems.isEmpty()) {</span>
<span class="fc" id="L309">          return new MtasSpanSequenceQuery(newItems, newLeftMinimum,</span>
              newLeftMaximum, newRightMinimum, newRightMaximum, newIgnoreClause,
<span class="fc" id="L311">              maximumIgnoreLength).rewrite(reader);</span>
        } else {
<span class="nc" id="L313">          return new MtasSpanMatchNoneQuery(field);</span>
        }
      }
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.Query#toString(java.lang.String)
   */
  @Override
  public String toString(String field) {
<span class="nc" id="L326">    StringBuilder buffer = new StringBuilder();</span>
<span class="nc" id="L327">    buffer.append(this.getClass().getSimpleName() + &quot;([&quot;);</span>
<span class="nc" id="L328">    Iterator&lt;MtasSpanSequenceItem&gt; i = items.iterator();</span>
<span class="nc bnc" id="L329" title="All 2 branches missed.">    while (i.hasNext()) {</span>
<span class="nc" id="L330">      MtasSpanSequenceItem item = i.next();</span>
<span class="nc" id="L331">      MtasSpanQuery clause = item.getQuery();</span>
<span class="nc" id="L332">      buffer.append(clause.toString(field));</span>
<span class="nc bnc" id="L333" title="All 2 branches missed.">      if (item.isOptional()) {</span>
<span class="nc" id="L334">        buffer.append(&quot;{OPTIONAL}&quot;);</span>
      }
<span class="nc bnc" id="L336" title="All 2 branches missed.">      if (i.hasNext()) {</span>
<span class="nc" id="L337">        buffer.append(&quot;, &quot;);</span>
      }
<span class="nc" id="L339">    }</span>
<span class="nc" id="L340">    buffer.append(&quot;[&quot; + leftMinimum + &quot;,&quot; + leftMaximum + &quot;]&quot;);</span>
<span class="nc" id="L341">    buffer.append(&quot;[&quot; + rightMinimum + &quot;,&quot; + rightMaximum + &quot;]&quot;);</span>
<span class="nc" id="L342">    buffer.append(&quot;]&quot;);</span>
<span class="nc" id="L343">    buffer.append(&quot;, &quot;);</span>
<span class="nc" id="L344">    buffer.append(ignoreQuery);</span>
<span class="nc" id="L345">    buffer.append(&quot;)&quot;);</span>
<span class="nc" id="L346">    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="L356" title="All 2 branches covered.">    if (this == obj)</span>
<span class="fc" id="L357">      return true;</span>
<span class="pc bpc" id="L358" title="1 of 2 branches missed.">    if (obj == null)</span>
<span class="nc" id="L359">      return false;</span>
<span class="fc bfc" id="L360" title="All 2 branches covered.">    if (getClass() != obj.getClass())</span>
<span class="fc" id="L361">      return false;</span>
<span class="fc" id="L362">    MtasSpanSequenceQuery other = (MtasSpanSequenceQuery) obj;</span>
    boolean isEqual;
<span class="fc" id="L364">    isEqual = field.equals(other.field);</span>
<span class="fc" id="L365">    isEqual &amp;= items.equals(other.items);</span>
<span class="pc bpc" id="L366" title="1 of 2 branches missed.">    isEqual &amp;= leftMinimum == other.leftMinimum;</span>
<span class="pc bpc" id="L367" title="1 of 2 branches missed.">    isEqual &amp;= leftMaximum == other.leftMaximum;</span>
<span class="pc bpc" id="L368" title="1 of 2 branches missed.">    isEqual &amp;= rightMinimum == other.rightMinimum;</span>
<span class="pc bpc" id="L369" title="1 of 2 branches missed.">    isEqual &amp;= rightMaximum == other.rightMaximum;</span>
<span class="pc bpc" id="L370" title="3 of 8 branches missed.">    isEqual &amp;= ((ignoreQuery == null &amp;&amp; other.ignoreQuery == null)</span>
        || (ignoreQuery != null &amp;&amp; other.ignoreQuery != null
<span class="pc bpc" id="L372" title="1 of 2 branches missed.">            &amp;&amp; ignoreQuery.equals(other.ignoreQuery)));</span>
<span class="fc" id="L373">    return isEqual;</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.Query#hashCode()
   */
  @Override
  public int hashCode() {
<span class="fc" id="L383">    int h = this.getClass().getSimpleName().hashCode();</span>
<span class="fc" id="L384">    h = (h * 3) ^ field.hashCode();</span>
<span class="fc" id="L385">    h = (h * 5) ^ items.hashCode();</span>
<span class="fc" id="L386">    h = Integer.rotateLeft(h, leftMinimum) + leftMinimum;</span>
<span class="fc" id="L387">    h ^= 11;</span>
<span class="fc" id="L388">    h = Integer.rotateLeft(h, leftMaximum) + leftMaximum;</span>
<span class="fc" id="L389">    h ^= 13;</span>
<span class="fc" id="L390">    h = Integer.rotateLeft(h, rightMinimum) + rightMinimum;</span>
<span class="fc" id="L391">    h ^= 17;</span>
<span class="fc" id="L392">    h = Integer.rotateLeft(h, rightMaximum) + rightMaximum;</span>
<span class="pc bpc" id="L393" title="1 of 2 branches missed.">    if (ignoreQuery != null) {</span>
<span class="nc" id="L394">      h = (h * 7) ^ ignoreQuery.hashCode();</span>
<span class="nc" id="L395">      h = (h * 11) ^ maximumIgnoreLength.hashCode();</span>
    }
<span class="fc" id="L397">    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) throws IOException {
<span class="fc" id="L410">    List&lt;MtasSpanSequenceQueryWeight&gt; subWeights = new ArrayList&lt;&gt;();</span>
<span class="fc" id="L411">    SpanWeight ignoreWeight = null;</span>
<span class="fc bfc" id="L412" title="All 2 branches covered.">    for (MtasSpanSequenceItem item : items) {</span>
<span class="fc" id="L413">      subWeights.add(new MtasSpanSequenceQueryWeight(</span>
<span class="fc" id="L414">          item.getQuery().createWeight(searcher, false), item.isOptional()));</span>
<span class="fc" id="L415">    }</span>
<span class="fc bfc" id="L416" title="All 2 branches covered.">    if (ignoreQuery != null) {</span>
<span class="fc" id="L417">      ignoreWeight = ignoreQuery.createWeight(searcher, false);</span>
    }
<span class="pc bpc" id="L419" title="1 of 2 branches missed.">    return new SpanSequenceWeight(subWeights, ignoreWeight, maximumIgnoreLength,</span>
<span class="pc" id="L420">        searcher, needsScores ? getTermContexts(subWeights) : null);</span>
  }

  /**
   * Gets the term contexts.
   *
   * @param items the items
   * @return the term contexts
   */
  protected Map&lt;Term, TermContext&gt; getTermContexts(
      List&lt;MtasSpanSequenceQueryWeight&gt; items) {
<span class="nc" id="L431">    List&lt;SpanWeight&gt; weights = new ArrayList&lt;&gt;();</span>
<span class="nc bnc" id="L432" title="All 2 branches missed.">    for (MtasSpanSequenceQueryWeight item : items) {</span>
<span class="nc" id="L433">      weights.add(item.spanWeight);</span>
<span class="nc" id="L434">    }</span>
<span class="nc" id="L435">    return getTermContexts(weights);</span>
  }

  /* (non-Javadoc)
   * @see mtas.search.spans.util.MtasSpanQuery#disableTwoPhaseIterator()
   */
  @Override
  public void disableTwoPhaseIterator() {
<span class="fc" id="L443">    super.disableTwoPhaseIterator();</span>
<span class="fc bfc" id="L444" title="All 2 branches covered.">    for (MtasSpanSequenceItem item : items) {</span>
<span class="fc" id="L445">      item.getQuery().disableTwoPhaseIterator();</span>
<span class="fc" id="L446">    }</span>
<span class="fc bfc" id="L447" title="All 2 branches covered.">    if (ignoreQuery != null) {</span>
<span class="fc" id="L448">      ignoreQuery.disableTwoPhaseIterator();</span>
    }
<span class="fc" id="L450">  }</span>

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

    /** The sub weights. */
    final List&lt;MtasSpanSequenceQueryWeight&gt; subWeights;

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

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

    /**
     * Instantiates a new span sequence weight.
     *
     * @param subWeights the sub weights
     * @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 SpanSequenceWeight(List&lt;MtasSpanSequenceQueryWeight&gt; subWeights,
        SpanWeight ignoreWeight, Integer maximumIgnoreLength,
        IndexSearcher searcher, Map&lt;Term, TermContext&gt; terms)
<span class="fc" id="L479">        throws IOException {</span>
<span class="fc" id="L480">      super(MtasSpanSequenceQuery.this, searcher, terms);</span>
<span class="fc" id="L481">      this.subWeights = subWeights;</span>
<span class="fc" id="L482">      this.ignoreWeight = ignoreWeight;</span>
<span class="fc" id="L483">      this.maximumIgnoreLength = maximumIgnoreLength;</span>
<span class="fc" id="L484">    }</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 bfc" id="L495" title="All 2 branches covered.">      for (MtasSpanSequenceQueryWeight w : subWeights) {</span>
<span class="fc" id="L496">        w.spanWeight.extractTermContexts(contexts);</span>
<span class="fc" id="L497">      }</span>
<span class="pc bpc" id="L498" title="1 of 2 branches missed.">      if (ignoreWeight != null) {</span>
<span class="nc" id="L499">        ignoreWeight.extractTermContexts(contexts);</span>
      }
<span class="fc" id="L501">    }</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="L514" title="1 of 2 branches missed.">      if (field == null) {</span>
<span class="nc" id="L515">        return null;</span>
      } else {
<span class="fc" id="L517">        Terms terms = context.reader().terms(field);</span>
<span class="pc bpc" id="L518" title="1 of 2 branches missed.">        if (terms == null) {</span>
<span class="nc" id="L519">          return null; // field does not exist</span>
        }
<span class="fc" id="L521">        List&lt;MtasSpanSequenceQuerySpans&gt; setSequenceSpans = new ArrayList&lt;&gt;(</span>
<span class="fc" id="L522">            items.size());</span>
<span class="fc" id="L523">        Spans ignoreSpans = null;</span>
<span class="fc" id="L524">        boolean allSpansEmpty = true;</span>
<span class="fc bfc" id="L525" title="All 2 branches covered.">        for (MtasSpanSequenceQueryWeight w : subWeights) {</span>
<span class="fc" id="L526">          Spans sequenceSpans = w.spanWeight.getSpans(context,</span>
              requiredPostings);
<span class="pc bpc" id="L528" title="1 of 2 branches missed.">          if (sequenceSpans != null) {</span>
<span class="fc" id="L529">            setSequenceSpans.add(new MtasSpanSequenceQuerySpans(</span>
                MtasSpanSequenceQuery.this, sequenceSpans, w.optional));
<span class="fc" id="L531">            allSpansEmpty = false;</span>
          } else {
<span class="nc bnc" id="L533" title="All 2 branches missed.">            if (w.optional) {</span>
<span class="nc" id="L534">              setSequenceSpans.add(new MtasSpanSequenceQuerySpans(</span>
                  MtasSpanSequenceQuery.this, null, w.optional));
            } else {
<span class="nc" id="L537">              return null;</span>
            }
          }
<span class="fc" id="L540">        }</span>
<span class="pc bpc" id="L541" title="1 of 2 branches missed.">        if (allSpansEmpty) {</span>
<span class="nc" id="L542">          return null; // at least one required</span>
<span class="fc bfc" id="L543" title="All 2 branches covered.">        } else if (ignoreWeight != null) {</span>
<span class="fc" id="L544">          ignoreSpans = ignoreWeight.getSpans(context, requiredPostings);</span>
        }
<span class="fc" id="L546">        return new MtasSpanSequenceSpans(MtasSpanSequenceQuery.this,</span>
            setSequenceSpans, ignoreSpans, 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 bnc" id="L558" title="All 2 branches missed.">      for (MtasSpanSequenceQueryWeight w : subWeights) {</span>
<span class="nc" id="L559">        w.spanWeight.extractTerms(terms);</span>
<span class="nc" id="L560">      }</span>
<span class="nc bnc" id="L561" title="All 2 branches missed.">      if (ignoreWeight != null) {</span>
<span class="nc" id="L562">        ignoreWeight.extractTerms(terms);</span>
      }
<span class="nc" id="L564">    }</span>

  }

  /**
   * The Class MtasSpanSequenceQuerySpans.
   */
  protected static class MtasSpanSequenceQuerySpans {

    /** The spans. */
    public Spans spans;

    /** The optional. */
    public boolean optional;

    /**
     * Instantiates a new mtas span sequence query spans.
     *
     * @param query the query
     * @param spans the spans
     * @param optional the optional
     */
    public MtasSpanSequenceQuerySpans(MtasSpanSequenceQuery query, Spans spans,
<span class="fc" id="L587">        boolean optional) {</span>
<span class="pc bpc" id="L588" title="1 of 2 branches missed.">      this.spans = spans != null ? spans : new MtasSpanMatchNoneSpans(query);</span>
<span class="fc" id="L589">      this.optional = optional;</span>
<span class="fc" id="L590">    }</span>
  }

  /**
   * The Class MtasSpanSequenceQueryWeight.
   */
  private static class MtasSpanSequenceQueryWeight {

    /** The span weight. */
    public SpanWeight spanWeight;

    /** The optional. */
    public boolean optional;

    /**
     * Instantiates a new mtas span sequence query weight.
     *
     * @param spanWeight the span weight
     * @param optional the optional
     */
    public MtasSpanSequenceQueryWeight(SpanWeight spanWeight,
<span class="fc" id="L611">        boolean optional) {</span>
<span class="fc" id="L612">      this.spanWeight = spanWeight;</span>
<span class="fc" id="L613">      this.optional = optional;</span>
<span class="fc" id="L614">    }</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>