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

WFLY-15963 Combine ORDINALS and ORDINAL_TO_WEEK_NUMBER_MAPPING fields… #15146

Merged
merged 1 commit into from Feb 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -23,10 +23,8 @@

import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -75,7 +73,10 @@
* @version $Revision: $
*/
public class DayOfMonth extends IntegerBasedExpression {

/**
* Regex pattern for multiple space characters
*/
public static final Pattern REGEX_SPACES = Pattern.compile("\\s+");
/**
* The maximum allowed value for the {@link DayOfMonth}
*/
Expand Down Expand Up @@ -104,27 +105,16 @@ public class DayOfMonth extends IntegerBasedExpression {

}

private static final Set<String> ORDINALS = new HashSet<String>();

private static final Map<String, Integer> ORDINAL_TO_WEEK_NUMBER_MAPPING = new HashMap<String, Integer>();
private static final Map<String, Integer> ORDINAL_TO_WEEK_NUMBER_MAPPING = new HashMap<>(8);

static {
ORDINALS.add("1st");
ORDINALS.add("2nd");
ORDINALS.add("3rd");
ORDINALS.add("4th");
ORDINALS.add("5th");
ORDINALS.add("last");

ORDINAL_TO_WEEK_NUMBER_MAPPING.put("1st", 1);
ORDINAL_TO_WEEK_NUMBER_MAPPING.put("2nd", 2);
ORDINAL_TO_WEEK_NUMBER_MAPPING.put("3rd", 3);
ORDINAL_TO_WEEK_NUMBER_MAPPING.put("4th", 4);
ORDINAL_TO_WEEK_NUMBER_MAPPING.put("5th", 5);

}


/**
* Creates a {@link DayOfMonth} by parsing the passed {@link String} <code>value</code>
* <p>
Expand Down Expand Up @@ -258,32 +248,36 @@ private SortedSet<Integer> getEligibleDaysOfMonth(Calendar cal) {
return eligibleDaysOfMonth;
}

private int getAbsoluteDayOfMonth(Calendar cal, String relativeDayOfMonth) {
if (relativeDayOfMonth == null || relativeDayOfMonth.trim().isEmpty()) {
throw EjbLogger.EJB3_TIMER_LOGGER.invalidScheduleValue(DayOfMonth.class.getSimpleName(), relativeDayOfMonth);
/**
* Gets the absolute day of month.
* @param cal the calendar
* @param trimmedRelativeDayOfMonth a non-null, trimmed, relative day of month
* @return the absolute day of month
*/
private int getAbsoluteDayOfMonth(Calendar cal, String trimmedRelativeDayOfMonth) {
if (trimmedRelativeDayOfMonth.isEmpty()) {
throw EjbLogger.EJB3_TIMER_LOGGER.invalidScheduleValue(DayOfMonth.class.getSimpleName(), trimmedRelativeDayOfMonth);
}
String trimmedRelativeDayOfMonth = relativeDayOfMonth.trim();
if (trimmedRelativeDayOfMonth.equalsIgnoreCase("last")) {
int lastDayOfCurrentMonth = CalendarUtil.getLastDateOfMonth(cal);
return lastDayOfCurrentMonth;
trimmedRelativeDayOfMonth = trimmedRelativeDayOfMonth.toLowerCase(Locale.ROOT);
if (trimmedRelativeDayOfMonth.equals("last")) {
return CalendarUtil.getLastDateOfMonth(cal);
}
if (this.isValidNegativeDayOfMonth(trimmedRelativeDayOfMonth)) {
Integer negativeRelativeDayOfMonth = Integer.parseInt(trimmedRelativeDayOfMonth);
int lastDayOfCurrentMonth = CalendarUtil.getLastDateOfMonth(cal);
return lastDayOfCurrentMonth + negativeRelativeDayOfMonth;
}
if (this.isDayOfWeekBased(trimmedRelativeDayOfMonth)) {

String[] parts = trimmedRelativeDayOfMonth.split("\\s+");
String[] parts = splitDayOfWeekBased(trimmedRelativeDayOfMonth);
if (parts != null) {
String ordinal = parts[0];
String day = parts[1];
int dayOfWeek = DAY_OF_MONTH_ALIAS.get(day.toLowerCase(Locale.ENGLISH));
int dayOfWeek = DAY_OF_MONTH_ALIAS.get(day);

Integer date = null;
if (ordinal.equalsIgnoreCase("last")) {
if (ordinal.equals("last")) {
date = CalendarUtil.getDateOfLastDayOfWeekInMonth(cal, dayOfWeek);
} else {
int weekNumber = ORDINAL_TO_WEEK_NUMBER_MAPPING.get(ordinal.toLowerCase(Locale.ENGLISH));
int weekNumber = ORDINAL_TO_WEEK_NUMBER_MAPPING.get(ordinal);
date = CalendarUtil.getNthDayOfMonth(cal, weekNumber, dayOfWeek);
}

Expand All @@ -297,7 +291,7 @@ private int getAbsoluteDayOfMonth(Calendar cal, String relativeDayOfMonth) {

return date;
}
throw EjbLogger.EJB3_TIMER_LOGGER.invalidScheduleValue(DayOfMonth.class.getSimpleName(), relativeDayOfMonth);
throw EjbLogger.EJB3_TIMER_LOGGER.invalidScheduleValue(DayOfMonth.class.getSimpleName(), trimmedRelativeDayOfMonth);
}

private boolean isValidNegativeDayOfMonth(String dayOfMonth) {
Expand All @@ -313,45 +307,44 @@ private boolean isValidNegativeDayOfMonth(String dayOfMonth) {

}

private boolean isDayOfWeekBased(String relativeVal) {
String trimmedVal = relativeVal.trim();
// one or more spaces (which includes tabs and other forms of space)
Pattern p = Pattern.compile("\\s+");
String[] relativeParts = p.split(trimmedVal);
/**
* Checks if a relative value is weekOfDay-based, and splits the passed
* {@code trimmedLowerCaseRelativeVal} to 2 parts.
* @param trimmedLowerCaseRelativeVal must be non-null, trimmed and lower case value
* @return 2 parts, or null if {@code trimmedLowerCaseRelativeVal} is not dayOfWeek-based
*/
private String[] splitDayOfWeekBased(String trimmedLowerCaseRelativeVal) {
String[] relativeParts = REGEX_SPACES.split(trimmedLowerCaseRelativeVal);
if (relativeParts == null) {
return false;
return null;
}
if (relativeParts.length != 2) {
return false;
return null;
}
String ordinal = relativeParts[0];
String dayOfWeek = relativeParts[1];
if (ordinal == null || dayOfWeek == null) {
return false;
String lowerCaseOrdinal = relativeParts[0];
String lowerCaseDayOfWeek = relativeParts[1];
if (lowerCaseOrdinal == null || lowerCaseDayOfWeek == null) {
return null;
}
String lowerCaseOrdinal = ordinal.toLowerCase(Locale.ENGLISH);
if (ORDINALS.contains(lowerCaseOrdinal) == false) {
return false;
if (!ORDINAL_TO_WEEK_NUMBER_MAPPING.containsKey(lowerCaseOrdinal) && !lowerCaseOrdinal.equals("last")) {
return null;
}
String lowerCaseDayOfWeek = dayOfWeek.toLowerCase(Locale.ENGLISH);
if (DAY_OF_MONTH_ALIAS.containsKey(lowerCaseDayOfWeek) == false) {
return false;
return null;
}
return true;
return relativeParts;
}

@Override
public boolean isRelativeValue(String value) {
if (value == null) {
throw EjbLogger.EJB3_TIMER_LOGGER.invalidScheduleValue(DayOfMonth.class.getSimpleName(), value);
}
if (value.equalsIgnoreCase("last")) {
String lowerCaseValue = value.toLowerCase(Locale.ROOT);
if (lowerCaseValue.equals("last")) {
return true;
}
if (this.isValidNegativeDayOfMonth(value)) {
if (this.isValidNegativeDayOfMonth(lowerCaseValue)) {
return true;
}
if (this.isDayOfWeekBased(value)) {
if (this.splitDayOfWeekBased(lowerCaseValue) != null) {
return true;
}
return false;
Expand Down
Expand Up @@ -210,6 +210,11 @@ protected void assertValid(Integer value) throws IllegalArgumentException {
}
}

/**
* Checks if relative value is supported.
* @param value non-null value
* @return true if relative value is supported
*/
public abstract boolean isRelativeValue(String value);

}