Skip to content

Commit 4168a57

Browse files
author
Yuriy Krymlov
committed
ISwissEph extends AutoCloseable
1 parent a93837d commit 4168a57

File tree

9 files changed

+132
-1849
lines changed

9 files changed

+132
-1849
lines changed

src/main/java/org/swisseph/ISwissEph.java

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,30 @@
2121
import static org.swisseph.api.ISweConstants.*;
2222
import static org.swisseph.api.ISweJulianDate.*;
2323
import static swisseph.SweConst.ERR;
24-
import static swisseph.SweConst.SE_GREG_CAL;
2524

2625
/**
26+
* {@link ISwissEph} is a wrapper interface to the Swiss Ephemeris API defined in {@link SwephExp}.
27+
* It also adds default factory methods to create and initialize {@link ISweJulianDate}
28+
* which is a wrapper for different date/time fields needed for datetime presentation.
29+
* <br><br>
30+
* This interface has two implementations: Native {@link SwephNative}, which is the default one
31+
* and pure Java {@link swisseph.SwissEph}, which was created by:
32+
* Thomas Mack (http://th-mack.de/international/download/index.html)
33+
* <br><br>
34+
* {@link ISwissEph} is the object that may hold resources (such as file handles) until it is closed.
35+
* The close() method of an AutoCloseable object is called automatically when exiting a try-with-resources block.
36+
*
2737
* @author Yura
28-
* @version 1.0, 2020-05
38+
* @version 1.1, 2021-12
2939
*/
30-
public interface ISwissEph extends Serializable {
40+
public interface ISwissEph extends Serializable, AutoCloseable {
41+
/**
42+
* @return SweConst.SE_GREG_CAL if julDay relates to Gregorian calendar date (>= October 15, 1582),
43+
* else returns SweConst.SE_JUL_CAL if julDay relates to Julian calendar (< October 15, 1582)
44+
*/
45+
static int getCalendarType(final double julDay) {
46+
return julDay >= JD_GC0 ? SweConst.SE_GREG_CAL : SweConst.SE_JUL_CAL;
47+
}
3148

3249
/**
3350
* @return true if it is the native implementation
@@ -36,8 +53,12 @@ default boolean isNativeAPI() {
3653
return true;
3754
}
3855

39-
static int getCalendarType(final double julDay) {
40-
return julDay >= JD_GC0 ? SweConst.SE_GREG_CAL : SweConst.SE_JUL_CAL;
56+
/**
57+
* Closes this resource, relinquishing any underlying resources (close Swiss Ephemeris).
58+
* This method is invoked automatically on objects managed by the try-with-resources statement.
59+
*/
60+
default void close() {
61+
swe_close();
4162
}
4263

4364
default ISweJulianDate getJulianDate(final double julDay) {
@@ -48,8 +69,8 @@ default ISweJulianDate getJulianDate(final double julDay, final double timeZone)
4869
return initJulianDate(new SweJulianDate(julDay, timeZone));
4970
}
5071

51-
default ISweJulianDate getJulianDate(final int[] date, final boolean gregorianCalendar, final double timeZone) {
52-
return initJulianDate(new SweJulianDate(date, gregorianCalendar, timeZone));
72+
default ISweJulianDate getJulianDate(final int[] date, final double timeZone) {
73+
return initJulianDate(new SweJulianDate(date, timeZone));
5374
}
5475

5576
default ISweJulianDate getJulianDate(final Calendar calendar) {
@@ -68,7 +89,7 @@ default ISweJulianDate getJulianDate(final Calendar calendar, final double timeZ
6889
calendar.get(MILLISECOND)
6990
};
7091

71-
return initJulianDate(new SweJulianDate(datetime, true, timeZone));
92+
return initJulianDate(new SweJulianDate(datetime, timeZone));
7293
}
7394

7495
/**
@@ -338,7 +359,7 @@ default ISweJulianDate swe_revjul(double jd, int gregflag) {
338359
final int[] yearMonDay = new int[3];
339360

340361
SwephExp.swe_revjul(jd, gregflag, yearMonDay, utime);
341-
return new SweJulianDate(jd, yearMonDay, utime[0], SE_GREG_CAL == gregflag);
362+
return new SweJulianDate(jd, yearMonDay, utime[0]);
342363
}
343364

344365
default ISweJulianDate swe_utc_to_jd(int year, int month, int day, int hour, int min, double sec, int gregflag, StringBuilder serr) {
@@ -355,7 +376,7 @@ default ISweJulianDate swe_utc_to_jd(int year, int month, int day, int hour, int
355376
// dret[1] = Julian day number UT1
356377

357378
return new SweJulianDate(dret[1], new int[]{year, month, day,
358-
hour, min, (int) sec}, time, SE_GREG_CAL == gregflag);
379+
hour, min, (int) sec}, time);
359380
}
360381

361382
default ISweJulianDate swe_jdet_to_utc(double tjd_et, int gregflag) {
@@ -369,7 +390,7 @@ default ISweJulianDate swe_jdet_to_utc(double tjd_et, int gregflag) {
369390
time += (outYearMonthDayHourMin[IDXI_MINUTE] / d60);
370391
time += (outDsec[0] / d3600);
371392

372-
return new SweJulianDate(tjd_et, outYearMonthDayHourMin, time, SE_GREG_CAL == gregflag);
393+
return new SweJulianDate(tjd_et, outYearMonthDayHourMin, time);
373394
}
374395

375396
default ISweJulianDate swe_jdut1_to_utc(double tjd_ut, int gregflag) {
@@ -383,7 +404,7 @@ default ISweJulianDate swe_jdut1_to_utc(double tjd_ut, int gregflag) {
383404
time += (outYearMonthDayHourMin[IDXI_MINUTE] / d60);
384405
time += (outDsec[0] / d3600);
385406

386-
return new SweJulianDate(tjd_ut, outYearMonthDayHourMin, time, SE_GREG_CAL == gregflag);
407+
return new SweJulianDate(tjd_ut, outYearMonthDayHourMin, time);
387408
}
388409

389410
/**
@@ -411,9 +432,7 @@ default ISweJulianDate swe_utc_time_zone(int iyear, int imonth, int iday,
411432
utime += (outYearMonthDayHourMin[IDXI_MINUTE] / d60);
412433
utime += (outDsec[0] / d3600);
413434

414-
SweJulianDate sweJulDate = new SweJulianDate(outYearMonthDayHourMin, utime, true);
415-
sweJulDate.timeZone(timezone);
416-
return sweJulDate;
435+
return new SweJulianDate(outYearMonthDayHourMin, utime, timezone);
417436
}
418437

419438
/****************************

src/main/java/org/swisseph/api/ISweJulianDate.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
import java.io.Serializable;
1313

1414
/**
15+
* This is the wrapper class for date,time,tmz,julianDay,deltaT,universal time (decimal hours)
16+
* The class instance should be created or initialized by corresponding {@link org.swisseph.ISwissEph} methods
17+
*
1518
* @author Yura Krymlov
16-
* @version 1.0, 2020-05
19+
* @version 1.1, 2021-12
1720
*/
1821
public interface ISweJulianDate extends Serializable {
1922
boolean SE_JUL_CAL = false;
@@ -58,7 +61,13 @@ default int sweCalendarType() {
5861
return gregorianCalendar() ? SweConst.SE_GREG_CAL : SweConst.SE_JUL_CAL;
5962
}
6063

61-
boolean gregorianCalendar();
64+
default boolean gregorianCalendar() {
65+
final int[] date = date();
66+
if (null != date && date.length > 2) {
67+
return gregorianCalendar(date[0], date[1], date[2]);
68+
}
69+
return sweGregorianCalendar(julianDay());
70+
}
6271

6372
/**
6473
* @return julian day number or NaN if not calculated yet
@@ -72,7 +81,6 @@ default int sweCalendarType() {
7281
double deltaT();
7382

7483
double timeZone();
75-
ISweJulianDate timeZone(double timeZone);
7684

7785
/**
7886
* @return time zone, julian day, delta T, universal time (decimal hours)<br>

0 commit comments

Comments
 (0)