MtasDataItemNumberComparator.java.html 8.45 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>MtasDataItemNumberComparator.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">MtasDataItemNumberComparator.java</span></div><h1>MtasDataItemNumberComparator.java</h1><pre class="source lang-java linenums">package mtas.codec.util.collector;

import java.io.IOException;
import java.io.Serializable;

import mtas.codec.util.CodecUtil;

public final class MtasDataItemNumberComparator&lt;T extends Number &amp; Comparable&lt;T&gt;&gt;
    implements Comparable&lt;T&gt;, Serializable, Cloneable {

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

  /** The value. */
  T value;

  String sortDirection;

  /**
   * Instantiates a new number comparator.
   *
   * @param value
   *          the value
   */
<span class="fc" id="L25">  public MtasDataItemNumberComparator(T value, String sortDirection) {</span>
<span class="fc" id="L26">    this.value = value;</span>
<span class="fc" id="L27">    this.sortDirection = sortDirection;</span>
<span class="fc" id="L28">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Object#clone()
   */
  @Override
  public MtasDataItemNumberComparator&lt;T&gt; clone() {
<span class="nc" id="L37">    return new MtasDataItemNumberComparator&lt;&gt;(this.value, this.sortDirection);</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Comparable#compareTo(java.lang.Object)
   */
  @Override
  public int compareTo(T compareValue) {
<span class="fc" id="L47">    return value.compareTo(compareValue);</span>
  }

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

  /*
   * (non-Javadoc)
   * 
   * @see java.lang.Object#toString()
   */
  public String toString() {
<span class="nc" id="L65">    return value.toString();</span>
  }

  /**
   * Adds the.
   *
   * @param newValue
   *          the new value
   * @throws IOException
   *           Signals that an I/O exception has occurred.
   */
  @SuppressWarnings(&quot;unchecked&quot;)
  public void add(T newValue) throws IOException {
<span class="nc bnc" id="L78" title="All 4 branches missed.">    if (value instanceof Integer &amp;&amp; newValue instanceof Integer) {</span>
<span class="nc" id="L79">      value = (T) Integer.valueOf(value.intValue() + newValue.intValue());</span>
<span class="nc bnc" id="L80" title="All 4 branches missed.">    } else if (value instanceof Long &amp;&amp; newValue instanceof Long) {</span>
<span class="nc" id="L81">      value = (T) Long.valueOf(value.longValue() + newValue.longValue());</span>
<span class="nc bnc" id="L82" title="All 4 branches missed.">    } else if (value instanceof Float &amp;&amp; newValue instanceof Float) {</span>
<span class="nc" id="L83">      value = (T) Float.valueOf(value.floatValue() + newValue.floatValue());</span>
<span class="nc bnc" id="L84" title="All 4 branches missed.">    } else if (value instanceof Double &amp;&amp; newValue instanceof Double) {</span>
<span class="nc" id="L85">      value = (T) Double.valueOf(value.doubleValue() + newValue.longValue());</span>
    } else {
<span class="nc" id="L87">      throw new IOException(&quot;incompatible NumberComparators&quot;);</span>
    }
<span class="nc" id="L89">  }</span>

  /**
   * Subtract.
   *
   * @param newValue
   *          the new value
   * @throws IOException
   *           Signals that an I/O exception has occurred.
   */
  @SuppressWarnings(&quot;unchecked&quot;)
  public void subtract(T newValue) throws IOException {
<span class="nc bnc" id="L101" title="All 4 branches missed.">    if (value instanceof Integer &amp;&amp; newValue instanceof Integer) {</span>
<span class="nc" id="L102">      value = (T) Integer.valueOf(value.intValue() - newValue.intValue());</span>
<span class="nc bnc" id="L103" title="All 4 branches missed.">    } else if (value instanceof Long &amp;&amp; newValue instanceof Long) {</span>
<span class="nc" id="L104">      value = (T) Long.valueOf(value.longValue() - newValue.longValue());</span>
<span class="nc bnc" id="L105" title="All 4 branches missed.">    } else if (value instanceof Float &amp;&amp; newValue instanceof Float) {</span>
<span class="nc" id="L106">      value = (T) Float.valueOf(value.floatValue() - newValue.floatValue());</span>
<span class="nc bnc" id="L107" title="All 4 branches missed.">    } else if (value instanceof Double &amp;&amp; newValue instanceof Double) {</span>
<span class="nc" id="L108">      value = (T) Double.valueOf(value.doubleValue() - newValue.longValue());</span>
    } else {
<span class="nc" id="L110">      throw new IOException(&quot;incompatible NumberComparators&quot;);</span>
    }
<span class="nc" id="L112">  }</span>

  /**
   * Recompute boundary.
   *
   * @param n
   *          the n
   * @return the number comparator
   * @throws IOException
   *           Signals that an I/O exception has occurred.
   */
  public MtasDataItemNumberComparator&lt;T&gt; recomputeBoundary(int n)
      throws IOException {
<span class="nc bnc" id="L125" title="All 2 branches missed.">    if (sortDirection.equals(CodecUtil.SORT_DESC)) {</span>
<span class="nc bnc" id="L126" title="All 2 branches missed.">      if (value instanceof Integer) {</span>
<span class="nc" id="L127">        return new MtasDataItemNumberComparator(</span>
<span class="nc" id="L128">            Math.floorDiv((Integer) value, n), sortDirection);</span>
<span class="nc bnc" id="L129" title="All 2 branches missed.">      } else if (value instanceof Long) {</span>
<span class="nc" id="L130">        return new MtasDataItemNumberComparator(Math.floorDiv((Long) value, n),</span>
            sortDirection);
<span class="nc bnc" id="L132" title="All 2 branches missed.">      } else if (value instanceof Float) {</span>
<span class="nc" id="L133">        return new MtasDataItemNumberComparator(((Float) value) / n,</span>
            sortDirection);
<span class="nc bnc" id="L135" title="All 2 branches missed.">      } else if (value instanceof Double) {</span>
<span class="nc" id="L136">        return new MtasDataItemNumberComparator(((Double) value) / n,</span>
            sortDirection);
      } else {
<span class="nc" id="L139">        throw new IOException(&quot;unknown NumberComparator&quot;);</span>
      }
<span class="nc bnc" id="L141" title="All 2 branches missed.">    } else if (sortDirection.equals(CodecUtil.SORT_ASC)) {</span>
<span class="nc" id="L142">      return new MtasDataItemNumberComparator(getValue(), sortDirection);</span>
    } else {
<span class="nc" id="L144">      throw new IOException(&quot;unknown sortDirection &quot; + sortDirection);</span>
    }
  }
  
  @Override
  public boolean equals(Object obj) {
<span class="nc bnc" id="L150" title="All 2 branches missed.">    if (this == obj)</span>
<span class="nc" id="L151">      return true;</span>
<span class="nc bnc" id="L152" title="All 2 branches missed.">    if (obj == null)</span>
<span class="nc" id="L153">      return false;</span>
<span class="nc bnc" id="L154" title="All 2 branches missed.">    if (getClass() != obj.getClass())</span>
<span class="nc" id="L155">      return false;</span>
<span class="nc" id="L156">    MtasDataItemNumberComparator&lt;?&gt; that = (MtasDataItemNumberComparator&lt;?&gt;) obj;</span>
<span class="nc" id="L157">    return value.equals(that.value);</span>
  }
  
  @Override
  public int hashCode() {
<span class="nc" id="L162">    int h = this.getClass().getSimpleName().hashCode();</span>
<span class="nc" id="L163">    h = (h * 7) ^ value.hashCode();</span>
<span class="nc" id="L164">    return h;</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>