]> git.basschouten.com Git - openhab-addons.git/blob
ea98a26b417d495b6cf4518672f5c935a6e619a7
[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.fmiweather;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.hamcrest.Description;
18 import org.hamcrest.TypeSafeMatcher;
19
20 /**
21  * Hamcrest matcher for timestamps
22  *
23  * @author Sami Salonen - Initial contribution
24  */
25 @NonNullByDefault
26 public class TimestampMatcher extends TypeSafeMatcher<long[]> {
27
28     private long start;
29     private int intervalMinutes;
30     private long steps;
31
32     public TimestampMatcher(long start, int intervalMinutes, long steps) {
33         this.start = start;
34         this.intervalMinutes = intervalMinutes;
35         this.steps = steps;
36     }
37
38     @Override
39     public void describeTo(@Nullable Description description) {
40         if (description == null) {
41             return;
42         }
43         description.appendText(new StringBuilder("start=").append(start).append(", length=").append(steps)
44                 .append(", interval=").append(intervalMinutes).toString());
45     }
46
47     @Override
48     protected boolean matchesSafely(long[] timestamps) {
49         return verifyLength(timestamps) && verifyStart(timestamps) && verifyStep(timestamps);
50     }
51
52     private boolean verifyLength(long[] timestamps) {
53         return timestamps.length == steps;
54     }
55
56     private boolean verifyStart(long[] timestamps) {
57         return timestamps[0] == start;
58     }
59
60     private boolean verifyStep(long[] timestamps) {
61         for (int i = 1; i < timestamps.length; i++) {
62             if (timestamps[i] - timestamps[i - 1] != intervalMinutes * 60) {
63                 return false;
64             }
65         }
66         return true;
67     }
68 }