MtasToken.java.html 33.5 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 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
<?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>MtasToken.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.analysis.token</a> &gt; <span class="el_source">MtasToken.java</span></div><h1>MtasToken.java</h1><pre class="source lang-java linenums">package mtas.analysis.token;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.analysis.payloads.PayloadHelper;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.automaton.Automaton;
import org.apache.lucene.util.automaton.ByteRunAutomaton;
import org.apache.lucene.util.automaton.CompiledAutomaton;
import org.apache.lucene.util.automaton.Operations;
import org.apache.lucene.util.automaton.RegExp;
import org.apache.lucene.util.automaton.TooComplexToDeterminizeException;

/**
 * The Class MtasToken.
 */
public abstract class MtasToken {

  /** The Constant log. */
<span class="fc" id="L33">  private static final Log log = LogFactory.getLog(MtasToken.class);</span>

  /** The Constant DELIMITER. */
  public static final String DELIMITER = &quot;\u0001&quot;;

  /** The Constant regexpPrePostFix. */
  public static final String regexpPrePostFix = &quot;(.*)&quot; + DELIMITER
      + &quot;(.[^\u0000]*)&quot;;

  /** The Constant patternPrePostFix. */
<span class="fc" id="L43">  public static final Pattern patternPrePostFix = Pattern</span>
<span class="fc" id="L44">      .compile(regexpPrePostFix);</span>

  /** The token id. */
  private Integer tokenId;

  /** The token ref. */
<span class="fc" id="L50">  private Long tokenRef = null;</span>

  /** The term ref. */
<span class="fc" id="L53">  private Long termRef = null;</span>

  /** The prefix id. */
<span class="fc" id="L56">  private Integer prefixId = null;</span>

  /** The token type. */
<span class="fc" id="L59">  protected String tokenType = null;</span>

  /** The token parent id. */
<span class="fc" id="L62">  private Integer tokenParentId = null;</span>

  /** The token value. */
<span class="fc" id="L65">  private String tokenValue = null;</span>

  /** The token position. */
<span class="fc" id="L68">  private MtasPosition tokenPosition = null;</span>

  /** The token offset. */
<span class="fc" id="L71">  private MtasOffset tokenOffset = null;</span>

  /** The token real offset. */
<span class="fc" id="L74">  private MtasOffset tokenRealOffset = null;</span>

  /** The token payload. */
<span class="fc" id="L77">  private BytesRef tokenPayload = null;</span>

  /** The provide offset. */
<span class="fc" id="L80">  private Boolean provideOffset = true;</span>

  /** The provide real offset. */
<span class="fc" id="L83">  private Boolean provideRealOffset = true;</span>

  /** The provide parent id. */
<span class="fc" id="L86">  private Boolean provideParentId = true;</span>

  /**
   * Instantiates a new mtas token.
   *
   * @param tokenId the token id
   * @param value the value
   */
<span class="fc" id="L94">  protected MtasToken(Integer tokenId, String value) {</span>
<span class="fc" id="L95">    this.tokenId = tokenId;</span>
<span class="fc" id="L96">    setType();</span>
<span class="fc" id="L97">    setValue(value);</span>
<span class="fc" id="L98">  }</span>

  /**
   * Instantiates a new mtas token.
   *
   * @param tokenId the token id
   * @param value the value
   * @param position the position
   */
  protected MtasToken(Integer tokenId, String value, Integer position) {
<span class="nc" id="L108">    this(tokenId, value);</span>
<span class="nc" id="L109">    addPosition(position);</span>
<span class="nc" id="L110">  }</span>

  /**
   * Sets the token ref.
   *
   * @param ref the new token ref
   */
  final public void setTokenRef(Long ref) {
<span class="fc" id="L118">    tokenRef = ref;</span>
<span class="fc" id="L119">  }</span>

  /**
   * Gets the token ref.
   *
   * @return the token ref
   */
  final public Long getTokenRef() {
<span class="fc" id="L127">    return tokenRef;</span>
  }

  /**
   * Sets the term ref.
   *
   * @param ref the new term ref
   */
  final public void setTermRef(Long ref) {
<span class="fc" id="L136">    termRef = ref;</span>
<span class="fc" id="L137">  }</span>

  /**
   * Gets the term ref.
   *
   * @return the term ref
   */
  final public Long getTermRef() {
<span class="fc" id="L145">    return termRef;</span>
  }

