Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FINERACT-2081: Code quality, eliminate duplicated methods, use utility #4137

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -51,7 +50,7 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext chunkCon
.append("where je.transaction_date > (select coalesce(MAX(created_date),'2010-01-01') from m_trial_balance)");
final List<LocalDate> tbGaps = jdbcTemplate.queryForList(tbGapSqlBuilder.toString(), LocalDate.class);
for (LocalDate tbGap : tbGaps) {
int days = Math.toIntExact(ChronoUnit.DAYS.between(tbGap, DateUtils.getBusinessLocalDate()));
int days = DateUtils.getExactDifferenceInDays(tbGap, DateUtils.getBusinessLocalDate());
if (days < 1) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.apache.fineract.infrastructure.core.domain;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.fineract.infrastructure.core.service.DateUtils;
Expand Down Expand Up @@ -51,7 +50,7 @@ public Integer daysInPeriodInclusiveOfEndDate() {
}

private Integer daysBetween() {
return Math.toIntExact(ChronoUnit.DAYS.between(this.startDate, this.endDate));
return DateUtils.getExactDifferenceInDays(this.startDate, this.endDate);
}

public boolean containsPortionOf(final LocalDateInterval interval) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,23 @@ public static boolean isAfter(LocalDate first, LocalDate second) {
return first != null && (second == null || first.isAfter(second));
}

public static long getDifferenceInDays(final LocalDate localDateBefore, final LocalDate localDateAfter) {
return DAYS.between(localDateBefore, localDateAfter);
public static long getDifference(LocalDate first, LocalDate second, @NotNull ChronoUnit unit) {
if (first == null || second == null) {
throw new IllegalArgumentException("Dates must not be null to get difference");
}
return unit.between(first, second);
}

public static int getExactDifference(LocalDate first, LocalDate second, @NotNull ChronoUnit unit) {
return Math.toIntExact(getDifference(first, second, unit));
}

public static long getDifferenceInDays(LocalDate first, LocalDate second) {
return getDifference(first, second, DAYS);
}

public static int getExactDifferenceInDays(LocalDate first, LocalDate second) {
return getExactDifference(first, second, DAYS);
}

// Parse, format
Expand Down Expand Up @@ -362,17 +377,29 @@ public static String format(LocalDateTime dateTime, String format, Locale locale
*
* @param targetDate
* the date to be checked
* @param startDate
* @param fromDate
* the start date of the range
* @param endDate
* @param toDate
* the end date of the range
* @return true if targetDate is within range or equal to start/end dates, otherwise false
*/
public static boolean isDateWithinRange(LocalDate targetDate, LocalDate startDate, LocalDate endDate) {
if (targetDate == null || startDate == null || endDate == null) {
public static boolean isDateWithinRange(LocalDate targetDate, LocalDate fromDate, LocalDate toDate) {
if (targetDate == null || fromDate == null || toDate == null) {
throw new IllegalArgumentException("Dates must not be null");
}
return !targetDate.isBefore(startDate) && !targetDate.isAfter(endDate);
return isDateInRangeInclusive(targetDate, fromDate, toDate);
}

public static boolean isDateInRangeInclusive(LocalDate targetDate, LocalDate fromDate, LocalDate toDate) {
return fromDate != null && !DateUtils.isBefore(targetDate, fromDate) && !DateUtils.isAfter(targetDate, toDate);
}

public static boolean isDateInRangeExclusive(LocalDate targetDate, LocalDate fromDate, LocalDate toDate) {
return fromDate != null && DateUtils.isAfter(targetDate, fromDate) && DateUtils.isBefore(targetDate, toDate);
}

public static boolean isDateInRangeFromExclusiveToInclusive(LocalDate targetDate, LocalDate fromDate, LocalDate toDate) {
return fromDate != null && DateUtils.isAfter(targetDate, fromDate) && !DateUtils.isAfter(targetDate, toDate);
}

@NotNull
Expand All @@ -398,14 +425,4 @@ private static DateTimeFormatter getDateTimeFormatter(String format, Locale loca
}
return formatter;
}

public static boolean occursOnDayFromExclusiveAndUpToAndIncluding(final LocalDate fromNotInclusive, final LocalDate upToAndInclusive,
final LocalDate target) {
return DateUtils.isAfter(target, fromNotInclusive) && !DateUtils.isAfter(target, upToAndInclusive);
}

public static boolean occursOnDayFromAndUpToAndIncluding(final LocalDate fromAndInclusive, final LocalDate upToAndInclusive,
final LocalDate target) {
return target != null && !DateUtils.isBefore(target, fromAndInclusive) && !DateUtils.isAfter(target, upToAndInclusive);
}
}
Loading
Loading