]> git.basschouten.com Git - openhab-addons.git/blob
5eaefe7a7faec33c46f08a1cb9a27f47b7624067
[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.fmiweather;
14
15 import java.math.BigDecimal;
16 import java.util.Arrays;
17 import java.util.Objects;
18 import java.util.stream.Stream;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.hamcrest.Description;
23 import org.hamcrest.TypeSafeMatcher;
24
25 /**
26  * Hamcrest matcher for values
27  *
28  * @author Sami Salonen - Initial contribution
29  */
30 @NonNullByDefault
31 public class ValuesMatcher extends TypeSafeMatcher<@Nullable BigDecimal[]> {
32
33     private Object[] values;
34
35     public ValuesMatcher(@Nullable String... values) {
36         this.values = Stream.of(values).map(s -> stringToBigDecimal(s)).toArray();
37     }
38
39     private static @Nullable BigDecimal stringToBigDecimal(@Nullable String s) {
40         return s == null ? null : new BigDecimal(s);
41     }
42
43     @SuppressWarnings("null")
44     @Override
45     public void describeTo(@Nullable Description description) {
46         if (description == null) {
47             return;
48         }
49         description.appendText(Arrays.deepToString(values));
50     }
51
52     @Override
53     protected boolean matchesSafely(@Nullable BigDecimal[] data) {
54         return Objects.deepEquals(data, values);
55     }
56 }