IntValue.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.integer;

  18. import org.csp.constraint.model.Value;

  19. /**
  20.  * Value for an integer variable.
  21.  */
  22. public class IntValue implements Value<IntValue> {

  23.     /**
  24.      * Maximum value for the type IntValue (the quarter of the Integer size is
  25.      * considered in order to make the "compare" function work effectively).
  26.      */
  27.     protected static final IntValue MAX_INT_VALUE = new IntValue(
  28.             Integer.MAX_VALUE / 2 - 1);

  29.     /**
  30.      * Minimum value for the type IntValue (the quarter of the Integer size is
  31.      * considered in order to make the "compare" function work effectively).
  32.      */
  33.     protected static final IntValue MIN_INT_VALUE = new IntValue(
  34.             Integer.MIN_VALUE / 2 + 1);

  35.     /** Value. */
  36.     private int value_;

  37.     /**
  38.      * Constructor.
  39.      * @param value
  40.      *            Integer value
  41.      */
  42.     public IntValue(final int value) {
  43.         value_ = value;
  44.     }

  45.     /**
  46.      * Compute the difference of two integer variables.
  47.      * @param first
  48.      *            First argument
  49.      * @param second
  50.      *            Second argument
  51.      * @return Difference of the two arguments
  52.      */
  53.     public static IntValue diff(final IntValue first, final IntValue second) {
  54.         return new IntValue(first.value_ - second.value_);
  55.     }

  56.     /**
  57.      * Compute the sum of two integer variables.
  58.      * @param first
  59.      *            First argument
  60.      * @param second
  61.      *            Second argument
  62.      * @return Sum of the two arguments
  63.      */
  64.     public static IntValue sum(final IntValue first, final IntValue second) {
  65.         return new IntValue(first.value_ + second.value_);
  66.     }

  67.     /**
  68.      * {@inheritDoc}
  69.      */
  70.     @Override
  71.     public int compareTo(final IntValue other) {
  72.         return value_ - other.value_;
  73.     }

  74.     /**
  75.      * {@inheritDoc}
  76.      */
  77.     @Override
  78.     public boolean equals(final Object other) {

  79.         // Assume not equal
  80.         boolean equal = false;

  81.         if (other instanceof IntValue) {
  82.             // Compare with an other IntValue
  83.             equal = value_ == ((IntValue) other).value_;
  84.         }

  85.         return equal;
  86.     }

  87.     /**
  88.      * {@inheritDoc}
  89.      */
  90.     @Override
  91.     public int getDistance(final IntValue from) {
  92.         return Math.abs(value_ - from.value_);
  93.     }

  94.     /**
  95.      * Get the integer value.
  96.      * @return Integer value
  97.      */
  98.     public int getIntValue() {
  99.         return value_;
  100.     }

  101.     /**
  102.      * {@inheritDoc}
  103.      */
  104.     @Override
  105.     public int hashCode() {
  106.         return value_;
  107.     }

  108.     /**
  109.      * {@inheritDoc}
  110.      */
  111.     @Override
  112.     public IntValue maxValue() {
  113.         return MAX_INT_VALUE;
  114.     }

  115.     /**
  116.      * {@inheritDoc}
  117.      */
  118.     @Override
  119.     public IntValue minValue() {
  120.         return MIN_INT_VALUE;
  121.     }

  122.     /**
  123.      * {@inheritDoc}
  124.      */
  125.     @Override
  126.     public IntValue nextValue() {
  127.         return new IntValue(value_ + 1);
  128.     }

  129.     /**
  130.      * {@inheritDoc}
  131.      */
  132.     @Override
  133.     public IntValue previousValue() {
  134.         return new IntValue(value_ - 1);
  135.     }

  136.     /**
  137.      * {@inheritDoc}
  138.      */
  139.     @Override
  140.     public String toString() {
  141.         return Integer.toString(value_);
  142.     }

  143.     /**
  144.      * {@inheritDoc}
  145.      */
  146.     @Override
  147.     public IntValue value() {
  148.         return this;
  149.     }
  150. }