lt.monarch.chart.mapper
Class NormalizedMathRange

java.lang.Object
  extended by lt.monarch.chart.mapper.NormalizedMathRange
All Implemented Interfaces:
java.io.Serializable, Range<java.lang.Double>

public class NormalizedMathRange
extends java.lang.Object
implements Range<java.lang.Double>, java.io.Serializable

Manages a range of double values. Range minimum and maximum values are adjusted to the nearest round values in a decimal system.
For example: [-4, 19] will be adjusted to [-5,20]

Then reasonable division mark spacings are calculated.

See Also:
Serialized Form

Field Summary
Modifier and Type Field and Description
protected  double firstMark
           
protected static double log10
           
protected  double max
           
protected  double min
           
protected  int minSteps
           
protected  int neededPrecision
           
protected  double precision
           
protected  int precisionPlaces
           
protected  double requestedMax
           
protected  double requestedMin
           
protected  double step
           
protected  int steps
           
 
Constructor Summary
Constructor and Description
NormalizedMathRange(double min, double max)
          Constructs a new NormalizedMathRange with the specified start and end of the range.
NormalizedMathRange(Range<java.lang.Double> r)
          Constructs a new NormalizedMathRange.
 
Method Summary
Modifier and Type Method and Description
 void adjustRange()
          Adjusts the range.
 void adjustSteps()
          Adjusts step size.
protected  void determineStepCount()
          Determines step count in the range.
protected  int findNeededPrecision()
           
 java.lang.String format(double val)
          Returns a string representation of the specified double value.
 java.lang.String formatPrecise(double val)
          Returns a string representation of the specified double value.
 double getFirstMark()
          Gets a value of the first mark in the range.
 java.lang.Double getMaximum()
          Gets the end (maximum value) of the range.
 java.lang.Double getMinimum()
          Gets the start (minimum value) of the range.
 int getPrecisionPlaces()
          Gets the precision places.
 double getRange()
          Gets an extent of the range.
 double getStep()
          Gets a size of step in the range.
 int getStepCount()
          Gets a number of steps in the range.
 boolean inRange(double v)
          Checks whether the specified double value is in this range.
 double map(double value)
          Finds a position of the specified double value in this range.
 double mapBack(double pos)
          Finds a number corresponding to the specified position in this range.
 void setMinimumSteps(int minSteps)
          Sets a minimum number of steps for this range.
 void setPrecisionPlaces(int places)
          Sets precision places for this range.
 void setRange(double min, double max)
          Sets the bounds of this range to the specified values.
 void setRange(Range<java.lang.Double> r)
          Sets bounds of this range equal to the bounds of the specified range.
 void setRangeMinimum(double m)
          Sets minimum of the range to the specified value.
 void setStep(double step)
          Sets a step size for this range.
 java.lang.String toString()
          Returns string representation of this range.
 void updateRange(double d)
          Updates this range so that the specified value will be in the range.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

log10

protected static final double log10

requestedMin

protected double requestedMin

requestedMax

protected double requestedMax

min

protected double min

max

protected double max

firstMark

protected double firstMark

step

protected double step

minSteps

protected int minSteps

steps

protected int steps

precisionPlaces

protected int precisionPlaces

precision

protected double precision

neededPrecision

protected int neededPrecision
Constructor Detail

NormalizedMathRange

public NormalizedMathRange(double min,
                           double max)
Constructs a new NormalizedMathRange with the specified start and end of the range.

Parameters:
min - start of the range
max - end of the range

NormalizedMathRange

public NormalizedMathRange(Range<java.lang.Double> r)
Constructs a new NormalizedMathRange. The range bounds are taken from the specified range.

Parameters:
r - the range to take bounds from
Method Detail

setRange

public void setRange(Range<java.lang.Double> r)
Sets bounds of this range equal to the bounds of the specified range.

Parameters:
r - the range to take bounds from

setRange

public void setRange(double min,
                     double max)
Sets the bounds of this range to the specified values.

Parameters:
min - start of the range
max - end of the range

setRangeMinimum

