diff --git a/NAMESPACE b/NAMESPACE index 0eb16cc7..2251db90 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -244,6 +244,8 @@ exportMethods("*") exportMethods("+") exportMethods("-") exportMethods("/") +exportMethods("==") +exportMethods("!=") exportMethods("[") exportMethods("[<-") exportMethods("[[") diff --git a/R/intervals.r b/R/intervals.r index 26ad2151..c5b72ab1 100644 --- a/R/intervals.r +++ b/R/intervals.r @@ -656,6 +656,51 @@ setMethod("%within%", signature(a = "Interval", b = "list"), function(a, b) { out }) +#' Equality check for Interval objects +#' +#' Check whether `e1` and `e2` have the same start and end date-time objects. +#' +#' @param e1 An Interval object +#' @param e2 Another Interval object +#' @return TRUE if the start and end of `e1` match the start and end of `e1`, otherwise FALSE +#' @method == Interval +#' @export +#' @examples +#' int1 <- interval(as.Date("2021-01-01"), as.Date("2021-12-30")) +#' int2 <- interval(as.Date("2021-01-01"), as.Date("2021-12-30")) +#' int3 <- interval(as.Date("2021-01-02"), as.Date("2021-12-31")) +#' +#' int1 == int2 # TRUE +#' int1 == int3 # FALSE +#' c(int1, int2) == c(int2, int3) # TRUE FALSE +#' int1 == c(int1, int2, int3) # TRUE TRUE FALSE +#' c(int1, int2, int3) == int1 # TRUE TRUE FALSE +setMethod("==", signature(e1 = "Interval", e2 = "Interval"), function(e1, e2) { + int_start(e1) == int_start(e2) & int_end(e1) == int_end(e2) +}) + +#' Inequality check for Interval objects +#' +#' Check whether `e1` and `e2` have different start or end date-time objects. +#' +#' @param e1 An Interval object +#' @param e2 Another Interval object +#' @return TRUE if the start and end of `e1` do not match the start and end of `e1`, otherwise FALSE +#' @method != Interval +#' @export +#' @examples +#' int1 <- interval(as.Date("2021-01-01"), as.Date("2021-12-30")) +#' int2 <- interval(as.Date("2021-01-01"), as.Date("2021-12-30")) +#' int3 <- interval(as.Date("2021-01-02"), as.Date("2021-12-31")) +#' +#' int1 != int2 # FALSE +#' int1 != int3 # TRUE +#' c(int1, int2) != c(int2, int3) # FALSE TRUE +#' int1 != c(int1, int2, int3) # FALSE FALSE TRUE +#' c(int1, int2, int3) != int1 # FALSE FALSE TRUE +setMethod("!=", signature(e1 = "Interval", e2 = "Interval"), function(e1, e2) { + int_start(e1) != int_start(e2) | int_end(e1) != int_end(e2) +}) #' @export as.list.Interval <- function(x, ...) { diff --git a/_pkgdown.yml b/_pkgdown.yml index 86bcc00f..b00c0fb6 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -80,6 +80,8 @@ reference: - as.interval - "`%within%`" - Interval-class + - "`==,Interval,Interval-method`" + - "`!=,Interval,Interval-method`" - title: Timespans desc: > diff --git a/man/equals-Interval-Interval-method.Rd b/man/equals-Interval-Interval-method.Rd new file mode 100644 index 00000000..ad387c10 --- /dev/null +++ b/man/equals-Interval-Interval-method.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/intervals.r +\name{==,Interval,Interval-method} +\alias{==,Interval,Interval-method} +\title{Equality check for Interval objects} +\usage{ +\S4method{==}{Interval,Interval}(e1, e2) +} +\arguments{ +\item{e1}{An Interval object} + +\item{e2}{Another Interval object} +} +\value{ +TRUE if the start and end of \code{e1} match the start and end of \code{e1}, otherwise FALSE +} +\description{ +Check whether \code{e1} and \code{e2} have the same start and end date-time objects. +} +\examples{ +int1 <- interval(as.Date("2021-01-01"), as.Date("2021-12-30")) +int2 <- interval(as.Date("2021-01-01"), as.Date("2021-12-30")) +int3 <- interval(as.Date("2021-01-02"), as.Date("2021-12-31")) + +int1 == int2 # TRUE +int1 == int3 # FALSE +c(int1, int2) == c(int2, int3) # TRUE FALSE +int1 == c(int1, int2, int3) # TRUE TRUE FALSE +c(int1, int2, int3) == int1 # TRUE TRUE FALSE +} diff --git a/man/not-equals-Interval-Interval-method.Rd b/man/not-equals-Interval-Interval-method.Rd new file mode 100644 index 00000000..b08a124d --- /dev/null +++ b/man/not-equals-Interval-Interval-method.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/intervals.r +\name{!=,Interval,Interval-method} +\alias{!=,Interval,Interval-method} +\title{Inequality check for Interval objects} +\usage{ +\S4method{!=}{Interval,Interval}(e1, e2) +} +\arguments{ +\item{e1}{An Interval object} + +\item{e2}{Another Interval object} +} +\value{ +TRUE if the start and end of \code{e1} do not match the start and end of \code{e1}, otherwise FALSE +} +\description{ +Check whether \code{e1} and \code{e2} have different start or end date-time objects. +} +\examples{ +int1 <- interval(as.Date("2021-01-01"), as.Date("2021-12-30")) +int2 <- interval(as.Date("2021-01-01"), as.Date("2021-12-30")) +int3 <- interval(as.Date("2021-01-02"), as.Date("2021-12-31")) + +int1 != int2 # FALSE +int1 != int3 # TRUE +c(int1, int2) != c(int2, int3) # FALSE TRUE +int1 != c(int1, int2, int3) # FALSE FALSE TRUE +c(int1, int2, int3) != int1 # FALSE FALSE TRUE +} diff --git a/tests/testthat/test-intervals.R b/tests/testthat/test-intervals.R index e348a52a..f0abecda 100644 --- a/tests/testthat/test-intervals.R +++ b/tests/testthat/test-intervals.R @@ -883,3 +883,35 @@ test_that("Intervals handles missing numbers", { expect_equal(intersect(int, int), int) }) + +test_that("Equality operator checks for matching start and end", { + int1 <- interval(as.Date("2021-01-01"), as.Date("2021-12-30")) + int2 <- interval(as.Date("2021-01-01"), as.Date("2021-12-30")) + int3 <- interval(as.Date("2021-01-02"), as.Date("2021-12-31")) + int4 <- interval(as.Date("2020-12-31"), as.Date("2021-12-30")) + + expect_true(int1 == int2) + expect_false(int1 == int3) + + expect_equal(c(int1, int2) == c(int2, int1), c(TRUE, TRUE)) + expect_equal(c(int1, int2) == c(int3, int4), c(FALSE, FALSE)) + expect_equal(c(int1, int2) == c(int2, int3), c(TRUE, FALSE)) + expect_equal(int1 == c(int1, int2, int3), c(TRUE, TRUE, FALSE)) + expect_equal(c(int1, int2, int3) == int1, c(TRUE, TRUE, FALSE)) +}) + +test_that("Inequality operator checks for non matching start and end", { + int1 <- interval(as.Date("2021-01-01"), as.Date("2021-12-30")) + int2 <- interval(as.Date("2021-01-01"), as.Date("2021-12-30")) + int3 <- interval(as.Date("2021-01-02"), as.Date("2021-12-31")) + int4 <- interval(as.Date("2020-12-31"), as.Date("2021-12-30")) + + expect_false(int1 != int2) + expect_true(int1 != int3) + + expect_equal(c(int1, int2) != c(int2, int1), c(FALSE, FALSE)) + expect_equal(c(int1, int2) != c(int3, int4), c(TRUE, TRUE)) + expect_equal(c(int1, int2) != c(int2, int3), c(FALSE, TRUE)) + expect_equal(int1 != c(int1, int2, int3), c(FALSE, FALSE, TRUE)) + expect_equal(c(int1, int2, int3) != int1, c(FALSE, FALSE, TRUE)) +})