Skip to content

Instantly share code, notes, and snippets.

@arnaldog
Created August 14, 2020 00:00
Show Gist options
  • Save arnaldog/dcedbf7d36518479851a4c9342957cfd to your computer and use it in GitHub Desktop.
Save arnaldog/dcedbf7d36518479851a4c9342957cfd to your computer and use it in GitHub Desktop.
vigencias
{
id: 4,
name: "Entre pascua y año nuevo con tolerancia"
type: "or" // compuesta
childs: [
{
id: 1,
name: "Tolerancia antes de navidad"
fromDate: "navidad"
timeUnit: -1 dias
class: "ValidityTimeAmounmt" // LeafValidity
},
{
id: 2,
name: "Tolerancia entre navidad y año nuevo"
fromDate: "navidad"
untilDate: "año nuevo"
class: "RangeValidity" // LeafValidity
},
{
id: 3,
name: "Tolerancia después de año nuevo"
fromDate: "año nuevo"
timeUnit: +1 dias
class: "ValidityTimeAmount" // LeafValidity
}
]
}
interface Recurrencia {
fun nextDate() : DateTime
fun datesFor(fromDate, untilDare) : List<DateRange>
}
interface Validity {
fun isValid(dateTime, cb : (Validity) -> Unit )) : Boolean
}
class OrValidity : Validity {
@OneToMany
val validities: Validity? = listOf()
fun isValid(dateTime) {
return validities.reduce(true) { acc, it ->
if (it.isSatiesfiedBy(dateTime) or acc)
{
cb(this)
retrun true
}
}
}
}
class RangeValidity {
fun isValid(dateTime, cb: (Validity) -> Unit ) : Boolean {
if (dateTime.between(fromDate, toDate)) {
cb(this)
return true
}
}
}
// type: "or" // compuesta
DataOrValidity ( @OneToMany validities: List<Validities> ) {
}
// type: "and" // compuesta
DataAndValidity ()
Tabla Validity
- id
- tipo
- fromTime
- untilTime
- timeUnit
- timeAmoutn
- recurrence
Recurrencia "Todos los lunes"
Validity : "Entre las 9 y las 10 con tolerancia de 1 hora y despues"
Vigencia Final : Recurrencia y validity
val recurrencia = Recurrencia.newBuilder(
.days([Monday]);
val lunes = DateTime.day(Lunes)
val proximoLunes = DateTime.day(proximoLunes)
recurrencia.isValid(lunes) -> true
id | tipo | fromTime | untilTime | amount | recurrence | parent
-----------------------------------------------------------------------------------------
1 | "OrValidity" | null | null | null | null | null
1 | "ValidityTimeAmounmt" | navidad | null | -1 | null | 4
1 | "RangeValidity" | navidad | añonuevo | null | null | 4
1 | "ValidityTimeAmount" | añonuevo | null | 1 | null | 4
Tabla que relaciona validity-con-hijos
TablaValidityValidity
- parentId
- childId
parentId | childId
------------------------
1 | 2
1 | 3
1 | 4
// Api usage
val navidadValidity
.save
val toleranciaMenor
.save
val toreanciaMayor
.save
val validityGuardada = OrValidity(listOf(navidad, toleranmciaMenor, toleranciaMayor)).save()
val nuevo = AndValidity(navidad, validityGuardada).save()
validityGuardada.isSatiesfiedBy(navidad) { validity ->
listOfValidities.forEach { it ->
println("la vigencia que hizo match fue $i.class" )
}
}
class RecurrenciaPorDia(val day: DayOfWeek) : Vigencia, Recurrencia {
fun isValid(dateTime: DateTime) : Boolean {
return dayOf(dateTime) == day
}
override fun nextDateOf(dateTime) : Date? {
return /* ... */
}
override fun nextTimeOf(dateTime) : Time? {
return /* ... */
}
}
class RecurrenciaPorMes(val month: MontOfYear) : Vigencia {
fun isValid(dateTime: DateTime) : Boolean {
return monthOYear(dateTime) == month
}
}
IntervalValidity(fromHour, toHour) : Validity {
override fun isValid(dateTime: DateTime) : Boolean {
return dateTime.between(fromHour, toHour)
}
}
AndValidity(
private val validites : List<Validity>
) : Validity {
override fun isValid(dateTime: DateTime) : Boolean {
retun validities.fold(true) { acc, it -> it.isValid and acc }
}
}
val recurrencia = RecurrenciaPorDia(MONDAY)
val siguienteLunes = ...
val subsiguienteLunes = ..../
assertTrue(recurrencia.isValid(siguienteLunes)) # => true
assertTrue(recurrencia.isValid(subsiguienteLunes)) # => true
val siguienteMartes = ...
val recurrenciaMartes = RecurrenciaPorDia(TUESDAY)
assertTrue(recurrenciaMartes.isValid(siguienteMartes))
val duranteToEnero = DateTime.now().plusYears(2)
val recurrenciaEnero = RecurrenciaPorMes(ENERO)
assertTrue(recurrenciaEnero.isValid(duranteEnero)))
val siguienteLunesDeEnero = ... // el primero de enero
val todosLosPrimerosDeEneroRecurrencia = AndValidity(recurrenciaEnero, recurrenciaLunes)
assertTrue(todosLosPrimerosDeEneroRecurrencia.isValid(siguienteLunesDeEnero))
// Todos los lunes de enero entre 9 y 10 de la mañana
val siguenteLunesDeEneroEntre9y10deLaMañana = ...
val recurrenciaEntre9y10 = IntevalValidity(fromHour, toHour)
val recurrencia = AndValidity(
recurrenciaEnero, recurrenciaLunes, recurrenciaEntre9y10
)
assertTrue(recurrencia.isValid(siguenteLunesDeEneroEntre9y10deLaMañana))
assertEquals(recurrencia.nextDate(), siguenteLunesDeEneroEntre9y10deLaMañana)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment