forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eugenp#17769 from vBarbaros/newtmp
BAEL-8318 Code and Unit Tests for Years Start on Sunday
- Loading branch information
Showing
6 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
...-operations-4/src/main/java/com/baeldung/sundaystartyears/FindSundayStartYearsLegacy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ations-4/src/main/java/com/baeldung/sundaystartyears/FindSundayStartYearsSpliterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...operations-4/src/main/java/com/baeldung/sundaystartyears/FindSundayStartYearsTimeApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ons-4/src/test/java/com/baeldung/sundaystartyears/FindSundayStartYearsLegacyUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
.../src/test/java/com/baeldung/sundaystartyears/FindSundayStartYearsSpliteratorUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ns-4/src/test/java/com/baeldung/sundaystartyears/FindSundayStartYearsTimeApiUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |