You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by ld...@apache.org on 2020/11/12 00:24:26 UTC

[plc4x] branch issue/PLC4X-258 created (now af212c6)

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

ldywicki pushed a change to branch issue/PLC4X-258
in repository https://gitbox.apache.org/repos/asf/plc4x.git.


      at af212c6  PLC4X-258 Replace lambdas which static classes.

This branch includes the following new commits:

     new af212c6  PLC4X-258 Replace lambdas which static classes.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[plc4x] 01/01: PLC4X-258 Replace lambdas which static classes.

Posted by ld...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ldywicki pushed a commit to branch issue/PLC4X-258
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit af212c627327f3d6f21d2ae5f781465953e7b62a
Author: Ɓukasz Dywicki <lu...@code-house.org>
AuthorDate: Sun Nov 8 01:23:00 2020 +0100

    PLC4X-258 Replace lambdas which static classes.
    
    Lambdas which are present on stack trace have a big impact on debugging capabilities.
    They do not point place in code which is faulty, instead they represent kind of named state for code which formed it.
    In order to improve overall developer experience swap to regular classes gives us a possiblity to find which `only` call refused message from further processing.
---
 .../apache/plc4x/java/spi/Plc4xNettyWrapper.java   |  5 ++-
 .../spi/internal/DefaultSendRequestContext.java    | 46 +++++++++++++++++++---
 2 files changed, 43 insertions(+), 8 deletions(-)

diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/Plc4xNettyWrapper.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/Plc4xNettyWrapper.java
index 6955d39..dba2566 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/Plc4xNettyWrapper.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/Plc4xNettyWrapper.java
@@ -157,7 +157,8 @@ public class Plc4xNettyWrapper<T> extends MessageToMessageCodec<T, Object> {
                         Predicate predicate = either.get();
                         if (predicate.test(instance) == false) {
                             // We do not match -> cannot handle
-                            logger.trace("Registration {} does not match object {} (currently wrapped to {})", registration, t.getClass().getSimpleName(), instance.getClass().getSimpleName());
+                            logger.trace("Registration {} with predicate {} does not match object {} (currently wrapped to {})", registration, predicate,
+                                t.getClass().getSimpleName(), instance.getClass().getSimpleName());
                             continue registrations;
                         }
                     }
@@ -171,7 +172,7 @@ public class Plc4xNettyWrapper<T> extends MessageToMessageCodec<T, Object> {
                 return;
             }
         }
-        logger.trace("No registered handler found for message {}, using default decode method", t);
+        logger.trace("None of {} registered handlers could handle message {}, using default decode method", this.registeredHandlers.size(), t);
         protocolBase.decode(new DefaultConversationContext<>(channelHandlerContext, passive), t);
     }
 
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/DefaultSendRequestContext.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/DefaultSendRequestContext.java
index 376c0fd..51141a6 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/DefaultSendRequestContext.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/DefaultSendRequestContext.java
@@ -77,7 +77,7 @@ public class DefaultSendRequestContext<T> implements ConversationContext.SendReq
             throw new ConversationContext.PlcWiringException("can't expect class of type " + clazz + " as we already expecting clazz of type " + expectClazz);
         }
         expectClazz = clazz;
-        commands.addLast(Either.right(clazz::isInstance));
+        commands.addLast(Either.right(new TypePredicate<>(clazz)));
         return this;
     }
 
@@ -123,9 +123,7 @@ public class DefaultSendRequestContext<T> implements ConversationContext.SendReq
             throw new ConversationContext.PlcWiringException("expectResponse must be called before first unwrap");
         }
         if (onTimeoutConsumer == null) {
-            onTimeoutConsumer = e -> {
-                // NOOP
-            };
+            onTimeoutConsumer = new NoopTimeoutConsumer();
         }
         commands.addLast(Either.left(unwrapper));
         return new DefaultSendRequestContext<>(commands, timeout, finisher, request, context, expectClazz, packetConsumer, onTimeoutConsumer, errorConsumer);
@@ -133,8 +131,44 @@ public class DefaultSendRequestContext<T> implements ConversationContext.SendReq
 
     @Override
     public <R> ConversationContext.SendRequestContext<R> only(Class<R> clazz) {
-        this.check(clazz::isInstance);
-        return this.unwrap(clazz::cast);
+        this.check(new TypePredicate<>(clazz));
+        return this.unwrap(new CastFunction<>(clazz));
     }
 
+    static class TypePredicate<T, R> implements Predicate<R> {
+
+        private final Class<T> type;
+
+        TypePredicate(Class<T> type) {
+            this.type = type;
+        }
+
+        @Override
+        public boolean test(R value) {
+            return type.isInstance(value);
+        }
+    }
+
+    static class CastFunction<T, R> implements Function<R, T> {
+
+        private final Class<T> type;
+
+        CastFunction(Class<T> type) {
+            this.type = type;
+        }
+
+        @Override
+        public T apply(R value) {
+            return type.cast(value);
+        }
+
+    }
+
+    static class NoopTimeoutConsumer implements Consumer<TimeoutException> {
+
+        @Override
+        public void accept(TimeoutException e) {
+
+        }
+    }
 }