2 * Copyright (c) 2010-2020 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.io.transport.modbus.test;
15 import java.util.Arrays;
16 import java.util.Objects;
17 import java.util.stream.IntStream;
18 import java.util.stream.StreamSupport;
20 import org.hamcrest.Description;
21 import org.openhab.io.transport.modbus.ModbusRegisterArray;
22 import org.openhab.io.transport.modbus.ModbusWriteFunctionCode;
23 import org.openhab.io.transport.modbus.ModbusWriteRegisterRequestBlueprint;
26 * @author Sami Salonen - Initial contribution
28 class RegisterMatcher extends AbstractRequestComparer<ModbusWriteRegisterRequestBlueprint> {
30 private Integer[] expectedRegisterValues;
32 public RegisterMatcher(int expectedUnitId, int expectedAddress, int expectedMaxTries,
33 ModbusWriteFunctionCode expectedFunctionCode, Integer... expectedRegisterValues) {
34 super(expectedUnitId, expectedAddress, expectedFunctionCode, expectedMaxTries);
35 this.expectedRegisterValues = expectedRegisterValues;
39 public void describeTo(Description description) {
40 super.describeTo(description);
41 description.appendText(" registers=");
42 description.appendValue(Arrays.toString(expectedRegisterValues));
46 protected boolean doMatchData(ModbusWriteRegisterRequestBlueprint item) {
47 ModbusRegisterArray registers = item.getRegisters();
48 Object[] actual = StreamSupport
49 .stream(IntStream.range(0, registers.size()).mapToObj(registers::getRegister).spliterator(), false)
51 return Objects.deepEquals(actual, expectedRegisterValues);