  /**
   * Sets the prefix id.
   *
   * @param id the new prefix id
   */
  final public void setPrefixId(int id) {
<span class="fc" id="L154">    prefixId = id;</span>
<span class="fc" id="L155">  }</span>

  /**
   * Gets the prefix id.
   *
   * @return the prefix id
   * @throws IOException Signals that an I/O exception has occurred.
   */
  final public int getPrefixId() throws IOException {
<span class="pc bpc" id="L164" title="1 of 2 branches missed.">    if (prefixId != null) {</span>
<span class="fc" id="L165">      return prefixId;</span>
    } else {
<span class="nc" id="L167">      throw new IOException(&quot;no prefixId&quot;);</span>
    }
  }

  /**
   * Sets the id.
   *
   * @param id the new id
   */
  final public void setId(Integer id) {
<span class="fc" id="L177">    tokenId = id;</span>
<span class="fc" id="L178">  }</span>

  /**
   * Gets the id.
   *
   * @return the id
   */
  final public Integer getId() {
<span class="fc" id="L186">    return tokenId;</span>
  }

  /**
   * Sets the parent id.
   *
   * @param id the new parent id
   */
  final public void setParentId(Integer id) {
<span class="fc" id="L195">    tokenParentId = id;</span>
<span class="fc" id="L196">  }</span>

  /**
   * Gets the parent id.
   *
   * @return the parent id
   */
  final public Integer getParentId() {
<span class="fc" id="L204">    return tokenParentId;</span>
  }

  /**
   * Sets the provide parent id.
   *
   * @param provide the new provide parent id
   */
  final public void setProvideParentId(Boolean provide) {
<span class="fc" id="L213">    provideParentId = provide;</span>
<span class="fc" id="L214">  }</span>

  /**
   * Gets the provide parent id.
   *
   * @return the provide parent id
   */
  final public boolean getProvideParentId() {
<span class="nc" id="L222">    return provideParentId;</span>
  }

  /**
   * Sets the type.
   */
  protected void setType() {
<span class="nc" id="L229">    throw new IllegalArgumentException(&quot;Type not implemented&quot;);</span>
  }

  /**
   * Gets the type.
   *
   * @return the type
   */
  final public String getType() {
<span class="fc" id="L238">    return tokenType;</span>
  }

  /**
   * Adds the position.
   *
   * @param position the position
   */
  final public void addPosition(int position) {
<span class="pc bpc" id="L247" title="1 of 2 branches missed.">    if (tokenPosition == null) {</span>
<span class="fc" id="L248">      tokenPosition = new MtasPosition(position);</span>
    } else {
<span class="nc" id="L250">      tokenPosition.add(position);</span>
    }
<span class="fc" id="L252">  }</span>

  /**
   * Adds the position range.
   *
   * @param start the start
   * @param end the end
   */
  final public void addPositionRange(int start, int end) {
<span class="pc bpc" id="L261" title="1 of 2 branches missed.">    if (tokenPosition == null) {</span>
<span class="fc" id="L262">      tokenPosition = new MtasPosition(start, end);</span>
    } else {
<span class="nc" id="L264">      int[] positions = new int[end - start + 1];</span>
<span class="nc bnc" id="L265" title="All 2 branches missed.">      for (int i = start; i &lt;= end; i++) {</span>
<span class="nc" id="L266">        positions[i - start] = i;</span>
      }
<span class="nc" id="L268">      tokenPosition.add(positions);</span>
    }
<span class="fc" id="L270">  }</span>

  /**
   * Adds the positions.
   *
   * @param positions the positions
   */
  final public void addPositions(int[] positions) {
<span class="pc bpc" id="L278" title="2 of 4 branches missed.">    if (positions != null &amp;&amp; positions.length &gt; 0) {</span>
<span class="fc bfc" id="L279" title="All 2 branches covered.">      if (tokenPosition == null) {</span>
<span class="fc" id="L280">        tokenPosition = new MtasPosition(positions);</span>
      } else {
<span class="fc" id="L282">        tokenPosition.add(positions);</span>
      }
    }
<span class="fc" id="L285">  }</span>

  /**
   * Adds the positions.
   *
   * @param list the list
   */
  final public void addPositions(Set&lt;Integer&gt; list) {
<span class="fc" id="L293">    int[] positions = ArrayUtils</span>
<span class="fc" id="L294">        .toPrimitive(list.toArray(new Integer[list.size()]));</span>
<span class="fc" id="L295">    addPositions(positions);</span>
<span class="fc" id="L296">  }</span>

