]> git.basschouten.com Git - openhab-addons.git/blob
84b7dddff1cd32f3df9aeb9b019bbde8440e74a7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.lutron.internal.protocol.leap;
14
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 /**
22  *
23  * Abstract base class for LEAP message body objects
24  *
25  * @author Bob Adair - Initial contribution
26  */
27 @NonNullByDefault
28 public abstract class AbstractMessageBody {
29
30     /**
31      * Utility method to extract the int from a href String using the supplied Pattern.
32      *
33      * @return the int or 0 if unable to extract
34      */
35     protected static int hrefNumber(Pattern pattern, @Nullable String href) {
36         if (href == null) {
37             return 0;
38         }
39         Matcher matcher = pattern.matcher(href);
40         if (matcher.find()) {
41             try {
42                 return Integer.parseInt(matcher.group(1));
43             } catch (NumberFormatException e) {
44                 return 0;
45             }
46         } else {
47             return 0;
48         }
49     }
50 }