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

Preserve "step intertia" when calculating a next or previous date #52

Merged
merged 6 commits into from
Feb 24, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Include direction when stepping over expressions
  • Loading branch information
Antonio Alonso Dominguez committed Feb 23, 2017
commit 90ea0fe20b8c35b1d0c28b3b11265f1396111f49
46 changes: 32 additions & 14 deletions core/shared/src/main/scala/cron4s/base/Enumerated.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,36 @@ import Scalaz._
/**
* Created by alonsodomin on 23/08/2016.
*/

private[cron4s] sealed trait Direction
private[cron4s] object Direction {
case object Forward extends Direction
case object Backwards extends Direction
}

trait Enumerated[A] {

def min(a: A): Int = range(a).min
def max(a: A): Int = range(a).max

def step(a: A)(from: Int, stepSize: Int): Option[(Int, Int)] = {
private[cron4s] def stepInDirection(a: A, from: Int, stepSize: Int, direction: Direction): Option[(Int, Int, Direction)] = {
if (stepSize == Int.MinValue || stepSize == Int.MaxValue) {
None
} else {
val aRange = range(a)

val nearestNeighbourIndex = if (stepSize > 0) {
aRange.lastIndexWhere(from >= _).some
} else if (stepSize < 0) {
val idx = aRange.indexWhere(from <= _)
if (idx == -1) aRange.size.some
else idx.some
} else {
none[Int]
val nearestNeighbourIndex = direction match {
case Direction.Forward =>
aRange.lastIndexWhere(from >= _)

case Direction.Backwards =>
val idx = aRange.indexWhere(from <= _)
if (idx == -1) aRange.size
else idx
}

nearestNeighbourIndex.map { idx =>
val pointer = idx + stepSize
if (stepSize != 0) {
val pointer = nearestNeighbourIndex + stepSize
val index = {
val mod = pointer % aRange.size
if (mod < 0) aRange.size + mod
Expand All @@ -56,18 +63,29 @@ trait Enumerated[A] {
pointer
}

aRange(index) -> offsetPointer / aRange.size
} orElse {
(aRange(index), offsetPointer / aRange.size, direction).some
} else {
val result = {
if (from <= min(a)) min(a)
else if (from >= max(a)) max(a)
else from
}
(result -> 0).some
(result, 0, direction).some
}
}
}

def step(a: A)(from: Int, stepSize: Int): Option[(Int, Int)] = {
val direction = {
if (stepSize >= 0) Direction.Forward
else Direction.Backwards
}

stepInDirection(a, from, stepSize, direction).map { case (res, co, _) =>
res -> co
}
}

def next(a: A)(from: Int): Option[Int] = step(a)(from, 1).map(_._1)
def prev(a: A)(from: Int): Option[Int] = step(a)(from, -1).map(_._1)

Expand Down
3 changes: 2 additions & 1 deletion core/shared/src/main/scala/cron4s/datetime/Stepper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ private[datetime] final class Stepper[DateTime](DT: IsDateTime[DateTime]) {
}
}

dateStepLoop(Some(from -> step): Step)
//dateStepLoop(Some(from -> step): Step)
doStep(Some(from -> step): Step)
}

def stepOverTime(rawExpr: RawTimeCronExpr, from: DateTime, initial: Int): Step =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ trait EnumeratedTests[A] extends Laws {
"max" -> forAll(laws.max _),
"forward" -> forAll(laws.forward _),
"backwards" -> forAll(laws.backwards _),
"zeroStepSize" -> forAll(laws.zeroStepSize _),
"zeroStepSize" -> forAll(laws.zeroStepSize2 _),
"fromMinToMinForwards" -> forAll(laws.fromMinToMinForwards _),
"fromMaxToMaxForwards" -> forAll(laws.fromMaxToMaxForwards _),
"fromMinToMaxForwards" -> forAll(laws.fromMinToMaxForwards _),
"fromMinToMaxBackwards" -> forAll(laws.fromMinToMaxBackwards _),
"fromMaxToMinForwards" -> forAll(laws.fromMaxToMinForwards _),
"fromMaxToMinBackwards" -> forAll(laws.fromMaxToMinBackwards _),
"backAndForth" -> forAll(laws.backAndForth _)
"fromMaxToMinBackwards" -> forAll(laws.fromMaxToMinBackwards _)
//"backAndForth" -> forAll(laws.backAndForth _)
)

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@

package cron4s.testkit

import org.scalacheck.Prop
import cron4s.base.Direction
import org.scalacheck.{Arbitrary, Gen, Prop}

import scala.language.implicitConversions

import scalaz.Equal

/**
* Created by alonsodomin on 03/01/2017.
*/
package object discipline {

implicit def isEqualToProp[A: Equal](isEqual: IsEqual[A]): Prop =
isEqual.lhs ?== isEqual.rhs

private[cron4s] val directionGen: Gen[Direction] = Gen.oneOf(Direction.Forward, Direction.Backwards)
private[cron4s] implicit lazy val arbitraryDirection: Arbitrary[Direction] = Arbitrary(directionGen)

}
15 changes: 13 additions & 2 deletions testkit/src/main/scala/cron4s/testkit/laws/EnumeratedLaws.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@

package cron4s.testkit.laws

import cron4s.testkit._
import cron4s.base.Enumerated
import cron4s.base.{Direction, Enumerated}
import cron4s.syntax.enumerated._
import cron4s.testkit._

import org.scalacheck.Prop

import scalaz._
import Scalaz._

/**
* Created by alonsodomin on 27/08/2016.
Expand Down Expand Up @@ -54,6 +59,12 @@ trait EnumeratedLaws[A] {
a.step(from, 0) <-> zeroStepExpected(a, from)
}

private[cron4s] def zeroStepSize2(a: A, from: Int, direction: Direction): Prop = {
val stepped = TC.stepInDirection(a, from, 0, direction).map(_._1)

stepped.map(a.range.contains).map(_ ?== true).getOrElse(proved)
}

def fromMinToMinForwards(a: A): IsEqual[Option[(Int, Int)]] =
a.step(a.min, a.range.size) <-> Some(a.min -> 1)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2017 Antonio Alonso Dominguez
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cron4s.lib.threetenbp

import org.scalatest.{FlatSpec, Matchers}

/**
* Created by domingueza on 20/02/2017.
*/
class RegressionSpec extends FlatSpec with Matchers {



}