Skip to content

Commit

Permalink
Merge pull request eugenp#17769 from vBarbaros/newtmp
Browse files Browse the repository at this point in the history
BAEL-8318 Code and Unit Tests for Years Start on Sunday
  • Loading branch information
davidmartinezbarua authored Oct 6, 2024
2 parents 947cfc7 + 686fd30 commit cc7fcf2
Showing 6 changed files with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.baeldung.sundaystartyears;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

public class FindSundayStartYearsLegacy {

public static List<Integer> getYearsStartingOnSunday(int startYear, int endYear) {
List<Integer> years = new ArrayList<>();

for (int year = startYear; year <= endYear; year++) {
Calendar calendar = new GregorianCalendar(year, Calendar.JANUARY, 1);
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
years.add(year);
}
}
return years;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.baeldung.sundaystartyears;

import java.time.DayOfWeek;
import java.time.temporal.ChronoUnit;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class FindSundayStartYearsSpliterator {

public static List<Integer> getYearsStartingOnSunday(int startYear, int endYear) {
List<Integer> years = new ArrayList<>();

Spliterator<LocalDate> spliterator = Spliterators.spliteratorUnknownSize(
Stream.iterate(LocalDate.of(startYear, 1, 1), date -> date.plus(1, ChronoUnit.YEARS))
.limit(endYear - startYear + 1)
.iterator(), Spliterator.ORDERED);

Stream<LocalDate> stream = StreamSupport.stream(spliterator, false);

stream.filter(date -> date.getDayOfWeek() == DayOfWeek.SUNDAY)
.forEach(date -> years.add(date.getYear()));
return years;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.baeldung.sundaystartyears;

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

public class FindSundayStartYearsTimeApi {

public static List<Integer> getYearsStartingOnSunday(int startYear, int endYear) {
List<Integer> years = new ArrayList<>();

for (int year = startYear; year <= endYear; year++) {
LocalDate firstDayOfYear = LocalDate.of(year, 1, 1);
if (firstDayOfYear.getDayOfWeek() == DayOfWeek.SUNDAY) {
years.add(year);
}
}
return years;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.sundaystartyears;

import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class FindSundayStartYearsLegacyUnitTest {

@Test
public void givenYearRange_whenCheckingStartDayLegacy_thenReturnYearsStartingOnSunday() {
List<Integer> expected = List.of(2006, 2012, 2017, 2023);
List<Integer> result = FindSundayStartYearsLegacy.getYearsStartingOnSunday(2000, 2025);
assertEquals(expected, result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.sundaystartyears;

import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class FindSundayStartYearsSpliteratorUnitTest {

@Test
public void givenYearRange_whenCheckingStartDaySpliterator_thenReturnYearsStartingOnSunday() {
List<Integer> expected = List.of(2006, 2012, 2017, 2023);
List<Integer> result = FindSundayStartYearsSpliterator.getYearsStartingOnSunday(2000, 2025);
assertEquals(expected, result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.sundaystartyears;

import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class FindSundayStartYearsTimeApiUnitTest {

@Test
public void givenYearRange_whenCheckingStartDayTimeApi_thenReturnYearsStartingOnSunday() {
List<Integer> expected = List.of(2006, 2012, 2017, 2023);
List<Integer> result = FindSundayStartYearsTimeApi.getYearsStartingOnSunday(2000, 2025);
assertEquals(expected, result);
}
}

0 comments on commit cc7fcf2

Please sign in to comment.