  /**
   * Check position type.
   *
   * @param type the type
   * @return the boolean
   */
  final public Boolean checkPositionType(String type) {
<span class="pc bpc" id="L305" title="1 of 2 branches missed.">    if (tokenPosition == null) {</span>
<span class="nc" id="L306">      return false;</span>
    } else {
<span class="fc" id="L308">      return tokenPosition.checkType(type);</span>
    }
  }

  /**
   * Gets the position start.
   *
   * @return the position start
   */
  final public Integer getPositionStart() {
<span class="pc bpc" id="L318" title="1 of 2 branches missed.">    return tokenPosition == null ? null : tokenPosition.getStart();</span>
  }

  /**
   * Gets the position end.
   *
   * @return the position end
   */
  final public Integer getPositionEnd() {
<span class="pc bpc" id="L327" title="1 of 2 branches missed.">    return tokenPosition == null ? null : tokenPosition.getEnd();</span>
  }

  /**
   * Gets the position length.
   *
   * @return the position length
   */
  final public Integer getPositionLength() {
<span class="nc bnc" id="L336" title="All 2 branches missed.">    return tokenPosition == null ? null : tokenPosition.getLength();</span>
  }

  /**
   * Gets the positions.
   *
   * @return the positions
   */
  final public int[] getPositions() {
<span class="pc bpc" id="L345" title="1 of 2 branches missed.">    return tokenPosition == null ? null : tokenPosition.getPositions();</span>
  }

  /**
   * Check offset.
   *
   * @return the boolean
   */
  final public Boolean checkOffset() {
<span class="nc bnc" id="L354" title="All 4 branches missed.">    if ((tokenOffset == null) || !provideOffset) {</span>
<span class="nc" id="L355">      return false;</span>
    } else {
<span class="nc" id="L357">      return true;</span>
    }
  }

  /**
   * Check real offset.
   *
   * @return the boolean
   */
  final public Boolean checkRealOffset() {
<span class="nc bnc" id="L367" title="All 4 branches missed.">    if ((tokenRealOffset == null) || !provideRealOffset) {</span>
<span class="nc" id="L368">      return false;</span>
<span class="nc bnc" id="L369" title="All 2 branches missed.">    } else if (tokenOffset == null) {</span>
<span class="nc" id="L370">      return true;</span>
<span class="nc bnc" id="L371" title="All 2 branches missed.">    } else if (tokenOffset.getStart() == tokenRealOffset.getStart()</span>
<span class="nc bnc" id="L372" title="All 2 branches missed.">        &amp;&amp; tokenOffset.getEnd() == tokenRealOffset.getEnd()) {</span>
<span class="nc" id="L373">      return false;</span>
    } else {
<span class="nc" id="L375">      return true;</span>
    }
  }

  /**
   * Sets the offset.
   *
   * @param start the start
   * @param end the end
   */
  final public void setOffset(Integer start, Integer end) {
<span class="pc bpc" id="L386" title="2 of 4 branches missed.">    if ((start == null) || (end == null)) {</span>
      // do nothing
<span class="pc bpc" id="L388" title="1 of 2 branches missed.">    } else if (start &gt; end) {</span>
<span class="nc" id="L389">      throw new IllegalArgumentException(&quot;Start offset after end offset&quot;);</span>
    } else {
<span class="fc" id="L391">      tokenOffset = new MtasOffset(start, end);</span>
    }
<span class="fc" id="L393">  }</span>

  /**
   * Adds the offset.
   *
   * @param start the start
   * @param end the end
   */
  final public void addOffset(Integer start, Integer end) {
<span class="fc bfc" id="L402" title="All 2 branches covered.">    if (tokenOffset == null) {</span>
<span class="fc" id="L403">      setOffset(start, end);</span>
<span class="pc bpc" id="L404" title="2 of 4 branches missed.">    } else if ((start == null) || (end == null)) {</span>
      // do nothing
<span class="pc bpc" id="L406" title="1 of 2 branches missed.">    } else if (start &gt; end) {</span>
<span class="nc" id="L407">      throw new IllegalArgumentException(&quot;Start offset after end offset&quot;);</span>
    } else {
<span class="fc" id="L409">      tokenOffset.add(start, end);</span>
    }
<span class="fc" id="L411">  }</span>

