Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You might want to also specify an amount by which to count. Default value would be <code>1</code>.</p> <p>You can get around some of this by using the <code>Number</code> method <code>.doubleValue()</code> and doing double arithmetic.</p> <p>Here is one of the methods converted to use this idea.</p> <pre><code>public void decrement(double valToDecrementBy) { double work = this.value.doubleValue(); work -= valToDecrementBy; // should use some value related to incrementing amount if ((this.value.doubleValue() - this.lowValue.doubleValue()) &lt; 0.1D) { work = ((this.value.doubleValue() + this.highValue.doubleValue() + 1) - this.lowValue.doubleValue()); } // ... no way to put it back } </code></pre> <p>But, there is still no way to put the value back that's clean and easy. Since 'Number' only has a few commonly used non-abstract subclasses, you could do some ugly <code>instanceof</code> stuff to store the value back. It would look something like this:</p> <pre><code>if (theValue instanceof Double) { // depends on it having a non-null value prior theValue = (T)(new Double(work)); } </code></pre> <p>Or you could convert the starting values to <code>double</code> when you start and just work with doubles.</p> <pre><code>private double value; private double lowValue; private double highValue; public GenericRolloverCounter(T l_startValue, T l_highValue) { this.lowValue = l_startValue.doubleValue(); this.highValue = l_highValue.doubleValue(); this.value = l_startValue.doubleValue(); } </code></pre> <p>That does introduce the issues of incrementing floating point values and the rounding/evaluation problem there.</p> <p>Oh ... and your <code>toString()</code> should be:</p> <pre><code>return value.toString(); </code></pre> <p>To use the native <code>toString()</code> method on the <code>T</code> class.</p> <p>@Crusher's comments suggest another way to do it. Map everything to 'int' and keep a multiplier. Here's some bits of code to show what I mean. (Thanks Crusher)</p> <pre><code>private int value; private int lowValue; private int highValue; private double incr; public GenericRolloverCounter(T l_startValue, T l_highValue, T incrementAmount) { double incr = incrementAmount.doubleValue(); this.lowValue = Math.round(l_startValue.doubleValue() / incr); this.highValue = Math.round(l_highValue.doubleValue() / incr); this.value = Math.round(l_startValue.doubleValue() / incr); } public void increment(int valToIncrementBy) { this.value += valToIncrementBy; if (this.value &gt; this.highValue) { this.value = (this.lowValue + (this.value - (this.highValue + 1))); } } @Override public String toString() { return String.valueOf(incr * this.value); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload