MtasToken.java.html 34.7 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 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
<?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.Objects;
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="L34">  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="L44">  public static final Pattern patternPrePostFix = Pattern</span>
<span class="fc" id="L45">      .compile(regexpPrePostFix);</span>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  /**
   * Instantiates a new mtas token.
   *
   * @param tokenId the token id
   * @param prefix the prefix
   * @param postfix the postfix
   */
<span class="nc" id="L108">  protected MtasToken(Integer tokenId, String prefix, String postfix) {</span>
<span class="nc" id="L109">    Objects.requireNonNull(prefix, &quot;prefix is obligatory&quot;);</span>
<span class="nc" id="L110">    this.tokenId = tokenId;</span>
<span class="nc" id="L111">    setType();</span>
<span class="nc bnc" id="L112" title="All 2 branches missed.">    if (postfix != null) {</span>
<span class="nc" id="L113">      setValue(prefix + DELIMITER + postfix);</span>
    } else {
<span class="nc" id="L115">      setValue(prefix + DELIMITER);</span>
    }
<span class="nc" id="L117">  }</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="L127">    this(tokenId, value);</span>
<span class="nc" id="L128">    addPosition(position);</span>
<span class="nc" id="L129">  }</span>

  /**
   * Instantiates a new mtas token.
   *
   * @param tokenId the token id
   * @param prefix the prefix
   * @param postfix the postfix
   * @param position the position
   */
  protected MtasToken(Integer tokenId, String prefix, String postfix,
      Integer position) {
<span class="nc" id="L141">    this(tokenId, prefix, postfix);</span>
<span class="nc" id="L142">    addPosition(position);</span>
<span class="nc" id="L143">  }</span>

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

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

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

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

  /**
   * Sets the prefix id.
   *
   * @param id the new prefix id
   */
  final public void setPrefixId(int id) {
<span class="fc" id="L187">    prefixId = id;</span>
<span class="fc" id="L188">  }</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="L197" title="1 of 2 branches missed.">    if (prefixId != null) {</span>
<span class="fc" id="L198">      return prefixId;</span>
    } else {
<span class="nc" id="L200">      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="L210">    tokenId = id;</span>
<span class="fc" id="L211">  }</span>

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

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

  /**
   * Gets the parent id.
   *
   * @return the parent id
   */
  final public Integer getParentId() {
<span class="fc" id="L237">    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="L246">    provideParentId = provide;</span>
<span class="fc" id="L247">  }</span>

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

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

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

  /**
   * Adds the position.
   *
   * @param position the position
   */
  final public void addPosition(int position) {
<span class="pc bpc" id="L280" title="1 of 2 branches missed.">    if (tokenPosition == null) {</span>
<span class="fc" id="L281">      tokenPosition = new MtasPosition(position);</span>
    } else {
<span class="nc" id="L283">      tokenPosition.add(position);</span>
    }
<span class="fc" id="L285">  }</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="L294" title="1 of 2 branches missed.">    if (tokenPosition == null) {</span>
<span class="fc" id="L295">      tokenPosition = new MtasPosition(start, end);</span>
    } else {
<span class="nc" id="L297">      int[] positions = new int[end - start + 1];</span>
<span class="nc bnc" id="L298" title="All 2 branches missed.">      for (int i = start; i &lt;= end; i++) {</span>
<span class="nc" id="L299">        positions[i - start] = i;</span>
      }
<span class="nc" id="L301">      tokenPosition.add(positions);</span>
    }
