MtasDataItemAdvanced.java.html 14 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="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>MtasDataItemAdvanced.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.codec.util.collector</a> &gt; <span class="el_source">MtasDataItemAdvanced.java</span></div><h1>MtasDataItemAdvanced.java</h1><pre class="source lang-java linenums">package mtas.codec.util.collector;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import mtas.codec.util.CodecUtil;

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

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

  /** The value sum. */
  protected T1 valueSum;

  /** The value sum of logs. */
  protected T2 valueSumOfLogs;

  /** The value sum of squares. */
  protected T1 valueSumOfSquares;

  /** The value min. */
  protected T1 valueMin;

  /** The value max. */
  protected T1 valueMax;

  /** The value N. */
  protected Long valueN;

  /** The operations. */
  protected MtasDataOperations&lt;T1, T2&gt; operations;

  /**
   * Instantiates a new mtas data item advanced.
   *
   * @param valueSum the value sum
   * @param valueSumOfLogs the value sum of logs
   * @param valueSumOfSquares the value sum of squares
   * @param valueMin the value min
   * @param valueMax the value max
   * @param valueN the value N
   * @param sub the sub
   * @param statsItems the stats items
   * @param sortType the sort type
   * @param sortDirection the sort direction
   * @param errorNumber the error number
   * @param errorList the error list
   * @param operations the operations
   * @param sourceNumber the source number
   */
  public MtasDataItemAdvanced(T1 valueSum, T2 valueSumOfLogs,
      T1 valueSumOfSquares, T1 valueMin, T1 valueMax, Long valueN,
      MtasDataCollector&lt;?, ?&gt; sub, Set&lt;String&gt; statsItems, String sortType,
      String sortDirection, int errorNumber, Map&lt;String, Integer&gt; errorList,
      MtasDataOperations&lt;T1, T2&gt; operations, int sourceNumber) {
<span class="fc" id="L68">    super(sub, statsItems, sortType, sortDirection, errorNumber, errorList,</span>
        sourceNumber);
<span class="fc" id="L70">    this.valueSum = valueSum;</span>
<span class="fc" id="L71">    this.valueSumOfLogs = valueSumOfLogs;</span>
<span class="fc" id="L72">    this.valueSumOfSquares = valueSumOfSquares;</span>
<span class="fc" id="L73">    this.valueMin = valueMin;</span>
<span class="fc" id="L74">    this.valueMax = valueMax;</span>
<span class="fc" id="L75">    this.valueN = valueN;</span>
<span class="fc" id="L76">    this.operations = operations;</span>
<span class="fc" id="L77">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see mtas.codec.util.DataCollector.MtasDataItem#add(mtas.codec.util.
   * DataCollector.MtasDataItem)
   */
  @Override
  public void add(MtasDataItem&lt;T1, T2&gt; newItem) throws IOException {
<span class="nc bnc" id="L87" title="All 2 branches missed.">    if (newItem instanceof MtasDataItemAdvanced) {</span>
<span class="nc" id="L88">      MtasDataItemAdvanced&lt;T1, T2&gt; newTypedItem = (MtasDataItemAdvanced&lt;T1, T2&gt;) newItem;</span>
<span class="nc" id="L89">      valueSum = operations.add11(valueSum, newTypedItem.valueSum);</span>
<span class="nc" id="L90">      valueSumOfLogs = operations.add22(valueSumOfLogs,</span>
          newTypedItem.valueSumOfLogs);
<span class="nc" id="L92">      valueSumOfSquares = operations.add11(valueSumOfSquares,</span>
          newTypedItem.valueSumOfSquares);
<span class="nc" id="L94">      valueMin = operations.min11(valueMin, newTypedItem.valueMin);</span>
<span class="nc" id="L95">      valueMax = operations.max11(valueMax, newTypedItem.valueMax);</span>
<span class="nc" id="L96">      valueN += newTypedItem.valueN;</span>
<span class="nc" id="L97">      recomputeComparableSortValue = true;</span>
<span class="nc" id="L98">    } else {</span>
<span class="nc" id="L99">      throw new IOException(&quot;can only add MtasDataItemAdvanced&quot;);</span>
    }
<span class="nc" id="L101">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see mtas.codec.util.DataCollector.MtasDataItem#rewrite()
   */
  @Override
  public Map&lt;String, Object&gt; rewrite(boolean showDebugInfo) throws IOException {
<span class="fc" id="L110">    Map&lt;String, Object&gt; response = new HashMap&lt;&gt;();</span>
<span class="fc bfc" id="L111" title="All 2 branches covered.">    for (String statsItem : getStatsItems()) {</span>
<span class="fc bfc" id="L112" title="All 2 branches covered.">      if (statsItem.equals(CodecUtil.STATS_TYPE_SUM)) {</span>
<span class="fc" id="L113">        response.put(statsItem, valueSum);</span>
<span class="fc bfc" id="L114" title="All 2 branches covered.">      } else if (statsItem.equals(CodecUtil.STATS_TYPE_N)) {</span>
<span class="fc" id="L115">        response.put(statsItem, valueN);</span>
<span class="fc bfc" id="L116" title="All 2 branches covered.">      } else if (statsItem.equals(CodecUtil.STATS_TYPE_MAX)) {</span>
<span class="fc" id="L117">        response.put(statsItem, valueMax);</span>
<span class="fc bfc" id="L118" title="All 2 branches covered.">      } else if (statsItem.equals(CodecUtil.STATS_TYPE_MIN)) {</span>
<span class="fc" id="L119">        response.put(statsItem, valueMin);</span>
<span class="fc bfc" id="L120" title="All 2 branches covered.">      } else if (statsItem.equals(CodecUtil.STATS_TYPE_SUMSQ)) {</span>
<span class="fc" id="L121">        response.put(statsItem, valueSumOfSquares);</span>
<span class="pc bpc" id="L122" title="1 of 2 branches missed.">      } else if (statsItem.equals(CodecUtil.STATS_TYPE_SUMOFLOGS)) {</span>
<span class="nc" id="L123">        response.put(statsItem, valueSumOfLogs);</span>
<span class="pc bpc" id="L124" title="1 of 2 branches missed.">      } else if (statsItem.equals(CodecUtil.STATS_TYPE_MEAN)) {</span>
<span class="fc" id="L125">        response.put(statsItem, getValue(statsItem));</span>
<span class="nc bnc" id="L126" title="All 2 branches missed.">      } else if (statsItem.equals(CodecUtil.STATS_TYPE_GEOMETRICMEAN)) {</span>
<span class="nc" id="L127">        response.put(statsItem, getValue(statsItem));</span>
<span class="nc bnc" id="L128" title="All 2 branches missed.">      } else if (statsItem.equals(CodecUtil.STATS_TYPE_STANDARDDEVIATION)) {</span>
<span class="nc" id="L129">        response.put(statsItem, getValue(statsItem));</span>
<span class="nc bnc" id="L130" title="All 2 branches missed.">      } else if (statsItem.equals(CodecUtil.STATS_TYPE_VARIANCE)) {</span>
<span class="nc" id="L131">        response.put(statsItem, getValue(statsItem));</span>
<span class="nc bnc" id="L132" title="All 2 branches missed.">      } else if (statsItem.equals(CodecUtil.STATS_TYPE_POPULATIONVARIANCE)) {</span>
<span class="nc" id="L133">        response.put(statsItem, getValue(statsItem));</span>
<span class="nc bnc" id="L134" title="All 2 branches missed.">      } else if (statsItem.equals(CodecUtil.STATS_TYPE_QUADRATICMEAN)) {</span>
<span class="nc" id="L135">        response.put(statsItem, getValue(statsItem));</span>
      } else {
<span class="nc" id="L137">        response.put(statsItem, null);</span>
      }
<span class="fc" id="L139">    }</span>
<span class="pc bpc" id="L140" title="1 of 2 branches missed.">    if (errorNumber &gt; 0) {</span>
<span class="nc" id="L141">      Map&lt;String, Object&gt; errorResponse = new HashMap&lt;&gt;();</span>
<span class="nc bnc" id="L142" title="All 2 branches missed.">      for (Entry&lt;String, Integer&gt; entry : getErrorList().entrySet()) {</span>
<span class="nc" id="L143">        errorResponse.put(entry.getKey(), entry.getValue());</span>
<span class="nc" id="L144">      }</span>
<span class="nc" id="L145">      response.put(&quot;errorNumber&quot;, errorNumber);</span>
<span class="nc" id="L146">      response.put(&quot;errorList&quot;, errorResponse);</span>
    }
<span class="pc bpc" id="L148" title="1 of 2 branches missed.">    if (showDebugInfo) {</span>
<span class="nc" id="L149">      response.put(&quot;sourceNumber&quot;, sourceNumber);</span>
<span class="nc" id="L150">      response.put(&quot;stats&quot;, &quot;advanced&quot;);</span>
    }
<span class="fc" id="L152">    return response;</span>
  }

  /**
   * Gets the value.
   *
   * @param statsType the stats type
   * @return the value
   */
  protected T2 getValue(String statsType) {
<span class="pc bpc" id="L162" title="1 of 2 branches missed.">    if (statsType.equals(CodecUtil.STATS_TYPE_MEAN)) {</span>
<span class="fc" id="L163">      return operations.divide1(valueSum, valueN);</span>
<span class="nc bnc" id="L164" title="All 2 branches missed.">    } else if (statsType.equals(CodecUtil.STATS_TYPE_GEOMETRICMEAN)) {</span>
<span class="nc" id="L165">      return operations.exp2(operations.divide2(valueSumOfLogs, valueN));</span>
<span class="nc bnc" id="L166" title="All 2 branches missed.">    } else if (statsType.equals(CodecUtil.STATS_TYPE_STANDARDDEVIATION)) {</span>
<span class="nc" id="L167">      return operations</span>
<span class="nc" id="L168">          .sqrt2(</span>
<span class="nc" id="L169">              operations.divide2(</span>
<span class="nc" id="L170">                  operations.subtract12(valueSumOfSquares,</span>
<span class="nc" id="L171">                      operations.divide1(</span>
<span class="nc" id="L172">                          operations.product11(valueSum, valueSum), valueN)),</span>
<span class="nc" id="L173">                  (valueN - 1)));</span>
<span class="nc bnc" id="L174" title="All 2 branches missed.">    } else if (statsType.equals(CodecUtil.STATS_TYPE_VARIANCE)) {</span>
<span class="nc" id="L175">      return operations</span>
<span class="nc" id="L176">          .divide2(</span>
              operations
<span class="nc" id="L178">                  .subtract12(valueSumOfSquares,</span>
<span class="nc" id="L179">                      operations.divide1(</span>
<span class="nc" id="L180">                          operations.product11(valueSum, valueSum), valueN)),</span>
<span class="nc" id="L181">              (valueN - 1));</span>
<span class="nc bnc" id="L182" title="All 2 branches missed.">    } else if (statsType.equals(CodecUtil.STATS_TYPE_POPULATIONVARIANCE)) {</span>
<span class="nc" id="L183">      return operations</span>
<span class="nc" id="L184">          .divide2(</span>
              operations
<span class="nc" id="L186">                  .subtract12(valueSumOfSquares,</span>
<span class="nc" id="L187">                      operations.divide1(</span>
<span class="nc" id="L188">                          operations.product11(valueSum, valueSum), valueN)),</span>
<span class="nc" id="L189">              valueN);</span>
<span class="nc bnc" id="L190" title="All 2 branches missed.">    } else if (statsType.equals(CodecUtil.STATS_TYPE_QUADRATICMEAN)) {</span>
<span class="nc" id="L191">      return operations.sqrt2(operations.divide1(valueSumOfSquares, valueN));</span>
    } else {
<span class="nc" id="L193">      return null;</span>
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see mtas.codec.util.collector.MtasDataItem#getCompareValueType()
   */
  @Override
  public int getCompareValueType() throws IOException {
<span class="pc bpc" id="L204" title="45 of 50 branches missed.">    switch (sortType) {</span>
    case CodecUtil.STATS_TYPE_N:
<span class="nc" id="L206">      return 0;</span>
    case CodecUtil.STATS_TYPE_SUM:
<span class="fc" id="L208">      return 1;</span>
    case CodecUtil.STATS_TYPE_MAX:
<span class="nc" id="L210">      return 1;</span>
    case CodecUtil.STATS_TYPE_MIN:
<span class="nc" id="L212">      return 1;</span>
    case CodecUtil.STATS_TYPE_SUMSQ:
<span class="nc" id="L214">      return 1;</span>
    case CodecUtil.STATS_TYPE_SUMOFLOGS:
<span class="nc" id="L216">      return 2;</span>
    case CodecUtil.STATS_TYPE_MEAN:
<span class="nc" id="L218">      return 2;</span>
    case CodecUtil.STATS_TYPE_GEOMETRICMEAN:
<span class="nc" id="L220">      return 2;</span>
    case CodecUtil.STATS_TYPE_STANDARDDEVIATION:
<span class="nc" id="L222">      return 2;</span>
    case CodecUtil.STATS_TYPE_VARIANCE:
<span class="nc" id="L224">      return 2;</span>
    case CodecUtil.STATS_TYPE_POPULATIONVARIANCE:
<span class="nc" id="L226">      return 2;</span>
    case CodecUtil.STATS_TYPE_QUADRATICMEAN:
<span class="nc" id="L228">      return 2;</span>
    default:
<span class="fc" id="L230">      throw new IOException(&quot;sortType &quot; + sortType + &quot; not supported&quot;);</span>
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see mtas.codec.util.collector.MtasDataItem#getCompareValue0()
   */
  @Override
  public final MtasDataItemNumberComparator&lt;Long&gt; getCompareValue0() {
<span class="nc bnc" id="L241" title="All 6 branches missed.">    switch (sortType) {</span>
    case CodecUtil.STATS_TYPE_N:
<span class="nc" id="L243">      return new MtasDataItemNumberComparator&lt;Long&gt;(valueN, sortDirection);</span>
    default:
<span class="nc" id="L245">      return null;</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>