From 69828d01afefa773019513f08f1f9a75551bac38 Mon Sep 17 00:00:00 2001 From: Nate Clark Date: Fri, 2 Aug 2019 09:09:59 -0400 Subject: [PATCH] CronExpression.FieldPart use int rather than Integer It is more expensive to perform operations on Integer objects rather than primitive int. Since an object is not needed use the primitive instead. --- .../src/main/java/fc/cron/CronExpression.java | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/java8/src/main/java/fc/cron/CronExpression.java b/java8/src/main/java/fc/cron/CronExpression.java index 74e7698..a4abc3e 100644 --- a/java8/src/main/java/fc/cron/CronExpression.java +++ b/java8/src/main/java/fc/cron/CronExpression.java @@ -20,6 +20,7 @@ package fc.cron; import java.time.*; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.regex.Matcher; @@ -256,9 +257,14 @@ public class CronExpression { return getClass().getSimpleName() + "<" + expr + ">"; } - static class FieldPart { - private Integer from, to, increment; + static class FieldPart implements Comparable { + private int from = -1, to = -1, increment = -1; private String modifier, incrementModifier; + + @Override + public int compareTo(FieldPart o) { + return Integer.compare(from, o.from); + } } abstract static class BasicField { @@ -322,14 +328,15 @@ public class CronExpression { if (increment != null) { part.incrementModifier = incrementModifier; - part.increment = Integer.valueOf(increment); + part.increment = Integer.parseInt(increment); } validateRange(part); validatePart(part); parts.add(part); - } + + Collections.sort(parts); } protected void validatePart(FieldPart part) { @@ -341,10 +348,10 @@ public class CronExpression { } private void validateRange(FieldPart part) { - if ((part.from != null && part.from < fieldType.from) || (part.to != null && part.to > fieldType.to)) { + if ((part.from != -1 && part.from < fieldType.from) || (part.to != -1 && part.to > fieldType.to)) { throw new IllegalArgumentException(String.format("Invalid interval [%s-%s], must be %s<=_<=%s", part.from, part.to, fieldType.from, fieldType.to)); - } else if (part.from != null && part.to != null && part.from > part.to) { + } else if (part.from != -1 && part.to != -1 && part.from > part.to) { throw new IllegalArgumentException( String.format( "Invalid interval [%s-%s]. Rolling periods are not supported (ex. 5-1, only 1-5) since this won't give a deterministic result. Must be %s<=_<=%s", @@ -352,12 +359,12 @@ public class CronExpression { } } - protected Integer mapValue(String value) { - Integer idx; + protected int mapValue(String value) { + int idx; if (fieldType.names != null && (idx = fieldType.names.indexOf(value.toUpperCase(Locale.getDefault()))) >= 0) { - return idx + 1; + return idx + fieldType.from; } - return Integer.valueOf(value); + return Integer.parseInt(value); } protected boolean matches(int val, FieldPart part) { @@ -410,9 +417,9 @@ public class CronExpression { } @Override - protected Integer mapValue(String value) { + protected int mapValue(String value) { // Use 1-7 for weedays, but 0 will also represent sunday (linux practice) - return "0".equals(value) ? Integer.valueOf(7) : super.mapValue(value); + return "0".equals(value) ? 7 : super.mapValue(value); } @Override @@ -439,7 +446,7 @@ public class CronExpression { for (FieldPart part : parts) { if ("L".equals(part.modifier)) { YearMonth ym = YearMonth.of(dato.getYear(), dato.getMonth().getValue()); - return dato.getDayOfMonth() == (ym.lengthOfMonth() - (part.from == null ? 0 : part.from)); + return dato.getDayOfMonth() == (ym.lengthOfMonth() - (part.from == -1 ? 0 : part.from)); } else if ("W".equals(part.modifier)) { if (dato.getDayOfWeek().getValue() <= 5) { if (dato.getDayOfMonth() == part.from) {