]> git.basschouten.com Git - openhab-addons.git/blob
fd140bf9ebc4c92907f590dcc23914b6b205fda2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.deutschebahn.internal.timetable;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.deutschebahn.internal.EventAttribute;
18 import org.openhab.binding.deutschebahn.internal.timetable.dto.Event;
19 import org.openhab.binding.deutschebahn.internal.timetable.dto.TimetableStop;
20
21 /**
22  * Utility for merging timetable stops.
23  * This is required, thus first only the plan is returned from the API and afterwards the loaded timetable-stops must be
24  * merged with the fetched changes.
25  *
26  * @author Sönke Küper - initial contribution
27  */
28 @NonNullByDefault
29 final class TimetableStopMerger {
30
31     /**
32      * Merges the {@link TimetableStop} inplace to the first TimetableStop.
33      */
34     public static void merge(final TimetableStop first, final TimetableStop second) {
35         mergeStopAttributes(first, second);
36     }
37
38     /**
39      * Updates all values from the second {@link TimetableStop} into the first one.
40      */
41     private static void mergeStopAttributes(final TimetableStop first, final TimetableStop second) {
42         mergeEventAttributes(first.getAr(), second.getAr());
43         mergeEventAttributes(first.getDp(), second.getDp());
44     }
45
46     /**
47      * Updates all values from the second Event into the first one.
48      */
49     private static void mergeEventAttributes(@Nullable final Event first, @Nullable final Event second) {
50         if ((first == null) || (second == null)) {
51             return;
52         }
53
54         for (final EventAttribute<?, ?> attribute : EventAttribute.ALL_ATTRIBUTES) {
55             updateAttribute(attribute, first, second);
56         }
57     }
58
59     /**
60      * Sets the value of the given {@link EventAttribute} from the second Event in the first event, if not
61      * <code>null</code>.
62      */
63     private static <VALUE_TYPE> void updateAttribute(final EventAttribute<VALUE_TYPE, ?> attribute, final Event first,
64             final Event second) {
65         final @Nullable VALUE_TYPE value = attribute.getValue(second);
66         if (value != null) {
67             attribute.setValue(first, value);
68         }
69     }
70 }