You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2018/01/05 13:34:19 UTC

[incubator-plc4x] branch refactoring/java_generify updated: added request build for convenient request creation.

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

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


The following commit(s) were added to refs/heads/refactoring/java_generify by this push:
     new fd66d1d  added request build for convenient request creation.
fd66d1d is described below

commit fd66d1da6a9af9f70dc6e8f324827a29c1bed8ab
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri Jan 5 14:34:15 2018 +0100

    added request build for convenient request creation.
---
 .../plc4x/java/api/messages/PlcReadRequest.java    | 93 ++++++++++++++++++++++
 .../plc4x/java/api/messages/PlcWriteRequest.java   | 92 +++++++++++++++++++++
 2 files changed, 185 insertions(+)

diff --git a/plc4j/api/src/main/java/org/apache/plc4x/java/api/messages/PlcReadRequest.java b/plc4j/api/src/main/java/org/apache/plc4x/java/api/messages/PlcReadRequest.java
index 324aa16..1d9f37a 100644
--- a/plc4j/api/src/main/java/org/apache/plc4x/java/api/messages/PlcReadRequest.java
+++ b/plc4j/api/src/main/java/org/apache/plc4x/java/api/messages/PlcReadRequest.java
@@ -19,7 +19,9 @@ under the License.
 package org.apache.plc4x.java.api.messages;
 
 import org.apache.plc4x.java.api.messages.items.ReadRequestItem;
+import org.apache.plc4x.java.api.model.Address;
 
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Optional;
 
@@ -41,5 +43,96 @@ public interface PlcReadRequest extends PlcRequest {
     default int getNumberOfItems() {
         return getReadRequestItems().size();
     }
+
+    static Builder builder() {
+        return new Builder();
+    }
+
+    class Builder {
+
+        private Class firstType;
+
+        private boolean mixed = false;
+
+        private List<ReadRequestItem> requests = new LinkedList<>();
+
+        public <T> Builder addItem(Class<T> dataType, Address address) {
+            checkType(dataType);
+            requests.add(new ReadRequestItem<>(dataType, address));
+            return this;
+        }
+
+        public <T> Builder addItem(Class<T> dataType, Address address, int size) {
+            checkType(dataType);
+            requests.add(new ReadRequestItem<>(dataType, address, size));
+            return this;
+        }
+
+        private void checkType(Class dataType) {
+            if (firstType != null) {
+                firstType = dataType;
+            }
+            if (firstType != dataType) {
+                mixed = true;
+            }
+        }
+
+        @SuppressWarnings("unchecked")
+        public PlcReadRequest build() {
+            if (requests.size() < 1) {
+                throw new IllegalStateException("No requests added");
+            }
+            if (requests.size() < 2) {
+                SinglePlcReadRequest<?> singlePlcReadRequest = new SinglePlcReadRequest<>();
+                singlePlcReadRequest.addItem(requests.get(0));
+                return singlePlcReadRequest;
+            }
+            PlcReadRequest plcReadRequest;
+            if (mixed) {
+                plcReadRequest = new BulkPlcReadRequest();
+            } else {
+                plcReadRequest = new CheckedBulkPlcReadRequest<>(firstType);
+            }
+            for (ReadRequestItem request : requests) {
+                plcReadRequest.addItem(request);
+            }
+            return plcReadRequest;
+        }
+
+        @SuppressWarnings("unchecked")
+        public BulkPlcReadRequest buildBulk() {
+            if (requests.size() < 2) {
+                throw new IllegalStateException("Bulk request needs more than one request");
+            }
+            return (BulkPlcReadRequest) build();
+        }
+
+        @SuppressWarnings("unchecked")
+        public <T> SinglePlcReadRequest<T> build(Class<T> type) {
+            if (requests.size() != 1) {
+                throw new IllegalStateException("Checked request needs exactly one request");
+            }
+            if (firstType != type) {
+                throw new ClassCastException("Incompatible type " + type + ". Required " + firstType);
+            }
+            return (SinglePlcReadRequest<T>) build();
+        }
+
+        @SuppressWarnings("unchecked")
+        public <T> CheckedBulkPlcReadRequest<T> buildBulk(Class<T> type) {
+            if (requests.size() < 2) {
+                throw new IllegalStateException("Checked bulk request needs more than one request");
+            }
+            if (firstType != type) {
+                throw new ClassCastException("Incompatible type " + type + ". Required " + firstType);
+            }
+            if (mixed) {
+                throw new IllegalStateException("Mixed types contained");
+            }
+            return (CheckedBulkPlcReadRequest<T>) build();
+        }
+
+    }
+
 }
 