  /**
   * Sets the provide offset.
   *
   * @param provide the new provide offset
   */
  final public void setProvideOffset(Boolean provide) {
<span class="fc" id="L419">    provideOffset = provide;</span>
<span class="fc" id="L420">  }</span>

  /**
   * Sets the real offset.
   *
   * @param start the start
   * @param end the end
   */
  final public void setRealOffset(Integer start, Integer end) {
<span class="pc bpc" id="L429" title="2 of 4 branches missed.">    if ((start == null) || (end == null)) {</span>
      // do nothing
<span class="pc bpc" id="L431" title="1 of 2 branches missed.">    } else if (start &gt; end) {</span>
<span class="nc" id="L432">      throw new IllegalArgumentException(</span>
          &quot;Start real offset after end real offset&quot;);
    } else {
<span class="fc" id="L435">      tokenRealOffset = new MtasOffset(start, end);</span>
    }
<span class="fc" id="L437">  }</span>

  /**
   * Sets the provide real offset.
   *
   * @param provide the new provide real offset
   */
  final public void setProvideRealOffset(Boolean provide) {
<span class="fc" id="L445">    provideRealOffset = provide;</span>
<span class="fc" id="L446">  }</span>

  /**
   * Gets the provide offset.
   *
   * @return the provide offset
   */
  final public boolean getProvideOffset() {
<span class="nc" id="L454">    return provideOffset;</span>
  }

  /**
   * Gets the provide real offset.
   *
   * @return the provide real offset
   */
  final public boolean getProvideRealOffset() {
<span class="nc" id="L463">    return provideRealOffset;</span>
  }

  /**
   * Gets the offset start.
   *
   * @return the offset start
   */
  final public Integer getOffsetStart() {
<span class="pc bpc" id="L472" title="1 of 2 branches missed.">    return tokenOffset == null ? null : tokenOffset.getStart();</span>
  }

  /**
   * Gets the offset end.
   *
   * @return the offset end
   */
  final public Integer getOffsetEnd() {
<span class="pc bpc" id="L481" title="1 of 2 branches missed.">    return tokenOffset == null ? null : tokenOffset.getEnd();</span>
  }

  /**
   * Gets the real offset start.
   *
   * @return the real offset start
   */
  final public Integer getRealOffsetStart() {
<span class="nc bnc" id="L490" title="All 2 branches missed.">    return tokenRealOffset == null ? null : tokenRealOffset.getStart();</span>
  }

  /**
   * Gets the real offset end.
   *
   * @return the real offset end
   */
  final public Integer getRealOffsetEnd() {
<span class="nc bnc" id="L499" title="All 2 branches missed.">    return tokenRealOffset == null ? null : tokenRealOffset.getEnd();</span>
  }

  /**
   * Sets the value.
   *
   * @param value the new value
   */
  public void setValue(String value) {
<span class="fc" id="L508">    tokenValue = value;</span>
<span class="fc" id="L509">  }</span>

  /**
   * Gets the prefix from value.
   *
   * @param value the value
   * @return the prefix from value
   */
  public static String getPrefixFromValue(String value) {
<span class="pc bpc" id="L518" title="1 of 2 branches missed.">    if (value == null) {</span>
<span class="nc" id="L519">      return null;</span>
<span class="pc bpc" id="L520" title="1 of 2 branches missed.">    } else if (value.contains(DELIMITER)) {</span>
<span class="fc" id="L521">      String[] list = value.split(DELIMITER);</span>
<span class="pc bpc" id="L522" title="2 of 4 branches missed.">      if (list != null &amp;&amp; list.length &gt; 0) {</span>
<span class="fc" id="L523">        return list[0].replaceAll(&quot;\u0000&quot;, &quot;&quot;);</span>
      } else {
<span class="nc" id="L525">        return null;</span>
      }
    } else {
<span class="nc" id="L528">      return value.replaceAll(&quot;\u0000&quot;, &quot;&quot;);</span>
    }
  }

  /**
   * Gets the postfix from value.
   *
   * @param value the value
   * @return the postfix from value
   */
  public static String getPostfixFromValue(String value) {
<span class="fc" id="L539">    String postfix = &quot;&quot;;</span>
<span class="fc" id="L540">    Matcher m = patternPrePostFix.matcher(value);</span>
<span class="fc bfc" id="L541" title="All 2 branches covered.">    if (m.find()) {</span>
<span class="fc" id="L542">      postfix = m.group(2);</span>

    }
<span class="fc" id="L545">    return postfix;</span>
  }

