MtasDataCollectorResult.java.html 12.2 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>MtasDataCollectorResult.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.codec.util.collector</a> &gt; <span class="el_source">MtasDataCollectorResult.java</span></div><h1>MtasDataCollectorResult.java</h1><pre class="source lang-java linenums">package mtas.codec.util.collector;

import java.io.IOException;
import java.io.Serializable;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;

import mtas.codec.util.CodecUtil;
import mtas.codec.util.DataCollector;

/**
 * The Class MtasDataCollectorResult.
 *
 * @param &lt;T1&gt;
 *          the generic type
 * @param &lt;T2&gt;
 *          the generic type
 */
public class MtasDataCollectorResult&lt;T1 extends Number &amp; Comparable&lt;T1&gt;, T2 extends Number &amp; Comparable&lt;T2&gt;&gt;
    implements Serializable {

  /** The Constant serialVersionUID. */
  private static final long serialVersionUID = 1L;

  /** The list. */
  SortedMap&lt;String, MtasDataItem&lt;T1, T2&gt;&gt; list;

  /** The item. */
  MtasDataItem&lt;T1, T2&gt; item;

  /** The collector type. */
  String sortType, sortDirection, collectorType;

  /** The last sort value. */
  @SuppressWarnings(&quot;rawtypes&quot;)
  MtasDataItemNumberComparator lastSortValue;

  /** The end key. */
  String startKey, endKey;

  /**
   * Instantiates a new mtas data collector result.
   *
   * @param collectorType
   *          the collector type
   * @param sortType
   *          the sort type
   * @param sortDirection
   *          the sort direction
   * @param basicList
   *          the basic list
   * @param start
   *          the start
   * @param number
   *          the number
   * @throws IOException
   *           Signals that an I/O exception has occurred.
   */
  public MtasDataCollectorResult(String collectorType, String sortType,
      String sortDirection, TreeMap&lt;String, MtasDataItem&lt;T1, T2&gt;&gt; basicList,
      Integer start, Integer number) throws IOException {
<span class="fc" id="L65">    this(collectorType, sortType, sortDirection);</span>
<span class="pc bpc" id="L66" title="2 of 4 branches missed.">    if (sortType == null || sortType.equals(CodecUtil.SORT_TERM)) {</span>
<span class="nc bnc" id="L67" title="All 4 branches missed.">      if (sortDirection == null || sortDirection.equals(CodecUtil.SORT_ASC)) {</span>
<span class="nc" id="L68">        list = basicList;</span>
<span class="nc bnc" id="L69" title="All 2 branches missed.">      } else if (sortDirection.equals(CodecUtil.SORT_DESC)) {</span>
<span class="nc" id="L70">        list = basicList.descendingMap();</span>
      } else {
<span class="nc" id="L72">        throw new IOException(&quot;unknown sort direction &quot; + sortDirection);</span>
      }
<span class="pc bpc" id="L74" title="1 of 2 branches missed.">    } else if (CodecUtil.isStatsType(sortType)) {</span>
      // comperator
<span class="fc" id="L76">      Comparator&lt;String&gt; valueComparator = new Comparator&lt;String&gt;() {</span>
        @Override
        public int compare(String k1, String k2) {
<span class="fc" id="L79">          int compare = basicList.get(k1).compareTo(basicList.get(k2));</span>
<span class="fc bfc" id="L80" title="All 2 branches covered.">          return compare == 0 ? k1.compareTo(k2) : compare;</span>
        }
      };
<span class="fc" id="L83">      SortedMap&lt;String, MtasDataItem&lt;T1, T2&gt;&gt; sortedByValues = new TreeMap&lt;String, MtasDataItem&lt;T1, T2&gt;&gt;(</span>
          valueComparator);
<span class="fc" id="L85">      sortedByValues.putAll(basicList);</span>
<span class="fc" id="L86">      list = sortedByValues;</span>
<span class="fc" id="L87">    } else {</span>
<span class="nc" id="L88">      throw new IOException(&quot;unknown sort type &quot; + sortType);</span>
    }
<span class="pc bpc" id="L90" title="1 of 2 branches missed.">    int listStart = start == null ? 0 : start;</span>
<span class="pc bpc" id="L91" title="2 of 6 branches missed.">    if (number == null || (start == 0 &amp;&amp; number &gt;= list.size())) {</span>
      // do nothing, full list is ok
<span class="pc bpc" id="L93" title="2 of 4 branches missed.">    } else if (listStart &lt; list.size() &amp;&amp; number &gt; 0) {</span>
      // subset
<span class="fc" id="L95">      String boundaryEndKey = null;</span>
<span class="fc" id="L96">      int counter = 0;</span>
<span class="fc" id="L97">      MtasDataItem&lt;T1, T2&gt; previous = null;</span>
<span class="pc bpc" id="L98" title="1 of 2 branches missed.">      for (Entry&lt;String, MtasDataItem&lt;T1, T2&gt;&gt; entry : list.entrySet()) {</span>
<span class="fc bfc" id="L99" title="All 2 branches covered.">        if (listStart == counter) {</span>
<span class="fc" id="L100">          startKey = entry.getKey();</span>
<span class="fc bfc" id="L101" title="All 2 branches covered.">        } else if (listStart + number &lt;= counter) {</span>
<span class="pc bpc" id="L102" title="2 of 4 branches missed.">          if (sortType==null || sortType.equals(CodecUtil.SORT_TERM)) {</span>
<span class="nc" id="L103">            endKey = entry.getKey();</span>
<span class="nc" id="L104">            boundaryEndKey = entry.getKey();</span>
<span class="nc" id="L105">            break;</span>
<span class="fc bfc" id="L106" title="All 2 branches covered.">          } else if (previous != null) {</span>
<span class="fc bfc" id="L107" title="All 2 branches covered.">            if (previous.compareTo(entry.getValue()) != 0) {</span>
<span class="fc" id="L108">              break;</span>
            } else {
<span class="fc" id="L110">              boundaryEndKey = entry.getKey();</span>
            }
          } else {
<span class="fc" id="L113">            endKey = entry.getKey();</span>
<span class="fc" id="L114">            boundaryEndKey = entry.getKey();</span>
<span class="fc" id="L115">            previous = entry.getValue();</span>
          }
        } else {
<span class="fc" id="L118">          endKey = entry.getKey();</span>
        }
<span class="fc" id="L120">        counter++;</span>
<span class="fc" id="L121">      }</span>
<span class="fc" id="L122">      list = list.subMap(startKey, boundaryEndKey);</span>
<span class="fc" id="L123">    } else {</span>
<span class="nc" id="L124">      list = new TreeMap&lt;&gt;();</span>
    }
<span class="pc bpc" id="L126" title="2 of 4 branches missed.">    if (list.size() &gt; 0 &amp;&amp; sortType != null) {</span>
<span class="fc" id="L127">      lastSortValue = list.get(list.lastKey()).getComparableValue();</span>
    }
<span class="fc" id="L129">  }</span>

  /**
   * Instantiates a new mtas data collector result.
   *
   * @param collectorType
   *          the collector type
   * @param item
   *          the item
   */
  public MtasDataCollectorResult(String collectorType,
      MtasDataItem&lt;T1, T2&gt; item) {
<span class="fc" id="L141">    this(collectorType, null, null);</span>
<span class="fc" id="L142">    this.item = item;</span>
<span class="fc" id="L143">  }</span>

  /**
   * Instantiates a new mtas data collector result.
   *
   * @param collectorType
   *          the collector type
   * @param sortType
   *          the sort type
   * @param sortDirection
   *          the sort direction
   */
  public MtasDataCollectorResult(String collectorType, String sortType,
<span class="fc" id="L156">      String sortDirection) {</span>
<span class="fc" id="L157">    list = null;</span>
<span class="fc" id="L158">    item = null;</span>
<span class="fc" id="L159">    lastSortValue = null;</span>
<span class="fc" id="L160">    this.collectorType = collectorType;</span>
<span class="fc" id="L161">    this.sortType = sortType;</span>
<span class="fc" id="L162">    this.sortDirection = sortDirection;</span>
<span class="fc" id="L163">  }</span>

  /**
   * Gets the list.
   *
   * @return the list
   * @throws IOException
   *           Signals that an I/O exception has occurred.
   */
  public final SortedMap&lt;String, MtasDataItem&lt;T1, T2&gt;&gt; getList()
      throws IOException {
<span class="fc" id="L174">    return getList(true);</span>
  }

  /**
   * Gets the list.
   *
   * @param reduce
   *          the reduce
   * @return the list
   * @throws IOException
   *           Signals that an I/O exception has occurred.
   */
  public final SortedMap&lt;String, MtasDataItem&lt;T1, T2&gt;&gt; getList(boolean reduce)
      throws IOException {
<span class="pc bpc" id="L188" title="1 of 2 branches missed.">    if (collectorType.equals(DataCollector.COLLECTOR_TYPE_LIST)) {</span>
<span class="pc bpc" id="L189" title="2 of 6 branches missed.">      if (reduce &amp;&amp; startKey != null &amp;&amp; endKey != null) {</span>
<span class="fc" id="L190">        return list.subMap(startKey, endKey);</span>
      } else {
<span class="fc" id="L192">        return list;</span>
      }
    } else {
<span class="nc" id="L195">      throw new IOException(&quot;type &quot; + collectorType + &quot; not supported&quot;);</span>
    }
  }

  /**
   * Gets the comparator list.
   *
   * @return the comparator list
   * @throws IOException
   *           Signals that an I/O exception has occurred.
   */
  @SuppressWarnings(&quot;rawtypes&quot;)
  public final LinkedHashMap&lt;String, MtasDataItemNumberComparator&gt; getComparatorList()
      throws IOException {
<span class="pc bpc" id="L209" title="1 of 2 branches missed.">    if (collectorType.equals(DataCollector.COLLECTOR_TYPE_LIST)) {</span>
<span class="fc" id="L210">      LinkedHashMap&lt;String, MtasDataItemNumberComparator&gt; comparatorList = new LinkedHashMap&lt;String, MtasDataItemNumberComparator&gt;();</span>
<span class="fc bfc" id="L211" title="All 2 branches covered.">      for (Entry&lt;String, MtasDataItem&lt;T1,T2&gt;&gt; entry: list.entrySet()) {</span>
<span class="fc" id="L212">        comparatorList.put(entry.getKey(), entry.getValue().getComparableValue());</span>
<span class="fc" id="L213">      }</span>
<span class="fc" id="L214">      return comparatorList;</span>
    } else {
<span class="nc" id="L216">      throw new IOException(&quot;type &quot; + collectorType + &quot; not supported&quot;);</span>
    }
  }

  /**
   * Gets the last sort value.
   *
   * @return the last sort value
   */
  @SuppressWarnings(&quot;rawtypes&quot;)
  public final MtasDataItemNumberComparator getLastSortValue() {
<span class="nc" id="L227">    return lastSortValue;</span>
  }

  /**
   * Gets the data.
   *
   * @return the data
   * @throws IOException
   *           Signals that an I/O exception has occurred.
   */
  public final MtasDataItem&lt;T1, T2&gt; getData() throws IOException {
<span class="pc bpc" id="L238" title="1 of 2 branches missed.">    if (collectorType.equals(DataCollector.COLLECTOR_TYPE_DATA)) {</span>
<span class="fc" id="L239">      return item;</span>
    } else {
<span class="nc" id="L241">      throw new IOException(&quot;type &quot; + collectorType + &quot; not supported&quot;);</span>
    }
  }
  
  @Override
  public String toString() {
<span class="nc" id="L247">    StringBuilder buffer = new StringBuilder();</span>
<span class="nc" id="L248">    buffer.append(this.getClass().getSimpleName() + &quot;(&quot;);</span>
<span class="nc" id="L249">    buffer.append(collectorType+&quot;,&quot;+sortType+&quot;,&quot;+sortDirection);</span>
<span class="nc" id="L250">    buffer.append(&quot;)&quot;);</span>
<span class="nc" id="L251">    return buffer.toString();</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>