Java Number Range Check inRange(int firstIndex, int secondIndex)

Here you can find the source of inRange(int firstIndex, int secondIndex)

Description

Check whether the first index is in range of the second one.

License

Open Source License

Parameter

Parameter Description
firstIndex a parameter
secondIndex a parameter

Declaration

private static boolean inRange(int firstIndex, int secondIndex) 

Method Source Code

//package com.java2s;
/*/*  w  w  w. j a  v a 2s  .  c o  m*/
* Copyright (C) 2010 Gregor Trefs, Dominique Ritze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

public class Main {
    /**
     * Check whether the first index is in range of the second one.
     * This means firstIndex in [secondIndex-6,secondIndex-1]
     *
     * @param firstIndex
     * @param secondIndex
     * @return
     */
    private static boolean inRange(int firstIndex, int secondIndex) {

        //before the "circle" closes, nothing to consider
        if (firstIndex < 34 && secondIndex < 34) {
            //just check whether the difference is less than 7
            if (Math.abs(secondIndex - firstIndex) <= 6.0) {
                return true;
            } else {
                return false;
            }
        }
        //at the end of the "circle", be careful of indicies
        if (firstIndex > 33) {
            //adapt the indicies
            int adaptedFirstIndex = firstIndex - 40;
            if (secondIndex > 33) {
                int adaptedSecondIndex = secondIndex - 40;
                //with adapted indicies easy to check whether difference less than 7
                if (Math.abs(adaptedSecondIndex - adaptedFirstIndex) <= 6.0) {
                    return true;
                } else {
                    return false;
                }
            }
            //second one must not be adapted because it is at
            //the beginning and not at the end
            if (Math.abs(secondIndex - adaptedFirstIndex) <= 6.0) {
                return true;
            } else {
                return false;
            }

        }
        return false;
    }
}

Related

  1. inRange(final int value, final int min, final int max, final String fieldName)
  2. inRange(float n, float min, float max)
  3. inrange(int actual, int leftRange, int rightRange)
  4. inRange(int check, int range)
  5. inRange(int checkValue, int min, int max)
  6. inRange(int l, int u, int v, int... v_)
  7. inRange(int minValue, int maxValue, int value)
  8. inRange(int n, int lo, int hi)
  9. inRange(int num, int from, int to, int flag)