2 * Copyright (c) 2010-2022 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.binding.serial.internal.util;
15 import java.util.Arrays;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.io.transport.serial.SerialPort;
21 * Enum to convert config parity value to serial port value
23 * @author Mike Major - Initial contribution
27 NONE("N", SerialPort.PARITY_NONE),
28 ODD("O", SerialPort.PARITY_ODD),
29 EVEN("E", SerialPort.PARITY_EVEN),
30 MARK("M", SerialPort.PARITY_MARK),
31 SPACE("S", SerialPort.PARITY_SPACE);
33 final String configValue;
34 final int serialPortValue;
36 private Parity(final String configValue, final int serialPortValue) {
37 this.configValue = configValue;
38 this.serialPortValue = serialPortValue;
42 * Return the serial port value
44 * @return the serial port value
46 public int getSerialPortValue() {
47 return serialPortValue;
51 * Return the enum value from the config value
53 * @param configValue the config value
54 * @return the enum value
56 public static Parity fromConfig(final String configValue) {
57 return Arrays.asList(values()).stream().filter(p -> p.configValue.equals(configValue)).findFirst().orElse(NONE);