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;
15 import java.util.Iterator;
16 import java.util.stream.IntStream;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
21 * Immutable {@link ModbusRegisterArray} implementation
23 * @author Sami Salonen - Initial contribution
26 public class ModbusRegisterArray implements Iterable<ModbusRegister> {
28 private final ModbusRegister[] registers;
31 * Construct plain <code>ModbusRegister[]</code> array from register values
33 * @param registerValues register values, each <code>int</code> corresponding to one register
36 public static ModbusRegister[] registersFromValues(int... registerValues) {
37 ModbusRegister[] registers = new ModbusRegister[registerValues.length];
38 for (int i = 0; i < registerValues.length; i++) {
39 registers[i] = new ModbusRegister(registerValues[i]);
45 * Construct ModbusRegisterArrayImpl from array of {@link ModbusRegister}
49 public ModbusRegisterArray(ModbusRegister[] registers) {
50 this.registers = registers;
54 * Construct plain <code>ModbusRegisterArrayImpl</code> array from register values
56 * @param registerValues register values, each <code>int</code> corresponding to one register
59 public ModbusRegisterArray(int... registerValues) {
60 this(registersFromValues(registerValues));
64 * Return register at the given index
66 * Index 0 matches first register (lowest register index).
69 * @param index the index of the register to be returned.
70 * @throws IndexOutOfBoundsException if the index is out of bounds.
72 public ModbusRegister getRegister(int index) {
73 return registers[index];
77 * Get number of registers stored in this instance
82 return registers.length;
86 public String toString() {
87 if (registers.length == 0) {
88 return "ModbusRegisterArrayImpl(<empty>)";
90 StringBuffer buffer = new StringBuffer(registers.length * 2).append("ModbusRegisterArrayImpl(");
91 return appendHexString(buffer).append(')').toString();
95 * Iterator over all the registers
98 public Iterator<ModbusRegister> iterator() {
99 return IntStream.range(0, size()).mapToObj(i -> getRegister(i)).iterator();
103 * Get register data as a hex string
105 * For example, 04 45 00 00
107 * @return string representing the bytes of the register array
109 public String toHexString() {
113 // Initialize capacity to (n*2 + n-1), two chars per byte + spaces in between
114 StringBuffer buffer = new StringBuffer(size() * 2 + (size() - 1));
115 return appendHexString(buffer).toString();
119 * Appends the register data as hex string to the given StringBuffer
122 public StringBuffer appendHexString(StringBuffer buffer) {
123 IntStream.range(0, size()).forEachOrdered(index -> {
124 getRegister(index).appendHexString(buffer);
125 if (index < size() - 1) {