Class Limits

  • All Implemented Interfaces:
    java.io.Serializable

    public final class Limits
    extends java.lang.Object
    implements java.io.Serializable
    Immutable object representing minimum and maximum values.

    Note: these values are inclusive, and so the minimum can be equal to the maximum, implying a range of one value.

    Also note that minimum must be less than or equal the maximum at construction or decode time.

    See Also:
    contains(int), constrain(int), Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      Limits()  
      Limits​(int value)
      Construct a new limits of a single value, setting both the minimum and maximum to this value.
      Limits​(int minimum, int maximum)  
      Limits​(Dictionary<java.lang.String,​?> limits)
      Construct a new Limits based on the given Dictionary or Map.
      Limits​(Sequence<?> limits)  
      Limits​(Limits limits)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int constrain​(int value)
      Limits the specified value to the minimum and maximum values of this object.
      boolean contains​(int value)
      Determines whether the given value is contained by this Limits, that is, whether the value >= minimum and value <= maximum.
      static Limits decode​(java.lang.String value)
      Decode a JSON-encoded string (map or list) that contains the values for a new limits.
      boolean equals​(java.lang.Object object)  
      int hashCode()  
      long range()  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • minimum

        public final int minimum
      • maximum

        public final int maximum
    • Constructor Detail

      • Limits

        public Limits()
      • Limits

        public Limits​(int minimum,
                      int maximum)
      • Limits

        public Limits​(int value)
        Construct a new limits of a single value, setting both the minimum and maximum to this value.
        Parameters:
        value - The single value range for this limits.
      • Limits

        public Limits​(Limits limits)
      • Limits

        public Limits​(Dictionary<java.lang.String,​?> limits)
        Construct a new Limits based on the given Dictionary or Map.

        The map keys for the values are MINIMUM_KEY and MAXIMUM_KEY. Missing minimum value will set Integer.MIN_VALUE as the min, and missing maximum will set Integer.MAX_VALUE as the max.

        Parameters:
        limits - The map/dictionary containing the desired limits values.
        Throws:
        java.lang.IllegalArgumentException - if the min is greater than the max.
      • Limits

        public Limits​(Sequence<?> limits)
    • Method Detail

      • range

        public long range()
        Returns:
        The range of this limits, that is, the maximum less the minimum plus one (since the limits are inclusive). Returns a long value because the default min and max are the maximum range of the integers, so that the range in this case cannot be represented by an integer.
      • constrain

        public int constrain​(int value)
        Limits the specified value to the minimum and maximum values of this object.
        Parameters:
        value - The value to limit.
        Returns:
        The bounded value.
      • contains

        public boolean contains​(int value)
        Determines whether the given value is contained by this Limits, that is, whether the value >= minimum and value <= maximum.
        Parameters:
        value - The value to test.
        Returns:
        Whether the value is contained within the limits.
      • equals

        public boolean equals​(java.lang.Object object)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • decode

        public static Limits decode​(java.lang.String value)
        Decode a JSON-encoded string (map or list) that contains the values for a new limits.

        The format of a JSON map format will be:

        { "minimum": nnn, "maximum": nnn }

        The format of a JSON list format will be:

        [ minimum, maximum ]

        Also accepted is a simple comma- or semicolon-separated list of two integer values, as in:

        min, max
        , or as
        min-max
        (as in the format produced by toString()).
        Parameters:
        value - The JSON string containing the map or list of limits values (must not be null).
        Returns:
        The new limits object if the string can be successfully decoded.
        Throws:
        java.lang.IllegalArgumentException - if the given string is null or empty or the string could not be parsed as a JSON map or list.
        See Also:
        Limits(Dictionary), Limits(int, int)