  /**
   * Gets the postfix from value.
   *
   * @param term the term
   * @return the postfix from value
   */
  public static String getPostfixFromValue(BytesRef term) {
<span class="fc" id="L555">    int i = term.offset;</span>
<span class="fc" id="L556">    int length = term.offset + term.length;</span>
<span class="fc" id="L557">    byte[] postfix = new byte[length];</span>
<span class="pc bpc" id="L558" title="1 of 2 branches missed.">    while (i &lt; length) {</span>
<span class="pc bpc" id="L559" title="1 of 2 branches missed.">      if ((term.bytes[i] &amp; 0b10000000) == 0b00000000) {</span>
<span class="fc bfc" id="L560" title="All 2 branches covered.">        if (term.bytes[i] == 0b00000001) {</span>
<span class="fc" id="L561">          i++;</span>
<span class="fc" id="L562">          break;</span>
        } else {
<span class="fc" id="L564">          i++;</span>
        }
<span class="nc bnc" id="L566" title="All 2 branches missed.">      } else if ((term.bytes[i] &amp; 0b11100000) == 0b11000000) {</span>
<span class="nc" id="L567">        i += 2;</span>
<span class="nc bnc" id="L568" title="All 2 branches missed.">      } else if ((term.bytes[i] &amp; 0b11110000) == 0b11100000) {</span>
<span class="nc" id="L569">        i += 3;</span>
<span class="nc bnc" id="L570" title="All 2 branches missed.">      } else if ((term.bytes[i] &amp; 0b11111000) == 0b11110000) {</span>
<span class="nc" id="L571">        i += 4;</span>
<span class="nc bnc" id="L572" title="All 2 branches missed.">      } else if ((term.bytes[i] &amp; 0b11111100) == 0b11111000) {</span>
<span class="nc" id="L573">        i += 5;</span>
<span class="nc bnc" id="L574" title="All 2 branches missed.">      } else if ((term.bytes[i] &amp; 0b11111110) == 0b11111100) {</span>
<span class="nc" id="L575">        i += 6;</span>
      } else {
<span class="nc" id="L577">        return &quot;&quot;;</span>
      }
    }
<span class="fc" id="L580">    int start = i;</span>
<span class="pc bpc" id="L581" title="1 of 2 branches missed.">    while (i &lt; length) {</span>
<span class="pc bpc" id="L582" title="1 of 2 branches missed.">      if ((term.bytes[i] &amp; 0b10000000) == 0b00000000) {</span>
<span class="fc bfc" id="L583" title="All 2 branches covered.">        if (term.bytes[i] == 0b00000000) {</span>
<span class="fc" id="L584">          break;</span>
        }
<span class="fc" id="L586">        postfix[i] = term.bytes[i];</span>
<span class="fc" id="L587">        i++;</span>
<span class="nc bnc" id="L588" title="All 2 branches missed.">      } else if ((term.bytes[i] &amp; 0b11100000) == 0b11000000) {</span>
<span class="nc" id="L589">        postfix[i] = term.bytes[i];</span>
<span class="nc" id="L590">        postfix[i + 1] = term.bytes[i + 1];</span>
<span class="nc" id="L591">        i += 2;</span>
<span class="nc bnc" id="L592" title="All 2 branches missed.">      } else if ((term.bytes[i] &amp; 0b11110000) == 0b11100000) {</span>
<span class="nc" id="L593">        postfix[i] = term.bytes[i];</span>
<span class="nc" id="L594">        postfix[i + 1] = term.bytes[i + 1];</span>
<span class="nc" id="L595">        postfix[i + 2] = term.bytes[i + 2];</span>
<span class="nc" id="L596">        i += 3;</span>
<span class="nc bnc" id="L597" title="All 2 branches missed.">      } else if ((term.bytes[i] &amp; 0b11111000) == 0b11110000) {</span>
<span class="nc" id="L598">        postfix[i] = term.bytes[i];</span>
<span class="nc" id="L599">        postfix[i + 1] = term.bytes[i + 1];</span>
<span class="nc" id="L600">        postfix[i + 2] = term.bytes[i + 2];</span>
<span class="nc" id="L601">        postfix[i + 3] = term.bytes[i + 3];</span>
<span class="nc" id="L602">        i += 4;</span>
<span class="nc bnc" id="L603" title="All 2 branches missed.">      } else if ((term.bytes[i] &amp; 0b11111100) == 0b11111000) {</span>
<span class="nc" id="L604">        postfix[i] = term.bytes[i];</span>
<span class="nc" id="L605">        postfix[i + 1] = term.bytes[i + 1];</span>
<span class="nc" id="L606">        postfix[i + 2] = term.bytes[i + 2];</span>
<span class="nc" id="L607">        postfix[i + 3] = term.bytes[i + 3];</span>
<span class="nc" id="L608">        postfix[i + 4] = term.bytes[i + 4];</span>
<span class="nc" id="L609">        i += 5;</span>
<span class="nc bnc" id="L610" title="All 2 branches missed.">      } else if ((term.bytes[i] &amp; 0b11111110) == 0b11111100) {</span>
<span class="nc" id="L611">        postfix[i] = term.bytes[i];</span>
<span class="nc" id="L612">        postfix[i + 1] = term.bytes[i + 1];</span>
<span class="nc" id="L613">        postfix[i + 2] = term.bytes[i + 2];</span>
<span class="nc" id="L614">        postfix[i + 3] = term.bytes[i + 3];</span>
<span class="nc" id="L615">        postfix[i + 4] = term.bytes[i + 4];</span>
<span class="nc" id="L616">        postfix[i + 5] = term.bytes[i + 5];</span>
<span class="nc" id="L617">        i += 6;</span>
      } else {
<span class="nc" id="L619">        return &quot;&quot;;</span>
      }
    }
<span class="fc" id="L622">    return new String(Arrays.copyOfRange(postfix, start, i),</span>
        StandardCharsets.UTF_8);
  }

