]> git.basschouten.com Git - openhab-addons.git/blob
13df948093d7fa3e6942474f3842c5a0628e3341
[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.serial.internal.util;
14
15 import java.util.Arrays;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.io.transport.serial.SerialPort;
19
20 /**
21  * Enum to convert config parity value to serial port value
22  *
23  * @author Mike Major - Initial contribution
24  */
25 @NonNullByDefault
26 public enum Parity {
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);
32
33     final String configValue;
34     final int serialPortValue;
35
36     private Parity(final String configValue, final int serialPortValue) {
37         this.configValue = configValue;
38         this.serialPortValue = serialPortValue;
39     }
40
41     /**
42      * Return the serial port value
43      *
44      * @return the serial port value
45      */
46     public int getSerialPortValue() {
47         return serialPortValue;
48     }
49
50     /**
51      * Return the enum value from the config value
52      *
53      * @param configValue the config value
54      * @return the enum value
55      */
56     public static Parity fromConfig(final String configValue) {
57         return Arrays.asList(values()).stream().filter(p -> p.configValue.equals(configValue)).findFirst().orElse(NONE);
58     }
59 }