hutcheb commented on a change in pull request #172:
URL: https://github.com/apache/plc4x/pull/172#discussion_r456321048



##########
File path: 
plc4j/drivers/modbus/src/main/java/org/apache/plc4x/java/modbus/field/ModbusFieldDiscreteInput.java
##########
@@ -26,21 +26,40 @@ Licensed to the Apache Software Foundation (ASF) under one
 public class ModbusFieldDiscreteInput extends ModbusField {
 
     public static final Pattern ADDRESS_PATTERN = 
Pattern.compile("discrete-input:" + ModbusField.ADDRESS_PATTERN);
+    public static final Pattern ADDRESS_SHORTER_PATTERN = Pattern.compile("1" 
+ ModbusField.ADDRESS_PATTERN);
+    public static final Pattern ADDRESS_SHORT_PATTERN = Pattern.compile("1x" + 
ModbusField.ADDRESS_PATTERN);
 
     public ModbusFieldDiscreteInput(int address, Integer quantity) {
         super(address, quantity);
     }
 
-    public static ModbusFieldDiscreteInput of(String addressString) throws 
PlcInvalidFieldException {
-        Matcher matcher = ADDRESS_PATTERN.matcher(addressString);
-        if (!matcher.matches()) {
-            throw new PlcInvalidFieldException(addressString, ADDRESS_PATTERN);
+    public static boolean matches(String addressString) {
+        return ADDRESS_PATTERN.matcher(addressString).matches() ||
+            ADDRESS_SHORTER_PATTERN.matcher(addressString).matches() ||
+            ADDRESS_SHORT_PATTERN.matcher(addressString).matches();
+    }
+
+    public static Matcher getMatcher(String addressString) throws 
PlcInvalidFieldException {
+        Matcher matcher;
+        if (ADDRESS_PATTERN.matcher(addressString).matches()) {
+          matcher = ADDRESS_PATTERN.matcher(addressString);
+        } else if (ADDRESS_SHORT_PATTERN.matcher(addressString).matches()) {
+          matcher = ADDRESS_SHORT_PATTERN.matcher(addressString);
+        } else if (ADDRESS_SHORTER_PATTERN.matcher(addressString).matches()) {
+          matcher = ADDRESS_SHORTER_PATTERN.matcher(addressString);
+        } else {
+          throw new PlcInvalidFieldException(addressString, ADDRESS_PATTERN);
         }
-        int address = Integer.parseInt(matcher.group("address"));
+        return matcher;
+    }
+
+    public static ModbusFieldDiscreteInput of(String addressString) throws 
PlcInvalidFieldException {
+        Matcher matcher = getMatcher(addressString);
+        matcher.find();

Review comment:
       Calling the getMatcher mehod checks for the case that the pattern isn't 
found by raising the exception. I moved the exception to that method so I 
didn't have to worry about returning a null value.
   
   I found that the find() call then needs to be added as I wasn't calling the 
matches() method to find the first match. We can ignore the result of this as 
the getMatcher method has already confirmed that at least one instance will be 
found.
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to