MtasPosition.java.html 13 KB
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../.resources/report.css" type="text/css"/><link rel="shortcut icon" href="../.resources/report.gif" type="image/gif"/><title>MtasPosition.java</title><link rel="stylesheet" href="../.resources/prettify.css" type="text/css"/><script type="text/javascript" src="../.resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../.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">MtasPosition.java</span></div><h1>MtasPosition.java</h1><pre class="source lang-java linenums">package mtas.analysis.token;

import java.util.Arrays;
import java.util.TreeSet;

import org.apache.commons.lang.ArrayUtils;

/**
 * The Class MtasPosition.
 */
public class MtasPosition {

  /** The position single. */
  public final static String POSITION_SINGLE = &quot;single&quot;;

  /** The position range. */
  public final static String POSITION_RANGE = &quot;range&quot;;

  /** The position set. */
  public final static String POSITION_SET = &quot;set&quot;;

  /** The mtas position type. */
  private String mtasPositionType;

  /** The mtas position start. */
  private int mtasPositionStart;

  /** The mtas position end. */
  private int mtasPositionEnd;

  /** The mtas position list. */
<span class="fc" id="L32">  private int[] mtasPositionList = null;</span>

  /**
   * Instantiates a new mtas position.
   *
   * @param position
   *          the position
   */
<span class="fc" id="L40">  public MtasPosition(int position) {</span>
<span class="fc" id="L41">    mtasPositionType = POSITION_SINGLE;</span>
<span class="fc" id="L42">    mtasPositionStart = position;</span>
<span class="fc" id="L43">  }</span>

  /**
   * Instantiates a new mtas position.
   *
   * @param start
   *          the start
   * @param end
   *          the end
   */
<span class="fc" id="L53">  public MtasPosition(int start, int end) {</span>
<span class="pc bpc" id="L54" title="1 of 2 branches missed.">    if (start == end) {</span>
<span class="nc" id="L55">      mtasPositionType = POSITION_SINGLE;</span>
<span class="nc" id="L56">      mtasPositionStart = start;</span>
    } else {
<span class="fc" id="L58">      mtasPositionType = POSITION_RANGE;</span>
<span class="fc" id="L59">      mtasPositionStart = start;</span>
<span class="fc" id="L60">      mtasPositionEnd = end;</span>
    }
<span class="fc" id="L62">  }</span>

  /**
   * Instantiates a new mtas position.
   *
   * @param positions
   *          the positions
   */
<span class="fc" id="L70">  public MtasPosition(int[] positions) {</span>
<span class="fc" id="L71">    TreeSet&lt;Integer&gt; list = new TreeSet&lt;Integer&gt;();</span>
<span class="fc bfc" id="L72" title="All 2 branches covered.">    for (int p : positions) {</span>
<span class="fc" id="L73">      list.add(p);</span>
    }
<span class="fc bfc" id="L75" title="All 2 branches covered.">    if (list.size() == 1) {</span>
<span class="fc" id="L76">      mtasPositionType = POSITION_SINGLE;</span>
<span class="fc" id="L77">      mtasPositionStart = list.first();</span>
    } else {
<span class="fc" id="L79">      mtasPositionType = POSITION_SET;</span>
<span class="fc" id="L80">      mtasPositionList = ArrayUtils</span>
<span class="fc" id="L81">          .toPrimitive(list.toArray(new Integer[list.size()]));</span>
<span class="fc" id="L82">      mtasPositionStart = list.first();</span>
<span class="fc" id="L83">      mtasPositionEnd = list.last();</span>
<span class="fc bfc" id="L84" title="All 2 branches covered.">      if (mtasPositionList.length == (1 + mtasPositionEnd</span>
          - mtasPositionStart)) {
<span class="fc" id="L86">        mtasPositionType = POSITION_RANGE;</span>
<span class="fc" id="L87">        mtasPositionList = null;</span>
      }
    }
<span class="fc" id="L90">  }</span>

  /**
   * Check type.
   *
   * @param type
   *          the type
   * @return the boolean
   */
  public Boolean checkType(String type) {
<span class="pc bpc" id="L100" title="1 of 2 branches missed.">    if (mtasPositionType == null) {</span>
<span class="nc" id="L101">      return false;</span>
    } else {
<span class="fc" id="L103">      return mtasPositionType.equals(type);</span>
    }
  }

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

