]> git.basschouten.com Git - openhab-addons.git/commitdiff
[Astro] Removing org.apache.commons.* (#11327)
authorGaël L'hopital <gael@lhopital.org>
Sat, 2 Oct 2021 12:16:23 +0000 (14:16 +0200)
committerGitHub <noreply@github.com>
Sat, 2 Oct 2021 12:16:23 +0000 (14:16 +0200)
* Removing org.apache.commons.*
Some code cleansing

Signed-off-by: Gaël L'hopital <gael@lhopital.org>
* Some more code cleansing.

Signed-off-by: Gaël L'hopital <gael@lhopital.org>
* Satisfy integration tests

Signed-off-by: Gaël L'hopital <gael@lhopital.org>
* Taking in account @lolodomo feed-back

Signed-off-by: Gaël L'hopital <gael@lhopital.org>
17 files changed:
bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/AstroBindingConstants.java
bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/AstroHandlerFactory.java
bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/action/AstroActions.java
bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/calc/MoonCalc.java
bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/calc/SunCalc.java
bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/config/AstroThingConfig.java
bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/discovery/AstroDiscoveryService.java
bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/handler/AstroThingHandler.java
bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/handler/MoonHandler.java
bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/handler/SunHandler.java
bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/job/Job.java
bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/model/Position.java
bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/model/SunPhaseName.java
bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/model/ZodiacSign.java
bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/util/DateTimeUtils.java
bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/util/PropertyUtils.java
bundles/org.openhab.binding.astro/src/test/java/org/openhab/binding/astro/internal/util/DateTimeUtilsTest.java

index 6ff558265e6235e12451162b68b7a9eefcf9e2ce..13c04b19a25c3388b9840ef8121e64fce5d4cfa0 100644 (file)
@@ -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
index d6f0791c8a13deda3a90283ac5bce8a8a65942d8..b08e39b5af213f1f692773f39e4de25481d71df7 100644 (file)
@@ -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<ThingTypeUID> SUPPORTED_THING_TYPES = Stream
-            .concat(SunHandler.SUPPORTED_THING_TYPES.stream(), MoonHandler.SUPPORTED_THING_TYPES.stream())
-            .collect(Collectors.toSet());
+    private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_SUN, THING_TYPE_MOON);
     private static final Map<String, AstroThingHandler> ASTRO_THING_HANDLERS = new HashMap<>();
     private final CronScheduler scheduler;
     private final TimeZoneProvider timeZoneProvider;
index 064cdf984667023254894e1fcce4b276dc8f1c6e..7e0abdef49d94bc5c95d65d0f6ed70650cc0f5c0 100644 (file)
@@ -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");
index d9cd5ca4a9dd2866f39a61e2289d1b5576e80f5d..56a51c907b4091bb5db97dabac5c2d09b8b24a9c 100644 (file)
@@ -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;
index 28bb03deeab1ac0f212557dc90db2c9c62277a66..75ebeb288ce4c2ed84423aabbc432c27c5b813c1 100644 (file)
@@ -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);
index 6d95d379b635111acd8ca9dbc31cd70b7fcc71f3..e2079a15b54007f8b04c5d6b094685c6f8cf0fd9 100644 (file)
@@ -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]);
             }
         }
index 4eed4db6d73435f4e7e600a402cc2957143b57b2..3ec1b5735404a312e6464d9e148fd0121db2d493 100644 (file)
@@ -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<String> METEO_BASED_COUNTRIES = new HashSet<>(Arrays.asList("NZ", "AU"));
+    private static final Set<String> 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());
     }
 }
index 2e06df173cfc26e8e15dcf9269d24098c67a61f4..3b1987456d2c1274cf538f00f0f41873c79a9056 100644 (file)
@@ -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);
         }
     }
index 0958780ed799c169b3a73c40d3a0c331caeee5ec..1c03b7bc9408aef5d15be56a2641a5eb00aa0ad7 100644 (file)
  */
 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<ThingTypeUID> 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();
index 66949f3cb5c323185642dac14d1717ea8ec55c36..eeaf3b350def078efc8c214702d7dcc65d39745a 100644 (file)
  */
 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<ThingTypeUID> 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();
index b4a5b8ee8ba7e79319b4899513f2ddf282d4713f..870bff0b75411c576bab3bf91266f6c04744b958 100644 (file)
  */
 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);
     }
 
     /**
index be2d6fcd5c24861367d9b986283cca7bc0c52ddd..530fa351c47651c6bf5eb3694aef18344ca51d5d 100644 (file)
@@ -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;
index 7c908edbb50f4c20199b24024ba0901f407c9a9b..7f2645ed4205e5abdf5a424f90d97abc1b15ae90 100644 (file)
  */
 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,
index d8d310096801c85fa22e3448f1e4404b04db930f..be3f30c8f38228126259816c1f6c1395ab10b87c 100644 (file)
  */
 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,
index 6e18febb2c5d46d1f08d3443a77caea619e3c8ad..f4efbe916c578f3ccc9b36223d3fec322ed87671 100644 (file)
 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;
         }
index 831348016426d5775befb29417f2f9004831e408..76ea491355fd57be4cfb67d638dcb8609a032a3c 100644 (file)
@@ -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<String> 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<String> 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;
     }
index 52c2789285ef39ff7720a074a885f7bca09456f6..841ae5ac3200f08cec9aa8aaccaaa25048d02cd4 100644 (file)
@@ -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()));