]> git.basschouten.com Git - openhab-addons.git/blob
431b4ecab9f640b5f250ab034d45cdc230bd0063
[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 java.io.IOException;
16 import java.util.Date;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.deutschebahn.internal.timetable.dto.Timetable;
21
22 /**
23  * Stub Implementation of {@link TimetablesV1Api}, that may return a preconfigured Timetable or
24  * throws an {@link IOException} if not data has been set.
25  * 
26  * @author Sönke Küper - initial contribution
27  */
28 @NonNullByDefault
29 public final class TimetablesV1ApiStub implements TimetablesV1Api {
30
31     @Nullable
32     private final Timetable result;
33
34     private TimetablesV1ApiStub(@Nullable Timetable result) {
35         this.result = result;
36     }
37
38     /**
39      * Creates a new {@link TimetablesV1ApiStub}, that returns the given result.
40      */
41     public static TimetablesV1ApiStub createWithResult(Timetable timetable) {
42         return new TimetablesV1ApiStub(timetable);
43     }
44
45     /**
46      * Creates a new {@link TimetablesV1ApiStub} that throws an Exception.
47      */
48     public static TimetablesV1ApiStub createWithException() {
49         return new TimetablesV1ApiStub(null);
50     }
51
52     @Override
53     public Timetable getPlan(String evaNo, Date time) throws IOException {
54         final Timetable currentResult = this.result;
55         if (currentResult == null) {
56             throw new IOException("No timetable data is available");
57         } else {
58             return currentResult;
59         }
60     }
61
62     @Override
63     public Timetable getFullChanges(String evaNo) throws IOException {
64         return new Timetable();
65     }
66
67     @Override
68     public Timetable getRecentChanges(String evaNo) throws IOException {
69         return new Timetable();
70     }
71 }