private int binarySearchDate()

in tcbot-teamcity-ignited/src/main/java/org/apache/ignite/tcignited/TeamcityIgnitedImpl.java [309:355]


    private int binarySearchDate(List<BuildRefCompacted> buildRefs, int fromIdx, int toIdx, Date key, boolean since) {
        final int allDatesOutOfBounds = -1;
        final int someDatesOutOfBounds = -2;
        final int invalidVal = -3;

        int low = fromIdx;
        int high = toIdx - 1;
        long minDiff = key.getTime();
        int minDiffId = since ? low : high;
        long temp;

        Date highBuildStartDate = getBuildStartDate(buildRefs.get(high).id());
        Date lowBuildStartDate = getBuildStartDate(buildRefs.get(low).id());

        if (highBuildStartDate != null) {
            if (highBuildStartDate.before(key))
                return since ? allDatesOutOfBounds : someDatesOutOfBounds;
        }

        if (lowBuildStartDate != null) {
            if (lowBuildStartDate.after(key))
                return since ? someDatesOutOfBounds : allDatesOutOfBounds;
        }

        while (low <= high) {
            int mid = (low + high) >>> 1;
            Date midValStartDate = getBuildStartDate(buildRefs.get(mid).id());

            if (midValStartDate != null) {
                if (midValStartDate.after(key))
                    high = mid - 1;
                else if (midValStartDate.before(key))
                    low = mid + 1;
                else
                    return mid;

                temp = midValStartDate.getTime() - key.getTime();

                if ((temp > 0 == since) && (Math.abs(temp) < minDiff)) {
                    minDiff = Math.abs(temp);
                    minDiffId = mid;
                }
            } else
                return invalidVal;
        }
        return minDiffId;
    }