Interval.java

  1. /* Copyright 2015 Laurent COCAULT
  2.  * Licensed to Laurent COCAULT under one or more contributor license agreements.
  3.  * See the NOTICE file distributed with this work for additional information
  4.  * regarding copyright ownership. Laurent COCAULT licenses this file to You
  5.  * under the Apache License, Version 2.0 (the "License"); you may not use this
  6.  * file except in compliance with the License.  You may obtain a copy of the
  7.  * License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.csp.constraint.model;

  18. /**
  19.  * Represents a continuous interval on integer values. The _min and _max values
  20.  * are inside the interval. The interval is defined as [_min;_max].
  21.  */
  22. public class Interval<T extends Value<T>> implements Comparable<Interval<T>> {

  23.     /**
  24.      * Lower bound of the interval.
  25.      */
  26.     private T min_;

  27.     /**
  28.      * Upper bound of the interval.
  29.      */
  30.     private T max_;

  31.     /**
  32.      * Default constructor.
  33.      * @param min
  34.      *            Initial lower bound of the interval
  35.      * @param max
  36.      *            Initial upper bound of the interval
  37.      */
  38.     public Interval(final T min, final T max) {
  39.         min_ = min;
  40.         max_ = max;
  41.     }

  42.     /**
  43.      * {@inheritDoc}
  44.      */
  45.     @Override
  46.     public int compareTo(final Interval<T> other) {
  47.         return min_.compareTo(other.min_);
  48.     }

  49.     /**
  50.      * True if the given value is greater than the lower bound and lesser than
  51.      * the upper bound.
  52.      * @param value
  53.      *            to find in the interval
  54.      * @return True if the value is in the interval
  55.      */
  56.     public boolean isInInterval(final T value) {
  57.         return value != null && value.compareTo(min_) >= 0 &&
  58.                 value.compareTo(max_) <= 0;
  59.     }

  60.     /**
  61.      * Returns the upper bound of the interval.
  62.      * @return The maximum value of the interval
  63.      */
  64.     public T getMaxValue() {
  65.         return max_;
  66.     }

  67.     /**
  68.      * Returns the lower bound of the interval.
  69.      * @return The minimum value of the interval
  70.      */
  71.     public T getMinValue() {
  72.         return min_;
  73.     }

  74.     /**
  75.      * Returns the number of elements in the interval.
  76.      * @return The number of elements in the interval, 0 if it is empty
  77.      */
  78.     public int getSize() {
  79.         return max_.getDistance(min_) + 1;
  80.     }

  81.     /**
  82.      * True if the interval contains no value.
  83.      * @return True if the interval is empty
  84.      */
  85.     public boolean isEmpty() {
  86.         return getSize() == 0;
  87.     }

  88.     /**
  89.      * Change the lower bound of the interval.
  90.      * @param value
  91.      *            New lower bound
  92.      */
  93.     public void setMinValue(final T value) {
  94.         min_ = value;
  95.     }

  96.     /**
  97.      * Change the upper bound of the interval.
  98.      * @param value
  99.      *            New upper bound
  100.      */
  101.     public void setMaxValue(final T value) {
  102.         max_ = value;
  103.     }

  104.     /**
  105.      * {@inheritDoc}
  106.      */
  107.     @Override
  108.     public String toString() {
  109.         return "[" + min_ + ";" + max_ + "]";
  110.     }

  111. }