Skip to content

Commit

Permalink
Issue #15: Add support for offset from last day of the month
Browse files Browse the repository at this point in the history
  • Loading branch information
jmrozanec committed May 18, 2015
1 parent d6d94df commit fe62c30
Show file tree
Hide file tree
Showing 47 changed files with 578 additions and 276 deletions.
16 changes: 11 additions & 5 deletions src/main/java/com/cronutils/descriptor/DescriptionStrategy.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.cronutils.descriptor;

import com.cronutils.model.field.*;
import com.cronutils.model.field.value.FieldValue;
import com.cronutils.model.field.value.IntegerFieldValue;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.Validate;
Expand Down Expand Up @@ -87,11 +89,15 @@ protected String describe(FieldExpression fieldExpression, boolean and) {
* 1 in weeks context, may mean "Monday",
* so nominal value for 1 would be "Monday"
* Default will return int as String
* @param value - some integer
* @param fieldValue - some FieldValue
* @return String
*/
protected String nominalValue(int value) {
return nominalValueFunction.apply(value);
protected String nominalValue(FieldValue fieldValue) {
Validate.notNull(fieldValue, "FieldValue must not be null");
if(fieldValue instanceof IntegerFieldValue){
return nominalValueFunction.apply(((IntegerFieldValue)fieldValue).getValue());
}
return fieldValue.toString();
}

/**
Expand All @@ -100,7 +106,7 @@ protected String nominalValue(int value) {
* @return human readable description - String
*/
protected String describe(Always always, boolean and) {
if (always.getEvery().getTime() <= 1) {
if (always.getEvery().getTime().getValue() <= 1) {
return "";
}
return describe(always.getEvery(), and);
Expand Down Expand Up @@ -176,7 +182,7 @@ protected String describe(Between between, boolean and) {
*/
protected String describe(Every every, boolean and) {
String description;
if (every.getTime() > 1) {
if (every.getTime().getValue() > 1) {
description = String.format("%s %s ", bundle.getString("every"), nominalValue(every.getTime())) + " %p ";
} else {
description = bundle.getString("every")+" %s ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public String apply(Integer integer) {
public String apply(FieldExpression fieldExpression) {
if (fieldExpression instanceof On) {
On on = (On) fieldExpression;
switch (on.getSpecialChar()) {
switch (on.getSpecialChar().getValue()) {
case HASH:
return String.format("%s %s %s ", nominal.apply(on.getTime()), on.getNth(), bundle.getString("of_every_month"));
return String.format("%s %s %s ", nominal.apply(on.getTime().getValue()), on.getNth(), bundle.getString("of_every_month"));
case L:
return String.format("%s %s %s ", bundle.getString("last"), nominal.apply(on.getTime()), bundle.getString("of_every_month"));
return String.format("%s %s %s ", bundle.getString("last"), nominal.apply(on.getTime().getValue()), bundle.getString("of_every_month"));
default:
return "";
}
Expand All @@ -69,9 +69,9 @@ public static DescriptionStrategy daysOfMonthInstance(final ResourceBundle bundl
public String apply(FieldExpression fieldExpression) {
if (fieldExpression instanceof On) {
On on = (On) fieldExpression;
switch (on.getSpecialChar()) {
switch (on.getSpecialChar().getValue()) {
case W:
return String.format("%s %s %s ", bundle.getString("the_nearest_weekday_to_the"), on.getTime(), bundle.getString("of_the_month"));
return String.format("%s %s %s ", bundle.getString("the_nearest_weekday_to_the"), on.getTime().getValue(), bundle.getString("of_the_month"));
case L:
return bundle.getString("last_day_of_month");
case LW:
Expand Down
56 changes: 33 additions & 23 deletions src/main/java/com/cronutils/descriptor/TimeDescriptionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.cronutils.model.field.*;
import com.cronutils.model.field.constraint.FieldConstraintsBuilder;
import com.cronutils.model.field.value.IntegerFieldValue;
import com.google.common.base.Function;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.Validate;
Expand Down Expand Up @@ -59,7 +60,7 @@ class TimeDescriptionStrategy extends DescriptionStrategy {
new On(
FieldConstraintsBuilder.instance()
.forField(CronFieldName.SECOND).createConstraintsInstance(),
"" + defaultSeconds)
new IntegerFieldValue(defaultSeconds))
);
descriptions = Sets.newHashSet();
registerFunctions();
Expand Down Expand Up @@ -125,8 +126,9 @@ public String apply(TimeFields timeFields) {
if (isDefault((On) timeFields.seconds)) {
return String.format("%s %s ", bundle.getString("every"), bundle.getString("minute"));
} else {
return String.format("%s %s %s %s %02d", bundle.getString("every"), bundle.getString("minute"),
bundle.getString("at"), bundle.getString("second"), ((On) timeFields.seconds).getTime());
return String.format("%s %s %s %s %02d", bundle.getString("every"),
bundle.getString("minute"), bundle.getString("at"),
bundle.getString("second"), ((On) timeFields.seconds).getTime().getValue());
}
}
}
Expand All @@ -148,13 +150,13 @@ public String apply(TimeFields timeFields) {
}
return String.format("%s %s %s %s %s", bundle.getString("every"),
bundle.getString("hour"), bundle.getString("at"),
bundle.getString("minute"), ((On) timeFields.minutes).getTime());
bundle.getString("minute"), ((On) timeFields.minutes).getTime().getValue());
} else {
return String.format("%s %s %s %s %s %s %s %s", bundle.getString("every"),
bundle.getString("hour"), bundle.getString("at"),
bundle.getString("minute"), ((On) timeFields.minutes).getTime(),
bundle.getString("minute"), ((On) timeFields.minutes).getTime().getValue(),
bundle.getString("and"), bundle.getString("second"),
((On) timeFields.seconds).getTime());
((On) timeFields.seconds).getTime().getValue());
}
}
return "";
Expand All @@ -171,7 +173,7 @@ public String apply(TimeFields timeFields) {
&& timeFields.seconds instanceof Always) {
return String.format("%s %s %s %02d:%02d", bundle.getString("every"),
bundle.getString("second"), bundle.getString("at"),
((On) hours).getTime(), ((On) minutes).getTime());
((On) hours).getTime().getValue(), ((On) minutes).getTime().getValue());
}
return "";
}
Expand All @@ -187,11 +189,13 @@ public String apply(TimeFields timeFields) {
timeFields.minutes instanceof On
&& timeFields.seconds instanceof On) {
if (isDefault((On) timeFields.seconds)) {
return String.format("%s %02d:%02d", bundle.getString("at"), ((On) hours).getTime(),
((On) minutes).getTime());
return String.format("%s %02d:%02d", bundle.getString("at"),
((On) hours).getTime().getValue(),
((On) minutes).getTime().getValue());
} else {
return String.format("%s %02d:%02d:%02d", bundle.getString("at"), ((On) hours).getTime(),
((On) minutes).getTime(), ((On) seconds).getTime());
return String.format("%s %02d:%02d:%02d", bundle.getString("at"),
((On) hours).getTime().getValue(),
((On) minutes).getTime().getValue(), ((On) seconds).getTime().getValue());
}
}
return "";
Expand All @@ -206,7 +210,7 @@ public String apply(TimeFields timeFields) {
if (timeFields.hours instanceof On &&
timeFields.minutes instanceof Always &&
timeFields.seconds instanceof Always) {
return String.format("%s %02d:00", bundle.getString("at"), ((On) hours).getTime());
return String.format("%s %02d:00", bundle.getString("at"), ((On) hours).getTime().getValue());
}
return "";
}
Expand All @@ -225,18 +229,22 @@ public String apply(TimeFields timeFields) {
bundle.getString("every"),
bundle.getString("minute"),
bundle.getString("between"),
((On) timeFields.hours).getTime(), ((Between) timeFields.minutes).getFrom(),
((On) timeFields.hours).getTime().getValue(),
((Between) timeFields.minutes).getFrom().getValue(),
bundle.getString("and"),
((On) timeFields.hours).getTime(), ((Between) timeFields.minutes).getTo());
((On) timeFields.hours).getTime().getValue(),
((Between) timeFields.minutes).getTo().getValue());
}
if (timeFields.seconds instanceof Always) {
return String.format("%s %s %s %02d:%02d %s %02d:%02d",
bundle.getString("every"),
bundle.getString("second"),
bundle.getString("between"),
((On) timeFields.hours).getTime(), ((Between) timeFields.minutes).getFrom(),
((On) timeFields.hours).getTime().getValue(),
((Between) timeFields.minutes).getFrom().getValue(),
bundle.getString("and"),
((On) timeFields.hours).getTime(), ((Between) timeFields.minutes).getTo());
((On) timeFields.hours).getTime().getValue(),
((Between) timeFields.minutes).getTo().getValue());
}
}
return "";
Expand All @@ -251,11 +259,12 @@ public String apply(TimeFields timeFields) {
if (timeFields.hours instanceof Always &&
timeFields.minutes instanceof Every &&
timeFields.seconds instanceof On) {
if (((Every) timeFields.minutes).getTime()==1 &&
if (((Every) timeFields.minutes).getTime().getValue()==1 &&
isDefault((On) timeFields.seconds)) {
return String.format("%s %s", bundle.getString("every"), bundle.getString("minute"));
}
return String.format("%s %s %s ", bundle.getString("every"), ((Every) minutes).getTime(), bundle.getString("minutes"));
return String.format("%s %s %s ", bundle.getString("every"),
((Every) minutes).getTime().getValue(), bundle.getString("minutes"));
}
return "";
}
Expand All @@ -270,17 +279,18 @@ public String apply(TimeFields timeFields) {
timeFields.minutes instanceof On &&
timeFields.seconds instanceof On) {
//every hour
if(((On) timeFields.minutes).getTime()==0 && ((On) timeFields.seconds).getTime()==0){
if(((On) timeFields.minutes).getTime().getValue()==0 &&
((On) timeFields.seconds).getTime().getValue()==0){
return String.format("%s %s", bundle.getString("every"), bundle.getString("hour"));
}
String result = String.format("%s %s %s %s %s %s ",
bundle.getString("every"), ((Every) hours).getTime(), bundle.getString("hours"),
bundle.getString("at"), bundle.getString("minute"), ((On) minutes).getTime());
bundle.getString("every"), ((Every) hours).getTime().getValue(), bundle.getString("hours"),
bundle.getString("at"), bundle.getString("minute"), ((On) minutes).getTime().getValue());
if (isDefault((On) timeFields.seconds)) {
return result;
} else {
return String.format("%s %s %s", bundle.getString("and"),
bundle.getString("second"), ((On) seconds).getTime());
bundle.getString("second"), ((On) seconds).getTime().getValue());
}
}
return "";
Expand Down Expand Up @@ -310,6 +320,6 @@ public TimeFields(FieldExpression hours, FieldExpression minutes, FieldExpressio
* @return boolean - true if time value matches a default; false otherwise.
*/
private boolean isDefault(On on) {
return on.getTime() == defaultSeconds;
return on.getTime().getValue() == defaultSeconds;
}
}
12 changes: 7 additions & 5 deletions src/main/java/com/cronutils/mapper/CronMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import com.cronutils.model.Cron;
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.field.Always;
import com.cronutils.model.field.CronField;
import com.cronutils.model.field.CronFieldName;
import com.cronutils.model.field.On;
import com.cronutils.model.field.*;
import com.cronutils.model.field.constraint.FieldConstraints;
import com.cronutils.model.field.constraint.FieldConstraintsBuilder;
import com.cronutils.model.field.definition.DayOfWeekFieldDefinition;
import com.cronutils.model.field.definition.FieldDefinition;
import com.cronutils.model.field.value.IntegerFieldValue;
import com.cronutils.parser.field.FieldParser;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -127,7 +128,8 @@ static Function<CronField, CronField> returnOnZeroExpression(final CronFieldName
return new Function<CronField, CronField>() {
@Override
public CronField apply(CronField field) {
return new CronField(name, new On(FieldConstraintsBuilder.instance().forField(name).createConstraintsInstance(),"0"));
FieldConstraints constraints = FieldConstraintsBuilder.instance().forField(name).createConstraintsInstance();
return new CronField(name, new On(constraints, new IntegerFieldValue(0)));
}
};
}
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/cronutils/model/field/Always.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/

import com.cronutils.model.field.constraint.FieldConstraints;
import com.cronutils.model.field.value.FieldValue;
import com.cronutils.model.field.value.IntegerFieldValue;

/**
* Represents a star (*) value on cron expression field
Expand All @@ -25,12 +27,16 @@ public Always(FieldConstraints constraints) {
this(constraints, null);
}

public Always(FieldConstraints constraints, String every) {
private Always(Always always) {
this(always.getConstraints(), always.getEvery().getTime());
}

public Always(FieldConstraints constraints, IntegerFieldValue every) {
super(constraints);
if (every != null) {
this.every = new Every(getConstraints(), every);
} else {
this.every = new Every(getConstraints(), "1");
this.every = new Every(getConstraints(), new IntegerFieldValue(1));
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/cronutils/model/field/And.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cronutils.model.field;

import com.cronutils.model.field.constraint.FieldConstraintsBuilder;
import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -27,7 +28,12 @@ public class And extends FieldExpression {

public And() {
super(FieldConstraintsBuilder.instance().createConstraintsInstance());
expressions = new ArrayList<FieldExpression>();
expressions = Lists.newArrayList();
}

private And(And and) {
super(and.getConstraints());
expressions = Lists.newArrayList(and.expressions);
}

@Override
Expand Down
Loading

0 comments on commit fe62c30

Please sign in to comment.