  /**
   * Gets the value.
   *
   * @return the value
   */
  public String getValue() {
<span class="fc" id="L632">    return tokenValue;</span>
  }

  /**
   * Gets the prefix.
   *
   * @return the prefix
   */
  public String getPrefix() {
<span class="fc" id="L641">    return getPrefixFromValue(tokenValue);</span>
  }

  /**
   * Gets the postfix.
   *
   * @return the postfix
   */
  public String getPostfix() {
<span class="nc" id="L650">    return getPostfixFromValue(tokenValue);</span>
  }

  /**
   * Check parent id.
   *
   * @return the boolean
   */
  final public Boolean checkParentId() {
<span class="fc bfc" id="L659" title="All 4 branches covered.">    if ((tokenParentId == null) || !provideParentId) {</span>
<span class="fc" id="L660">      return false;</span>
    } else {
<span class="fc" id="L662">      return true;</span>
    }
  }

  /**
   * Check payload.
   *
   * @return the boolean
   */
  final public Boolean checkPayload() {
<span class="nc bnc" id="L672" title="All 2 branches missed.">    if (tokenPayload == null) {</span>
<span class="nc" id="L673">      return false;</span>
    } else {
<span class="nc" id="L675">      return true;</span>
    }
  }

  /**
   * Sets the payload.
   *
   * @param payload the new payload
   */
  public void setPayload(BytesRef payload) {
<span class="fc" id="L685">    tokenPayload = payload;</span>
<span class="fc" id="L686">  }</span>

  /**
   * Gets the payload.
   *
   * @return the payload
   */
  public BytesRef getPayload() {
<span class="nc" id="L694">    return tokenPayload;</span>
  }

