]> git.basschouten.com Git - openhab-addons.git/blob
1daf52e82089c610cd24e4831a65c6403bc09403
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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 stopBits value to serial port value
22  *
23  * @author Mike Major - Initial contribution
24  */
25 @NonNullByDefault
26 public enum StopBits {
27     STOPBITS_1("1", SerialPort.STOPBITS_1),
28     STOPBITS_1_5("1.5", SerialPort.STOPBITS_1_5),
29     STOPBITS_2("2", SerialPort.STOPBITS_2);
30
31     final String configValue;
32     final int serialPortValue;
33
34     private StopBits(final String configValue, final int serialPortValue) {
35         this.configValue = configValue;
36         this.serialPortValue = serialPortValue;
37     }
38
39     /**
40      * Return the serial port value
41      *
42      * @return the serial port value
43      */
44     public int getSerialPortValue() {
45         return serialPortValue;
46     }
47
48     /**
49      * Return the enum value from the config value
50      *
51      * @param configValue the config value
52      * @return the enum value
53      */
54     public static StopBits fromConfig(final String configValue) {
55         return Arrays.asList(values()).stream().filter(p -> p.configValue.equals(configValue)).findFirst()
56                 .orElse(STOPBITS_1);
57     }
58 }