From b1f412377f1a889e818b1875449772c4100bd4e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20L=27hopital?= Date: Sat, 2 Oct 2021 14:16:23 +0200 Subject: [PATCH] [Astro] Removing org.apache.commons.* (#11327) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Removing org.apache.commons.* Some code cleansing Signed-off-by: Gaël L'hopital * Some more code cleansing. Signed-off-by: Gaël L'hopital * Satisfy integration tests Signed-off-by: Gaël L'hopital * Taking in account @lolodomo feed-back Signed-off-by: Gaël L'hopital --- .../astro/internal/AstroBindingConstants.java | 4 +- .../astro/internal/AstroHandlerFactory.java | 6 +- .../astro/internal/action/AstroActions.java | 2 +- .../binding/astro/internal/calc/MoonCalc.java | 4 +- .../binding/astro/internal/calc/SunCalc.java | 2 +- .../internal/config/AstroThingConfig.java | 9 +-- .../discovery/AstroDiscoveryService.java | 17 +++--- .../internal/handler/AstroThingHandler.java | 8 +-- .../astro/internal/handler/MoonHandler.java | 8 --- .../astro/internal/handler/SunHandler.java | 8 --- .../binding/astro/internal/job/Job.java | 15 ++--- .../astro/internal/model/Position.java | 2 + .../astro/internal/model/SunPhaseName.java | 3 + .../astro/internal/model/ZodiacSign.java | 3 + .../astro/internal/util/DateTimeUtils.java | 59 ++++++++++++------- .../astro/internal/util/PropertyUtils.java | 17 +++--- .../internal/util/DateTimeUtilsTest.java | 13 ++++ 17 files changed, 97 insertions(+), 83 deletions(-) diff --git a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/AstroBindingConstants.java b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/AstroBindingConstants.java index 6ff558265..13c04b19a 100644 --- a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/AstroBindingConstants.java +++ b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/AstroBindingConstants.java @@ -32,8 +32,8 @@ public final class AstroBindingConstants { public static final String BINDING_ID = "astro"; - public static final String SUN = "sun"; - public static final String MOON = "moon"; + private static final String SUN = "sun"; + private static final String MOON = "moon"; public static final String LOCAL = "local"; // things diff --git a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/AstroHandlerFactory.java b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/AstroHandlerFactory.java index d6f0791c8..b08e39b5a 100644 --- a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/AstroHandlerFactory.java +++ b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/AstroHandlerFactory.java @@ -17,8 +17,6 @@ import static org.openhab.binding.astro.internal.AstroBindingConstants.*; import java.util.HashMap; import java.util.Map; import java.util.Set; -import java.util.stream.Collectors; -import java.util.stream.Stream; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; @@ -45,9 +43,7 @@ import org.osgi.service.component.annotations.Reference; @Component(configurationPid = "binding.astro", service = ThingHandlerFactory.class) public class AstroHandlerFactory extends BaseThingHandlerFactory { - private static final Set SUPPORTED_THING_TYPES = Stream - .concat(SunHandler.SUPPORTED_THING_TYPES.stream(), MoonHandler.SUPPORTED_THING_TYPES.stream()) - .collect(Collectors.toSet()); + private static final Set SUPPORTED_THING_TYPES = Set.of(THING_TYPE_SUN, THING_TYPE_MOON); private static final Map ASTRO_THING_HANDLERS = new HashMap<>(); private final CronScheduler scheduler; private final TimeZoneProvider timeZoneProvider; diff --git a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/action/AstroActions.java b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/action/AstroActions.java index 064cdf984..7e0abdef4 100644 --- a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/action/AstroActions.java +++ b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/action/AstroActions.java @@ -42,7 +42,7 @@ import org.slf4j.LoggerFactory; public class AstroActions implements ThingActions { private final Logger logger = LoggerFactory.getLogger(AstroActions.class); - protected @Nullable AstroThingHandler handler; + private @Nullable AstroThingHandler handler; public AstroActions() { logger.debug("Astro actions service instanciated"); diff --git a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/calc/MoonCalc.java b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/calc/MoonCalc.java index d9cd5ca4a..56a51c907 100644 --- a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/calc/MoonCalc.java +++ b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/calc/MoonCalc.java @@ -391,7 +391,7 @@ public class MoonCalc { /** * Calculates the previous moon phase. */ - public double getPreviousPhase(Calendar cal, double jd, double mode) { + private double getPreviousPhase(Calendar cal, double jd, double mode) { double tz = 0; double phaseJd = 0; do { @@ -494,7 +494,7 @@ public class MoonCalc { return sr; } - public double[] calcMoon(double t) { + private double[] calcMoon(double t) { double p2 = 6.283185307; double arc = 206264.8062; double coseps = .91748; diff --git a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/calc/SunCalc.java b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/calc/SunCalc.java index 28bb03dee..75ebeb288 100644 --- a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/calc/SunCalc.java +++ b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/calc/SunCalc.java @@ -90,7 +90,7 @@ public class SunCalc { /** * Calculates sun radiation data. */ - public void setRadiationInfo(Calendar calendar, double elevation, Double altitude, Sun sun) { + private void setRadiationInfo(Calendar calendar, double elevation, Double altitude, Sun sun) { double sinAlpha = Math.sin(DEG2RAD * elevation); int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR); diff --git a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/config/AstroThingConfig.java b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/config/AstroThingConfig.java index 6d95d379b..e2079a15b 100644 --- a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/config/AstroThingConfig.java +++ b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/config/AstroThingConfig.java @@ -22,6 +22,8 @@ import org.eclipse.jdt.annotation.Nullable; */ @NonNullByDefault public class AstroThingConfig { + public static final String GEOLOCATION = "geolocation"; + public static final String USE_METEOROLOGICAL_SEASON = "useMeteorologicalSeason"; public @Nullable String geolocation; public @Nullable Double altitude; public @Nullable Double latitude; @@ -35,12 +37,11 @@ public class AstroThingConfig { public void parseGeoLocation() { if (geolocation != null) { String[] geoParts = geolocation.split(","); - if (geoParts.length == 2) { - latitude = toDouble(geoParts[0]); - longitude = toDouble(geoParts[1]); - } else if (geoParts.length == 3) { + if (geoParts.length >= 2) { latitude = toDouble(geoParts[0]); longitude = toDouble(geoParts[1]); + } + if (geoParts.length == 3) { altitude = toDouble(geoParts[2]); } } diff --git a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/discovery/AstroDiscoveryService.java b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/discovery/AstroDiscoveryService.java index 4eed4db6d..3ec1b5735 100644 --- a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/discovery/AstroDiscoveryService.java +++ b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/discovery/AstroDiscoveryService.java @@ -13,6 +13,7 @@ package org.openhab.binding.astro.internal.discovery; import static org.openhab.binding.astro.internal.AstroBindingConstants.*; +import static org.openhab.binding.astro.internal.config.AstroThingConfig.*; import java.util.Arrays; import java.util.HashSet; @@ -50,7 +51,7 @@ import org.slf4j.LoggerFactory; public class AstroDiscoveryService extends AbstractDiscoveryService { private static final int DISCOVER_TIMEOUT_SECONDS = 2; private static final int LOCATION_CHANGED_CHECK_INTERVAL = 60; - private static final Set METEO_BASED_COUNTRIES = new HashSet<>(Arrays.asList("NZ", "AU")); + private static final Set METEO_BASED_COUNTRIES = Set.of("NZ", "AU"); private static final ThingUID SUN_THING = new ThingUID(THING_TYPE_SUN, LOCAL); private static final ThingUID MOON_THING = new ThingUID(THING_TYPE_MOON, LOCAL); @@ -110,15 +111,15 @@ public class AstroDiscoveryService extends AbstractDiscoveryService { } public void createResults(PointType location) { - String propGeolocation; + String propGeolocation = location.toString(); String country = localeProvider.getLocale().getCountry(); - propGeolocation = String.format("%s,%s,%s", location.getLatitude(), location.getLongitude(), - location.getAltitude()); + thingDiscovered(DiscoveryResultBuilder.create(SUN_THING).withLabel("Local Sun") - .withProperty("geolocation", propGeolocation) - .withProperty("useMeteorologicalSeason", METEO_BASED_COUNTRIES.contains(country)) - .withRepresentationProperty("geolocation").build()); + .withProperty(GEOLOCATION, propGeolocation) + .withProperty(USE_METEOROLOGICAL_SEASON, METEO_BASED_COUNTRIES.contains(country)) + .withRepresentationProperty(GEOLOCATION).build()); + thingDiscovered(DiscoveryResultBuilder.create(MOON_THING).withLabel("Local Moon") - .withProperty("geolocation", propGeolocation).withRepresentationProperty("geolocation").build()); + .withProperty(GEOLOCATION, propGeolocation).withRepresentationProperty(GEOLOCATION).build()); } } diff --git a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/handler/AstroThingHandler.java b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/handler/AstroThingHandler.java index 2e06df173..3b1987456 100644 --- a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/handler/AstroThingHandler.java +++ b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/handler/AstroThingHandler.java @@ -17,6 +17,7 @@ import static org.openhab.core.thing.type.ChannelKind.TRIGGER; import static org.openhab.core.types.RefreshType.REFRESH; import java.lang.invoke.MethodHandles; +import java.text.SimpleDateFormat; import java.time.ZonedDateTime; import java.util.Arrays; import java.util.Calendar; @@ -34,7 +35,6 @@ import java.util.concurrent.locks.ReentrantLock; import javax.measure.quantity.Angle; -import org.apache.commons.lang3.time.DateFormatUtils; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.astro.internal.action.AstroActions; @@ -67,11 +67,11 @@ import org.slf4j.LoggerFactory; */ @NonNullByDefault public abstract class AstroThingHandler extends BaseThingHandler { - private static final String DAILY_MIDNIGHT = "30 0 0 * * ? *"; /** Logger Instance */ - protected final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + private final SimpleDateFormat isoFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); /** Scheduler to schedule jobs */ private final CronScheduler cronScheduler; @@ -310,7 +310,7 @@ public abstract class AstroThingHandler extends BaseThingHandler { monitor.unlock(); } if (logger.isDebugEnabled()) { - String formattedDate = DateFormatUtils.ISO_DATETIME_FORMAT.format(eventAt); + String formattedDate = this.isoFormatter.format(eventAt); logger.debug("Scheduled {} in {}ms (at {})", job, sleepTime, formattedDate); } } diff --git a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/handler/MoonHandler.java b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/handler/MoonHandler.java index 0958780ed..1c03b7bc9 100644 --- a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/handler/MoonHandler.java +++ b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/handler/MoonHandler.java @@ -12,14 +12,9 @@ */ package org.openhab.binding.astro.internal.handler; -import static org.openhab.binding.astro.internal.AstroBindingConstants.THING_TYPE_MOON; - import java.time.ZonedDateTime; -import java.util.Arrays; import java.util.Calendar; import java.util.GregorianCalendar; -import java.util.HashSet; -import java.util.Set; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; @@ -32,7 +27,6 @@ import org.openhab.binding.astro.internal.model.Position; import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.scheduler.CronScheduler; import org.openhab.core.thing.Thing; -import org.openhab.core.thing.ThingTypeUID; /** * The MoonHandler is responsible for updating calculated moon data. @@ -43,8 +37,6 @@ import org.openhab.core.thing.ThingTypeUID; @NonNullByDefault public class MoonHandler extends AstroThingHandler { - public static final Set SUPPORTED_THING_TYPES = new HashSet<>(Arrays.asList(THING_TYPE_MOON)); - private final String[] positionalChannelIds = new String[] { "phase#name", "phase#age", "phase#agePercent", "phase#ageDegree", "phase#illumination", "position#azimuth", "position#elevation", "zodiac#sign" }; private final MoonCalc moonCalc = new MoonCalc(); diff --git a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/handler/SunHandler.java b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/handler/SunHandler.java index 66949f3cb..eeaf3b350 100644 --- a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/handler/SunHandler.java +++ b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/handler/SunHandler.java @@ -12,14 +12,9 @@ */ package org.openhab.binding.astro.internal.handler; -import static org.openhab.binding.astro.internal.AstroBindingConstants.THING_TYPE_SUN; - import java.time.ZonedDateTime; -import java.util.Arrays; import java.util.Calendar; import java.util.GregorianCalendar; -import java.util.HashSet; -import java.util.Set; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; @@ -34,7 +29,6 @@ import org.openhab.binding.astro.internal.model.SunPhaseName; import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.scheduler.CronScheduler; import org.openhab.core.thing.Thing; -import org.openhab.core.thing.ThingTypeUID; /** * The SunHandler is responsible for updating calculated sun data. @@ -45,8 +39,6 @@ import org.openhab.core.thing.ThingTypeUID; @NonNullByDefault public class SunHandler extends AstroThingHandler { - public static final Set SUPPORTED_THING_TYPES = new HashSet<>(Arrays.asList(THING_TYPE_SUN)); - private final String[] positionalChannelIds = new String[] { "position#azimuth", "position#elevation", "radiation#direct", "radiation#diffuse", "radiation#total" }; private final SunCalc sunCalc = new SunCalc(); diff --git a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/job/Job.java b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/job/Job.java index b4a5b8ee8..870bff0b7 100644 --- a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/job/Job.java +++ b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/job/Job.java @@ -12,11 +12,8 @@ */ package org.openhab.binding.astro.internal.job; -import static java.util.Arrays.asList; -import static java.util.Calendar.SECOND; import static java.util.Collections.singletonList; import static java.util.stream.Collectors.toList; -import static org.apache.commons.lang3.time.DateUtils.truncatedEquals; import static org.openhab.binding.astro.internal.AstroBindingConstants.*; import static org.openhab.binding.astro.internal.util.DateTimeUtils.*; @@ -132,15 +129,11 @@ public interface Job extends SchedulerRunnable, Runnable { return; } AstroChannelConfig config = channel.getConfiguration().as(AstroChannelConfig.class); - Calendar configStart = applyConfig(start, config); - Calendar configEnd = applyConfig(end, config); + Calendar configStart = truncateToSecond(applyConfig(start, config)); + Calendar configEnd = truncateToSecond(applyConfig(end, config)); - if (truncatedEquals(configStart, configEnd, SECOND)) { - scheduleEvent(thingUID, astroHandler, configStart, asList(EVENT_START, EVENT_END), channelId, true); - } else { - scheduleEvent(thingUID, astroHandler, configStart, EVENT_START, channelId, true); - scheduleEvent(thingUID, astroHandler, configEnd, EVENT_END, channelId, true); - } + scheduleEvent(thingUID, astroHandler, configStart, EVENT_START, channelId, true); + scheduleEvent(thingUID, astroHandler, configEnd, EVENT_END, channelId, true); } /** diff --git a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/model/Position.java b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/model/Position.java index be2d6fcd5..530fa351c 100644 --- a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/model/Position.java +++ b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/model/Position.java @@ -14,6 +14,7 @@ package org.openhab.binding.astro.internal.model; import javax.measure.quantity.Angle; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.core.library.types.QuantityType; import org.openhab.core.library.unit.Units; @@ -24,6 +25,7 @@ import org.openhab.core.library.unit.Units; * @author Gaël L'hopital - Added shade length * @author Christoph Weitkamp - Introduced UoM */ +@NonNullByDefault public class Position { private double azimuth; diff --git a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/model/SunPhaseName.java b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/model/SunPhaseName.java index 7c908edbb..7f2645ed4 100644 --- a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/model/SunPhaseName.java +++ b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/model/SunPhaseName.java @@ -12,11 +12,14 @@ */ package org.openhab.binding.astro.internal.model; +import org.eclipse.jdt.annotation.NonNullByDefault; + /** * All sun phases. * * @author Gerhard Riegler - Initial contribution */ +@NonNullByDefault public enum SunPhaseName { SUN_RISE, ASTRO_DAWN, diff --git a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/model/ZodiacSign.java b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/model/ZodiacSign.java index d8d310096..be3f30c8f 100644 --- a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/model/ZodiacSign.java +++ b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/model/ZodiacSign.java @@ -12,11 +12,14 @@ */ package org.openhab.binding.astro.internal.model; +import org.eclipse.jdt.annotation.NonNullByDefault; + /** * All zodiac signs. * * @author Gerhard Riegler - Initial contribution */ +@NonNullByDefault public enum ZodiacSign { ARIES, TAURUS, diff --git a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/util/DateTimeUtils.java b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/util/DateTimeUtils.java index 6e18febb2..f4efbe916 100644 --- a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/util/DateTimeUtils.java +++ b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/util/DateTimeUtils.java @@ -13,10 +13,8 @@ package org.openhab.binding.astro.internal.util; import java.util.Calendar; -import java.util.Date; import java.util.regex.Pattern; -import org.apache.commons.lang3.time.DateUtils; import org.openhab.binding.astro.internal.config.AstroChannelConfig; import org.openhab.binding.astro.internal.model.Range; import org.slf4j.Logger; @@ -31,19 +29,40 @@ public class DateTimeUtils { private static final Logger LOGGER = LoggerFactory.getLogger(DateTimeUtils.class); private static final Pattern HHMM_PATTERN = Pattern.compile("^([0-1][0-9]|2[0-3])(:[0-5][0-9])$"); - public static final double J1970 = 2440588.0; - public static final double MILLISECONDS_PER_DAY = 1000 * 60 * 60 * 24; + private static final double J1970 = 2440588.0; + private static final double MILLISECONDS_PER_DAY = 1000 * 60 * 60 * 24; /** Constructor */ private DateTimeUtils() { throw new IllegalAccessError("Non-instantiable"); } + /** + * Truncates the time from the calendar object. + */ + public static Calendar truncateToSecond(Calendar calendar) { + Calendar cal = (Calendar) calendar.clone(); + cal.set(Calendar.MILLISECOND, 0); + return cal; + } + + /** + * Truncates the time from the calendar object. + */ + private static Calendar truncateToMinute(Calendar calendar) { + Calendar cal = truncateToSecond(calendar); + cal.set(Calendar.SECOND, 0); + return cal; + } + /** * Truncates the time from the calendar object. */ public static Calendar truncateToMidnight(Calendar calendar) { - return DateUtils.truncate(calendar, Calendar.DAY_OF_MONTH); + Calendar cal = truncateToMinute(calendar); + cal.set(Calendar.HOUR_OF_DAY, 0); + cal.set(Calendar.MINUTE, 0); + return cal; } /** @@ -79,7 +98,11 @@ public class DateTimeUtils { long millis = (long) ((julianDate + 0.5 - J1970) * MILLISECONDS_PER_DAY); Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(millis); - return DateUtils.round(cal, Calendar.MINUTE); + int second = cal.get(Calendar.SECOND); + if (second > 30) { + cal.add(Calendar.MINUTE, 1); + } + return truncateToMinute(cal); } /** @@ -100,8 +123,8 @@ public class DateTimeUtils { * Returns the end of day from the calendar object. */ public static Calendar endOfDayDate(Calendar calendar) { - Calendar cal = (Calendar) calendar.clone(); - cal = DateUtils.ceiling(cal, Calendar.DATE); + Calendar cal = truncateToMidnight(calendar); + cal.add(Calendar.DATE, 1); cal.add(Calendar.MILLISECOND, -1); return cal; } @@ -139,21 +162,16 @@ public class DateTimeUtils { } cal.set(Calendar.HOUR_OF_DAY, hour); cal.set(Calendar.MINUTE, minute); - return DateUtils.truncate(cal, Calendar.MINUTE); + return truncateToMinute(cal); } /** * Returns true, if two calendar objects are on the same day ignoring time. */ public static boolean isSameDay(Calendar cal1, Calendar cal2) { - return cal1 != null && cal2 != null && DateUtils.isSameDay(cal1, cal2); - } - - /** - * Returns a date object from a calendar. - */ - public static Date getDate(Calendar calendar) { - return calendar == null ? null : calendar.getTime(); + return cal1 != null && cal2 != null && cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) + && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) + && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR); } /** @@ -181,8 +199,8 @@ public class DateTimeUtils { * Returns true, if cal1 is greater or equal than cal2, ignoring seconds. */ public static boolean isTimeGreaterEquals(Calendar cal1, Calendar cal2) { - Calendar truncCal1 = DateUtils.truncate(cal1, Calendar.MINUTE); - Calendar truncCal2 = DateUtils.truncate(cal2, Calendar.MINUTE); + Calendar truncCal1 = truncateToMinute(cal1); + Calendar truncCal2 = truncateToMinute(cal2); return truncCal1.getTimeInMillis() >= truncCal2.getTimeInMillis(); } @@ -212,8 +230,7 @@ public class DateTimeUtils { private static Calendar adjustTime(Calendar cal, int minutes) { if (minutes > 0) { - Calendar cTime = Calendar.getInstance(); - cTime = DateUtils.truncate(cal, Calendar.DAY_OF_MONTH); + Calendar cTime = truncateToMidnight(cal); cTime.add(Calendar.MINUTE, minutes); return cTime; } diff --git a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/util/PropertyUtils.java b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/util/PropertyUtils.java index 831348016..76ea49135 100644 --- a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/util/PropertyUtils.java +++ b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/util/PropertyUtils.java @@ -17,8 +17,10 @@ import java.math.BigDecimal; import java.math.RoundingMode; import java.time.ZoneId; import java.time.ZonedDateTime; +import java.util.ArrayList; import java.util.Calendar; import java.util.GregorianCalendar; +import java.util.List; import java.util.Objects; import java.util.TimeZone; @@ -77,23 +79,22 @@ public class PropertyUtils { * Returns the property value from the object instance, nested properties are possible. If the propertyName is for * example rise.start, the methods getRise().getStart() are called. */ - public static @Nullable Object getPropertyValue(ChannelUID channelUID, Object instance) throws Exception { - String[] properties = channelUID.getId().split("#"); - return getPropertyValue(instance, properties, 0); + private static @Nullable Object getPropertyValue(ChannelUID channelUID, Object instance) throws Exception { + ArrayList properties = new ArrayList<>(List.of(channelUID.getId().split("#"))); + return getPropertyValue(instance, properties); } /** * Iterates through the nested properties and returns the getter value. */ @SuppressWarnings("all") - private static @Nullable Object getPropertyValue(Object instance, String[] properties, int nestedIndex) - throws Exception { - String propertyName = properties[nestedIndex]; + private static @Nullable Object getPropertyValue(Object instance, List properties) throws Exception { + String propertyName = properties.remove(0); Method m = instance.getClass().getMethod(toGetterString(propertyName), null); Object result = m.invoke(instance, (Object[]) null); - if (nestedIndex + 1 < properties.length) { + if (!properties.isEmpty()) { Objects.requireNonNull(result); - return getPropertyValue(result, properties, nestedIndex + 1); + return getPropertyValue(result, properties); } return result; } diff --git a/bundles/org.openhab.binding.astro/src/test/java/org/openhab/binding/astro/internal/util/DateTimeUtilsTest.java b/bundles/org.openhab.binding.astro/src/test/java/org/openhab/binding/astro/internal/util/DateTimeUtilsTest.java index 52c278928..841ae5ac3 100644 --- a/bundles/org.openhab.binding.astro/src/test/java/org/openhab/binding/astro/internal/util/DateTimeUtilsTest.java +++ b/bundles/org.openhab.binding.astro/src/test/java/org/openhab/binding/astro/internal/util/DateTimeUtilsTest.java @@ -63,6 +63,19 @@ public class DateTimeUtilsTest { assertNextSeason(season.getAutumn(), DEC_10_2020, season); } + @Test + void testTruncate() { + Calendar cal = newCalendar(2021, 9, 30, 11, 54, TIME_ZONE); + Calendar target = newCalendar(2021, 9, 30, 0, 0, TIME_ZONE); + Calendar truncated = DateTimeUtils.truncateToMidnight(cal); + assertEquals(truncated, target); + Calendar endOfDay = DateTimeUtils.endOfDayDate(cal); + Calendar target2 = new GregorianCalendar(2021, 9, 30, 23, 59, 59); + target2.setTimeZone(TIME_ZONE); + target2.set(Calendar.MILLISECOND, 999); + assertEquals(endOfDay, target2); + } + private void assertNextSeason(Calendar expectedSeason, Calendar date, Season season) { assertEquals(expectedSeason, DateTimeUtils.getNext(date, season.getSpring(), season.getSummer(), season.getAutumn(), season.getWinter()));