次相互に2つの要素を比較するために、リストを反復処理するために、よりエレガントな方法

ジョー・D:

私はこのように動作する方法があります:

  1. 引数3のparamsとしてテイク - 日付(昇順にソート)、区間単位インターバル値のリストを
  2. 次の要素が前の日付(間隔)を超えていないかどうかを確認してください。10:00次の10:29 - - 反復さらに換言すれば、30分の間隔、PREVを与えられました。次回は10時31分であれば - それを破ると、行の日付のカウンターを返します。

そのためのコードは以下の通りです:

public static void main(String[] args)
{
    Date d1 = new Date();
    Date d2 = addOrSubtractTimeUnitFromDate(d1, Calendar.MINUTE, 10, true);
    Date d3 = addOrSubtractTimeUnitFromDate(d2, Calendar.MINUTE, 10, true);
    Date d4 = addOrSubtractTimeUnitFromDate(d3, Calendar.MINUTE, 10, true);
    Date d5 = addOrSubtractTimeUnitFromDate(d4, Calendar.MINUTE, 10, true);
    Date d6 = addOrSubtractTimeUnitFromDate(d5, Calendar.MINUTE, 10, true);

    List<Date> threeDates = new ArrayList<>();
    threeDates.add(d1);
    threeDates.add(d2);
    threeDates.add(d3);
    threeDates.add(d4);
    threeDates.add(d5);
    threeDates.add(d6);

    System.out.println(returnDatesInARowCounter(threeDates, Calendar.MINUTE, 30));
}

private static int returnDatesInARowCounter(List<Date> allDates, int intervalBetween2DatesTimeUnit, int intervalValue)
{
    int datesInARowCounter = allDates.size() > 0 ? 1 : 0; // esp. this line (in case allDates is empty)
    Date lastDate = null;
    Date nextDate;

    Iterator<Date> iter = allDates.iterator();

    while (iter.hasNext())
    {
        nextDate = iter.next();

        if (lastDate != null) // both lastDate и nextDate are initialized now
        {
            if(isNextIncidentInIntervalWithLastOneOrNot(lastDate, nextDate, intervalBetween2DatesTimeUnit, intervalValue, true))
            {
                datesInARowCounter += 1;
            }
            else break;
        }

        lastDate = nextDate;
    }

    return datesInARowCounter;
}

public static Date addOrSubtractTimeUnitFromDate(Date dateToAddToOrSubtractFrom, int calendarTimeUnit, int value, boolean isAdd)
{
    if(!isAdd)
    {
        value = -value;
    }
    Calendar cal = Calendar.getInstance();
    cal.setTime(dateToAddToOrSubtractFrom);
    cal.add(calendarTimeUnit, value);
    return cal.getTime();
}

private static boolean isNextIncidentInIntervalWithLastOneOrNot(Date lastIncidentRegDate, Date nextIncidentRegDate, int intervalTimeUnit, int intervalValue, boolean isBetween)
{
    Date currentIncidentPlusInterval = addOrSubtractTimeUnitFromDate(lastIncidentRegDate, intervalTimeUnit, intervalValue, true);
    boolean betweenBool = isDateBetween(nextIncidentRegDate, lastIncidentRegDate, currentIncidentPlusInterval);

    return isBetween == betweenBool;
}

private static boolean isDateBetween(Date targetDate, Date startDate, Date endDate)
{
    return targetDate.compareTo(startDate) >= 0 && targetDate.compareTo(endDate) <= 0;
}

しかし、コードのルックスは私に独特。それをより読みに見えるようにする方法はありますか?

Quaffel:

あなたは、Java 8以降を使用している場合は、代わりにjava.time-APIを使用することができます。これは、ビルトインのサポート「時間の期間」のために、実際の実装は非常に簡単になります。

static int daysInARow(List<Instant> allInstants, Duration maxDifference) {
        int counter = allInstants.size() > 0 ? 1 : 0;
        Instant previous = allInstants.get(0);

        for (int i = 1; i < allInstants.size(); i++) {
            Instant current = allInstants.get(i);
            if (Duration.between(previous, current).compareTo(maxDifference) > 0)
                break;
            counter++;
            previous = current;
        }

        return counter;
    }

あなたが使用している場合はjava.util.Date、プロジェクトの他の部分では、簡単の間で変換することができますInstant使用することにより、S

Date#from(Instant)

そして

Date#toInstant()

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=313977&siteId=1