From 22ea02ec52d9b4c76151673fb974f85ac73dd7e4 Mon Sep 17 00:00:00 2001 From: kahles Date: Mon, 6 May 2024 20:31:48 +0200 Subject: [PATCH 1/3] upgrade to jdk 17, cleanup code --- .github/workflows/gradle.yml | 2 +- README.md | 10 +-- build.gradle | 12 ++-- .../de/kah2/zodiac/libZodiac/Calendar.java | 29 +++----- .../kah2/zodiac/libZodiac/CalendarData.java | 2 +- .../zodiac/libZodiac/CalendarGenerator.java | 39 ++++------- .../de/kah2/zodiac/libZodiac/DateRange.java | 10 +-- .../java/de/kah2/zodiac/libZodiac/Day.java | 4 -- .../zodiac/libZodiac/DayStorableDataSet.java | 4 +- .../zodiac/libZodiac/LocationProvider.java | 4 +- .../zodiac/libZodiac/ProgressManager.java | 4 +- .../libZodiac/interpretation/Gardening.java | 22 ++---- .../libZodiac/planetary/PlanetaryDayData.java | 9 +-- .../libZodiac/zodiac/ZodiacDirection.java | 28 ++------ .../libZodiac/zodiac/ZodiacElement.java | 29 ++------ .../zodiac/libZodiac/zodiac/ZodiacSign.java | 67 ++++++++----------- .../de/kah2/zodiac/libZodiac/Benchmark.java | 3 +- .../libZodiac/CalendarGeneratorStub.java | 6 +- .../libZodiac/CalendarGeneratorTest.java | 7 +- .../kah2/zodiac/libZodiac/CalendarTest.java | 31 +++------ .../libZodiac/TestConstantsAndHelpers.java | 35 +++------- .../example/CalendarDataStringBuilder.java | 2 +- .../example/CalendarExampleSimple.java | 15 ++--- .../example/CalendarExampleStorage.java | 44 ++++++------ .../example/ProgressListenerExample.java | 11 +-- .../interpretation/InterpreterTest.java | 4 +- .../planetary/PlanetaryDayDataStub.java | 10 +-- .../libZodiac/planetary/PositionTest.java | 2 +- 28 files changed, 157 insertions(+), 288 deletions(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index a30602f..67a6808 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -9,7 +9,7 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Set up JDK 1.8 + - name: Set up JDK uses: actions/setup-java@v2 with: distribution: 'zulu' diff --git a/README.md b/README.md index 8f14f5b..1d42511 100644 --- a/README.md +++ b/README.md @@ -25,13 +25,15 @@ dependencies { 0.9.4 ``` +https://github.com/kahles/libZodiac/blob/master/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarExampleSimple.java +https://github.com/kahles/libZodiac/blob/master/src/test/java/de/kah2/libZodiac/example/CalendarExampleSimple.java ### Usage -- [`CalendarExampleSimple.java`](src/test/java/de/kah2/libZodiac/example/CalendarExampleSimple.java) shows basic usage of this framework. -- [`CalendarDataStringBuilder.java`](src/test/java/de/kah2/libZodiac/example/CalendarDataStringBuilder.java) shows how to access data. -- [`CalendarExampleStorage.java`](src/test/java/de/kah2/libZodiac/example/CalendarExampleStorage.java) shows how to create, store , load and +- [`CalendarExampleSimple.java`](src/test/java/de/kah2/zodiac/libZodiac/example/CalendarExampleSimple.java) shows basic usage of this framework. +- [`CalendarDataStringBuilder.java`](src/test/java/de/kah2/zodiac/libZodiac/example/CalendarDataStringBuilder.java) shows how to access data. +- [`CalendarExampleStorage.java`](src/test/java/de/kah2/zodiac/libZodiac/example/CalendarExampleStorage.java) shows how to create, store , load and extend a calendar. -- [`ProgressListenerExample.java`](src/test/java/de/kah2/libZodiac/example/ProgressListenerExample.java) shows how to enable progress +- [`ProgressListenerExample.java`](src/test/java/de/kah2/zodiac/libZodiac/example/ProgressListenerExample.java) shows how to enable progress listening. diff --git a/build.gradle b/build.gradle index 39feb3f..7fb56cc 100644 --- a/build.gradle +++ b/build.gradle @@ -32,8 +32,8 @@ repositories { dependencies { - implementation 'org.slf4j:slf4j-api:1.7.36' - testImplementation 'org.slf4j:slf4j-simple:1.7.36' + implementation 'org.slf4j:slf4j-api:2.0.12' + testImplementation 'org.slf4j:slf4j-simple:2.0.13' // Removed, because actual version doesn't work for multi-threaded use // compile 'net.sourceforge.novaforjava:novaforjava:0.15.0.0' @@ -49,13 +49,13 @@ dependencies { // maven central releases implementation 'de.kah2.zodiac:nova4jmt:0.15.0.1' - testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0' - testImplementation 'org.assertj:assertj-core:3.23.1' + testImplementation 'org.junit.jupiter:junit-jupiter:5.10.2' + testImplementation 'org.assertj:assertj-core:3.25.3' } java { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 withJavadocJar() withSourcesJar() } diff --git a/src/main/java/de/kah2/zodiac/libZodiac/Calendar.java b/src/main/java/de/kah2/zodiac/libZodiac/Calendar.java index f6c271e..fd04123 100644 --- a/src/main/java/de/kah2/zodiac/libZodiac/Calendar.java +++ b/src/main/java/de/kah2/zodiac/libZodiac/Calendar.java @@ -1,6 +1,5 @@ package de.kah2.zodiac.libZodiac; -import de.kah2.zodiac.libZodiac.interpretation.Interpreter; import de.kah2.zodiac.libZodiac.planetary.Position; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,8 +31,6 @@ public class Calendar implements LocationProvider { private CalendarGenerator generator = new CalendarGenerator(this); - private Class interpreterClass; - /** * Tells the calendar, how much data is needed / how much overhead to * produce @@ -189,12 +186,12 @@ public void fixRangeExpectedToIncludeExistingDays() { if (!this.days.isEmpty()) { if (allDays.getFirst().getDate().isBefore(expectedStart)) { - this.log.debug("Fixing start of expected range: " + allDays.getFirst().getDate() + " => " + expectedStart); + this.log.debug( "Fixing start of expected range: {} => {}", allDays.getFirst().getDate(), expectedStart ); expectedStart = allDays.getFirst().getDate(); } if (allDays.getLast().getDate().isAfter(expectedEnd)) { - this.log.debug("Fixing end of expected range: " + allDays.getLast().getDate() + " => " + expectedEnd); + this.log.debug( "Fixing end of expected range: {} => {}", allDays.getLast().getDate(), expectedEnd ); expectedEnd = allDays.getLast().getDate(); } @@ -218,22 +215,12 @@ public void fixRangeExpectedToIncludeExistingDays() { */ public LinkedList removeOverhead(final boolean alsoDeleteFutureDays) { - DateRange rangeToKeep; - - switch (this.scope) { - - case PHASE: - rangeToKeep = new DateRange( this.getRangeExpected().getStart().minusDays(1), this.getRangeExpected().getEnd().plusDays(1) ); - break; - - case CYCLE: - rangeToKeep = this.getRangeNeededToKeepCycle(alsoDeleteFutureDays); - break; - - default: // DAY: - rangeToKeep = this.getRangeExpected(); - break; - } + DateRange rangeToKeep = switch ( this.scope ) { + case PHASE -> new DateRange( this.getRangeExpected().getStart().minusDays( 1 ), this.getRangeExpected().getEnd().plusDays( 1 ) ); + case CYCLE -> this.getRangeNeededToKeepCycle( alsoDeleteFutureDays ); + default -> // DAY: + this.getRangeExpected(); + }; LinkedList removed = this.days.removeBefore( rangeToKeep.getStart() ); diff --git a/src/main/java/de/kah2/zodiac/libZodiac/CalendarData.java b/src/main/java/de/kah2/zodiac/libZodiac/CalendarData.java index 403d214..5342890 100644 --- a/src/main/java/de/kah2/zodiac/libZodiac/CalendarData.java +++ b/src/main/java/de/kah2/zodiac/libZodiac/CalendarData.java @@ -111,7 +111,7 @@ Day get(final LocalDate date) { NavigableSet subSet = this.data.subSet(dummyDay, true, dummyDay, true); - if ( subSet.size() == 0 ) { + if ( subSet.isEmpty() ) { return null; } else { return subSet.first(); diff --git a/src/main/java/de/kah2/zodiac/libZodiac/CalendarGenerator.java b/src/main/java/de/kah2/zodiac/libZodiac/CalendarGenerator.java index 93717a6..8ad9294 100644 --- a/src/main/java/de/kah2/zodiac/libZodiac/CalendarGenerator.java +++ b/src/main/java/de/kah2/zodiac/libZodiac/CalendarGenerator.java @@ -6,25 +6,13 @@ import org.slf4j.LoggerFactory; import java.time.LocalDate; -import java.util.Collection; -import java.util.Collections; -import java.util.ConcurrentModificationException; -import java.util.LinkedList; -import java.util.List; -import java.util.ListIterator; -import java.util.SortedSet; -import java.util.TreeSet; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.Future; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; +import java.util.*; +import java.util.concurrent.*; /** * This class contains logic for calculation of planetary data. */ -class CalendarGenerator implements ProgressListener { +public class CalendarGenerator implements ProgressListener { private final Logger log = LoggerFactory.getLogger(this.getClass()); @@ -118,7 +106,7 @@ private void generateDaysNeededInExpectedRange(DateRange range) { Collection missingDates = this.days.getMissingDates( range ); - if (missingDates.size() == 0) { + if ( missingDates.isEmpty() ) { this.continueOnMainThread(); return; } @@ -176,7 +164,9 @@ private void continueOnMainThread() { /** This is not needed, because states are set by this class */ @Override - public void onStateChanged(State state) { log.trace("onStateChanged: State changed to " + state); } + public void onStateChanged(State state) { + + log.trace( "onStateChanged: State changed to {}", state ); } /** * Checks if a calculation step is finished and calls #doStateChange - argument is ignored. Must be synchronized, @@ -467,13 +457,14 @@ private void onFinished() { private void startDayCreationThread(final LocalDate date) { final CompletableFuture result = CompletableFuture.supplyAsync(() -> { - CalendarGenerator.this.log.trace(" ++++++++ Starting calculation for " + date); + CalendarGenerator.this.log.trace( " ++++++++ Starting calculation for {}", date ); Thread.currentThread().setPriority(threadPriority); final Day day = CalendarGenerator.this.createCalculatedDay(date); - CalendarGenerator.this.log.trace(" -------- Calculation finished for " + date); + CalendarGenerator.this.log.trace( " -------- Calculation finished for {}", + date ); return day; }, this.executor).exceptionally(throwable -> { @@ -534,9 +525,8 @@ private void countDaysToLunarExtremesInBothDirections() { } private int incrementDayCount(int counter, Day day) { - this.log.debug( - "Looking for extreme: " + day.getDate() + " - " + day.getPlanetaryData().getLunarPhase() + - "(count: " + counter + ")"); + + this.log.debug( "Looking for extreme: {} - {}(count: {})", day.getDate(), day.getPlanetaryData().getLunarPhase(), counter ); if (counter < PlanetaryDayData.DAY_COUNT_NOT_CALCULATED) { counter ++; @@ -568,8 +558,7 @@ private void updateLunarPhases(LinkedList days) { current.getPlanetaryData().setLunarPhase( LunarPhase.of(previous, current, next) ); - this.log.debug(" (" + previous.getDate() + ", " + current.getDate() + ", " + next.getDate() + ") => " - + current.getPlanetaryData().getLunarPhase()); + this.log.debug( " ({}, {}, {}) => {}", previous.getDate(), current.getDate(), next.getDate(), current.getPlanetaryData().getLunarPhase() ); previous = current; current = next; @@ -642,7 +631,7 @@ public int getMaxThreadCount() { if (this.maxThreadCount < 1) { - this.log.info("Setting maxThreadCount to NUMBER_OF_CORES=" + NUMBER_OF_CORES); + this.log.info( "Setting maxThreadCount to NUMBER_OF_CORES={}", NUMBER_OF_CORES ); this.maxThreadCount = NUMBER_OF_CORES; } diff --git a/src/main/java/de/kah2/zodiac/libZodiac/DateRange.java b/src/main/java/de/kah2/zodiac/libZodiac/DateRange.java index b481526..4bde659 100644 --- a/src/main/java/de/kah2/zodiac/libZodiac/DateRange.java +++ b/src/main/java/de/kah2/zodiac/libZodiac/DateRange.java @@ -79,20 +79,22 @@ public LocalDate getEnd() { @Override public Iterator iterator() { - return new Iterator() { + return new Iterator<>() { private LocalDate nextDate = DateRange.this.getStart(); @Override public boolean hasNext() { - return this.nextDate.isBefore(DateRange.this.getEnd()) - || this.nextDate.isEqual(DateRange.this.getEnd()); + + return this.nextDate.isBefore( DateRange.this.getEnd() ) + || this.nextDate.isEqual( DateRange.this.getEnd() ); } @Override public LocalDate next() { + final LocalDate currentDate = this.nextDate; - this.nextDate = currentDate.plusDays(1); + this.nextDate = currentDate.plusDays( 1 ); return currentDate; } }; diff --git a/src/main/java/de/kah2/zodiac/libZodiac/Day.java b/src/main/java/de/kah2/zodiac/libZodiac/Day.java index 44f66ac..fe0f0a0 100644 --- a/src/main/java/de/kah2/zodiac/libZodiac/Day.java +++ b/src/main/java/de/kah2/zodiac/libZodiac/Day.java @@ -2,8 +2,6 @@ import de.kah2.zodiac.libZodiac.planetary.PlanetaryDayData; import de.kah2.zodiac.libZodiac.zodiac.ZodiacDayData; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.time.LocalDate; @@ -16,8 +14,6 @@ */ public class Day implements Comparable{ - private final Logger log = LoggerFactory.getLogger(this.getClass()); - private final LocalDate date; private final PlanetaryDayData planetaryData; diff --git a/src/main/java/de/kah2/zodiac/libZodiac/DayStorableDataSet.java b/src/main/java/de/kah2/zodiac/libZodiac/DayStorableDataSet.java index e84e0e8..746bbc7 100644 --- a/src/main/java/de/kah2/zodiac/libZodiac/DayStorableDataSet.java +++ b/src/main/java/de/kah2/zodiac/libZodiac/DayStorableDataSet.java @@ -1,10 +1,10 @@ package de.kah2.zodiac.libZodiac; -import java.time.LocalDate; - import de.kah2.zodiac.libZodiac.planetary.PlanetaryDayData; import de.kah2.zodiac.libZodiac.planetary.ZonedRiseSet; +import java.time.LocalDate; + /** * This class contains a basic set of essential data defining a {@link Day} and * its properties. This class is intended to be extended with functionality for diff --git a/src/main/java/de/kah2/zodiac/libZodiac/LocationProvider.java b/src/main/java/de/kah2/zodiac/libZodiac/LocationProvider.java index 7c88097..9caf1b3 100644 --- a/src/main/java/de/kah2/zodiac/libZodiac/LocationProvider.java +++ b/src/main/java/de/kah2/zodiac/libZodiac/LocationProvider.java @@ -1,9 +1,9 @@ package de.kah2.zodiac.libZodiac; -import java.time.ZoneId; - import de.kah2.zodiac.libZodiac.planetary.Position; +import java.time.ZoneId; + /** * This interface allows us to stub the functionality {@link Calendar} provides * for {@link Day} objects. diff --git a/src/main/java/de/kah2/zodiac/libZodiac/ProgressManager.java b/src/main/java/de/kah2/zodiac/libZodiac/ProgressManager.java index dabd35d..bc00421 100644 --- a/src/main/java/de/kah2/zodiac/libZodiac/ProgressManager.java +++ b/src/main/java/de/kah2/zodiac/libZodiac/ProgressManager.java @@ -91,7 +91,7 @@ private void addEstimatedExtensions(final int count) { */ void notifyStateChanged(final ProgressListener.State state) { - this.log.debug(">>>>>>>> State changes to " + state); + this.log.debug( ">>>>>>>> State changes to {}", state ); if (this.state != state) { @@ -122,7 +122,7 @@ void notifyDayCreated() { this.daysGenerated++; this.updatePercentageOnCalculationProgress(); - this.log.debug("Day created, progress is " + this.percentGenerated); + this.log.debug( "Day created, progress is {}", this.percentGenerated ); for (ProgressListener listener : this.progressListeners) { listener.onCalculationProgress(this.percentGenerated); diff --git a/src/main/java/de/kah2/zodiac/libZodiac/interpretation/Gardening.java b/src/main/java/de/kah2/zodiac/libZodiac/interpretation/Gardening.java index f5dd3f6..d604370 100644 --- a/src/main/java/de/kah2/zodiac/libZodiac/interpretation/Gardening.java +++ b/src/main/java/de/kah2/zodiac/libZodiac/interpretation/Gardening.java @@ -6,18 +6,8 @@ import static de.kah2.zodiac.libZodiac.zodiac.ZodiacDirection.ASCENDING; import static de.kah2.zodiac.libZodiac.zodiac.ZodiacDirection.DESCENDING; -import static de.kah2.zodiac.libZodiac.zodiac.ZodiacElement.PlantPart.FLOWER; -import static de.kah2.zodiac.libZodiac.zodiac.ZodiacElement.PlantPart.FRUIT; -import static de.kah2.zodiac.libZodiac.zodiac.ZodiacElement.PlantPart.LEAF; -import static de.kah2.zodiac.libZodiac.zodiac.ZodiacElement.PlantPart.ROOT; -import static de.kah2.zodiac.libZodiac.zodiac.ZodiacSign.ARIES; -import static de.kah2.zodiac.libZodiac.zodiac.ZodiacSign.CANCER; -import static de.kah2.zodiac.libZodiac.zodiac.ZodiacSign.CAPRICORN; -import static de.kah2.zodiac.libZodiac.zodiac.ZodiacSign.GEMINI; -import static de.kah2.zodiac.libZodiac.zodiac.ZodiacSign.LEO; -import static de.kah2.zodiac.libZodiac.zodiac.ZodiacSign.SAGITTARIUS; -import static de.kah2.zodiac.libZodiac.zodiac.ZodiacSign.SCORPIO; -import static de.kah2.zodiac.libZodiac.zodiac.ZodiacSign.VIRGO; +import static de.kah2.zodiac.libZodiac.zodiac.ZodiacElement.PlantPart.*; +import static de.kah2.zodiac.libZodiac.zodiac.ZodiacSign.*; /** *

This class containes {@link Interpreter}s for basic gardening activities. As source I used the book @@ -135,7 +125,7 @@ protected Quality doInterpretation() { * Mow the lawn - Rasen mähen * Source: 151, 155, 159 */ - public static class MowLawnInterpreter extends Interpreter { + public static class MowLawnInterpreter extends Interpreter { @Override protected Quality doInterpretation() { @@ -157,7 +147,7 @@ protected Quality doInterpretation() { * Water plants - Gießen * Source: 120 */ - public static class WaterInterpreter extends Interpreter { + public static class WaterInterpreter extends Interpreter { @Override protected Quality doInterpretation() { @@ -215,7 +205,7 @@ protected Quality doInterpretation() { * Make cuttings / transplant - Stecklinge schneiden / Pflanzen umsetzen * Source: 123 */ - public static class CuttingTransplantInterpreter extends Interpreter { + public static class CuttingTransplantInterpreter extends Interpreter { @Override protected Quality doInterpretation() { @@ -236,7 +226,7 @@ protected Quality doInterpretation() { * Graft - Veredeln * Source: 135 */ - public static class GraftInterpreter extends Interpreter { + public static class GraftInterpreter extends Interpreter { @Override protected Quality doInterpretation() { diff --git a/src/main/java/de/kah2/zodiac/libZodiac/planetary/PlanetaryDayData.java b/src/main/java/de/kah2/zodiac/libZodiac/planetary/PlanetaryDayData.java index 24ec14d..e0c47e4 100644 --- a/src/main/java/de/kah2/zodiac/libZodiac/planetary/PlanetaryDayData.java +++ b/src/main/java/de/kah2/zodiac/libZodiac/planetary/PlanetaryDayData.java @@ -1,12 +1,5 @@ package de.kah2.zodiac.libZodiac.planetary; -import java.time.Instant; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.ZoneId; -import java.time.ZonedDateTime; - import de.kah2.zodiac.libZodiac.Calendar; import de.kah2.zodiac.libZodiac.Day; import de.kah2.zodiac.libZodiac.DayStorableDataSet; @@ -18,6 +11,8 @@ import de.kah2.zodiac.nova4jmt.solarsystem.Lunar; import de.kah2.zodiac.nova4jmt.solarsystem.Solar; +import java.time.*; + /** *

* This class calculates basic planetary data based on novaforjava framework. diff --git a/src/main/java/de/kah2/zodiac/libZodiac/zodiac/ZodiacDirection.java b/src/main/java/de/kah2/zodiac/libZodiac/zodiac/ZodiacDirection.java index 71fb8f8..e46a2c2 100644 --- a/src/main/java/de/kah2/zodiac/libZodiac/zodiac/ZodiacDirection.java +++ b/src/main/java/de/kah2/zodiac/libZodiac/zodiac/ZodiacDirection.java @@ -3,10 +3,6 @@ /** * This class represents the lunar direction within the zodiac circle. * - * TODO_later allow different interpretations of zodiac directions? - * + https://www.astro.com/astrowiki/de/Maria_Thun#Aufsteigender_und_absteigender_Mond - * + https://www.astro.com/astrowiki/de/Siderischer_Tierkreis - * * @author kahles */ public enum ZodiacDirection { @@ -15,30 +11,14 @@ public enum ZodiacDirection { public static ZodiacDirection of(final ZodiacSign zodiacSign) { - ZodiacDirection direction = null; - - switch (zodiacSign) { + ZodiacDirection direction = switch ( zodiacSign ) { // Moon is ascending from capricorn to taurus - case CAPRICORN: - case AQUARIUS: - case PISCES: - case ARIES: - case TAURUS: - case GEMINI: - direction = ASCENDING; - break; + case CAPRICORN, AQUARIUS, PISCES, ARIES, TAURUS, GEMINI -> ASCENDING; // Moon is descending from cancer to scorpio - case CANCER: - case LEO: - case VIRGO: - case LIBRA: - case SCORPIO: - case SAGITTARIUS: - direction = DESCENDING; - break; - } + case CANCER, LEO, VIRGO, LIBRA, SCORPIO, SAGITTARIUS -> DESCENDING; + }; return direction; } diff --git a/src/main/java/de/kah2/zodiac/libZodiac/zodiac/ZodiacElement.java b/src/main/java/de/kah2/zodiac/libZodiac/zodiac/ZodiacElement.java index a7888fa..52e6bd7 100644 --- a/src/main/java/de/kah2/zodiac/libZodiac/zodiac/ZodiacElement.java +++ b/src/main/java/de/kah2/zodiac/libZodiac/zodiac/ZodiacElement.java @@ -37,29 +37,12 @@ public enum FoodElement { } public static ZodiacElement of(final ZodiacSign sign) { - ZodiacElement type = null; - switch (sign) { - case ARIES: - case LEO: - case SAGITTARIUS: - type = FIRE; - break; - case CAPRICORN: - case TAURUS: - case VIRGO: - type = EARTH; - break; - case PISCES: - case CANCER: - case SCORPIO: - type = WATER; - break; - case AQUARIUS: - case GEMINI: - case LIBRA: - type = AIR; - break; - } + ZodiacElement type = switch ( sign ) { + case ARIES, LEO, SAGITTARIUS -> FIRE; + case CAPRICORN, TAURUS, VIRGO -> EARTH; + case PISCES, CANCER, SCORPIO -> WATER; + case AQUARIUS, GEMINI, LIBRA -> AIR; + }; return type; } diff --git a/src/main/java/de/kah2/zodiac/libZodiac/zodiac/ZodiacSign.java b/src/main/java/de/kah2/zodiac/libZodiac/zodiac/ZodiacSign.java index 2cd8a56..cede84b 100644 --- a/src/main/java/de/kah2/zodiac/libZodiac/zodiac/ZodiacSign.java +++ b/src/main/java/de/kah2/zodiac/libZodiac/zodiac/ZodiacSign.java @@ -51,46 +51,33 @@ public static ZodiacSign of(final double lunarLongitude) throws IllegalArgumentE } final int zodiacSignId = (int) lunarLongitude / 30; - ZodiacSign sign = null; - - switch (zodiacSignId) { - case 0: // 0 - 30 - sign = ARIES; - break; - case 1: // 30 - 60 - sign = TAURUS; - break; - case 2: // 60 - 90 - sign = GEMINI; - break; - case 3: // 90 - 120 - sign = CANCER; - break; - case 4: // 120 - 150 - sign = LEO; - break; - case 5: // 150 - 180 - sign = VIRGO; - break; - case 6: // 180 - 210 - sign = LIBRA; - break; - case 7: // 210 - 240 - sign = SCORPIO; - break; - case 8: // 240 - 270 - sign = SAGITTARIUS; - break; - case 9: // 270 - 300 - sign = CAPRICORN; - break; - case 10: // 300 - 330 - sign = AQUARIUS; - break; - case 11: // 330 - 360 - sign = PISCES; - break; - } + ZodiacSign sign = switch ( zodiacSignId ) { + case 0 -> // 0 - 30 + ARIES; + case 1 -> // 30 - 60 + TAURUS; + case 2 -> // 60 - 90 + GEMINI; + case 3 -> // 90 - 120 + CANCER; + case 4 -> // 120 - 150 + LEO; + case 5 -> // 150 - 180 + VIRGO; + case 6 -> // 180 - 210 + LIBRA; + case 7 -> // 210 - 240 + SCORPIO; + case 8 -> // 240 - 270 + SAGITTARIUS; + case 9 -> // 270 - 300 + CAPRICORN; + case 10 -> // 300 - 330 + AQUARIUS; + case 11 -> // 330 - 360 + PISCES; + default -> null; + }; return sign; } diff --git a/src/test/java/de/kah2/zodiac/libZodiac/Benchmark.java b/src/test/java/de/kah2/zodiac/libZodiac/Benchmark.java index 75234ed..606aec9 100644 --- a/src/test/java/de/kah2/zodiac/libZodiac/Benchmark.java +++ b/src/test/java/de/kah2/zodiac/libZodiac/Benchmark.java @@ -1,9 +1,10 @@ package de.kah2.zodiac.libZodiac; +import de.kah2.zodiac.libZodiac.Calendar.Scope; + import java.time.Duration; import java.time.Instant; import java.time.LocalDate; -import de.kah2.zodiac.libZodiac.Calendar.Scope; /** * This class is used to measure generation time of a {@link Calendar}. diff --git a/src/test/java/de/kah2/zodiac/libZodiac/CalendarGeneratorStub.java b/src/test/java/de/kah2/zodiac/libZodiac/CalendarGeneratorStub.java index 9b297ba..260c3bf 100644 --- a/src/test/java/de/kah2/zodiac/libZodiac/CalendarGeneratorStub.java +++ b/src/test/java/de/kah2/zodiac/libZodiac/CalendarGeneratorStub.java @@ -1,11 +1,11 @@ package de.kah2.zodiac.libZodiac; -import java.time.LocalDate; -import java.util.LinkedList; - import de.kah2.zodiac.libZodiac.planetary.PlanetaryDayData; import de.kah2.zodiac.libZodiac.planetary.PlanetaryDayDataStub; +import java.time.LocalDate; +import java.util.LinkedList; + /** * This is a stub of {@link CalendarGenerator}, which overrides Day creation to allow * stubbing of novaforjava. diff --git a/src/test/java/de/kah2/zodiac/libZodiac/CalendarGeneratorTest.java b/src/test/java/de/kah2/zodiac/libZodiac/CalendarGeneratorTest.java index b73e790..796dd2a 100644 --- a/src/test/java/de/kah2/zodiac/libZodiac/CalendarGeneratorTest.java +++ b/src/test/java/de/kah2/zodiac/libZodiac/CalendarGeneratorTest.java @@ -13,10 +13,7 @@ public class CalendarGeneratorTest { - static { -// Uncomment to have detailed output: -// TestConstantsAndHelpers.enableLogging("trace"); - } + private static final Logger log = LoggerFactory.getLogger( CalendarGeneratorTest.class ); @Test public void testGenerateScopeDay() { @@ -180,7 +177,7 @@ private void extendAndWait(CalendarGenerator generator) { Thread.sleep(100); } catch (InterruptedException e) { - e.printStackTrace(); + log.error( "Interrupted", e ); } } diff --git a/src/test/java/de/kah2/zodiac/libZodiac/CalendarTest.java b/src/test/java/de/kah2/zodiac/libZodiac/CalendarTest.java index 233e636..fb14c61 100644 --- a/src/test/java/de/kah2/zodiac/libZodiac/CalendarTest.java +++ b/src/test/java/de/kah2/zodiac/libZodiac/CalendarTest.java @@ -11,11 +11,6 @@ public class CalendarTest { - static { - // Uncomment to have detailed output - // TestConstantsAndHelpers.enableLogging("trace"); - } - @Test public void testGetValidDaysReturnsRightRangeForScopeDay() { @@ -165,23 +160,15 @@ public void testRemoveOverhead() { private void testRemoveOverheadRemovesNothing(final Calendar.Scope scope) { final DateRange initialRange = new DateRange( TestConstantsAndHelpers.SOME_DATE, TestConstantsAndHelpers.SOME_DATE.plusDays(2)); - DateRange rangeToGenerate=null; - - switch (scope) { - case DAY: - rangeToGenerate = initialRange; - break; - case PHASE: - rangeToGenerate = new DateRange( - initialRange.getStart().minusDays(1), - initialRange.getEnd().plusDays(1) ); - break; - case CYCLE: - rangeToGenerate = new DateRange( - TestConstantsAndHelpers.SOME_DATES_LAST_EXTREME.minusDays(1), - TestConstantsAndHelpers.SOME_DATES_NEXT_EXTREME.plusDays(1) ); - break; - } + DateRange rangeToGenerate = switch ( scope ) { + case DAY -> initialRange; + case PHASE -> new DateRange( + initialRange.getStart().minusDays( 1 ), + initialRange.getEnd().plusDays( 1 ) ); + case CYCLE -> new DateRange( + TestConstantsAndHelpers.SOME_DATES_LAST_EXTREME.minusDays( 1 ), + TestConstantsAndHelpers.SOME_DATES_NEXT_EXTREME.plusDays( 1 ) ); + }; final Calendar calendar = new CalendarStub(initialRange, scope); diff --git a/src/test/java/de/kah2/zodiac/libZodiac/TestConstantsAndHelpers.java b/src/test/java/de/kah2/zodiac/libZodiac/TestConstantsAndHelpers.java index 00dafc6..e36aeef 100644 --- a/src/test/java/de/kah2/zodiac/libZodiac/TestConstantsAndHelpers.java +++ b/src/test/java/de/kah2/zodiac/libZodiac/TestConstantsAndHelpers.java @@ -1,16 +1,16 @@ package de.kah2.zodiac.libZodiac; -import java.time.LocalDate; -import java.time.ZoneId; - +import de.kah2.zodiac.libZodiac.planetary.Position; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.slf4j.impl.SimpleLogger; -import de.kah2.zodiac.libZodiac.planetary.Position; +import java.time.LocalDate; +import java.time.ZoneId; public class TestConstantsAndHelpers { + private final static Logger LOG = LoggerFactory.getLogger(TestConstantsAndHelpers.class); + public final static Position POSITION_MUNICH = new Position(48.137, 11.57521); public final static LocalDate SOME_DATE = LocalDate.of(2016, 9, 3); @@ -38,22 +38,9 @@ public Position getObserverPosition() { private final static int sleepStepMs = 1000; - /** - * Enables and configures {@link SimpleLogger}. - */ - public static void enableLogging(final String level) { - System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, level); - System.setProperty(org.slf4j.impl.SimpleLogger.SHOW_THREAD_NAME_KEY, "true"); - System.setProperty(org.slf4j.impl.SimpleLogger.SHOW_DATE_TIME_KEY, "true"); - System.setProperty(org.slf4j.impl.SimpleLogger.DATE_TIME_FORMAT_KEY, "HH:mm:ss:SSS"); - System.setProperty(org.slf4j.impl.SimpleLogger.SHOW_SHORT_LOG_NAME_KEY, "true"); - } - /** Simple method to generate a {@link Calendar} and wait for results. */ public static void generateAndWaitFor(CalendarGenerator generator) { - final Logger log = LoggerFactory.getLogger("TestConstantsAndHelpers#generateAndWaitFor"); - final LastStateProgressListener listener = new LastStateProgressListener(); generator.getProgressManager().addProgressListener(listener); @@ -62,11 +49,11 @@ public static void generateAndWaitFor(CalendarGenerator generator) { while (listener.getLastState() != ProgressListener.State.FINISHED) { try { - log.trace("waiting " + sleepStepMs + "ms for state change to FINISHED"); + LOG.trace("waiting " + sleepStepMs + "ms for state change to FINISHED"); Thread.sleep(sleepStepMs); } catch (InterruptedException e) { - e.printStackTrace(); + LOG.error("Interrupted", e); } } @@ -96,14 +83,14 @@ public static boolean generateAndWaitFor(CalendarGenerator generator, int maxWai if (msWaited > maxWait) { int remaining = generator.getExecutor().shutdownNow().size(); - LoggerFactory.getLogger("TestConstantsAndHelpers#generateAndWaitFor(CalendarGenerator,int)") - .trace("Terminated executor - " + remaining + " jobs were still waiting"); + LoggerFactory.getLogger( "TestConstantsAndHelpers#generateAndWaitFor(CalendarGenerator,int)" ) + .trace( "Terminated executor - {} jobs were still waiting", remaining ); exitedNormally = false; break; } } catch (InterruptedException e) { - e.printStackTrace(); + LOG.error("Interrupted", e); } } @@ -126,7 +113,7 @@ public static void generateAndWaitFor(Calendar calendar) { Thread.sleep(sleepStepMs); } catch (InterruptedException e) { - e.printStackTrace(); + LOG.error("Interrupted", e); } } diff --git a/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarDataStringBuilder.java b/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarDataStringBuilder.java index 65ff86a..27ad3e3 100644 --- a/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarDataStringBuilder.java +++ b/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarDataStringBuilder.java @@ -67,7 +67,7 @@ public void appendZodiacData(final ZodiacDayData data) { this.appendLine("\tFood element:\t\t" + data.getElement().getFoodElement()); } - public void appendInterpretation( Interpreter interpreter) { + public void appendInterpretation( Interpreter interpreter) { final String[] annotations = interpreter.getAnnotationsAsStringArray(); diff --git a/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarExampleSimple.java b/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarExampleSimple.java index df2b177..ecb5659 100644 --- a/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarExampleSimple.java +++ b/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarExampleSimple.java @@ -19,17 +19,12 @@ */ public class CalendarExampleSimple { - static { - // Uncomment to have detailed output: - // TestConstantsAndHelpers.enableLogging("trace"); - } - private final static Logger LOG = LoggerFactory.getLogger(CalendarExampleSimple.class); /** * Runs the example. */ - public static void run(Class interpreterClass, LocalDate startDate, int days) + public static void run(Class > interpreterClass, LocalDate startDate, int days) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { /* First step: create a Calendar */ @@ -53,7 +48,7 @@ public static void run(Class interpreterClass, LocalDate * for larger Calendars - so this could be run by a background thread */ - LOG.info("Generating Calendar for DateRange: " + range); + LOG.info( "Generating Calendar for DateRange: {}", range ); TestConstantsAndHelpers.generateAndWaitFor(calendar); final CalendarDataStringBuilder builder = new CalendarDataStringBuilder(); @@ -70,7 +65,7 @@ public static void run(Class interpreterClass, LocalDate builder.appendPlanetaryData(day.getPlanetaryData()); builder.appendZodiacData(day.getZodiacData()); - final Interpreter interpreter = interpreterClass.getDeclaredConstructor().newInstance(); + final Interpreter interpreter = interpreterClass.getDeclaredConstructor().newInstance(); interpreter.setDayAndInterpret(day); builder.appendInterpretation(interpreter); @@ -81,7 +76,7 @@ public static void run(Class interpreterClass, LocalDate builder.appendLine(""); } - LOG.info("Result:\n" + builder.toString()); + LOG.info( "Result:\n{}", builder ); } public static void main(final String[] args) { @@ -90,7 +85,7 @@ public static void main(final String[] args) { } catch (Exception e) { // This shouldn't happen - e.printStackTrace(); + LOG.error( "Unexpcected error", e ); } } } diff --git a/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarExampleStorage.java b/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarExampleStorage.java index 6f89f0e..473f4c8 100644 --- a/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarExampleStorage.java +++ b/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarExampleStorage.java @@ -1,18 +1,12 @@ package de.kah2.zodiac.libZodiac.example; -import de.kah2.zodiac.libZodiac.Calendar; -import de.kah2.zodiac.libZodiac.CalendarStub; -import de.kah2.zodiac.libZodiac.DateRange; -import de.kah2.zodiac.libZodiac.Day; -import de.kah2.zodiac.libZodiac.DayStorableDataSet; -import de.kah2.zodiac.libZodiac.TestConstantsAndHelpers; +import de.kah2.zodiac.libZodiac.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.time.LocalDate; import java.util.LinkedList; import java.util.List; -import java.util.stream.Collectors; /** * This class shows how to create, store, load and extend a {@link Calendar}. @@ -21,11 +15,6 @@ */ public class CalendarExampleStorage { - static { - // Uncomment to have detailed output: - // TestConstantsAndHelpers.enableLogging(); - } - private final static Logger LOG = LoggerFactory.getLogger(CalendarExampleStorage.class); /** @@ -48,17 +37,20 @@ public static void run() { Calendar calendar = new CalendarStub(range, scope); // For this example we use a List as database - final List fakeDatabase = new LinkedList<>(); - CalendarExampleStorage.LOG.info("Generating Calendar for DateRange: " + range); + CalendarExampleStorage.LOG.info( "Generating Calendar for DateRange: {}", range ); TestConstantsAndHelpers.generateAndWaitFor(calendar); LinkedList generated = calendar.getNewlyGenerated(); - CalendarExampleStorage.LOG.info("=> Data generated: " + generated.getFirst().getDate() + " -> " + generated.getLast().getDate()); + CalendarExampleStorage.LOG.info( "=> Data generated: {} -> {}", + generated.getFirst().getDate(), + generated.getLast().getDate() ); // Here we can store the newly created days for next time ... // Have a look at DayStorableDataSet, which is intended to be // extended with serialization methods. - fakeDatabase.addAll(generated.stream().map(DayStorableDataSet::new).collect(Collectors.toList())); + final List fakeDatabase = new java.util.ArrayList<>( generated.stream() + .map( DayStorableDataSet::new ) + .toList() ); // Display the results to the user and the user closes our application // afterwards. @@ -75,16 +67,20 @@ public static void run() { calendar.importDays(fakeDatabase); LinkedList days = calendar.getAllDays(); - CalendarExampleStorage.LOG.info("Data loaded: " + days.getFirst().getDate() + " -> " + days.getLast().getDate()); + CalendarExampleStorage.LOG.info( "Data loaded: {} -> {}", days.getFirst().getDate(), days.getLast().getDate() ); // And generate the new ones: - CalendarExampleStorage.LOG.info("Generating Calendar for DateRange: " + range); + CalendarExampleStorage.LOG.info( "Generating Calendar for DateRange: {}", range ); TestConstantsAndHelpers.generateAndWaitFor(calendar); generated = calendar.getNewlyGenerated(); - CalendarExampleStorage.LOG.info("=> Data generated: " + generated.getFirst().getDate() + " -> " + generated.getLast().getDate()); + CalendarExampleStorage.LOG.info( "=> Data generated: {} -> {}", + generated.getFirst().getDate(), + generated.getLast().getDate() ); // Store the newly created ... - fakeDatabase.addAll(generated.stream().map(DayStorableDataSet::new).collect(Collectors.toList())); + fakeDatabase.addAll( generated.stream() + .map(DayStorableDataSet::new) + .toList() ); // Display the results and close the application // (...) @@ -100,18 +96,18 @@ public static void run() { calendar.importDays(fakeDatabase); days = calendar.getAllDays(); - CalendarExampleStorage.LOG.info("Data loaded: " + days.getFirst().getDate() + " -> " + days.getLast().getDate()); + CalendarExampleStorage.LOG.info( "Data loaded: {} -> {}", days.getFirst().getDate(), days.getLast().getDate() ); - CalendarExampleStorage.LOG.info("Generating Calendar for DateRange: " + range); + CalendarExampleStorage.LOG.info( "Generating Calendar for DateRange: {}", range ); TestConstantsAndHelpers.generateAndWaitFor(calendar); generated = calendar.getNewlyGenerated(); - CalendarExampleStorage.LOG.info("=> Data generated: " + generated.getFirst().getDate() + " -> " + generated.getLast().getDate()); + CalendarExampleStorage.LOG.info( "=> Data generated: {} -> {}", generated.getFirst().getDate(), generated.getLast().getDate() ); // Now we have an inconsistent Calendar containing a gap and should: // Remove past (or all) days that aren't needed anymore ... final LinkedList removed = calendar.removeOverhead(false); - CalendarExampleStorage.LOG.info("Removed overhead: " + removed.getFirst().getDate() + " -> " + removed.getLast().getDate()); + CalendarExampleStorage.LOG.info( "Removed overhead: {} -> {}", removed.getFirst().getDate(), removed.getLast().getDate() ); // OR correct the expectedRange to include imported days ... // calendar.fixRangeExpectedToIncludeExistingDays(); diff --git a/src/test/java/de/kah2/zodiac/libZodiac/example/ProgressListenerExample.java b/src/test/java/de/kah2/zodiac/libZodiac/example/ProgressListenerExample.java index b0c8c83..982fec5 100644 --- a/src/test/java/de/kah2/zodiac/libZodiac/example/ProgressListenerExample.java +++ b/src/test/java/de/kah2/zodiac/libZodiac/example/ProgressListenerExample.java @@ -1,9 +1,9 @@ package de.kah2.zodiac.libZodiac.example; -import java.time.LocalDate; - -import de.kah2.zodiac.libZodiac.planetary.LunarPhase; import de.kah2.zodiac.libZodiac.*; +import de.kah2.zodiac.libZodiac.planetary.LunarPhase; + +import java.time.LocalDate; /** * This example shows how to implement and use a {@link ProgressListener} and @@ -14,11 +14,6 @@ */ public class ProgressListenerExample { - static { - // Uncomment to have detailed output: - // TestConstantsAndHelpers.enableLogging(); - } - static class SysOutProgressListener implements ProgressListener { @Override diff --git a/src/test/java/de/kah2/zodiac/libZodiac/interpretation/InterpreterTest.java b/src/test/java/de/kah2/zodiac/libZodiac/interpretation/InterpreterTest.java index c892c9f..6b622c1 100644 --- a/src/test/java/de/kah2/zodiac/libZodiac/interpretation/InterpreterTest.java +++ b/src/test/java/de/kah2/zodiac/libZodiac/interpretation/InterpreterTest.java @@ -20,7 +20,7 @@ public void testSetQuality() { boolean threwRightException = false; - Interpreter testInterpreter = createInterpreterStub(null); + Interpreter testInterpreter = createInterpreterStub(null); try { @@ -100,7 +100,7 @@ public void testGetAnnotationsAsStringArray() { private static Interpreter createInterpreterStub(Interpreter.Quality expectedQuality) { - return new Interpreter() { + return new Interpreter<>() { @Override protected Quality doInterpretation() { diff --git a/src/test/java/de/kah2/zodiac/libZodiac/planetary/PlanetaryDayDataStub.java b/src/test/java/de/kah2/zodiac/libZodiac/planetary/PlanetaryDayDataStub.java index dbf957c..bf18c67 100644 --- a/src/test/java/de/kah2/zodiac/libZodiac/planetary/PlanetaryDayDataStub.java +++ b/src/test/java/de/kah2/zodiac/libZodiac/planetary/PlanetaryDayDataStub.java @@ -1,13 +1,13 @@ package de.kah2.zodiac.libZodiac.planetary; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; - import de.kah2.zodiac.libZodiac.LocationProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; + /** * This class simulates basic planetary data. * @@ -58,7 +58,7 @@ private static double getFakeLunarVisibility(LocalDate date) { double visibility = (double) count / HALF_CYCLE_LENGTH; - LOG.debug(date + " visibility: " + visibility); + LOG.debug( "{} visibility: {}", date, visibility ); return visibility; } diff --git a/src/test/java/de/kah2/zodiac/libZodiac/planetary/PositionTest.java b/src/test/java/de/kah2/zodiac/libZodiac/planetary/PositionTest.java index ce18ec3..7d45713 100644 --- a/src/test/java/de/kah2/zodiac/libZodiac/planetary/PositionTest.java +++ b/src/test/java/de/kah2/zodiac/libZodiac/planetary/PositionTest.java @@ -32,7 +32,7 @@ private void checkValidation(boolean isValidLat, double lat, boolean isValidLng, try { - final Position pos = new Position(lat, lng); + new Position(lat, lng); assertThat(isValid).as("Construction of position should NOT throw exception").isTrue(); From c1a90d64fa136b7fb1e3aff2de98851e6f78495d Mon Sep 17 00:00:00 2001 From: kahles Date: Mon, 6 May 2024 20:32:09 +0200 Subject: [PATCH 2/3] avoid zone-agnostic LocalDateTime --- .../de/kah2/zodiac/libZodiac/Calendar.java | 1 + .../zodiac/libZodiac/LocationProvider.java | 1 + .../libZodiac/planetary/PlanetaryDayData.java | 13 ++++++------ .../libZodiac/planetary/ZonedRiseSet.java | 19 +++++++++-------- .../planetary/PlanetaryDayDataStub.java | 21 ++++++++++++------- 5 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/main/java/de/kah2/zodiac/libZodiac/Calendar.java b/src/main/java/de/kah2/zodiac/libZodiac/Calendar.java index fd04123..fc77a23 100644 --- a/src/main/java/de/kah2/zodiac/libZodiac/Calendar.java +++ b/src/main/java/de/kah2/zodiac/libZodiac/Calendar.java @@ -20,6 +20,7 @@ * see de.kah2.libZodiac.example * @author kahles */ +// TODO externalize LocationProvider public class Calendar implements LocationProvider { private final Logger log = LoggerFactory.getLogger(this.getClass()); diff --git a/src/main/java/de/kah2/zodiac/libZodiac/LocationProvider.java b/src/main/java/de/kah2/zodiac/libZodiac/LocationProvider.java index 9caf1b3..17ebe37 100644 --- a/src/main/java/de/kah2/zodiac/libZodiac/LocationProvider.java +++ b/src/main/java/de/kah2/zodiac/libZodiac/LocationProvider.java @@ -14,5 +14,6 @@ public interface LocationProvider { ZoneId getTimeZoneId(); + // TODO add missing docs Position getObserverPosition(); } diff --git a/src/main/java/de/kah2/zodiac/libZodiac/planetary/PlanetaryDayData.java b/src/main/java/de/kah2/zodiac/libZodiac/planetary/PlanetaryDayData.java index e0c47e4..df560af 100644 --- a/src/main/java/de/kah2/zodiac/libZodiac/planetary/PlanetaryDayData.java +++ b/src/main/java/de/kah2/zodiac/libZodiac/planetary/PlanetaryDayData.java @@ -100,19 +100,18 @@ public static PlanetaryDayData calculateFor(final LocalDate date, final Location } private void calculateJulianDateAtDayStart(final LocalDate date, final ZoneId zoneId) { - final LocalDateTime dayStart = date.atStartOfDay(); - this.julianDateAtDayStart = localDateToJulianDate(dayStart, zoneId); + final ZonedDateTime zonedDayStart = ZonedDateTime.of( date.atStartOfDay(), zoneId ); + this.julianDateAtDayStart = zonedDateToJulianDate( zonedDayStart ); } private void calculateJulianDateAtNoon(final LocalDate date, final ZoneId zoneId) { - final LocalDateTime noon = LocalDateTime.of(date, LocalTime.NOON); - this.julianDateAtNoon = localDateToJulianDate(noon, zoneId); + final ZonedDateTime zonedNoon = ZonedDateTime.of( date, LocalTime.NOON, zoneId ); + this.julianDateAtNoon = zonedDateToJulianDate( zonedNoon ); } - private static double localDateToJulianDate(final LocalDateTime date, final ZoneId zoneId) { + private static double zonedDateToJulianDate(final ZonedDateTime date) { - final ZonedDateTime localZonedDateTime = ZonedDateTime.of(date, zoneId); - final Instant utcDate = localZonedDateTime.withZoneSameInstant(PlanetaryDayData.TIME_ZONE_ID_LIBNOVA) + final Instant utcDate = date.withZoneSameInstant(PlanetaryDayData.TIME_ZONE_ID_LIBNOVA) .toInstant(); final LnDate lnDate = new LnDate(); diff --git a/src/main/java/de/kah2/zodiac/libZodiac/planetary/ZonedRiseSet.java b/src/main/java/de/kah2/zodiac/libZodiac/planetary/ZonedRiseSet.java index 339f50a..16dd063 100644 --- a/src/main/java/de/kah2/zodiac/libZodiac/planetary/ZonedRiseSet.java +++ b/src/main/java/de/kah2/zodiac/libZodiac/planetary/ZonedRiseSet.java @@ -4,6 +4,7 @@ import de.kah2.zodiac.nova4jmt.api.LnDate; import de.kah2.zodiac.nova4jmt.api.LnRstTime; +import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; @@ -16,7 +17,7 @@ */ public class ZonedRiseSet { - private final LocalDateTime rise, set; + private final Instant rise, set; private ZoneId localTimeZone; /** @@ -27,31 +28,31 @@ public class ZonedRiseSet { ZonedRiseSet(final LnRstTime lnRstTime, final ZoneId localTimeZone) { this.localTimeZone = localTimeZone; - this.rise = this.getLocalDateTime(lnRstTime.rise); - this.set = this.getLocalDateTime(lnRstTime.set); + this.rise = this.getInstantFromJulian(lnRstTime.rise); + this.set = this.getInstantFromJulian(lnRstTime.set); } /** - * This constructor instantiates this class based on {@link LocalDateTime}. + * This constructor instantiates this class based on {@link Instant}s. * No time zone transformations will be done. * Only needed for testing purposes. * @param rise an UTC rise time * @param set an UTC set time */ - public ZonedRiseSet(final LocalDateTime rise, final LocalDateTime set) { + public ZonedRiseSet(final Instant rise, final Instant set) { this.rise = rise; this.set = set; } - public LocalDateTime getRise() { + public Instant getRise() { return this.rise; } - public LocalDateTime getSet() { + public Instant getSet() { return this.set; } - private LocalDateTime getLocalDateTime(final double julianDay) { + private Instant getInstantFromJulian(final double julianDay) { final LnDate date = new LnDate(); JulianDay.ln_get_date(julianDay, date); @@ -59,7 +60,7 @@ private LocalDateTime getLocalDateTime(final double julianDay) { final ZonedDateTime utcZonedTime = LocalDateTime .of(date.years, date.months, date.days, date.hours, date.minutes) .atZone(PlanetaryDayData.TIME_ZONE_ID_LIBNOVA); - return utcZonedTime.withZoneSameInstant(this.localTimeZone).toLocalDateTime(); + return utcZonedTime.withZoneSameInstant(this.localTimeZone).toInstant(); } @Override diff --git a/src/test/java/de/kah2/zodiac/libZodiac/planetary/PlanetaryDayDataStub.java b/src/test/java/de/kah2/zodiac/libZodiac/planetary/PlanetaryDayDataStub.java index bf18c67..afd805b 100644 --- a/src/test/java/de/kah2/zodiac/libZodiac/planetary/PlanetaryDayDataStub.java +++ b/src/test/java/de/kah2/zodiac/libZodiac/planetary/PlanetaryDayDataStub.java @@ -5,8 +5,6 @@ import org.slf4j.LoggerFactory; import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; /** * This class simulates basic planetary data. @@ -32,12 +30,19 @@ public static PlanetaryDayData calculateFor(final LocalDate date, final Location data.setLunarVisibility( getFakeLunarVisibility(date) ); - data.setLunarRiseSet( new ZonedRiseSet( - LocalDateTime.of( date, LocalTime.of(10, 0) ), - LocalDateTime.of( date, LocalTime.of(15,0) ) ) ); - data.setSolarRiseSet( new ZonedRiseSet( - LocalDateTime.of( date, LocalTime.of(7, 0) ), - LocalDateTime.of( date, LocalTime.of(19,0) ) ) ); + data.setLunarRiseSet( + new ZonedRiseSet( + date.atStartOfDay( locationProvider.getTimeZoneId() ).plusHours( 10 ).toInstant(), + date.atStartOfDay( locationProvider.getTimeZoneId() ).plusHours( 15 ).toInstant() + ) + ); + + data.setSolarRiseSet( + new ZonedRiseSet( + date.atStartOfDay( locationProvider.getTimeZoneId() ).plusHours( 7 ).toInstant(), + date.atStartOfDay( locationProvider.getTimeZoneId() ).plusHours( 19 ).toInstant() + ) + ); return data; } From b3f8df9572920b30b32b2f94c86390b4c043f5fa Mon Sep 17 00:00:00 2001 From: kahles Date: Mon, 6 May 2024 20:32:33 +0200 Subject: [PATCH 3/3] extract LocationProvider from Calendar --- .../de/kah2/zodiac/libZodiac/Calendar.java | 93 ++++--------------- .../zodiac/libZodiac/CalendarGenerator.java | 2 +- .../de/kah2/zodiac/libZodiac/Benchmark.java | 2 +- .../libZodiac/CalendarGeneratorStub.java | 4 +- .../kah2/zodiac/libZodiac/CalendarStub.java | 4 +- .../libZodiac/MunichLocationProvider.java | 21 +++++ .../libZodiac/TestConstantsAndHelpers.java | 17 ---- .../example/CalendarDataStringBuilder.java | 5 +- .../example/CalendarExampleSimple.java | 14 ++- .../example/ProgressListenerExample.java | 2 +- 10 files changed, 53 insertions(+), 111 deletions(-) create mode 100644 src/test/java/de/kah2/zodiac/libZodiac/MunichLocationProvider.java diff --git a/src/main/java/de/kah2/zodiac/libZodiac/Calendar.java b/src/main/java/de/kah2/zodiac/libZodiac/Calendar.java index fc77a23..9408999 100644 --- a/src/main/java/de/kah2/zodiac/libZodiac/Calendar.java +++ b/src/main/java/de/kah2/zodiac/libZodiac/Calendar.java @@ -20,11 +20,12 @@ * see de.kah2.libZodiac.example * @author kahles */ -// TODO externalize LocationProvider -public class Calendar implements LocationProvider { +public class Calendar { private final Logger log = LoggerFactory.getLogger(this.getClass()); + private final LocationProvider locationProvider; + // This is the range the Calendar shall contain. private DateRange rangeExpected; @@ -58,83 +59,24 @@ public enum Scope { private final Scope scope; - private final ZoneId timeZoneId; - private final Position observerPosition; - - /** - * Creates an empty Calendar with "CYCLE" as default for - * {@link Scope} and using system default {@link ZoneId}. - * - * @param observerPosition - * The position of the observer needed for rise and set - * calculation. - * @param expectedRange - * the range defining start date and end date of the - * {@link Calendar} instance. - * - */ - public Calendar(final Position observerPosition, final DateRange expectedRange) { - this(observerPosition, expectedRange, Scope.CYCLE); - } - - /** - * Creates an empty Calendar with "CYCLE" as default for - * {@link Scope}. - * - * @param observerPosition - * The position of the observer needed for rise and set - * calculation. - * @param zoneId - * the time zone of the observer needed for rise and set times of - * sun and moon - * @param expectedRange - * the range defining start date and end date of the - * {@link Calendar} instance. - * - */ - public Calendar(final Position observerPosition, final ZoneId zoneId, final DateRange expectedRange) { - this(observerPosition, zoneId, expectedRange, Scope.CYCLE); - } - - /** - * Creates an empty Calendar using system default {@link ZoneId}. - * - * @param observerPosition - * The position of the observer needed for rise and set - * calculation. - * @param expectedRange - * the range defining start date and end date of the - * {@link Calendar} instance. - * @param scope - * Allows to set the scope manually - */ - public Calendar(final Position observerPosition, final DateRange expectedRange, final Scope scope) { - this(observerPosition, ZoneId.systemDefault(), expectedRange, scope); - } - /** * Creates an empty Calendar. * - * @param observerPosition - * The position of the observer needed for rise and set - * calculation. - * @param zoneId - * the time zone of the observer needed for rise and set times of - * sun and moon * @param expectedRange * the range defining start date and end date of the * {@link Calendar} instance. * @param scope * Allows to set the scope manually + * @param locationProvider The {@link LocationProvider} that is needed for calculations */ - public Calendar(final Position observerPosition, final ZoneId zoneId, final DateRange expectedRange, - final Scope scope) { - - this.timeZoneId = zoneId; - this.observerPosition = observerPosition; + public Calendar( + final DateRange expectedRange, + final Scope scope, + final LocationProvider locationProvider ) { this.rangeExpected = expectedRange; this.scope = scope; + this.locationProvider = locationProvider; } /** @@ -300,16 +242,6 @@ private LocalDate findNextLunarExtreme(TreeSet days, LocalDate start, boole } } - @Override - public ZoneId getTimeZoneId() { - return this.timeZoneId; - } - - @Override - public Position getObserverPosition() { - return this.observerPosition; - } - /** * @return the range which the {@link Calendar} shall contain. */ @@ -425,4 +357,11 @@ CalendarGenerator getGenerator() { CalendarData getDays() { return days; } + + /** + * @return the {@link LocationProvider} that is used for calculations + */ + public LocationProvider getLocationProvider() { + return locationProvider; + } } diff --git a/src/main/java/de/kah2/zodiac/libZodiac/CalendarGenerator.java b/src/main/java/de/kah2/zodiac/libZodiac/CalendarGenerator.java index 8ad9294..fa78851 100644 --- a/src/main/java/de/kah2/zodiac/libZodiac/CalendarGenerator.java +++ b/src/main/java/de/kah2/zodiac/libZodiac/CalendarGenerator.java @@ -482,7 +482,7 @@ private void startDayCreationThread(final LocalDate date) { * calculation. */ Day createCalculatedDay(final LocalDate date) { - return Day.calculateFor(this.calendar, date); + return Day.calculateFor( calendar.getLocationProvider(), date ); } /** diff --git a/src/test/java/de/kah2/zodiac/libZodiac/Benchmark.java b/src/test/java/de/kah2/zodiac/libZodiac/Benchmark.java index 606aec9..1269705 100644 --- a/src/test/java/de/kah2/zodiac/libZodiac/Benchmark.java +++ b/src/test/java/de/kah2/zodiac/libZodiac/Benchmark.java @@ -27,7 +27,7 @@ private static void benchmark(Scope scope) { for (int loop = 0; loop < NUMBER_OF_LOOPS; loop ++) { - final Calendar calendar = new Calendar(TestConstantsAndHelpers.POSITION_MUNICH, RANGE, scope); + final Calendar calendar = new Calendar( RANGE, scope, new MunichLocationProvider() ); calendar.addProgressListener(new BenchmarkListener()); calendar.getGenerator().setMaxThreadCount(MAX_THREADS); diff --git a/src/test/java/de/kah2/zodiac/libZodiac/CalendarGeneratorStub.java b/src/test/java/de/kah2/zodiac/libZodiac/CalendarGeneratorStub.java index 260c3bf..ea73279 100644 --- a/src/test/java/de/kah2/zodiac/libZodiac/CalendarGeneratorStub.java +++ b/src/test/java/de/kah2/zodiac/libZodiac/CalendarGeneratorStub.java @@ -26,7 +26,7 @@ public class CalendarGeneratorStub extends CalendarGenerator { */ @Override Day createCalculatedDay(final LocalDate date) { - return stubDay(this.getCalendar(), date); + return stubDay( new MunichLocationProvider(), date); } /** @@ -41,7 +41,7 @@ private static Day stubDay(final LocationProvider locationProvider, final LocalD * This creates a day and stubs calculation of {@link PlanetaryDayData}. Uses a default {@link LocationProvider}. */ public static Day stubDay(final LocalDate date) { - return stubDay( TestConstantsAndHelpers.LOCATION_PROVIDER, date); + return stubDay( new MunichLocationProvider(), date); } static LinkedList stubDayStorableDataSets(DateRange range) { diff --git a/src/test/java/de/kah2/zodiac/libZodiac/CalendarStub.java b/src/test/java/de/kah2/zodiac/libZodiac/CalendarStub.java index 952bd0c..569b188 100644 --- a/src/test/java/de/kah2/zodiac/libZodiac/CalendarStub.java +++ b/src/test/java/de/kah2/zodiac/libZodiac/CalendarStub.java @@ -5,8 +5,8 @@ */ public class CalendarStub extends Calendar{ - public CalendarStub(DateRange expectedRange, Scope scope) { - super(TestConstantsAndHelpers.POSITION_MUNICH, expectedRange, scope); + public CalendarStub( DateRange expectedRange, Scope scope ) { + super( expectedRange, scope, new MunichLocationProvider() ); final CalendarGenerator generator = new CalendarGeneratorStub(this); diff --git a/src/test/java/de/kah2/zodiac/libZodiac/MunichLocationProvider.java b/src/test/java/de/kah2/zodiac/libZodiac/MunichLocationProvider.java new file mode 100644 index 0000000..eaff924 --- /dev/null +++ b/src/test/java/de/kah2/zodiac/libZodiac/MunichLocationProvider.java @@ -0,0 +1,21 @@ +package de.kah2.zodiac.libZodiac; + +import de.kah2.zodiac.libZodiac.planetary.Position; + +import java.time.ZoneId; + +public class MunichLocationProvider implements LocationProvider { + + public final static Position POSITION_MUNICH = new Position(48.137, 11.57521); + public final static ZoneId TIME_ZONE_ID = ZoneId.of( "Europe/Berlin" ); + + @Override + public ZoneId getTimeZoneId() { + return TIME_ZONE_ID; + } + + @Override + public Position getObserverPosition() { + return POSITION_MUNICH; + } +} diff --git a/src/test/java/de/kah2/zodiac/libZodiac/TestConstantsAndHelpers.java b/src/test/java/de/kah2/zodiac/libZodiac/TestConstantsAndHelpers.java index e36aeef..a4ceb42 100644 --- a/src/test/java/de/kah2/zodiac/libZodiac/TestConstantsAndHelpers.java +++ b/src/test/java/de/kah2/zodiac/libZodiac/TestConstantsAndHelpers.java @@ -11,8 +11,6 @@ public class TestConstantsAndHelpers { private final static Logger LOG = LoggerFactory.getLogger(TestConstantsAndHelpers.class); - public final static Position POSITION_MUNICH = new Position(48.137, 11.57521); - public final static LocalDate SOME_DATE = LocalDate.of(2016, 9, 3); /** Last lunar extreme before SOME_DATE - full moon */ @@ -21,21 +19,6 @@ public class TestConstantsAndHelpers { /** Next lunar extreme after SOME_DATE - new moon */ public final static LocalDate SOME_DATES_NEXT_EXTREME = LocalDate.of(2016, 9, 6); - - public final static String TIME_ZONE_STRING = "Europe/Berlin"; - - public final static LocationProvider LOCATION_PROVIDER = new LocationProvider() { - @Override - public ZoneId getTimeZoneId() { - return ZoneId.of(TIME_ZONE_STRING); - } - - @Override - public Position getObserverPosition() { - return POSITION_MUNICH; - } - }; - private final static int sleepStepMs = 1000; /** Simple method to generate a {@link Calendar} and wait for results. */ diff --git a/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarDataStringBuilder.java b/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarDataStringBuilder.java index 27ad3e3..12549d1 100644 --- a/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarDataStringBuilder.java +++ b/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarDataStringBuilder.java @@ -1,6 +1,7 @@ package de.kah2.zodiac.libZodiac.example; import de.kah2.zodiac.libZodiac.Calendar; +import de.kah2.zodiac.libZodiac.LocationProvider; import de.kah2.zodiac.libZodiac.interpretation.Interpreter; import de.kah2.zodiac.libZodiac.planetary.PlanetaryDayData; import de.kah2.zodiac.libZodiac.zodiac.ZodiacDayData; @@ -23,9 +24,9 @@ public CalendarDataStringBuilder() { /** * Adds Basic {@link Calendar} data to the result. */ - public void appendCalendarData(final Calendar calendar) { + public void appendCalendarData(final LocationProvider locationProvider) { this.appendLine( - "Calendar: Observer:\t" + calendar.getObserverPosition() + ", time zone: " + calendar.getTimeZoneId()); + "Calendar: Observer:\t" + locationProvider.getObserverPosition() + ", time zone: " + locationProvider.getTimeZoneId()); } /** diff --git a/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarExampleSimple.java b/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarExampleSimple.java index ecb5659..0946c4b 100644 --- a/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarExampleSimple.java +++ b/src/test/java/de/kah2/zodiac/libZodiac/example/CalendarExampleSimple.java @@ -1,9 +1,6 @@ package de.kah2.zodiac.libZodiac.example; -import de.kah2.zodiac.libZodiac.Calendar; -import de.kah2.zodiac.libZodiac.DateRange; -import de.kah2.zodiac.libZodiac.Day; -import de.kah2.zodiac.libZodiac.TestConstantsAndHelpers; +import de.kah2.zodiac.libZodiac.*; import de.kah2.zodiac.libZodiac.interpretation.Gardening; import de.kah2.zodiac.libZodiac.interpretation.Interpreter; import org.slf4j.Logger; @@ -33,15 +30,16 @@ public static void run(Class > interpreterClass, LocalD // moon), all days since last and until next lunar extreme get // calculated. final DateRange range = new DateRange(startDate, startDate.plusDays(days)); - final Calendar calendar = new Calendar( TestConstantsAndHelpers.POSITION_MUNICH, range); + final LocationProvider locationProvider = new MunichLocationProvider(); + final Calendar calendar = new Calendar( range, Calendar.Scope.CYCLE, locationProvider ); // If we don't need to know how far away next/previous lunar extreme is, // we could reduce the Scope (and calculation time): // Only 3 days get calculated: actual, previous and next - // final Calendar calendar = new Calendar(TestConstantsAndHelpers.POSITION_MUNICH, range, Calendar.Scope.PHASE); + // final Calendar calendar = new Calendar( range, Calendar.Scope.PHASE, locationProvider ); // And if we don't even need lunar PHASE: - // final Calendar calendar = new Calendar(TestConstantsAndHelpers.POSITION_MUNICH, range, Calendar.Scope.DAY); + // final Calendar calendar = new Calendar( range, Calendar.Scope.DAY, locationProvider ) /* * Second step: Initialize its "main" content. This could take some time @@ -53,7 +51,7 @@ public static void run(Class > interpreterClass, LocalD final CalendarDataStringBuilder builder = new CalendarDataStringBuilder(); - builder.appendCalendarData(calendar); + builder.appendCalendarData( locationProvider ); for ( final Day day : calendar.getValidDays() ) { diff --git a/src/test/java/de/kah2/zodiac/libZodiac/example/ProgressListenerExample.java b/src/test/java/de/kah2/zodiac/libZodiac/example/ProgressListenerExample.java index 982fec5..ba7d014 100644 --- a/src/test/java/de/kah2/zodiac/libZodiac/example/ProgressListenerExample.java +++ b/src/test/java/de/kah2/zodiac/libZodiac/example/ProgressListenerExample.java @@ -74,7 +74,7 @@ private static Calendar createFor(final LocalDate start, final LocalDate end) { // Set up a Calendar final DateRange range = new DateRange(start, end); - final Calendar calendar = new Calendar( TestConstantsAndHelpers.POSITION_MUNICH, range); + final Calendar calendar = new Calendar( range, Calendar.Scope.CYCLE, new MunichLocationProvider() ); // Add a ProgressListenerAdapter calendar.addProgressListener(new SysOutProgressListener());