Class FloatRange

java.lang.Object
org.jjazz.utilities.api.FloatRange

public class FloatRange extends Object
A range between 2 positive floats.
  • Field Details

    • from

      public float from
      Don't use negative value.
    • to

      public float to
      Don't use negative value.
    • EMPTY_FLOAT_RANGE

      public static final FloatRange EMPTY_FLOAT_RANGE
      The special shared instance for the empty range.

      EMPTY_FLOAT_RANGE.size() returns 0.

    • MAX_FLOAT_RANGE

      public static final FloatRange MAX_FLOAT_RANGE
      The biggest FloatRange possible.
  • Constructor Details

    • FloatRange

      public FloatRange(float from, float to)
      A range representing [from; to].

      Use the EMPTY_FLOAT_RANGE instance for empty range.

      Parameters:
      from - Must be >= 0
      to - Must be > from
      Throws:
      IllegalArgumentException - If from and to are not valid
  • Method Details

    • clone

      public FloatRange clone()
    • isEmpty

      public boolean isEmpty()
    • size

      public float size()
      Get the size of the range.
      Returns:
      (to - from), or 0 for the EMPTY_RANGE.
    • getCenter

      public float getCenter()
      The center of the range.
      Returns:
      (to-from)/2 + from
      Throws:
      IllegalStateException - If range is empty
    • getPercentage

      public float getPercentage(float value)
      Get the value expressed as a percentage of the range.
      Parameters:
      value -
      Returns:
      0 for a value <= from, 0.5 for getCenter(), 1 for a value >= to.
      Throws:
      IllegalStateException - If range is empty
      See Also:
    • contains

      public boolean contains(float x, boolean excludeUpperBound)
      Check if value is within the range.
      Parameters:
      x -
      excludeUpperBound - If true, return false if x == this.to.
      Returns:
    • contains

      public boolean contains(FloatRange fr, boolean excludeUpperBound)
      Check if specified range is within this float range.
      Parameters:
      fr -
      excludeUpperBound - If true, fr.to must be < this.to to be considered as contained.
      Returns:
    • getIntersectRange

      public FloatRange getIntersectRange(FloatRange rg)
      Parameters:
      rg -
      Returns:
      Can return the EMPTY_RANGE if no intersection.
    • intersects

      public boolean intersects(FloatRange rg)
      Check if specified range intersects with this range.

      Note: [2;4] and [4;7] does NOT intersect.

      Parameters:
      rg -
      Returns:
    • clamp

      public float clamp(float value, float upperBoundDelta)
      Clamps the value to fit in this range.

      If value is >= upper bound, return value is (upper bound - upperBoundDelta).

      Parameters:
      value -
      upperBoundDelta - Use 0 to clamp at the upper bound. If non zero, upperBoundDelta should be consistent with value and float precision (7 digits).
      Returns:
    • getTransformed

      public FloatRange getTransformed(float fromOffset, float toOffset)
      Get a new range with bounds modified.

      Modifying the empty range returns the empty range.

      Parameters:
      fromOffset -
      toOffset -
      Returns:
      Throws:
      IllegalArgumentException - If adding fromOffset and toOffset result in non-valid range values.
    • getTransformed

      public FloatRange getTransformed(float offset)
      Get a new range with bounds modified by adding offset.

      Modifying the empty range returns the empty range.

      Parameters:
      offset -
      Returns:
      Throws:
      IllegalArgumentException - If adding soffset result in non-valid range values.
    • setFrom

      public FloatRange setFrom(float newFrom)
      Return a copy of this FloatRange with a new "from" value.
      Parameters:
      newFrom - Must be stricly less thatn the "to" bound of this object.
      Returns:
    • setTo

      public FloatRange setTo(float newTo)
      Return a copy of this FloatRange with a new "to" value.
      Parameters:
      newTo - Must be stricly greater thatn the "from" bound of this object.
      Returns:
    • getUnion

      public FloatRange getUnion(FloatRange r)
      Get a new range made from the lowest and highest bounds from this object and r.

      If one of the range is empty, return the empty range.

      Parameters:
      r -
      Returns:
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object