public void setRangeMinimum(double m)
Sets minimum of the range to the specified value. The range is shifted. This means that range extent after this operation remains the same. First mark and number of steps of the range are updated.

Parameters:
m - the new range minimum

updateRange

public void updateRange(double d)
Updates this range so that the specified value will be in the range. If the specified value is in range at the moment then the range remains the same. Otherwise range bounds is changed.

Parameters:
d - the double value

getRange

public final double getRange()
Gets an extent of the range. The extent is counted as: getMaximum() - getMinimum().

Returns:
extent of the range

getFirstMark

public final double getFirstMark()
Gets a value of the first mark in the range.

Returns:
value of the first mark in the range

getMinimum

public final java.lang.Double getMinimum()
Gets the start (minimum value) of the range.

Specified by:
getMinimum in interface Range<java.lang.Double>
Returns:
start (minimum value) of the range

getMaximum

public final java.lang.Double getMaximum()
Gets the end (maximum value) of the range.

Specified by:
getMaximum in interface Range<java.lang.Double>
Returns:
end (maximum value) of the range

getStep

public final double getStep()
Gets a size of step in the range.

Returns:
size of step in the range

getStepCount

public final int getStepCount()
Gets a number of steps in the range.

Returns:
number of steps

inRange

public final boolean inRange(double v)
Checks whether the specified double value is in this range.

Parameters:
v - the double value
Returns:
true if ( v>=getMinimum() && v<=getMaximum() ); false otherwise

setStep

public void setStep(double step)
Sets a step size for this range. First mark, number of steps and number of precision places are updated.

Parameters:
step - the step size; a number greater than 0.0.

setPrecisionPlaces

public void setPrecisionPlaces(int places)
Sets precision places for this range. Minimum, maximum and step of this range are rounded. The precision is used for rounding operations.

Parameters:
places - the number of precision places

setMinimumSteps

public void setMinimumSteps(int minSteps)
Sets a minimum number of steps for this range.

Parameters:
minSteps - the minimum number of steps

adjustSteps

public void adjustSteps()
Adjusts step size. Step size is adjusted so that entire number of steps isn't less than minimum. After adjusting first mark, number of steps and number of precision places are updated.

See Also:
setMinimumSteps(int)

adjustRange

public void adjustRange()
Adjusts the range. The range bounds are adjusted so that: (getMaximum() - getMinimum()) / getStep() is equal to mathematical integer. First step is set to range minimum. Number of steps after adjustment is updated.


determineStepCount

protected void determineStepCount()
Determines step count in the range. Number of steps is saved in steps attribute.


format

public java.lang.String format(double val)
Returns a string representation of the specified double value. The double value is rounded using precision of the range.

Parameters:
val - the double value to format
Returns:
string representation of the double value
See Also:
setPrecisionPlaces(int)

formatPrecise

public java.lang.String formatPrecise(double val)
Returns a string representation of the specified double value. If step size of the range is less than 10, the double value is rounded using finer precision than in format method. If step size of the range is greater than 10 the double value is rounded to mathematical integer and converted to String.

Parameters:
val - the double value to format
Returns:
string representation of the double value
See Also:
setPrecisionPlaces(int)

map

public double map(double value)
Finds a position of the specified double value in this range. The position is counted as: (value-getMinimum()) / getRange() If getRange() returns 0, then Double.NaN is returned.

Parameters:
value - double value to map
Returns:
position of the specified double value in this range
See Also:
getMinimum(), getRange()

mapBack

public double mapBack(double pos)
Finds a number corresponding to the specified position in this range. The numeric value is calculated as: pos*getRange() + getMinimum() If getRange() returns 0, then Double.NaN is returned.

Parameters:
pos - position in the range; should be a value in range [0,1]
Returns:
number corresponding to the specified position
See Also:
getMinimum(), getRange()

toString

public java.lang.String toString()
Returns string representation of this range.

Overrides:
toString in class java.lang.Object
Returns:
string representation of this range

findNeededPrecision

protected int findNeededPrecision()

getPrecisionPlaces

public int getPrecisionPlaces()
Gets the precision places.

Returns:
Returns the precision places