This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git


The following commit(s) were added to refs/heads/master by this push:
     new 3216a62  added support for hex values in address string
3216a62 is described below

commit 3216a62beaf6b0345754f593378f67573d3fe48b
Author: Sebastian Rühl <sru...@apache.org>
AuthorDate: Thu May 17 16:31:46 2018 +0200

    added support for hex values in address string
---
 .../apache/plc4x/java/ads/model/AdsAddress.java    | 38 ++++++++++++++++++----
 .../plc4x/java/ads/model/SymbolicAdsAddress.java   | 11 +++++--
 .../plc4x/java/ads/model/AdsAddressTest.java       |  7 ++++
 3 files changed, 48 insertions(+), 8 deletions(-)

diff --git 
a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/model/AdsAddress.java
 
b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/model/AdsAddress.java
index c90d574..2f6c6a2 100644
--- 
a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/model/AdsAddress.java
+++ 
b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/model/AdsAddress.java
@@ -25,8 +25,12 @@ import java.util.Objects;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+/**
+ * ADS address witch is defined by {@code indexGroup/indexOffset}. These 
values can be either supplied as int or hex
+ * representation.
+ */
 public class AdsAddress implements Address {
-    private static final Pattern RESOURCE_ADDRESS_PATTERN = 
Pattern.compile("^(?<indexGroup>\\d+)/(?<indexOffset>\\d+)");
+    private static final Pattern RESOURCE_ADDRESS_PATTERN = 
Pattern.compile("^((0[xX](?<indexGroupHex>[0-9a-fA-F]+))|(?<indexGroup>\\d+))/((0[xX](?<indexOffsetHex>[0-9a-fA-F]+))|(?<indexOffset>\\d+))");
 
     private final long indexGroup;
 
@@ -49,10 +53,28 @@ public class AdsAddress implements Address {
             throw new IllegalArgumentException(
                 "address " + address + " doesn't match 
'{indexGroup}/{indexOffset}' RAW:" + RESOURCE_ADDRESS_PATTERN);
         }
-        String indexGroup = matcher.group("indexGroup");
-        String indexOffset = matcher.group("indexOffset");
 
-        return new AdsAddress(Long.parseLong(indexGroup), 
Long.parseLong(indexOffset));
+        String indexGroupStringHex = matcher.group("indexGroupHex");
+        String indexGroupString = matcher.group("indexGroup");
+
+        String indexOffsetStringHex = matcher.group("indexOffsetHex");
+        String indexOffsetString = matcher.group("indexOffset");
+
+        Long indexGroup;
+        if (indexGroupStringHex != null) {
+            indexGroup = Long.parseLong(indexGroupStringHex, 16);
+        } else {
+            indexGroup = Long.parseLong(indexGroupString);
+        }
+
+        Long indexOffset;
+        if (indexOffsetStringHex != null) {
+            indexOffset = Long.parseLong(indexOffsetStringHex, 16);
+        } else {
+            indexOffset = Long.parseLong(indexOffsetString);
+        }
+
+        return new AdsAddress(indexGroup, indexOffset);
     }
 
     public static boolean matches(String address) {
@@ -69,8 +91,12 @@ public class AdsAddress implements Address {
 
     @Override
     public boolean equals(Object o) {
-        if (this == o) return true;
-        if (!(o instanceof AdsAddress)) return false;
+        if (this == o) {
+            return true;
+        }
+        if (!(o instanceof AdsAddress)) {
+            return false;
+        }
         AdsAddress that = (AdsAddress) o;
         return indexGroup == that.indexGroup &&
             indexOffset == that.indexOffset;
diff --git 
a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/model/SymbolicAdsAddress.java
 
b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/model/SymbolicAdsAddress.java
index 3d91477..2bf000c 100644
--- 
a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/model/SymbolicAdsAddress.java
+++ 
b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/model/SymbolicAdsAddress.java
@@ -24,6 +24,9 @@ import java.util.Objects;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+/**
+ * ADS address witch is defined by symbolic name (e.g. {@code Main.items[0]}).
+ */
 public class SymbolicAdsAddress implements Address {
     private static final Pattern SYMBOLIC_ADDRESS_PATTERN = 
Pattern.compile("^(?<symbolicAddress>.+)");
 
@@ -54,8 +57,12 @@ public class SymbolicAdsAddress implements Address {
 
     @Override
     public boolean equals(Object o) {
-        if (this == o) return true;
-        if (!(o instanceof SymbolicAdsAddress)) return false;
+        if (this == o) {
+            return true;
+        }
+        if (!(o instanceof SymbolicAdsAddress)) {
+            return false;
+        }
         SymbolicAdsAddress that = (SymbolicAdsAddress) o;
         return Objects.equals(symbolicAddress, that.symbolicAddress);
     }
diff --git 
a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/model/AdsAddressTest.java
 
b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/model/AdsAddressTest.java
index f577a47..f3348da 100644
--- 
a/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/model/AdsAddressTest.java
+++ 
b/plc4j/protocols/ads/src/test/java/org/apache/plc4x/java/ads/model/AdsAddressTest.java
@@ -32,6 +32,13 @@ public class AdsAddressTest {
         assertThat(address.getIndexOffset(), is(10L));
     }
 
+    @Test
+    public void ofHex() {
+        AdsAddress address = AdsAddress.of("0x1/0xff");
+        assertThat(address.getIndexGroup(), is(1L));
+        assertThat(address.getIndexOffset(), is(255L));
+    }
+
     @Test(expected  = IllegalArgumentException.class)
     public void stringInAddress() {
         AdsAddress address = AdsAddress.of("group/offset");

-- 
To stop receiving notification emails like this one, please contact
sru...@apache.org.

Reply via email to