2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.astro.internal.model;
15 import java.util.AbstractMap.SimpleEntry;
16 import java.util.Calendar;
17 import java.util.HashMap;
19 import java.util.Map.Entry;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.astro.internal.handler.AstroThingHandler;
25 import org.openhab.core.i18n.TimeZoneProvider;
28 * Holds eclipse informations.
30 * @author Gerhard Riegler - Initial contribution
33 public class Eclipse {
34 private final Map<EclipseKind, @Nullable Entry<Calendar, @Nullable Double>> entries = new HashMap<>();
36 public Eclipse(EclipseKind... eclipses) {
37 for (EclipseKind eclipseKind : eclipses) {
38 entries.put(eclipseKind, null);
42 public Set<EclipseKind> getKinds() {
43 return entries.keySet();
47 * Returns the date of the next total eclipse.
49 public @Nullable Calendar getTotal() {
50 return getDate(EclipseKind.TOTAL);
54 * Returns the date of the next partial eclipse.
56 public @Nullable Calendar getPartial() {
57 return getDate(EclipseKind.PARTIAL);
61 * Returns the date of the next ring eclipse.
63 public @Nullable Calendar getRing() {
64 return getDate(EclipseKind.RING);
68 * Returns the elevation of the next total eclipse.
70 public @Nullable Double getTotalElevation() {
71 return getElevation(EclipseKind.TOTAL);
75 * Returns the elevation of the next partial eclipse.
77 public @Nullable Double getPartialElevation() {
78 return getElevation(EclipseKind.PARTIAL);
82 * Returns the elevation of the next ring eclipse.
84 public @Nullable Double getRingElevation() {
85 return getElevation(EclipseKind.RING);
88 public @Nullable Calendar getDate(EclipseKind eclipseKind) {
89 Entry<Calendar, @Nullable Double> entry = entries.get(eclipseKind);
90 return entry != null ? entry.getKey() : null;
93 private @Nullable Double getElevation(EclipseKind eclipseKind) {
94 Entry<Calendar, @Nullable Double> entry = entries.get(eclipseKind);
95 return entry != null ? entry.getValue() : null;
98 public void set(EclipseKind eclipseKind, Calendar eclipseDate, @Nullable Position position) {
99 entries.put(eclipseKind,
100 new SimpleEntry<>(eclipseDate, position != null ? position.getElevationAsDouble() : null));
103 public void setElevations(AstroThingHandler astroHandler, TimeZoneProvider timeZoneProvider) {
104 getKinds().forEach(eclipseKind -> {
105 Calendar eclipseDate = getDate(eclipseKind);
106 if (eclipseDate != null) {
107 set(eclipseKind, eclipseDate,
108 astroHandler.getPositionAt(eclipseDate.toInstant().atZone(timeZoneProvider.getTimeZone())));