  /**
   * Gets the end.
   *
   * @return the end
   */
  public Integer getEnd() {
<span class="fc bfc" id="L122" title="All 2 branches covered.">    if (mtasPositionType.equals(POSITION_RANGE)</span>
<span class="fc bfc" id="L123" title="All 2 branches covered.">        || mtasPositionType.equals(POSITION_SET)) {</span>
<span class="fc" id="L124">      return mtasPositionEnd;</span>
<span class="pc bpc" id="L125" title="1 of 2 branches missed.">    } else if (mtasPositionType.equals(POSITION_SINGLE)) {</span>
<span class="fc" id="L126">      return mtasPositionStart;</span>
    } else {
<span class="nc" id="L128">      return null;</span>
    }
  }

  /**
   * Gets the positions.
   *
   * @return the positions
   */
  public int[] getPositions() {
<span class="pc bpc" id="L138" title="1 of 2 branches missed.">    return (mtasPositionType.equals(POSITION_SET))</span>
<span class="pc" id="L139">        ? (int[]) mtasPositionList.clone() : null;</span>
  }

  /**
   * Gets the length.
   *
   * @return the length
   */
  public Integer getLength() {
<span class="nc bnc" id="L148" title="All 2 branches missed.">    if (mtasPositionType.equals(POSITION_SINGLE)) {</span>
<span class="nc" id="L149">      return 1;</span>
<span class="nc bnc" id="L150" title="All 2 branches missed.">    } else if (mtasPositionType.equals(POSITION_RANGE)</span>
<span class="nc bnc" id="L151" title="All 2 branches missed.">        || mtasPositionType.equals(POSITION_SET)) {</span>
<span class="nc" id="L152">      return 1 + mtasPositionEnd - mtasPositionStart;</span>
    } else {
<span class="nc" id="L154">      return null;</span>
    }
  }

  /**
   * Adds the.
   *
   * @param positions
   *          the positions
   */
  public void add(int[] positions) {
<span class="fc" id="L165">    TreeSet&lt;Integer&gt; list = new TreeSet&lt;Integer&gt;();</span>
<span class="fc bfc" id="L166" title="All 2 branches covered.">    for (int p : positions) {</span>
<span class="fc" id="L167">      list.add(p);</span>
    }
<span class="fc bfc" id="L169" title="All 2 branches covered.">    if (mtasPositionType.equals(POSITION_SINGLE)) {</span>
<span class="fc" id="L170">      mtasPositionType = POSITION_SET;</span>
<span class="fc" id="L171">      list.add(mtasPositionStart);</span>
<span class="fc bfc" id="L172" title="All 2 branches covered.">    } else if (mtasPositionType.equals(POSITION_RANGE)) {</span>
<span class="fc" id="L173">      mtasPositionType = POSITION_SET;</span>
<span class="fc bfc" id="L174" title="All 2 branches covered.">      for (int i = mtasPositionStart; i &lt;= mtasPositionEnd; i++) {</span>
<span class="fc" id="L175">        list.add(i);</span>
      }
<span class="pc bpc" id="L177" title="1 of 2 branches missed.">    } else if (mtasPositionType.equals(POSITION_SET)) {</span>
<span class="fc bfc" id="L178" title="All 2 branches covered.">      for (int p : mtasPositionList) {</span>
<span class="fc" id="L179">        list.add(p);</span>
      }
    }
<span class="fc" id="L182">    mtasPositionList = ArrayUtils</span>
<span class="fc" id="L183">        .toPrimitive(list.toArray(new Integer[list.size()]));</span>
<span class="fc" id="L184">    mtasPositionStart = list.first();</span>
<span class="fc" id="L185">    mtasPositionEnd = list.last();</span>
<span class="pc bpc" id="L186" title="1 of 2 branches missed.">    if (list.size() == 1) {</span>
<span class="nc" id="L187">      mtasPositionType = POSITION_SINGLE;</span>
<span class="nc" id="L188">      mtasPositionList = null;</span>
<span class="fc bfc" id="L189" title="All 2 branches covered.">    } else if (list.size() == (1 + mtasPositionEnd - mtasPositionStart)) {</span>
<span class="fc" id="L190">      mtasPositionType = POSITION_RANGE;</span>
<span class="fc" id="L191">      mtasPositionList = null;</span>
    }
<span class="fc" id="L193">  }</span>

