]> git.basschouten.com Git - openhab-addons.git/blob
d84f8aa442057fefb8e8253ad925a071877a98af
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.astro.internal.model;
14
15 import java.util.AbstractMap.SimpleEntry;
16 import java.util.Calendar;
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Map.Entry;
20 import java.util.Set;
21
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;
26
27 /**
28  * Holds eclipse informations.
29  *
30  * @author Gerhard Riegler - Initial contribution
31  */
32 @NonNullByDefault
33 public class Eclipse {
34     private final Map<EclipseKind, @Nullable Entry<Calendar, @Nullable Double>> entries = new HashMap<>();
35
36     public Eclipse(EclipseKind... eclipses) {
37         for (EclipseKind eclipseKind : eclipses) {
38             entries.put(eclipseKind, null);
39         }
40     }
41
42     public Set<EclipseKind> getKinds() {
43         return entries.keySet();
44     }
45
46     /**
47      * Returns the date of the next total eclipse.
48      */
49     public @Nullable Calendar getTotal() {
50         return getDate(EclipseKind.TOTAL);
51     }
52
53     /**
54      * Returns the date of the next partial eclipse.
55      */
56     public @Nullable Calendar getPartial() {
57         return getDate(EclipseKind.PARTIAL);
58     }
59
60     /**
61      * Returns the date of the next ring eclipse.
62      */
63     public @Nullable Calendar getRing() {
64         return getDate(EclipseKind.RING);
65     }
66
67     /**
68      * Returns the elevation of the next total eclipse.
69      */
70     public @Nullable Double getTotalElevation() {
71         return getElevation(EclipseKind.TOTAL);
72     }
73
74     /**
75      * Returns the elevation of the next partial eclipse.
76      */
77     public @Nullable Double getPartialElevation() {
78         return getElevation(EclipseKind.PARTIAL);
79     }
80
81     /**
82      * Returns the elevation of the next ring eclipse.
83      */
84     public @Nullable Double getRingElevation() {
85         return getElevation(EclipseKind.RING);
86     }
87
88     public @Nullable Calendar getDate(EclipseKind eclipseKind) {
89         Entry<Calendar, @Nullable Double> entry = entries.get(eclipseKind);
90         return entry != null ? entry.getKey() : null;
91     }
92
93     private @Nullable Double getElevation(EclipseKind eclipseKind) {
94         Entry<Calendar, @Nullable Double> entry = entries.get(eclipseKind);
95         return entry != null ? entry.getValue() : null;
96     }
97
98     public void set(EclipseKind eclipseKind, Calendar eclipseDate, @Nullable Position position) {
99         entries.put(eclipseKind,
100                 new SimpleEntry<>(eclipseDate, position != null ? position.getElevationAsDouble() : null));
101     }
102
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())));
109             }
110         });
111     }
112 }