-
-
Save rponte/1c6a5676fd5377cab50469213a4f6db5 to your computer and use it in GitHub Desktop.
| package br.com.triadworks.tinyobjects; | |
| import java.util.Date; | |
| public class Intervalo { | |
| private static int ONE_DAY_IN_MILLISECONDS = 24 * 60 * 60 * 1000; | |
| private Date inicio; | |
| private Date fim; | |
| public Intervalo(Date inicio, Date fim) { | |
| this.inicio = inicio; | |
| this.fim = fim; | |
| } | |
| public boolean isValido() { | |
| if (inicio == null || fim == null) return false; | |
| return inicio.compareTo(fim) <= 0; | |
| } | |
| public int diferencaEmDias() { | |
| if (!isValido()) | |
| throw new IllegalStateException("Data inicial ou final inválida."); | |
| long start = inicio.getTime(); | |
| long end = fim.getTime(); | |
| return (int) ((end - start) / ONE_DAY_IN_MILLISECONDS); | |
| } | |
| public Date getInicio() { | |
| return inicio; | |
| } | |
| public Date getFim() { | |
| return fim; | |
| } | |
| /** | |
| * Verifica se este intervalo conflita (sobrepoe) com outro intervalo | |
| */ | |
| public boolean conflitaCom(Intervalo outroIntervalo) { | |
| if ((inicio.compareTo(outroIntervalo.getFim()) <= 0) && (outroIntervalo.getInicio().compareTo(fim) <= 0)) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| } |
| package br.com.triadworks.tinyobjects; | |
| import static org.junit.Assert.assertEquals; | |
| import static org.junit.Assert.assertFalse; | |
| import static org.junit.Assert.assertTrue; | |
| import java.text.ParseException; | |
| import java.text.SimpleDateFormat; | |
| import java.util.Date; | |
| import org.junit.Test; | |
| public class IntervaloTest { | |
| /** | |
| * a|------| | |
| * b|------| | |
| */ | |
| @Test | |
| public void naoDeveIntervaloConflitarQuandoOutroIntervaloForAnteriorAoPeriodo() { | |
| Intervalo intervalo = new Intervalo(toDate("10/01/2016"), toDate("15/01/2016")); | |
| Intervalo conflitante = new Intervalo(toDate("04/01/2016"), toDate("09/01/2016")); | |
| boolean conflitou = intervalo.conflitaCom(conflitante); | |
| assertFalse("Não conflitou", conflitou); | |
| } | |
| /** | |
| * a|-------------| | |
| * b|------| | |
| */ | |
| @Test | |
| public void deveIntervaloConflitarQuandoIntervaloFinalEstiverDentroDoIntervaloInicial() { | |
| Intervalo intervalo = new Intervalo(toDate("10/01/2016"), toDate("20/01/2016")); | |
| Intervalo conflitante = new Intervalo(toDate("14/01/2016"), toDate("18/01/2016")); | |
| boolean conflitou = intervalo.conflitaCom(conflitante); | |
| assertTrue("conflitou", conflitou); | |
| } | |
| /** | |
| * a|--------| | |
| * b|--------------| | |
| */ | |
| @Test | |
| public void deveIntervaloConflitarQuandoIntervaloInicialEstiverDentroDoIntervaloFinal() { | |
| Intervalo intervalo = new Intervalo(toDate("14/01/2016"), toDate("18/01/2016")); | |
| Intervalo conflitante = new Intervalo(toDate("10/01/2016"), toDate("20/01/2016")); | |
| boolean conflitou = intervalo.conflitaCom(conflitante); | |
| assertTrue("conflitou", conflitou); | |
| } | |
| /** | |
| * a|------| | |
| * b|------| | |
| */ | |
| @Test | |
| public void naoDeveIntervaloConflitarQuandoOutroIntervaloForPosteriorAoPeriodo() { | |
| Intervalo intervalo = new Intervalo(toDate("04/01/2016"), toDate("09/01/2016")); | |
| Intervalo conflitante = new Intervalo(toDate("10/01/2016"), toDate("15/01/2016")); | |
| boolean conflitou = intervalo.conflitaCom(conflitante); | |
| assertFalse("Não conflitou", conflitou); | |
| } | |
| /** | |
| * a|------| | |
| * b|------| | |
| */ | |
| @Test | |
| public void deveIntervaloConflitarComOutroIntervaloNoMesmoPeriodo() { | |
| Intervalo intervalo = new Intervalo(toDate("05/01/2016"), toDate("10/01/2016")); | |
| Intervalo conflitante = new Intervalo(toDate("05/01/2016"), toDate("10/01/2016")); | |
| boolean conflitou = intervalo.conflitaCom(conflitante); | |
| assertTrue("conflitou", conflitou); | |
| } | |
| /** | |
| * a|------| | |
| * b|------| | |
| */ | |
| @Test | |
| public void deveIntervaloConflitarComOutroIntervaloQuandoDataFinalEstiverDentroDoPeriodo() { | |
| Intervalo intervalo = new Intervalo(toDate("05/01/2016"), toDate("10/01/2016")); | |
| Intervalo conflitante = new Intervalo(toDate("01/01/2016"), toDate("06/01/2016")); | |
| boolean conflitou = intervalo.conflitaCom(conflitante); | |
| assertTrue("conflitou", conflitou); | |
| } | |
| /** | |
| * a|------| | |
| * b|------| | |
| */ | |
| @Test | |
| public void deveIntervaloConflitarComOutroIntervaloQuandoDataInicialEstiverDentroDoPeriodo() { | |
| Intervalo intervalo = new Intervalo(toDate("05/01/2016"), toDate("10/01/2016")); | |
| Intervalo conflitante = new Intervalo(toDate("06/01/2016"), toDate("12/01/2016")); | |
| boolean conflitou = intervalo.conflitaCom(conflitante); | |
| assertTrue("conflitou", conflitou); | |
| } | |
| /** | |
| * a|------| | |
| * b|------| | |
| */ | |
| @Test | |
| public void deveIntervaloConflitarComOutroIntervaloQuandoDataFinalTiverOMesmoInicio() { | |
| Intervalo intervalo = new Intervalo(toDate("05/01/2016"), toDate("10/01/2016")); | |
| Intervalo conflitante = new Intervalo(toDate("01/01/2016"), toDate("05/01/2016")); | |
| boolean conflitou = intervalo.conflitaCom(conflitante); | |
| assertTrue("conflitou", conflitou); | |
| } | |
| /** | |
| * a|------| | |
| * b|------| | |
| */ | |
| @Test | |
| public void deveIntervaloConflitarComOutroIntervaloQuandoDataInicialTiverOMesmoFim() { | |
| Intervalo intervalo = new Intervalo(toDate("05/01/2016"), toDate("10/01/2016")); | |
| Intervalo conflitante = new Intervalo(toDate("10/01/2016"), toDate("15/01/2016")); | |
| boolean conflitou = intervalo.conflitaCom(conflitante); | |
| assertTrue("conflitou", conflitou); | |
| } | |
| @Test | |
| public void deveRetornarDiferencaEmDias() { | |
| assertEquals("0 dia", 0, diferencaEntre("01/01/2016", "01/01/2016")); | |
| assertEquals("1 dia", 1, diferencaEntre("01/01/2016", "02/01/2016")); | |
| assertEquals("4 dias", 4, diferencaEntre("01/01/2016", "05/01/2016")); | |
| assertEquals("1 semana", 7, diferencaEntre("01/01/2016", "08/01/2016")); | |
| assertEquals("1 mes", 30, diferencaEntre("01/01/2016", "31/01/2016")); | |
| assertEquals("1 ano", 365, diferencaEntre("01/01/2016", "31/12/2016")); | |
| } | |
| @Test | |
| public void deveSerUmIntervaloValidoQuandoDataInicialForMenorQueDataFinal() { | |
| Date inicio = toDate("01/01/2016"); | |
| Date fim = toDate("02/01/2016"); | |
| Intervalo intervalo = new Intervalo(inicio, fim); | |
| assertTrue("intervalo valido", intervalo.isValido()); | |
| } | |
| @Test | |
| public void deveSerUmIntervaloValidoQuandoDataInicialForIgualDataFinal() { | |
| Date inicio = toDate("02/01/2016"); | |
| Date fim = toDate("02/01/2016"); | |
| Intervalo intervalo = new Intervalo(inicio, fim); | |
| assertTrue("intervalo valido", intervalo.isValido()); | |
| } | |
| @Test | |
| public void deveSerUmIntervaloInvalidoQuandoDataInicialForMaiorDataFinal() { | |
| Date inicio = toDate("02/01/2016"); | |
| Date fim = toDate("01/01/2016"); | |
| Intervalo intervalo = new Intervalo(inicio, fim); | |
| assertFalse("intervalo invalido", intervalo.isValido()); | |
| } | |
| @Test | |
| public void deveSerUmIntervaloInvalidoQuandoDataInicialForNula() { | |
| Date inicio = null; | |
| Date fim = toDate("01/01/2016"); | |
| Intervalo intervalo = new Intervalo(inicio, fim); | |
| assertFalse("intervalo invalido", intervalo.isValido()); | |
| } | |
| @Test | |
| public void deveSerUmIntervaloInvalidoQuandoDataFinalForNula() { | |
| Date inicio = toDate("02/01/2016"); | |
| Date fim = null; | |
| Intervalo intervalo = new Intervalo(inicio, fim); | |
| assertFalse("intervalo invalido", intervalo.isValido()); | |
| } | |
| @Test | |
| public void deveSerUmIntervaloInvalidoQuandoDataInicialEFinalForemNulas() { | |
| Date inicio = null; | |
| Date fim = null; | |
| Intervalo intervalo = new Intervalo(inicio, fim); | |
| assertFalse("intervalo invalido", intervalo.isValido()); | |
| } | |
| private Date toDate(String ptBrDate) { | |
| try { | |
| return new SimpleDateFormat("dd/MM/yyyy").parse(ptBrDate); | |
| } catch (ParseException e) { | |
| throw new IllegalStateException(e); | |
| } | |
| } | |
| private int diferencaEntre(String inicio, String fim) { | |
| return new Intervalo(toDate(inicio), toDate(fim)).diferencaEmDias(); | |
| } | |
| } |
https://stewashton.wordpress.com/2014/03/11/sql-for-date-ranges-gaps-and-overlaps/ (abut/meet, overlap and gap)
https://stewashton.wordpress.com/2015/06/08/merging-overlapping-date-ranges/
https://stewashton.wordpress.com/2014/07/04/sql-and-date-ranges-dont-use-null/
https://stewashton.wordpress.com/2015/06/14/match_recognize-gaps-in-date-ranges/
https://stackoverflow.com/questions/39361063/find-gap-between-dates-ranges-sql-oracle
http://joda-time.sourceforge.net/apidocs/org/joda/time/Interval.html
http://joda-time.sourceforge.net/apidocs/org/joda/time/Interval.html#abuts(org.joda.time.ReadableInterval)
http://joda-time.sourceforge.net/apidocs/org/joda/time/Interval.html#gap(org.joda.time.ReadableInterval)
http://joda-time.sourceforge.net/apidocs/org/joda/time/Interval.html#overlap(org.joda.time.ReadableInterval)
http://joda-time.sourceforge.net/apidocs/src-html/org/joda/time/Interval.html#line.250
http://joda-time.sourceforge.net/apidocs/src-html/org/joda/time/base/BaseInterval.html
http://joda-time.sourceforge.net/apidocs/src-html/org/joda/time/base/AbstractInterval.html
http://wiki.c2.com/?TestIfDateRangesOverlap
https://martinfowler.com/eaaDev/Range.html
https://oracle-base.com/articles/misc/overlapping-date-ranges
https://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap
https://explainextended.com/2009/10/05/date-range-with-possible-null-values/