diff --git a/plc4j/api/src/main/java/org/apache/plc4x/java/api/messages/PlcWriteRequest.java b/plc4j/api/src/main/java/org/apache/plc4x/java/api/messages/PlcWriteRequest.java
index 46cdf3b..aa4eced 100644
--- a/plc4j/api/src/main/java/org/apache/plc4x/java/api/messages/PlcWriteRequest.java
+++ b/plc4j/api/src/main/java/org/apache/plc4x/java/api/messages/PlcWriteRequest.java
@@ -19,7 +19,9 @@ under the License.
 package org.apache.plc4x.java.api.messages;
 
 import org.apache.plc4x.java.api.messages.items.WriteRequestItem;
+import org.apache.plc4x.java.api.model.Address;
 
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Optional;
 
@@ -41,4 +43,94 @@ public interface PlcWriteRequest extends PlcRequest {
     default int getNumberOfItems() {
         return getRequestItems().size();
     }
+
+    static PlcWriteRequest.Builder builder() {
+        return new Builder();
+    }
+
+    class Builder {
+
+        private Class firstType;
+
+        private boolean mixed = false;
+
+        private List<WriteRequestItem> requests = new LinkedList<>();
+
+        public <T> PlcWriteRequest.Builder addItem(Class<T> dataType, Address address, T value) {
+            checkType(dataType);
+            requests.add(new WriteRequestItem<>(dataType, address, value));
+            return this;
+        }
+
+        public <T> PlcWriteRequest.Builder addItem(Class<T> dataType, Address address, T[] values) {
+            checkType(dataType);
+            requests.add(new WriteRequestItem<>(dataType, address, values));
+            return this;
+        }
+
+        private void checkType(Class dataType) {
+            if (firstType != null) {
+                firstType = dataType;
+            }
+            if (firstType != dataType) {
+                mixed = true;
+            }
+        }
+
+        @SuppressWarnings("unchecked")
+        public PlcWriteRequest build() {
+            if (requests.size() < 1) {
+                throw new IllegalStateException("No requests added");
+            }
+            if (requests.size() < 2) {
+                SinglePlcWriteRequest<?> singlePlcWriteRequest = new SinglePlcWriteRequest<>();
+                singlePlcWriteRequest.addItem(requests.get(0));
+                return singlePlcWriteRequest;
+            }
+            PlcWriteRequest plcWriteRequest;
+            if (mixed) {
+                plcWriteRequest = new BulkPlcWriteRequest();
+            } else {
+                plcWriteRequest = new CheckedBulkPlcWriteRequest<>(firstType);
+            }
+            for (WriteRequestItem request : requests) {
+                plcWriteRequest.addItem(request);
+            }
+            return plcWriteRequest;
+        }
+
+        @SuppressWarnings("unchecked")
+        public BulkPlcWriteRequest buildBulk() {
+            if (requests.size() < 2) {
+                throw new IllegalStateException("Bulk request needs more than one request");
+            }
+            return (BulkPlcWriteRequest) build();
+        }
+
+        @SuppressWarnings("unchecked")
+        public <T> SinglePlcWriteRequest<T> build(Class<T> type) {
+            if (requests.size() != 1) {
+                throw new IllegalStateException("Checked request needs exactly one request");
+            }
+            if (firstType != type) {
+                throw new ClassCastException("Incompatible type " + type + ". Required " + firstType);
+            }
+            return (SinglePlcWriteRequest<T>) build();
+        }
+
+        @SuppressWarnings("unchecked")
+        public <T> CheckedBulkPlcWriteRequest<T> buildBulk(Class<T> type) {
+            if (requests.size() < 2) {
+                throw new IllegalStateException("Checked bulk request needs more than one request");
+            }
+            if (firstType != type) {
+                throw new ClassCastException("Incompatible type " + type + ". Required " + firstType);
+            }
+            if (mixed) {
+                throw new IllegalStateException("Mixed types contained");
+            }
+            return (CheckedBulkPlcWriteRequest<T>) build();
+        }
+
+    }
 }

-- 
To stop receiving notification emails like this one, please contact
['"commits@plc4x.apache.org" <co...@plc4x.apache.org>'].