Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Generics of Subclasses of Number to Create a Rollover Counter
    primarykey
    data
    text
    <p>I'm trying to create a counter that will rollover whenever it reaches a preset ceiling and resets back to its floor value upon reaching said ceiling. I have implemented the class and it works just fine. However, on my way to my solution, I wanted to experiment with Java Generics. I want to try and extend my counter so that it doesn't only use integers, but instead can use any type of number. I know that counters typically call for just the use of integers anyway, but I wanted to see if it could be done.</p> <p>I figured that the code would be similar to below. However, java.lang.Number doesn't have a "generic" way of getting/setting its value. Do I need to create my own number class to enable this? Also, I know that if I do get this working, I need to alter my equals checks so that they have an error threshold for floating point values, this is more or less a modified version of my int counter with what I figured would work for generics.</p> <p><strong>Edit:</strong> It's been suggested that I take a mapping approach where I store an integer counter and keep a increment value so that when I want to spit out a number, I just multiply my current count by the increment value. However, I don't believe this will fill my exact needs because I don't want to necessarily increment by the same amount every time. The main focus of this counter is more of a way to have a fixed range number that, when added to or subtracted from, knows how to handle wrapping back around. </p> <p>I guess the best way to describe it (although probably improperly) would be like an <code>Integer</code> that automatically handles over/underflow.</p> <pre><code>package com.math; public class GenericRolloverCounter&lt;T extends Number&gt; { private T value; private T lowValue; private T highValue; public GenericRolloverCounter(T l_startValue, T l_highValue) { this.lowValue = l_startValue; this.highValue = l_highValue; this.value = l_startValue; } public T getValue() { return value; } public void setValue(T value) { this.value = value; } public void increment(T valToIncrementBy) { this.value += valToIncrementBy; if (this.value &gt; this.highValue) { this.value = (this.lowValue + (this.value - (this.highValue + 1))); } } public void increment() { this.increment(1); } public void decrement(T valToDecrementBy) { this.value -= valToDecrementBy; if (this.value &lt; this.lowValue) { this.value = ((this.value + this.highValue + 1) - this.lowValue); } } public void decrement() { this.decrement(1); } @Override public String toString() { return Integer.toString(this.value); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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