  /**
   * Creates the automaton map.
   *
   * @param prefix the prefix
   * @param valueList the value list
   * @param filter the filter
   * @return the map
   */
  public static Map&lt;String, Automaton&gt; createAutomatonMap(String prefix,
      List&lt;String&gt; valueList, Boolean filter) {
<span class="fc" id="L707">    HashMap&lt;String, Automaton&gt; automatonMap = new HashMap&lt;&gt;();</span>
<span class="pc bpc" id="L708" title="1 of 2 branches missed.">    if (valueList != null) {</span>
<span class="fc bfc" id="L709" title="All 2 branches covered.">      for (String item : valueList) {</span>
<span class="fc bfc" id="L710" title="All 2 branches covered.">        if (filter) {</span>
<span class="fc" id="L711">          item = item.replaceAll(&quot;([\\\&quot;\\)\\(\\&lt;\\&gt;\\.\\@\\#\\]\\[\\{\\}])&quot;,</span>
              &quot;\\\\$1&quot;);
        }
<span class="fc" id="L714">        automatonMap.put(item,</span>
            new RegExp(prefix + MtasToken.DELIMITER + item + &quot;\u0000*&quot;)
<span class="fc" id="L716">                .toAutomaton());</span>
<span class="fc" id="L717">      }</span>
    }
<span class="fc" id="L719">    return automatonMap;</span>
  }

  /**
   * Byte run automaton map.
   *
   * @param automatonMap the automaton map
   * @return the map
   */
  public static Map&lt;String, ByteRunAutomaton&gt; byteRunAutomatonMap(
      Map&lt;String, Automaton&gt; automatonMap) {
<span class="nc" id="L730">    HashMap&lt;String, ByteRunAutomaton&gt; byteRunAutomatonMap = new HashMap&lt;&gt;();</span>
<span class="nc bnc" id="L731" title="All 2 branches missed.">    if (automatonMap != null) {</span>
<span class="nc bnc" id="L732" title="All 2 branches missed.">      for (Entry&lt;String, Automaton&gt; entry : automatonMap.entrySet()) {</span>
<span class="nc" id="L733">        byteRunAutomatonMap.put(entry.getKey(),</span>
<span class="nc" id="L734">            new ByteRunAutomaton(entry.getValue()));</span>
<span class="nc" id="L735">      }</span>
    }
<span class="nc" id="L737">    return byteRunAutomatonMap;</span>
  }

  /**
   * Creates the automata.
   *
   * @param prefix the prefix
   * @param regexp the regexp
   * @param automatonMap the automaton map
   * @return the list
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public static List&lt;CompiledAutomaton&gt; createAutomata(String prefix,
      String regexp, Map&lt;String, Automaton&gt; automatonMap) throws IOException {
<span class="fc" id="L751">    List&lt;CompiledAutomaton&gt; list = new ArrayList&lt;&gt;();</span>
<span class="fc" id="L752">    Automaton automatonRegexp = null;</span>
<span class="fc bfc" id="L753" title="All 2 branches covered.">    if (regexp != null) {</span>
<span class="fc" id="L754">      RegExp re = new RegExp(prefix + MtasToken.DELIMITER + regexp + &quot;\u0000*&quot;);</span>
<span class="fc" id="L755">      automatonRegexp = re.toAutomaton();</span>
    }
<span class="fc" id="L757">    int step = 500;</span>
<span class="fc" id="L758">    List&lt;String&gt; keyList = new ArrayList&lt;&gt;(automatonMap.keySet());</span>
<span class="fc bfc" id="L759" title="All 2 branches covered.">    for (int i = 0; i &lt; keyList.size(); i += step) {</span>
<span class="fc" id="L760">      int localStep = step;</span>
<span class="fc" id="L761">      boolean success = false;</span>
<span class="fc" id="L762">      CompiledAutomaton compiledAutomaton = null;</span>
<span class="fc bfc" id="L763" title="All 2 branches covered.">      while (!success) {</span>
<span class="fc" id="L764">        success = true;</span>
<span class="fc" id="L765">        int next = Math.min(keyList.size(), i + localStep);</span>
<span class="fc" id="L766">        List&lt;Automaton&gt; listAutomaton = new ArrayList&lt;&gt;();</span>
<span class="fc bfc" id="L767" title="All 2 branches covered.">        for (int j = i; j &lt; next; j++) {</span>
<span class="fc" id="L768">          listAutomaton.add(automatonMap.get(keyList.get(j)));</span>
        }
<span class="fc" id="L770">        Automaton automatonList = Operations.union(listAutomaton);</span>
        Automaton automaton;
<span class="fc bfc" id="L772" title="All 2 branches covered.">        if (automatonRegexp != null) {</span>
<span class="fc" id="L773">          automaton = Operations.intersection(automatonList, automatonRegexp);</span>
        } else {
<span class="fc" id="L775">          automaton = automatonList;</span>
        }
        try {
<span class="fc" id="L778">          compiledAutomaton = new CompiledAutomaton(automaton);</span>
<span class="nc" id="L779">        } catch (TooComplexToDeterminizeException e) {</span>
<span class="nc" id="L780">          log.debug(e);</span>
<span class="nc" id="L781">          success = false;</span>
<span class="nc bnc" id="L782" title="All 2 branches missed.">          if (localStep &gt; 1) {</span>
<span class="nc" id="L783">            localStep /= 2;</span>
          } else {
<span class="nc" id="L785">            throw new IOException(&quot;TooComplexToDeterminizeException&quot;);</span>
          }
<span class="fc" id="L787">        }</span>
<span class="fc" id="L788">      }</span>
<span class="fc" id="L789">      list.add(compiledAutomaton);</span>
    }
<span class="fc" id="L791">    return list;</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString() {
<span class="nc" id="L801">    String text = &quot;&quot;;</span>
<span class="nc" id="L802">    text += &quot;[&quot; + String.format(&quot;%05d&quot;, getId()) + &quot;] &quot;;</span>
<span class="nc bnc" id="L803" title="All 2 branches missed.">    text += ((getRealOffsetStart() == null) ? &quot;[-------,-------]&quot;</span>
<span class="nc" id="L804">        : &quot;[&quot; + String.format(&quot;%07d&quot;, getRealOffsetStart()) + &quot;-&quot;</span>
<span class="nc" id="L805">            + String.format(&quot;%07d&quot;, getRealOffsetEnd()) + &quot;]&quot;);</span>
<span class="nc bnc" id="L806" title="All 2 branches missed.">    text += (provideRealOffset ? &quot;  &quot; : &quot;* &quot;);</span>
<span class="nc bnc" id="L807" title="All 2 branches missed.">    text += ((getOffsetStart() == null) ? &quot;[-------,-------]&quot;</span>
<span class="nc" id="L808">        : &quot;[&quot; + String.format(&quot;%07d&quot;, getOffsetStart()) + &quot;-&quot;</span>
<span class="nc" id="L809">            + String.format(&quot;%07d&quot;, getOffsetEnd()) + &quot;]&quot;);</span>
<span class="nc bnc" id="L810" title="All 2 branches missed.">    text += (provideOffset ? &quot;  &quot; : &quot;* &quot;);</span>
<span class="nc bnc" id="L811" title="All 2 branches missed.">    if (getPositionLength() == null) {</span>
<span class="nc" id="L812">      text += String.format(&quot;%11s&quot;, &quot;&quot;);</span>
<span class="nc bnc" id="L813" title="All 2 branches missed.">    } else if (getPositionStart().equals(getPositionEnd())) {</span>
<span class="nc" id="L814">      text += String.format(&quot;%11s&quot;, &quot;[&quot; + getPositionStart() + &quot;]&quot;);</span>
<span class="nc bnc" id="L815" title="All 2 branches missed.">    } else if ((getPositions() == null) || (getPositions().length == (1</span>
<span class="nc bnc" id="L816" title="All 2 branches missed.">        + getPositionEnd() - getPositionStart()))) {</span>
<span class="nc" id="L817">      text += String.format(&quot;%11s&quot;,</span>
<span class="nc" id="L818">          &quot;[&quot; + getPositionStart() + &quot;-&quot; + getPositionEnd() + &quot;]&quot;);</span>
    } else {
<span class="nc" id="L820">      text += String.format(&quot;%11s&quot;, Arrays.toString(getPositions()));</span>
    }
<span class="nc bnc" id="L822" title="All 2 branches missed.">    text += ((getParentId() == null) ? &quot;[-----]&quot;</span>
<span class="nc" id="L823">        : &quot;[&quot; + String.format(&quot;%05d&quot;, getParentId()) + &quot;]&quot;);</span>
<span class="nc bnc" id="L824" title="All 2 branches missed.">    text += (provideParentId ? &quot;  &quot; : &quot;* &quot;);</span>
<span class="nc" id="L825">    BytesRef payload = getPayload();</span>
<span class="nc bnc" id="L826" title="All 2 branches missed.">    text += (payload == null) ? &quot;[------] &quot;</span>
        : &quot;[&quot;
            + String
<span class="nc" id="L829">                .format(&quot;%.4f&quot;,</span>
<span class="nc" id="L830">                    PayloadHelper.decodeFloat(Arrays.copyOfRange(payload.bytes,</span>
                        payload.offset, (payload.offset + payload.length))))
            + &quot;] &quot;;
<span class="nc" id="L833">    text += String.format(&quot;%25s&quot;, &quot;[&quot; + getPrefix() + &quot;]&quot;) + &quot; &quot;;</span>
<span class="nc bnc" id="L834" title="All 2 branches missed.">    text += ((getPostfix() == null) ? &quot;---&quot; : &quot;[&quot; + getPostfix() + &quot;]&quot;) + &quot; &quot;;</span>
<span class="nc" id="L835">    return text;</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>