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.binding.dsmr.internal.device.cosem;
15 import java.text.ParseException;
16 import java.util.Objects;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
24 * Class representing an OBISIdentifier
26 * @author M. Volaart - Initial contribution
27 * @author Hilbrand Bouwkamp - Fix bug in regex pattern.
30 public class OBISIdentifier {
32 * String representing a-b:c.d.e.f OBIS ID
34 private static final String OBISID_REGEX = "((\\d+)\\-)?((\\d+):)?((\\d+)\\.)(\\d+)(\\.(\\d+))?(.(\\d+))?";
39 private static final Pattern OBIS_ID_PATTERN = Pattern.compile(OBISID_REGEX);
41 /* the six individual group values of the OBIS ID */
43 private @Nullable Integer groupB;
46 private @Nullable Integer groupE;
47 private @Nullable Integer groupF;
49 private boolean conflict;
52 * Constructs a new OBIS Identifier (A-B:C.D.E.F)
54 * @param groupA A value
55 * @param groupB B value
56 * @param groupC C value
57 * @param groupD D value
58 * @param groupE E value
59 * @param groupF F value
61 public OBISIdentifier(int groupA, @Nullable Integer groupB, int groupC, int groupD, @Nullable Integer groupE,
62 @Nullable Integer groupF) {
63 this(groupA, groupB, groupC, groupD, groupE, groupF, false);
67 * Constructs a new OBIS Identifier (A-B:C.D.E.F)
69 * @param groupA A value
70 * @param groupB B value
71 * @param groupC C value
72 * @param groupD D value
73 * @param groupE E value
74 * @param groupF F value
75 * @param conflict if true indicates this OBIS Identifier is used for different types of data.
77 public OBISIdentifier(int groupA, @Nullable Integer groupB, int groupC, int groupD, @Nullable Integer groupE,
78 @Nullable Integer groupF, boolean conflict) {
85 this.conflict = conflict;
89 * Creates a new {@link OBISIdentifier} of the specified String
91 * @param obisIDString the OBIS String ID
92 * @throws ParseException if obisIDString is not a valid OBIS Identifier
94 public OBISIdentifier(String obisIDString) throws ParseException {
95 Matcher m = OBIS_ID_PATTERN.matcher(obisIDString);
99 if (m.group(2) != null) {
100 this.groupA = Integer.parseInt(m.group(2));
104 if (m.group(4) != null) {
105 this.groupB = Integer.valueOf(m.group(4));
108 // Required value C & D
109 this.groupC = Integer.parseInt(m.group(6));
110 this.groupD = Integer.parseInt(m.group(7));
113 if (m.group(9) != null) {
114 this.groupE = Integer.valueOf(m.group(9));
118 if (m.group(11) != null) {
119 this.groupF = Integer.valueOf(m.group(11));
122 throw new ParseException("Invalid OBIS identifier:" + obisIDString, 0);
126 public boolean isConflict() {
133 public int getGroupA() {
140 public @Nullable Integer getGroupB() {
147 public int getGroupC() {
154 public int getGroupD() {
161 public @Nullable Integer getGroupE() {
168 public @Nullable Integer getGroupF() {
173 public String toString() {
174 return groupA + "-" + (groupB == null ? "" : (groupB + ":")) + groupC + "." + groupD
175 + (groupE == null ? "" : ("." + groupE)) + (groupF == null ? "" : ("*" + groupF));
179 * Returns whether or not both {@link OBISIdentifier} are exact equal (all identifiers match).
181 * If wild card matching is needed (since some fields are null in case of a wildcard) use
182 * {@link #equalsWildCard(OBISIdentifier)} instead
184 * @return true if both OBISIdentifiers match, false otherwise
187 public boolean equals(@Nullable Object other) {
189 if (other != null && other instanceof OBISIdentifier) {
190 o = (OBISIdentifier) other;
194 boolean result = true;
196 result &= groupA == o.groupA;
197 if (groupB != null && o.groupB != null) {
198 result &= (groupB.equals(o.groupB));
199 } else if (!(groupB == null && o.groupB == null)) {
202 result &= groupC == o.groupC;
203 result &= groupD == o.groupD;
204 if (groupE != null && o.groupE != null) {
205 result &= groupE.equals(o.groupE);
206 } else if (!(groupE == null && o.groupE == null)) {
209 if (groupF != null && o.groupF != null) {
210 result &= (groupF.equals(o.groupF));
211 } else if (!(groupF == null && o.groupF == null)) {
219 * Checks whether this OBIS Identifier and the other identifier equals taking the wildcards into account
221 * @param o OBISIdentifier to compare to
223 * @return true if identifiers match fully or against a wildcard, false otherwise
225 public boolean equalsWildCard(OBISIdentifier o) {
226 boolean result = true;
228 result &= groupA == o.groupA;
229 if (groupB != null && o.groupB != null) {
230 result &= (groupB.equals(o.groupB));
232 result &= groupC == o.groupC;
233 result &= groupD == o.groupD;
234 if (groupE != null && o.groupE != null) {
235 result &= (groupE.equals(o.groupE));
237 if (groupF != null && o.groupF != null) {
238 result &= (groupF.equals(o.groupF));
245 public int hashCode() {
246 return Objects.hash(groupA, (groupB != null ? groupB : 0), groupC, groupD, (groupE != null ? groupE : 0),
247 (groupF != null ? groupF : 0));
251 * Returns an reduced OBIS Identifier. This means group F is set to null
252 * (.i.e. not applicable)
254 * @return reduced OBIS Identifier
256 public OBISIdentifier getReducedOBISIdentifier() {
257 return new OBISIdentifier(groupA, groupB, groupC, groupD, groupE, null);
261 * Returns an reduced OBIS Identifier with both group E and F is set to null
262 * (.i.e. not applicable)
264 * @return reduced OBIS Identifier
266 public OBISIdentifier getReducedOBISIdentifierGroupE() {
267 return new OBISIdentifier(groupA, groupB, groupC, groupD, null, null);
271 * Returns whether or not the reduced OBIS Identifier is a wildcard identifier (meaning groupA groupB or groupC is
273 * Note that the DSMR specification does not use groupF so this is implemented always as a wildcard.
274 * To distinguish wildcard from non wildcard OBISIdentifiers, groupF is ignored.
276 * @return true if the reducedOBISIdentifier is a wildcard identifier, false otherwise.
278 public boolean reducedOBISIdentifierIsWildCard() {
279 return groupB == null;