<span class="fc" id="L303">  }</span>

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

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

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

  /**
   * Gets the position start.
   *
   * @return the position start
   */
  final public Integer getPositionStart() {
<span class="pc bpc" id="L351" 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="L360" 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="L369" 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="L378" 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="L387" title="All 4 branches missed.">    if ((tokenOffset == null) || !provideOffset) {</span>
<span class="nc" id="L388">      return false;</span>
    } else {
<span class="nc" id="L390">      return true;</span>
    }
  }

  /**
   * Check real offset.
   *
   * @return the boolean
   */
  final public Boolean checkRealOffset() {
<span class="nc bnc" id="L400" title="All 4 branches missed.">    if ((tokenRealOffset == null) || !provideRealOffset) {</span>
<span class="nc" id="L401">      return false;</span>
<span class="nc bnc" id="L402" title="All 2 branches missed.">    } else if (tokenOffset == null) {</span>
<span class="nc" id="L403">      return true;</span>
<span class="nc bnc" id="L404" title="All 2 branches missed.">    } else if (tokenOffset.getStart() == tokenRealOffset.getStart()</span>
<span class="nc bnc" id="L405" title="All 2 branches missed.">        &amp;&amp; tokenOffset.getEnd() == tokenRealOffset.getEnd()) {</span>
<span class="nc" id="L406">      return false;</span>
    } else {
<span class="nc" id="L408">      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="L419" title="2 of 4 branches missed.">    if ((start == null) || (end == null)) {</span>
      // do nothing
<span class="pc bpc" id="L421" title="1 of 2 branches missed.">    } else if (start &gt; end) {</span>
<span class="nc" id="L422">      throw new IllegalArgumentException(&quot;Start offset after end offset&quot;);</span>
    } else {
<span class="fc" id="L424">      tokenOffset = new MtasOffset(start, end);</span>
    }
<span class="fc" id="L426">  }</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="L435" title="All 2 branches covered.">    if (tokenOffset == null) {</span>
<span class="fc" id="L436">      setOffset(start, end);</span>
<span class="pc bpc" id="L437" title="2 of 4 branches missed.">    } else if ((start == null) || (end == null)) {</span>
      // do nothing
<span class="pc bpc" id="L439" title="1 of 2 branches missed.">    } else if (start &gt; end) {</span>
<span class="nc" id="L440">      throw new IllegalArgumentException(&quot;Start offset after end offset&quot;);</span>
    } else {
<span class="fc" id="L442">      tokenOffset.add(start, end);</span>
    }
<span class="fc" id="L444">  }</span>

  /**
   * Sets the provide offset.
   *
   * @param provide the new provide offset
   */
  final public void setProvideOffset(Boolean provide) {
<span class="fc" id="L452">    provideOffset = provide;</span>
<span class="fc" id="L453">  }</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="L462" title="2 of 4 branches missed.">    if ((start == null) || (end == null)) {</span>
      // do nothing
<span class="pc bpc" id="L464" title="1 of 2 branches missed.">    } else if (start &gt; end) {</span>
<span class="nc" id="L465">      throw new IllegalArgumentException(</span>
          &quot;Start real offset after end real offset&quot;);
    } else {
<span class="fc" id="L468">      tokenRealOffset = new MtasOffset(start, end);</span>
    }
