Skip to content

Commit

Permalink
Merge branch 'espiegel-refactor_joda_to_java8'
Browse files Browse the repository at this point in the history
  • Loading branch information
jmrozanec committed Aug 7, 2016
2 parents 767bad0 + 8e1fc51 commit f6e9901
Show file tree
Hide file tree
Showing 20 changed files with 560 additions and 579 deletions.
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
<slf4j.version>1.7.12</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<commonsLang.version>3.4</commonsLang.version>
<jodatime.version>2.8.2</jodatime.version>
<junit.version>4.12</junit.version>
<mockito.version>1.10.19</mockito.version>
<powermock.version>1.6.2</powermock.version>
Expand All @@ -84,16 +83,17 @@
<artifactId>commons-lang3</artifactId>
<version>${commonsLang.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${jodatime.version}</version>
</dependency>

<dependency>
<groupId>com.cronutils</groupId>
<artifactId>htime</artifactId>
<version>${htime.version}</version>
<exclusions>
<exclusion>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand Down
17 changes: 0 additions & 17 deletions src/main/java/com/cronutils/TimeConstants.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.cronutils.descriptor;

import com.cronutils.model.field.expression.FieldExpression;
import com.cronutils.model.field.expression.On;
import org.joda.time.DateTime;

import java.time.DayOfWeek;
import java.time.Month;
import java.time.format.TextStyle;
import java.util.ResourceBundle;
import java.util.function.Function;

import com.cronutils.model.field.expression.FieldExpression;
import com.cronutils.model.field.expression.On;

/*
* Copyright 2014 jmrozanec
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -30,7 +32,7 @@ private DescriptionStrategyFactory() {}
* @return - DescriptionStrategy instance, never null
*/
public static DescriptionStrategy daysOfWeekInstance(final ResourceBundle bundle, final FieldExpression expression) {
final Function<Integer, String> nominal = integer -> new DateTime().withDayOfWeek(integer).dayOfWeek().getAsText(bundle.getLocale());
final Function<Integer, String> nominal = integer -> DayOfWeek.of(integer).getDisplayName(TextStyle.FULL, bundle.getLocale());

NominalDescriptionStrategy dow = new NominalDescriptionStrategy(bundle, nominal, expression);

Expand Down Expand Up @@ -88,7 +90,7 @@ public static DescriptionStrategy daysOfMonthInstance(final ResourceBundle bundl
public static DescriptionStrategy monthsInstance(final ResourceBundle bundle, final FieldExpression expression) {
return new NominalDescriptionStrategy(
bundle,
integer -> new DateTime().withMonthOfYear(integer).monthOfYear().getAsText(bundle.getLocale()),
integer -> Month.of(integer).getDisplayName(TextStyle.FULL, bundle.getLocale()),
expression
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/cronutils/mapper/ConstantsMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ConstantsMapper {
private ConstantsMapper() {}

public static final WeekDay QUARTZ_WEEK_DAY = new WeekDay(2, false);
public static final WeekDay JODATIME_WEEK_DAY = new WeekDay(1, false);
public static final WeekDay JAVA8 = new WeekDay(1, false);
public static final WeekDay CRONTAB_WEEK_DAY = new WeekDay(1, true);

/**
Expand Down
297 changes: 150 additions & 147 deletions src/main/java/com/cronutils/model/time/ExecutionTime.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.cronutils.model.time.generator;

import java.time.LocalDate;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.Validate;

import com.cronutils.mapper.WeekDay;
import com.cronutils.model.field.CronField;
import com.cronutils.model.field.CronFieldName;
Expand All @@ -9,12 +16,6 @@
import com.cronutils.parser.CronParserField;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.Validate;
import org.joda.time.DateTime;

import java.util.Collections;
import java.util.List;
import java.util.Set;
/*
* Copyright 2015 jmrozanec
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -124,6 +125,6 @@ public int generatePreviousValue(int reference) throws NoSuchValueException {

@Override
public boolean isMatch(int value) {
return dowValidValues.contains(new DateTime(year, month, value, 0, 0).getDayOfWeek());
return dowValidValues.contains(LocalDate.of(year, month, value).getDayOfWeek().getValue());
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.cronutils.model.time.generator;

import java.time.ZonedDateTime;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cronutils.model.field.CronField;
import com.cronutils.model.field.expression.Every;
import com.cronutils.model.field.expression.FieldExpression;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
/*
* Copyright 2015 jmrozanec
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -29,7 +30,7 @@ public EveryFieldValueGenerator(CronField cronField) {
super(cronField);
log.trace(String.format(
"processing \"%s\" at %s",
cronField.getExpression().asString(), DateTime.now()
cronField.getExpression().asString(), ZonedDateTime.now()
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.Validate;

import com.cronutils.model.field.CronField;
import com.cronutils.model.field.expression.FieldExpression;
/*
* Copyright 2015 jmrozanec
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.cronutils.model.time.generator;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.List;

import org.apache.commons.lang3.Validate;

import com.cronutils.model.field.CronField;
import com.cronutils.model.field.CronFieldName;
import com.cronutils.model.field.expression.FieldExpression;
import com.cronutils.model.field.expression.On;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.Validate;
import org.joda.time.DateTime;

import java.util.List;
/*
* Copyright 2015 jmrozanec
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -24,53 +27,57 @@
class OnDayOfMonthValueGenerator extends FieldValueGenerator {
private int year;
private int month;

public OnDayOfMonthValueGenerator(CronField cronField, int year, int month) {
super(cronField);
Validate.isTrue(CronFieldName.DAY_OF_MONTH.equals(cronField.getField()), "CronField does not belong to day of month");
Validate.isTrue(CronFieldName.DAY_OF_MONTH.equals(cronField.getField()), "CronField does not belong to day of" +
" month");
this.year = year;
this.month = month;
}

@Override
public int generateNextValue(int reference) throws NoSuchValueException {
On on = ((On)cronField.getExpression());
On on = ((On) cronField.getExpression());
int value = generateValue(on, year, month);

if(value<=reference){
if (value <= reference) {
throw new NoSuchValueException();
}
return value;
}

@Override
public int generatePreviousValue(int reference) throws NoSuchValueException {
On on = ((On)cronField.getExpression());
On on = ((On) cronField.getExpression());
int value = generateValue(on, year, month);
if(value>=reference){
if (value >= reference) {
throw new NoSuchValueException();
}
return value;
}

@Override
protected List<Integer> generateCandidatesNotIncludingIntervalExtremes(int start, int end) {
List<Integer>values = Lists.newArrayList();
List<Integer> values = Lists.newArrayList();
try {
int reference = generateNextValue(start);
while(reference<end){
while (reference < end) {
values.add(reference);
reference=generateNextValue(reference);
reference = generateNextValue(reference);
}
} catch (NoSuchValueException e) {}
} catch (NoSuchValueException e) {
}
return values;
}

@Override
public boolean isMatch(int value) {
On on = ((On)cronField.getExpression());
On on = ((On) cronField.getExpression());
try {
return value == generateValue(on, year, month);
} catch (NoSuchValueException ignored) {}//we just skip, since we generate values until we get the exception
} catch (NoSuchValueException ignored) {
}//we just skip, since we generate values until we get the exception
return false;
}

Expand All @@ -80,34 +87,32 @@ protected boolean matchesFieldExpressionClass(FieldExpression fieldExpression) {
}

private int generateValue(On on, int year, int month) throws NoSuchValueException {
int time = on.getTime().getValue();
switch (on.getSpecialChar().getValue()){
int dayOfMonth = on.getTime().getValue();
switch (on.getSpecialChar().getValue()) {
case L:
return new DateTime(year, month, 1, 1, 1).dayOfMonth().getMaximumValue();
return LocalDate.of(year, month, 1).lengthOfMonth();
case W: // First work day of the week
DateTime doM = new DateTime(year, month, time, 1, 1);
if(doM.getDayOfWeek()==6){//dayOfWeek is Saturday!
if(time==1){//first day in month is Saturday! We execute on Monday
LocalDate doM = LocalDate.of(year, month, dayOfMonth);
if (doM.getDayOfWeek() == DayOfWeek.SATURDAY) {//dayOfWeek is Saturday!
if (dayOfMonth == 1) {//first day in month is Saturday! We execute on Monday
return 3;
}
return time-1;
return dayOfMonth - 1;
}
if(doM.getDayOfWeek()==7){ // dayOfWeek is Sunday
if((time+1)<=doM.dayOfMonth().getMaximumValue()){
return time+1;
if (doM.getDayOfWeek() == DayOfWeek.SUNDAY) { // dayOfWeek is Sunday
if ((dayOfMonth + 1) <= doM.lengthOfMonth()) {
return dayOfMonth + 1;
}
}
return time; // first day of week is a weekday
return dayOfMonth; // first day of week is a weekday
case LW:
DateTime lastDayOfMonth =
new DateTime(year, month, new DateTime(year, month, 1, 1, 1)
.dayOfMonth().getMaximumValue(), 1, 1);
int dow = lastDayOfMonth.getDayOfWeek();
LocalDate lastDayOfMonth = LocalDate.of(year, month, LocalDate.of(year, month, 1).lengthOfMonth());
int dow = lastDayOfMonth.getDayOfWeek().getValue();
int diff = dow - 5;
if(diff > 0){
return lastDayOfMonth.minusDays(diff).dayOfMonth().get();
if (diff > 0) {
return lastDayOfMonth.minusDays(diff).getDayOfMonth();
}
return lastDayOfMonth.dayOfMonth().get();
return lastDayOfMonth.getDayOfMonth();
}
throw new NoSuchValueException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.cronutils.model.time.generator;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.List;

import org.apache.commons.lang3.Validate;

import com.cronutils.mapper.ConstantsMapper;
import com.cronutils.mapper.WeekDay;
import com.cronutils.model.field.CronField;
import com.cronutils.model.field.CronFieldName;
import com.cronutils.model.field.expression.FieldExpression;
import com.cronutils.model.field.expression.On;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.Validate;
import org.joda.time.DateTime;

import java.util.List;
/*
* Copyright 2015 jmrozanec
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -96,11 +98,11 @@ private int generateValue(On on, int year, int month, int reference) throws NoSu
}

private int generateHashValues(On on, int year, int month){
int dowForFirstDoM = new DateTime(year, month, 1, 1, 1).getDayOfWeek();//1-7
int requiredDoW = ConstantsMapper.weekDayMapping(mondayDoWValue, ConstantsMapper.JODATIME_WEEK_DAY, on.getTime().getValue());//to normalize to joda-time value
DayOfWeek dowForFirstDoM = LocalDate.of(year, month, 1).getDayOfWeek();//1-7
int requiredDoW = ConstantsMapper.weekDayMapping(mondayDoWValue, ConstantsMapper.JAVA8, on.getTime().getValue());//to normalize to jdk8-time value
int requiredNth = on.getNth().getValue();
int baseDay = 1;//day 1 from given month
int diff = dowForFirstDoM - requiredDoW;
int diff = dowForFirstDoM.getValue() - requiredDoW;
if(diff == 0){
//base day remains the same
}
Expand All @@ -114,20 +116,20 @@ private int generateHashValues(On on, int year, int month){
}

private int generateLValues(On on, int year, int month) throws NoSuchValueException {
int lastDoM = new DateTime(year, month, 1, 1, 1).dayOfMonth().getMaximumValue();
DateTime lastDoMDateTime = new DateTime(year, month, lastDoM, 1, 1);
int dowForLastDoM = lastDoMDateTime.getDayOfWeek();//1-7
int requiredDoW = ConstantsMapper.weekDayMapping(mondayDoWValue, ConstantsMapper.JODATIME_WEEK_DAY, on.getTime().getValue());//to normalize to joda-time value
int lastDoM = LocalDate.of(year, month, 1).lengthOfMonth();
LocalDate lastDoMDateTime = LocalDate.of(year, month, lastDoM);
int dowForLastDoM = lastDoMDateTime.getDayOfWeek().getValue();//1-7
int requiredDoW = ConstantsMapper.weekDayMapping(mondayDoWValue, ConstantsMapper.JAVA8, on.getTime().getValue());//to normalize to jdk8-time value
int dowDiff = dowForLastDoM - requiredDoW;

if(dowDiff==0){
return lastDoMDateTime.dayOfMonth().get();
return lastDoMDateTime.getDayOfMonth();
}
if(dowDiff<0){
return lastDoMDateTime.minusDays(dowForLastDoM+(7-requiredDoW)).dayOfMonth().get();
return lastDoMDateTime.minusDays(dowForLastDoM+(7-requiredDoW)).getDayOfMonth();
}
if(dowDiff>0){
return lastDoMDateTime.minusDays(dowDiff).dayOfMonth().get();
return lastDoMDateTime.minusDays(dowDiff).getDayOfMonth();
}
throw new NoSuchValueException();
}
Expand All @@ -145,9 +147,9 @@ private int generateLValues(On on, int year, int month) throws NoSuchValueExcept
*/
private int generateNoneValues(On on, int year, int month, int reference) {
// the day of week the first of the month is on
int dowForFirstDoM = new DateTime(year, month, 1, 1, 1).getDayOfWeek();// 1-7
// the day of week we need, normalize to jodatime
int requiredDoW = ConstantsMapper.weekDayMapping(mondayDoWValue, ConstantsMapper.JODATIME_WEEK_DAY, on.getTime().getValue());
int dowForFirstDoM = LocalDate.of(year, month, 1).getDayOfWeek().getValue();// 1-7
// the day of week we need, normalize to jdk8time
int requiredDoW = ConstantsMapper.weekDayMapping(mondayDoWValue, ConstantsMapper.JAVA8, on.getTime().getValue());
// the first day of the month
int baseDay = 1;// day 1 from given month
// the difference between the days of week
Expand Down
Loading

0 comments on commit f6e9901

Please sign in to comment.