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.deutschebahn.internal.timetable;
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;
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.
26 * @author Sönke Küper - initial contribution
29 final class TimetableStopMerger {
32 * Merges the {@link TimetableStop} inplace to the first TimetableStop.
34 public static void merge(final TimetableStop first, final TimetableStop second) {
35 mergeStopAttributes(first, second);
39 * Updates all values from the second {@link TimetableStop} into the first one.
41 private static void mergeStopAttributes(final TimetableStop first, final TimetableStop second) {
42 mergeEventAttributes(first.getAr(), second.getAr());
43 mergeEventAttributes(first.getDp(), second.getDp());
47 * Updates all values from the second Event into the first one.
49 private static void mergeEventAttributes(@Nullable final Event first, @Nullable final Event second) {
50 if ((first == null) || (second == null)) {
54 for (final EventAttribute<?, ?> attribute : EventAttribute.ALL_ATTRIBUTES) {
55 updateAttribute(attribute, first, second);
60 * Sets the value of the given {@link EventAttribute} from the second Event in the first event, if not
63 private static <VALUE_TYPE> void updateAttribute(final EventAttribute<VALUE_TYPE, ?> attribute, final Event first,
65 final @Nullable VALUE_TYPE value = attribute.getValue(second);
67 attribute.setValue(first, value);