<span class="fc" id="L470">  }</span>

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

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

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

  /**
   * Gets the offset start.
   *
   * @return the offset start
   */
  final public Integer getOffsetStart() {
<span class="pc bpc" id="L505" 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="L514" 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="L523" 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="L532" 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="L541">    tokenValue = value;</span>
<span class="fc" id="L542">  }</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="L551" title="1 of 2 branches missed.">    if (value == null) {</span>
<span class="nc" id="L552">      return null;</span>
<span class="pc bpc" id="L553" title="1 of 2 branches missed.">    } else if (value.contains(DELIMITER)) {</span>
<span class="fc" id="L554">      String[] list = value.split(DELIMITER);</span>
<span class="pc bpc" id="L555" title="2 of 4 branches missed.">      if (list != null &amp;&amp; list.length &gt; 0) {</span>
<span class="fc" id="L556">        return list[0].replaceAll(&quot;\u0000&quot;, &quot;&quot;);</span>
      } else {
<span class="nc" id="L558">        return null;</span>
      }
    } else {
<span class="nc" id="L561">      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="L572">    String postfix = &quot;&quot;;</span>
<span class="fc" id="L573">    Matcher m = patternPrePostFix.matcher(value);</span>
<span class="fc bfc" id="L574" title="All 2 branches covered.">    if (m.find()) {</span>
<span class="fc" id="L575">      postfix = m.group(2);</span>

    }
<span class="fc" id="L578">    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="L588">    int i = term.offset;</span>
<span class="fc" id="L589">    int length = term.offset + term.length;</span>
<span class="fc" id="L590">    byte[] postfix = new byte[length];</span>
<span class="pc bpc" id="L591" title="1 of 2 branches missed.">    while (i &lt; length) {</span>
<span class="pc bpc" id="L592" title="1 of 2 branches missed.">      if ((term.bytes[i] &amp; 0b10000000) == 0b00000000) {</span>
<span class="fc bfc" id="L593" title="All 2 branches covered.">        if (term.bytes[i] == 0b00000001) {</span>
<span class="fc" id="L594">          i++;</span>
<span class="fc" id="L595">          break;</span>
        } else {
<span class="fc" id="L597">          i++;</span>
        }
<span class="nc bnc" id="L599" title="All 2 branches missed.">      } else if ((term.bytes[i] &amp; 0b11100000) == 0b11000000) {</span>
<span class="nc" id="L600">        i += 2;</span>
<span class="nc bnc" id="L601" title="All 2 branches missed.">      } else if ((term.bytes[i] &amp; 0b11110000) == 0b11100000) {</span>
<span class="nc" id="L602">        i += 3;</span>
<span class="nc bnc" id="L603" title="All 2 branches missed.">      } else if ((term.bytes[i] &amp; 0b11111000) == 0b11110000) {</span>
<span class="nc" id="L604">        i += 4;</span>
<span class="nc bnc" id="L605" title="All 2 branches missed.">      } else if ((term.bytes[i] &amp; 0b11111100) == 0b11111000) {</span>
<span class="nc" id="L606">        i += 5;</span>
<span class="nc bnc" id="L607" title="All 2 branches missed.">      } else if ((term.bytes[i] &amp; 0b11111110) == 0b11111100) {</span>
<span class="nc" id="L608">        i += 6;</span>
      } else {
<span class="nc" id="L610">        return &quot;&quot;;</span>
      }
    }
<span class="fc" id="L613">    int start = i;</span>
<span class="pc bpc" id="L614" title="1 of 2 branches missed.">    while (i &lt; length) {</span>
<span class="pc bpc" id="L615" title="1 of 2 branches missed.">      if ((term.bytes[i] &amp; 0b10000000) == 0b00000000) {</span>
<span class="fc bfc" id="L616" title="All 2 branches covered.">        if (term.bytes[i] == 0b00000000) {</span>
<span class="fc" id="L617">          break;</span>
        }
<span class="fc" id="L619">        postfix[i] = term.bytes[i];</span>
<span class="fc" id="L620">        i++;</span>
<span class="nc bnc" id="L621" title="All 2 branches missed.">      } else if ((term.bytes[i] &amp; 0b11100000) == 0b11000000) {</span>
<span class="nc" id="L622">        postfix[i] = term.bytes[i];</span>
<span class="nc" id="L623">        postfix[i + 1] = term.bytes[i + 1];</span>
<span class="nc" id="L624">        i += 2;</span>
<span class="nc bnc" id="L625" title="All 2 branches missed.">      } else if ((term.bytes[i] &amp; 0b11110000) == 0b11100000) {</span>
<span class="nc" id="L626">        postfix[i] = term.bytes[i];</span>
<span class="nc" id="L627">        postfix[i + 1] = term.bytes[i + 1];</span>
<span class="nc" id="L628">        postfix[i + 2] = term.bytes[i + 2];</span>
<span class="nc" id="L629">        i += 3;</span>
<span class="nc bnc" id="L630" title="All 2 branches missed.">      } else if ((term.bytes[i] &amp; 0b11111000) == 0b11110000) {</span>
<span class="nc" id="L631">        postfix[i] = term.bytes[i];</span>
<span class="nc" id="L632">        postfix[i + 1] = term.bytes[i + 1];</span>
<span class="nc" id="L633">        postfix[i + 2] = term.bytes[i + 2];</span>
<span class="nc" id="L634">        postfix[i + 3] = term.bytes[i + 3];</span>
<span class="nc" id="L635">        i += 4;</span>
<span class="nc bnc" id="L636" title="All 2 branches missed.">      } else if ((term.bytes[i] &amp; 0b11111100) == 0b11111000) {</span>
<span class="nc" id="L637">        postfix[i] = term.bytes[i];</span>
<span class="nc" id="L638">        postfix[i + 1] = term.bytes[i + 1];</span>
<span class="nc" id="L639">        postfix[i + 2] = term.bytes[i + 2];</span>
<span class="nc" id="L640">        postfix[i + 3] = term.bytes[i + 3];</span>
<span class="nc" id="L641">        postfix[i + 4] = term.bytes[i + 4];</span>
<span class="nc" id="L642">        i += 5;</span>
<span class="nc bnc" id="L643" title="All 2 branches missed.">      } else if ((term.bytes[i] &amp; 0b11111110) == 0b11111100) {</span>
<span class="nc" id="L644">        postfix[i] = term.bytes[i];</span>
<span class="nc" id="L645">        postfix[i + 1] = term.bytes[i + 1];</span>
<span class="nc" id="L646">        postfix[i + 2] = term.bytes[i + 2];</span>
<span class="nc" id="L647">        postfix[i + 3] = term.bytes[i + 3];</span>
<span class="nc" id="L648">        postfix[i + 4] = term.bytes[i + 4];</span>
<span class="nc" id="L649">        postfix[i + 5] = term.bytes[i + 5];</span>
<span class="nc" id="L650">        i += 6;</span>
      } else {
<span class="nc" id="L652">        return &quot;&quot;;</span>
      }
    }
<span class="fc" id="L655">    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="L665">    return tokenValue;</span>
  }

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

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

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

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

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

  /**
   * Gets the payload.
   *
   * @return the payload
   */
  public BytesRef getPayload() {
<span class="nc" id="L727">    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="L740">    HashMap&lt;String, Automaton&gt; automatonMap = new HashMap&lt;&gt;();</span>
<span class="pc bpc" id="L741" title="1 of 2 branches missed.">    if (valueList != null) {</span>
<span class="fc bfc" id="L742" title="All 2 branches covered.">      for (String item : valueList) {</span>
<span class="fc bfc" id="L743" title="All 2 branches covered.">        if (filter) {</span>
<span class="fc" id="L744">          item = item.replaceAll(&quot;([\\\&quot;\\)\\(\\&lt;\\&gt;\\.\\@\\#\\]\\[\\{\\}])&quot;,</span>
              &quot;\\\\$1&quot;);
        }
<span class="fc" id="L747">        automatonMap.put(item,</span>
            new RegExp(prefix + MtasToken.DELIMITER + item + &quot;\u0000*&quot;)
<span class="fc" id="L749">                .toAutomaton());</span>
<span class="fc" id="L750">      }</span>
    }
<span class="fc" id="L752">    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="L763">    HashMap&lt;String, ByteRunAutomaton&gt; byteRunAutomatonMap = new HashMap&lt;&gt;();</span>
<span class="nc bnc" id="L764" title="All 2 branches missed.">    if (automatonMap != null) {</span>
<span class="nc bnc" id="L765" title="All 2 branches missed.">      for (Entry&lt;String, Automaton&gt; entry : automatonMap.entrySet()) {</span>
<span class="nc" id="L766">        byteRunAutomatonMap.put(entry.getKey(),</span>
<span class="nc" id="L767">            new ByteRunAutomaton(entry.getValue()));</span>
<span class="nc" id="L768">      }</span>
    }
<span class="nc" id="L770">    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="L784">    List&lt;CompiledAutomaton&gt; list = new ArrayList&lt;&gt;();</span>
<span class="fc" id="L785">    Automaton automatonRegexp = null;</span>
<span class="fc bfc" id="L786" title="All 2 branches covered.">    if (regexp != null) {</span>
<span class="fc" id="L787">      RegExp re = new RegExp(prefix + MtasToken.DELIMITER + regexp + &quot;\u0000*&quot;);</span>
<span class="fc" id="L788">      automatonRegexp = re.toAutomaton();</span>
    }
<span class="fc" id="L790">    int step = 500;</span>
<span class="fc" id="L791">    List&lt;String&gt; keyList = new ArrayList&lt;&gt;(automatonMap.keySet());</span>
<span class="fc bfc" id="L792" title="All 2 branches covered.">    for (int i = 0; i &lt; keyList.size(); i += step) {</span>
<span class="fc" id="L793">      int localStep = step;</span>
<span class="fc" id="L794">      boolean success = false;</span>
<span class="fc" id="L795">      CompiledAutomaton compiledAutomaton = null;</span>
<span class="fc bfc" id="L796" title="All 2 branches covered.">      while (!success) {</span>
<span class="fc" id="L797">        success = true;</span>
<span class="fc" id="L798">        int next = Math.min(keyList.size(), i + localStep);</span>
<span class="fc" id="L799">        List&lt;Automaton&gt; listAutomaton = new ArrayList&lt;&gt;();</span>
<span class="fc bfc" id="L800" title="All 2 branches covered.">        for (int j = i; j &lt; next; j++) {</span>
<span class="fc" id="L801">          listAutomaton.add(automatonMap.get(keyList.get(j)));</span>
        }
<span class="fc" id="L803">        Automaton automatonList = Operations.union(listAutomaton);</span>
        Automaton automaton;
<span class="fc bfc" id="L805" title="All 2 branches covered.">        if (automatonRegexp != null) {</span>
<span class="fc" id="L806">          automaton = Operations.intersection(automatonList, automatonRegexp);</span>
        } else {
<span class="fc" id="L808">          automaton = automatonList;</span>
        }
        try {
<span class="fc" id="L811">          compiledAutomaton = new CompiledAutomaton(automaton);</span>
<span class="nc" id="L812">        } catch (TooComplexToDeterminizeException e) {</span>
<span class="nc" id="L813">          log.debug(e);</span>
<span class="nc" id="L814">          success = false;</span>
<span class="nc bnc" id="L815" title="All 2 branches missed.">          if (localStep &gt; 1) {</span>
<span class="nc" id="L816">            localStep /= 2;</span>
          } else {
<span class="nc" id="L818">            throw new IOException(&quot;TooComplexToDeterminizeException&quot;);</span>
          }
<span class="fc" id="L820">        }</span>
<span class="fc" id="L821">      }</span>
<span class="fc" id="L822">      list.add(compiledAutomaton);</span>
    }
<span class="fc" id="L824">    return list;</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString() {
<span class="nc" id="L834">    String text = &quot;&quot;;</span>
<span class="nc" id="L835">    text += &quot;[&quot; + String.format(&quot;%05d&quot;, getId()) + &quot;] &quot;;</span>
<span class="nc bnc" id="L836" title="All 2 branches missed.">    text += ((getRealOffsetStart() == null) ? &quot;[-------,-------]&quot;</span>
<span class="nc" id="L837">        : &quot;[&quot; + String.format(&quot;%07d&quot;, getRealOffsetStart()) + &quot;-&quot;</span>
<span class="nc" id="L838">            + String.format(&quot;%07d&quot;, getRealOffsetEnd()) + &quot;]&quot;);</span>
<span class="nc bnc" id="L839" title="All 2 branches missed.">    text += (provideRealOffset ? &quot;  &quot; : &quot;* &quot;);</span>
<span class="nc bnc" id="L840" title="All 2 branches missed.">    text += ((getOffsetStart() == null) ? &quot;[-------,-------]&quot;</span>
<span class="nc" id="L841">        : &quot;[&quot; + String.format(&quot;%07d&quot;, getOffsetStart()) + &quot;-&quot;</span>
<span class="nc" id="L842">            + String.format(&quot;%07d&quot;, getOffsetEnd()) + &quot;]&quot;);</span>
<span class="nc bnc" id="L843" title="All 2 branches missed.">    text += (provideOffset ? &quot;  &quot; : &quot;* &quot;);</span>
<span class="nc bnc" id="L844" title="All 2 branches missed.">    if (getPositionLength() == null) {</span>
<span class="nc" id="L845">      text += String.format(&quot;%11s&quot;, &quot;&quot;);</span>
<span class="nc bnc" id="L846" title="All 2 branches missed.">    } else if (getPositionStart().equals(getPositionEnd())) {</span>
<span class="nc" id="L847">      text += String.format(&quot;%11s&quot;, &quot;[&quot; + getPositionStart() + &quot;]&quot;);</span>
<span class="nc bnc" id="L848" title="All 2 branches missed.">    } else if ((getPositions() == null) || (getPositions().length == (1</span>
<span class="nc bnc" id="L849" title="All 2 branches missed.">        + getPositionEnd() - getPositionStart()))) {</span>
<span class="nc" id="L850">      text += String.format(&quot;%11s&quot;,</span>
<span class="nc" id="L851">          &quot;[&quot; + getPositionStart() + &quot;-&quot; + getPositionEnd() + &quot;]&quot;);</span>
    } else {
<span class="nc" id="L853">      text += String.format(&quot;%11s&quot;, Arrays.toString(getPositions()));</span>
    }
<span class="nc bnc" id="L855" title="All 2 branches missed.">    text += ((getParentId() == null) ? &quot;[-----]&quot;</span>
<span class="nc" id="L856">        : &quot;[&quot; + String.format(&quot;%05d&quot;, getParentId()) + &quot;]&quot;);</span>
<span class="nc bnc" id="L857" title="All 2 branches missed.">    text += (provideParentId ? &quot;  &quot; : &quot;* &quot;);</span>
<span class="nc" id="L858">    BytesRef payload = getPayload();</span>
<span class="nc bnc" id="L859" title="All 2 branches missed.">    text += (payload == null) ? &quot;[------] &quot;</span>
        : &quot;[&quot;
            + String
<span class="nc" id="L862">                .format(&quot;%.4f&quot;,</span>
<span class="nc" id="L863">                    PayloadHelper.decodeFloat(Arrays.copyOfRange(payload.bytes,</span>
                        payload.offset, (payload.offset + payload.length))))
            + &quot;] &quot;;
<span class="nc" id="L866">    text += String.format(&quot;%25s&quot;, &quot;[&quot; + getPrefix() + &quot;]&quot;) + &quot; &quot;;</span>
<span class="nc bnc" id="L867" title="All 2 branches missed.">    text += ((getPostfix() == null) ? &quot;---&quot; : &quot;[&quot; + getPostfix() + &quot;]&quot;) + &quot; &quot;;</span>
<span class="nc" id="L868">    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>