From deb13790f95e496bbb74120a60cdae30fcd57245 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Thu, 4 Jun 2020 05:57:47 +0200 Subject: [PATCH] [astro] Add null annotations on main classes (#7852) Config classes simplified Signed-off-by: Laurent Garnier --- .../internal/config/AstroChannelConfig.java | 53 +++-------------- .../internal/config/AstroThingConfig.java | 59 ++++--------------- .../internal/handler/AstroThingHandler.java | 48 ++++++++------- .../astro/internal/handler/MoonHandler.java | 17 ++++-- .../astro/internal/handler/SunHandler.java | 19 ++++-- .../astro/internal/util/DateTimeUtils.java | 8 +-- 6 files changed, 74 insertions(+), 130 deletions(-) diff --git a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/config/AstroChannelConfig.java b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/config/AstroChannelConfig.java index 17696174d037d..bd8fcb5832840 100644 --- a/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/config/AstroChannelConfig.java +++ b/bundles/org.openhab.binding.astro/src/main/java/org/openhab/binding/astro/internal/config/AstroChannelConfig.java @@ -12,56 +12,17 @@ */ package org.openhab.binding.astro.internal.config; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; + /** * Channel configuration from Eclipse SmartHome. * * @author Gerhard Riegler - Initial contribution */ - +@NonNullByDefault public class AstroChannelConfig { - private Integer offset; - private String earliest; - private String latest; - - /** - * Returns the offset. - */ - public Integer getOffset() { - return offset; - } - - /** - * Sets the offset. - */ - public void setOffset(Integer offset) { - this.offset = offset; - } - - /** - * Returns the earliest time. - */ - public String getEarliest() { - return earliest; - } - - /** - * Sets the earliest time. - */ - public void setEarliest(String earliest) { - this.earliest = earliest; - } - - /** - * Returns the latest time. - */ - public String getLatest() { - return latest; - } - - /** - * Sets the latest time. - */ - public void setLatest(String latest) { - this.latest = latest; - } + public int offset = 0; + public @Nullable String earliest; + public @Nullable String latest; } 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 ada798c0d55a1..64974f682e2d7 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 @@ -19,19 +19,22 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; /** * Thing configuration from Eclipse SmartHome. * * @author Gerhard Riegler - Initial contribution */ +@NonNullByDefault public class AstroThingConfig { - private String geolocation; - private Double altitude; - private Double latitude; - private Double longitude; - private Integer interval; - private String thingUid; + public @Nullable String geolocation; + public @Nullable Double altitude; + public @Nullable Double latitude; + public @Nullable Double longitude; + public int interval = 300; + private @Nullable String thingUid; /** * Splits the geolocation into latitude and longitude. @@ -48,7 +51,7 @@ public void parseGeoLocation() { } } - private Double toDouble(String value) { + private @Nullable Double toDouble(String value) { try { return Double.parseDouble(StringUtils.trimToNull(value)); } catch (NumberFormatException ex) { @@ -56,48 +59,6 @@ private Double toDouble(String value) { } } - /** - * Returns the geolocation. - */ - public String getGeolocation() { - return geolocation; - } - - /** - * Returns the latitude. - */ - public Double getLatitude() { - return latitude; - } - - /** - * Returns the longitude. - */ - public Double getLongitude() { - return longitude; - } - - /** - * Returns the longitude. - */ - public Double getAltitude() { - return altitude; - } - - /** - * Returns the interval. - */ - public Integer getInterval() { - return interval; - } - - /** - * Returns the thing uid as string. - */ - public String getThingUid() { - return thingUid; - } - /** * Sets the thing uid as string. */ 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 ca7ee6a025ef9..375aa046115d7 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 @@ -30,11 +30,14 @@ import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.time.DateFormatUtils; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.scheduler.CronScheduler; import org.eclipse.smarthome.core.scheduler.ScheduledCompletableFuture; import org.eclipse.smarthome.core.thing.Channel; import org.eclipse.smarthome.core.thing.ChannelUID; import org.eclipse.smarthome.core.thing.Thing; +import org.eclipse.smarthome.core.thing.ThingStatusDetail; import org.eclipse.smarthome.core.thing.binding.BaseThingHandler; import org.eclipse.smarthome.core.types.Command; import org.openhab.binding.astro.internal.config.AstroChannelConfig; @@ -52,6 +55,7 @@ * @author Gerhard Riegler - Initial contribution * @author Amit Kumar Mondal - Implementation to be compliant with ESH Scheduler */ +@NonNullByDefault public abstract class AstroThingHandler extends BaseThingHandler { private static final String DAILY_MIDNIGHT = "30 0 0 * * ? *"; @@ -62,13 +66,16 @@ public abstract class AstroThingHandler extends BaseThingHandler { /** Scheduler to schedule jobs */ private final CronScheduler cronScheduler; - private int linkedPositionalChannels = 0; - protected AstroThingConfig thingConfig; private final Lock monitor = new ReentrantLock(); - private ScheduledCompletableFuture dailyJob; private final Set> scheduledFutures = new HashSet<>(); + private int linkedPositionalChannels = 0; + + protected AstroThingConfig thingConfig = new AstroThingConfig(); + + private @Nullable ScheduledCompletableFuture dailyJob; + public AstroThingHandler(Thing thing, CronScheduler scheduler) { super(thing); this.cronScheduler = scheduler; @@ -82,7 +89,7 @@ public void initialize() { thingConfig.setThingUid(thingUid); boolean validConfig = true; - if (StringUtils.trimToNull(thingConfig.getGeolocation()) == null) { + if (StringUtils.trimToNull(thingConfig.geolocation) == null) { logger.error("Astro parameter geolocation is mandatory and must be configured, disabling thing '{}'", thingUid); validConfig = false; @@ -90,13 +97,13 @@ public void initialize() { thingConfig.parseGeoLocation(); } - if (thingConfig.getLatitude() == null || thingConfig.getLongitude() == null) { + if (thingConfig.latitude == null || thingConfig.longitude == null) { logger.error( "Astro parameters geolocation could not be split into latitude and longitude, disabling thing '{}'", thingUid); validConfig = false; } - if (thingConfig.getInterval() == null || thingConfig.getInterval() < 1 || thingConfig.getInterval() > 86400) { + if (thingConfig.interval < 1 || thingConfig.interval > 86400) { logger.error("Astro parameter interval must be in the range of 1-86400, disabling thing '{}'", thingUid); validConfig = false; } @@ -106,7 +113,7 @@ public void initialize() { updateStatus(ONLINE); restartJobs(); } else { - updateStatus(OFFLINE); + updateStatus(OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR); } logger.debug("Thing {} initialized {}", getThing().getUID(), getThing().getStatus()); } @@ -132,7 +139,11 @@ public void handleCommand(ChannelUID channelUID, Command command) { * Iterates all channels of the thing and updates their states. */ public void publishPlanet() { - logger.debug("Publishing planet {} for thing {}", getPlanet().getClass().getSimpleName(), getThing().getUID()); + Planet planet = getPlanet(); + if (planet == null) { + return; + } + logger.debug("Publishing planet {} for thing {}", planet.getClass().getSimpleName(), getThing().getUID()); for (Channel channel : getThing().getChannels()) { if (channel.getKind() != TRIGGER) { publishChannelIfLinked(channel.getUID()); @@ -170,10 +181,6 @@ private void restartJobs() { stopJobs(); if (getThing().getStatus() == ONLINE) { String thingUID = getThing().getUID().toString(); - if (cronScheduler == null) { - logger.warn("Thread Pool Executor is not available"); - return; - } // Daily Job Job runnable = getDailyJob(); dailyJob = cronScheduler.schedule(runnable, DAILY_MIDNIGHT); @@ -185,10 +192,10 @@ private void restartJobs() { // Use scheduleAtFixedRate to avoid time drift associated with scheduleWithFixedDelay if (isPositionalChannelLinked()) { Job positionalJob = new PositionalJob(thingUID); - ScheduledFuture future = scheduler.scheduleAtFixedRate(positionalJob, 0, - thingConfig.getInterval(), TimeUnit.SECONDS); + ScheduledFuture future = scheduler.scheduleAtFixedRate(positionalJob, 0, thingConfig.interval, + TimeUnit.SECONDS); scheduledFutures.add(future); - logger.info("Scheduled {} every {} seconds", positionalJob, thingConfig.getInterval()); + logger.info("Scheduled {} every {} seconds", positionalJob, thingConfig.interval); } } } finally { @@ -203,12 +210,11 @@ private void stopJobs() { logger.debug("Stopping scheduled jobs for thing {}", getThing().getUID()); monitor.lock(); try { - if (cronScheduler != null) { - if (dailyJob != null) { - dailyJob.cancel(true); - } - dailyJob = null; + ScheduledCompletableFuture job = dailyJob; + if (job != null) { + job.cancel(true); } + dailyJob = null; for (ScheduledFuture future : scheduledFutures) { if (!future.isDone()) { future.cancel(true); @@ -316,7 +322,7 @@ private void tidyScheduledFutures() { /** * Returns the {@link Planet} instance (cannot be {@code null}) */ - public abstract Planet getPlanet(); + public abstract @Nullable Planet getPlanet(); /** * Returns the channelIds for positional calculation (cannot be {@code null}) 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 46d84a1d69629..7b3df13ac3a1d 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 @@ -19,6 +19,8 @@ import java.util.HashSet; import java.util.Set; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.scheduler.CronScheduler; import org.eclipse.smarthome.core.thing.Thing; import org.eclipse.smarthome.core.thing.ThingTypeUID; @@ -34,6 +36,7 @@ * @author Gerhard Riegler - Initial contribution * @author Amit Kumar Mondal - Implementation to be compliant with ESH Scheduler */ +@NonNullByDefault public class MoonHandler extends AstroThingHandler { public static final Set SUPPORTED_THING_TYPES = new HashSet<>(Arrays.asList(THING_TYPE_MOON)); @@ -41,7 +44,7 @@ public class MoonHandler extends AstroThingHandler { 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(); - private Moon moon; + private @Nullable Moon moon; /** * Constructor @@ -59,12 +62,15 @@ public void publishDailyInfo() { @Override public void publishPositionalInfo() { initializeMoon(); - moonCalc.setPositionalInfo(Calendar.getInstance(), thingConfig.getLatitude(), thingConfig.getLongitude(), moon); + Double latitude = thingConfig.latitude; + Double longitude = thingConfig.longitude; + moonCalc.setPositionalInfo(Calendar.getInstance(), latitude != null ? latitude : 0, + longitude != null ? longitude : 0, moon); publishPlanet(); } @Override - public Planet getPlanet() { + public @Nullable Planet getPlanet() { return moon; } @@ -85,6 +91,9 @@ protected Job getDailyJob() { } private void initializeMoon() { - moon = moonCalc.getMoonInfo(Calendar.getInstance(), thingConfig.getLatitude(), thingConfig.getLongitude()); + Double latitude = thingConfig.latitude; + Double longitude = thingConfig.longitude; + moon = moonCalc.getMoonInfo(Calendar.getInstance(), latitude != null ? latitude : 0, + longitude != null ? longitude : 0); } } 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 1c6cdcca5108c..8003ad66c61d4 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 @@ -19,6 +19,8 @@ import java.util.HashSet; import java.util.Set; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.scheduler.CronScheduler; import org.eclipse.smarthome.core.thing.Thing; import org.eclipse.smarthome.core.thing.ThingTypeUID; @@ -34,6 +36,7 @@ * @author Gerhard Riegler - Initial contribution * @author Amit Kumar Mondal - Implementation to be compliant with ESH Scheduler */ +@NonNullByDefault public class SunHandler extends AstroThingHandler { public static final Set SUPPORTED_THING_TYPES = new HashSet<>(Arrays.asList(THING_TYPE_SUN)); @@ -41,7 +44,7 @@ public class SunHandler extends AstroThingHandler { private final String[] positionalChannelIds = new String[] { "position#azimuth", "position#elevation", "radiation#direct", "radiation#diffuse", "radiation#total" }; private final SunCalc sunCalc = new SunCalc(); - private Sun sun; + private @Nullable Sun sun; /** * Constructor @@ -59,13 +62,15 @@ public void publishDailyInfo() { @Override public void publishPositionalInfo() { initializeSun(); - sunCalc.setPositionalInfo(Calendar.getInstance(), thingConfig.getLatitude(), thingConfig.getLongitude(), - thingConfig.getAltitude(), sun); + Double latitude = thingConfig.latitude; + Double longitude = thingConfig.longitude; + sunCalc.setPositionalInfo(Calendar.getInstance(), latitude != null ? latitude : 0, + longitude != null ? longitude : 0, thingConfig.altitude, sun); publishPlanet(); } @Override - public Planet getPlanet() { + public @Nullable Planet getPlanet() { return sun; } @@ -86,7 +91,9 @@ protected Job getDailyJob() { } private void initializeSun() { - sun = sunCalc.getSunInfo(Calendar.getInstance(), thingConfig.getLatitude(), thingConfig.getLongitude(), - thingConfig.getAltitude()); + Double latitude = thingConfig.latitude; + Double longitude = thingConfig.longitude; + sun = sunCalc.getSunInfo(Calendar.getInstance(), latitude != null ? latitude : 0, + longitude != null ? longitude : 0, thingConfig.altitude); } } 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 dc3fbf43185ea..1bd046c37181a 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 @@ -185,18 +185,18 @@ public static boolean isTimeGreaterEquals(Calendar cal1, Calendar cal2) { */ public static Calendar applyConfig(Calendar cal, AstroChannelConfig config) { Calendar cCal = cal; - if (config.getOffset() != null && config.getOffset() != 0) { + if (config.offset != 0) { Calendar cOffset = Calendar.getInstance(); cOffset.setTime(cCal.getTime()); - cOffset.add(Calendar.MINUTE, config.getOffset()); + cOffset.add(Calendar.MINUTE, config.offset); cCal = cOffset; } - Calendar cEarliest = adjustTime(cCal, getMinutesFromTime(config.getEarliest())); + Calendar cEarliest = adjustTime(cCal, getMinutesFromTime(config.earliest)); if (cCal.before(cEarliest)) { return cEarliest; } - Calendar cLatest = adjustTime(cCal, getMinutesFromTime(config.getLatest())); + Calendar cLatest = adjustTime(cCal, getMinutesFromTime(config.latest)); if (cCal.after(cLatest)) { return cLatest; }