  /**
   * Adds the.
   *
   * @param position
   *          the position
   */
  public void add(int position) {
<span class="nc bnc" id="L202" title="All 2 branches missed.">    if (mtasPositionType.equals(POSITION_SINGLE)) {</span>
<span class="nc bnc" id="L203" title="All 2 branches missed.">      if (position != mtasPositionStart) {</span>
<span class="nc bnc" id="L204" title="All 2 branches missed.">        if (position == (mtasPositionStart + 1)) {</span>
<span class="nc" id="L205">          mtasPositionType = POSITION_RANGE;</span>
<span class="nc" id="L206">          mtasPositionEnd = position;</span>
<span class="nc bnc" id="L207" title="All 2 branches missed.">        } else if (position == (mtasPositionStart - 1)) {</span>
<span class="nc" id="L208">          mtasPositionType = POSITION_RANGE;</span>
<span class="nc" id="L209">          mtasPositionEnd = mtasPositionStart;</span>
<span class="nc" id="L210">          mtasPositionStart = position;</span>
        } else {
<span class="nc" id="L212">          mtasPositionType = POSITION_SET;</span>
<span class="nc" id="L213">          TreeSet&lt;Integer&gt; list = new TreeSet&lt;Integer&gt;();</span>
<span class="nc" id="L214">          list.add(position);</span>
<span class="nc" id="L215">          list.add(mtasPositionStart);</span>
<span class="nc" id="L216">          mtasPositionList = ArrayUtils</span>
<span class="nc" id="L217">              .toPrimitive(list.toArray(new Integer[list.size()]));</span>
<span class="nc" id="L218">          mtasPositionStart = list.first();</span>
<span class="nc" id="L219">          mtasPositionEnd = list.last();</span>
<span class="nc" id="L220">        }</span>
      }
    } else {
<span class="nc" id="L223">      TreeSet&lt;Integer&gt; list = new TreeSet&lt;Integer&gt;();</span>
<span class="nc bnc" id="L224" title="All 2 branches missed.">      if (mtasPositionType.equals(POSITION_RANGE)) {</span>
<span class="nc" id="L225">        mtasPositionType = POSITION_SET;</span>
<span class="nc bnc" id="L226" title="All 2 branches missed.">        for (int i = mtasPositionStart; i &lt;= mtasPositionEnd; i++) {</span>
<span class="nc" id="L227">          list.add(i);</span>
        }
<span class="nc" id="L229">        list.add(position);</span>
<span class="nc bnc" id="L230" title="All 2 branches missed.">      } else if (mtasPositionType.equals(POSITION_SET)) {</span>
<span class="nc bnc" id="L231" title="All 2 branches missed.">        for (int p : mtasPositionList) {</span>
<span class="nc" id="L232">          list.add(p);</span>
        }
<span class="nc" id="L234">        list.add(position);</span>
      }
<span class="nc" id="L236">      mtasPositionList = ArrayUtils</span>
<span class="nc" id="L237">          .toPrimitive(list.toArray(new Integer[list.size()]));</span>
<span class="nc" id="L238">      mtasPositionStart = list.first();</span>
<span class="nc" id="L239">      mtasPositionEnd = list.last();</span>
<span class="nc bnc" id="L240" title="All 2 branches missed.">      if (list.size() == (1 + mtasPositionEnd - mtasPositionStart)) {</span>
<span class="nc" id="L241">        mtasPositionType = POSITION_RANGE;</span>
<span class="nc" id="L242">        mtasPositionList = null;</span>
      }
    }
<span class="nc" id="L245">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString() {
<span class="nc bnc" id="L254" title="All 2 branches missed.">    if (mtasPositionType == null) {</span>
<span class="nc" id="L255">      return &quot;[null]&quot;;</span>
<span class="nc bnc" id="L256" title="All 2 branches missed.">    } else if (mtasPositionType.equals(POSITION_SINGLE)) {</span>
<span class="nc" id="L257">      return &quot;[&quot; + mtasPositionStart + &quot;]&quot;;</span>
<span class="nc bnc" id="L258" title="All 2 branches missed.">    } else if (mtasPositionType.equals(POSITION_RANGE)) {</span>
<span class="nc" id="L259">      return &quot;[&quot; + mtasPositionStart + &quot;-&quot; + mtasPositionEnd + &quot;]&quot;;</span>
<span class="nc bnc" id="L260" title="All 2 branches missed.">    } else if (mtasPositionType.equals(POSITION_SET)) {</span>
<span class="nc" id="L261">      return Arrays.toString(mtasPositionList);</span>
    } else {
<span class="nc" id="L263">      return &quot;[unknown]&quot;;</span>
    }
  }

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