You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2018/01/22 20:39:10 UTC

[isis] branch ISIS-1846_internal_utils updated: ISIS-1827 guava free drop-in replacement for all ExceptionRecognizers

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

ahuber pushed a commit to branch ISIS-1846_internal_utils
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/ISIS-1846_internal_utils by this push:
     new 03823d7  ISIS-1827 guava free drop-in replacement for all ExceptionRecognizers
03823d7 is described below

commit 03823d7f3ce95d73f944fc935e1a1dfd930f5177
Author: Andi Huber <ah...@apache.org>
AuthorDate: Mon Jan 22 21:39:07 2018 +0100

    ISIS-1827 guava free drop-in replacement for all ExceptionRecognizers
---
 .../exceprecog/ExceptionRecognizerAbstract.java    | 57 ++++++++++++++++----
 .../exceprecog/ExceptionRecognizerForType.java     |  1 +
 ...rType.java => ExceptionRecognizerForType2.java} | 63 +++++++++++-----------
 ...xceptionRecognizerForJDODataStoreException.java |  4 +-
 ...traintViolationForeignKeyNoActionException.java | 17 +++---
 ...ionRecognizerForJDOObjectNotFoundException.java |  5 +-
 ...yConstraintViolationUniqueOrIndexException.java | 17 +++---
 7 files changed, 104 insertions(+), 60 deletions(-)

diff --git a/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/ExceptionRecognizerAbstract.java b/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/ExceptionRecognizerAbstract.java
index 43628bd..f586624 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/ExceptionRecognizerAbstract.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/ExceptionRecognizerAbstract.java
@@ -20,18 +20,20 @@ package org.apache.isis.applib.services.exceprecog;
 
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
+import java.util.function.Predicate;
+
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 
-import com.google.common.base.Function;
-import com.google.common.base.Functions;
-import com.google.common.base.Predicate;
-import com.google.common.base.Throwables;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.apache.isis.applib.annotation.Programmatic;
 import org.apache.isis.applib.services.i18n.TranslatableString;
 import org.apache.isis.applib.services.i18n.TranslationService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Throwables;
 
 /**
  * Abstract implementation of {@link ExceptionRecognizer} that looks 
@@ -93,11 +95,14 @@ public abstract class ExceptionRecognizerAbstract implements ExceptionRecognizer
     private boolean logRecognizedExceptions;
 
     // //////////////////////////////////////
-
+    
+    // -- JAVA 8+
+    
     public ExceptionRecognizerAbstract(final Category category, Predicate<Throwable> predicate, final Function<String,String> messageParser) {
-        this.category = category;
+    	Objects.requireNonNull(predicate);
+    	this.category = category;
         this.predicate = predicate;
-        this.messageParser = messageParser != null? messageParser: Functions.<String>identity();
+        this.messageParser = messageParser != null ? messageParser : Function.identity();
     }
 
     public ExceptionRecognizerAbstract(Predicate<Throwable> predicate, final Function<String,String> messageParser) {
@@ -112,6 +117,38 @@ public abstract class ExceptionRecognizerAbstract implements ExceptionRecognizer
         this(Category.OTHER, predicate);
     }
 
+    // -- GUAVA LEGACY
+    
+    @Deprecated //TODO ISIS-1827 remove guava from public API
+    public ExceptionRecognizerAbstract(final Category category, 
+    		com.google.common.base.Predicate<Throwable> predicate, 
+    		final com.google.common.base.Function<String,String> messageParser) {
+    	Objects.requireNonNull(predicate);
+        this.category = category;
+        this.predicate = predicate::apply;
+        this.messageParser = messageParser != null ? messageParser::apply : Function.identity();
+    }
+
+    @Deprecated //TODO ISIS-1827 remove guava from public API
+    public ExceptionRecognizerAbstract(
+    		com.google.common.base.Predicate<Throwable> predicate, 
+    		final com.google.common.base.Function<String,String> messageParser) {
+        this(Category.OTHER, predicate, messageParser);
+    }
+
+    @Deprecated //TODO ISIS-1827 remove guava from public API
+    public ExceptionRecognizerAbstract(Category category, com.google.common.base.Predicate<Throwable> predicate) {
+        this(category, predicate, null);
+    }
+
+    @Deprecated //TODO ISIS-1827 remove guava from public API
+    public ExceptionRecognizerAbstract(com.google.common.base.Predicate<Throwable> predicate) {
+        this(Category.OTHER, predicate);
+    }
+
+    // --
+    
+    
 
     @PostConstruct
     public void init(Map<String, String> properties) {
@@ -129,7 +166,7 @@ public abstract class ExceptionRecognizerAbstract implements ExceptionRecognizer
     public String recognize(Throwable ex) {
         List<Throwable> causalChain = Throwables.getCausalChain(ex);
         for (Throwable throwable : causalChain) {
-            if(predicate.apply(throwable)) {
+            if(predicate.test(throwable)) {
                 if(logRecognizedExceptions) {
                     LOG.info("Recognized exception, stacktrace : ", throwable);
                 }
diff --git a/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/ExceptionRecognizerForType.java b/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/ExceptionRecognizerForType.java
index fa5b652..ff3653c 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/ExceptionRecognizerForType.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/ExceptionRecognizerForType.java
@@ -35,6 +35,7 @@ import com.google.common.base.Throwables;
  * If a messaging-parsing {@link Function} is provided through the constructor,
  * then the message can be altered.  Otherwise the exception's {@link Throwable#getMessage() message} is returned as-is.
  */
+@Deprecated //TODO ISIS-1827 remove guava from public API
 public class ExceptionRecognizerForType extends ExceptionRecognizerAbstract {
 
     protected final static Predicate<Throwable> ofTypeExcluding(final Class<? extends Throwable> exceptionType, final String... messages) {
diff --git a/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/ExceptionRecognizerForType.java b/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/ExceptionRecognizerForType2.java
similarity index 73%
copy from core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/ExceptionRecognizerForType.java
copy to core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/ExceptionRecognizerForType2.java
index fa5b652..99a9ab8 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/ExceptionRecognizerForType.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/ExceptionRecognizerForType2.java
@@ -19,10 +19,11 @@
 package org.apache.isis.applib.services.exceprecog;
 
 import java.util.List;
+import java.util.function.Function;
+import java.util.function.Predicate;
+
 import javax.jdo.JDODataStoreException;
-import com.google.common.base.Function;
-import com.google.common.base.Predicate;
-import com.google.common.base.Predicates;
+
 import com.google.common.base.Throwables;
 
 /**
@@ -35,23 +36,18 @@ import com.google.common.base.Throwables;
  * If a messaging-parsing {@link Function} is provided through the constructor,
  * then the message can be altered.  Otherwise the exception's {@link Throwable#getMessage() message} is returned as-is.
  */
-public class ExceptionRecognizerForType extends ExceptionRecognizerAbstract {
-
-    protected final static Predicate<Throwable> ofTypeExcluding(final Class<? extends Throwable> exceptionType, final String... messages) {
-        return Predicates.and(ofType(exceptionType), excluding(messages));
+public class ExceptionRecognizerForType2 extends ExceptionRecognizerAbstract {
+	
+	protected final static Predicate<Throwable> ofTypeExcluding(final Class<? extends Throwable> exceptionType, final String... messages) {
+        return ofType(exceptionType).and(excluding(messages));
     }
 
     protected final static Predicate<Throwable> ofTypeIncluding(final Class<? extends Throwable> exceptionType, final String... messages) {
-        return Predicates.and(ofType(exceptionType), including(messages));
+        return ofType(exceptionType).and(including(messages));
     }
     
     protected final static Predicate<Throwable> ofType(final Class<? extends Throwable> exceptionType) {
-        return new Predicate<Throwable>() {
-            @Override
-            public boolean apply(Throwable input) {
-                return exceptionType.isAssignableFrom(input.getClass());
-            }
-        };
+        return input->exceptionType.isAssignableFrom(input.getClass());
     }
     
     /**
@@ -63,9 +59,7 @@ public class ExceptionRecognizerForType extends ExceptionRecognizerAbstract {
      * Intended to prevent too eager matching of an overly general exception type.
      */
     protected final static Predicate<Throwable> excluding(final String... messages) {
-        return new Predicate<Throwable>() {
-            @Override
-            public boolean apply(Throwable input) {
+        return input->{
                 final List<Throwable> causalChain = Throwables.getCausalChain(input);
                 for (String message : messages) {
                     for (Throwable throwable : causalChain) {
@@ -86,8 +80,8 @@ public class ExceptionRecognizerForType extends ExceptionRecognizerAbstract {
                     }
                 }
                 return true;
-            }
-        };
+            };
+
     }
 
     /**
@@ -99,9 +93,7 @@ public class ExceptionRecognizerForType extends ExceptionRecognizerAbstract {
      * Intended to prevent more precise matching of a specific general exception type.
      */
     protected final static Predicate<Throwable> including(final String... messages) {
-        return new Predicate<Throwable>() {
-            @Override
-            public boolean apply(Throwable input) {
+        return input->{
                 final List<Throwable> causalChain = Throwables.getCausalChain(input);
                 for (String message : messages) {
                     for (Throwable throwable : causalChain) {
@@ -112,32 +104,41 @@ public class ExceptionRecognizerForType extends ExceptionRecognizerAbstract {
                     }
                 }
                 return false;
-            }
-        };
+            };
     }
 
-    public ExceptionRecognizerForType(Category category, final Class<? extends Exception> exceptionType, final Function<String,String> messageParser) {
+    public ExceptionRecognizerForType2(
+    		Category category, 
+    		final Class<? extends Exception> exceptionType, 
+    		final Function<String,String> messageParser) {
         this(category, ofType(exceptionType), messageParser);
     }
     
-    public ExceptionRecognizerForType(Category category, final Predicate<Throwable> predicate, final Function<String,String> messageParser) {
+    public ExceptionRecognizerForType2(
+    		Category category, 
+    		final Predicate<Throwable> predicate, 
+    		final Function<String,String> messageParser) {
         super(category, predicate, messageParser);
     }
     
-    public ExceptionRecognizerForType(Category category, Class<? extends Exception> exceptionType) {
+    public ExceptionRecognizerForType2(Category category, Class<? extends Exception> exceptionType) {
         this(category, exceptionType, null);
     }
 
-    public ExceptionRecognizerForType(final Class<? extends Exception> exceptionType, final Function<String,String> messageParser) {
+    public ExceptionRecognizerForType2(
+    		final Class<? extends Exception> exceptionType, 
+    		final Function<String,String> messageParser) {
         this(Category.OTHER, exceptionType, messageParser);
     }
 
-    public ExceptionRecognizerForType(final Predicate<Throwable> predicate, final Function<String,String> messageParser) {
+    public ExceptionRecognizerForType2(
+    		final Predicate<Throwable> predicate, 
+    		final Function<String,String> messageParser) {
         this(Category.OTHER, predicate, messageParser);
     }
 
-    public ExceptionRecognizerForType(Class<? extends Exception> exceptionType) {
+    public ExceptionRecognizerForType2(Class<? extends Exception> exceptionType) {
         this(Category.OTHER, exceptionType);
     }
-
+	
 }
diff --git a/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/jdo/ExceptionRecognizerForJDODataStoreException.java b/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/jdo/ExceptionRecognizerForJDODataStoreException.java
index b52fc16..2273bbe 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/jdo/ExceptionRecognizerForJDODataStoreException.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/jdo/ExceptionRecognizerForJDODataStoreException.java
@@ -18,9 +18,9 @@
  */
 package org.apache.isis.applib.services.exceprecog.jdo;
 
-import org.apache.isis.applib.services.exceprecog.ExceptionRecognizerForType;
+import org.apache.isis.applib.services.exceprecog.ExceptionRecognizerForType2;
 
-public class ExceptionRecognizerForJDODataStoreException extends ExceptionRecognizerForType {
+public class ExceptionRecognizerForJDODataStoreException extends ExceptionRecognizerForType2 {
 
     public ExceptionRecognizerForJDODataStoreException() {
         super(Category.SERVER_ERROR,
diff --git a/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/jdo/ExceptionRecognizerForJDODataStoreExceptionIntegrityConstraintViolationForeignKeyNoActionException.java b/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/jdo/ExceptionRecognizerForJDODataStoreExceptionIntegrityConstraintViolationForeignKeyNoActionException.java
index c9d4717..0e495a6 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/jdo/ExceptionRecognizerForJDODataStoreExceptionIntegrityConstraintViolationForeignKeyNoActionException.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/jdo/ExceptionRecognizerForJDODataStoreExceptionIntegrityConstraintViolationForeignKeyNoActionException.java
@@ -19,14 +19,17 @@
 package org.apache.isis.applib.services.exceprecog.jdo;
 
 import javax.jdo.JDODataStoreException;
-import org.apache.isis.applib.services.exceprecog.ExceptionRecognizerForType;
+import org.apache.isis.applib.services.exceprecog.ExceptionRecognizerForType2;
 
-public class ExceptionRecognizerForJDODataStoreExceptionIntegrityConstraintViolationForeignKeyNoActionException extends ExceptionRecognizerForType {
+public class ExceptionRecognizerForJDODataStoreExceptionIntegrityConstraintViolationForeignKeyNoActionException 
+extends ExceptionRecognizerForType2 {
 
-    public ExceptionRecognizerForJDODataStoreExceptionIntegrityConstraintViolationForeignKeyNoActionException() {
-        super(Category.CONSTRAINT_VIOLATION,
-              ofTypeIncluding(JDODataStoreException.class, "integrity constraint violation: foreign key no action"),
-                prefix("Related data exists"));
-    }
+	public ExceptionRecognizerForJDODataStoreExceptionIntegrityConstraintViolationForeignKeyNoActionException() {
+		super(Category.CONSTRAINT_VIOLATION,
+				ofTypeIncluding(
+						JDODataStoreException.class, 
+						"integrity constraint violation: foreign key no action"),
+				prefix("Related data exists"));
+	}
 
 }
diff --git a/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/jdo/ExceptionRecognizerForJDOObjectNotFoundException.java b/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/jdo/ExceptionRecognizerForJDOObjectNotFoundException.java
index 594fbac..ec315c3 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/jdo/ExceptionRecognizerForJDOObjectNotFoundException.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/jdo/ExceptionRecognizerForJDOObjectNotFoundException.java
@@ -18,10 +18,9 @@
  */
 package org.apache.isis.applib.services.exceprecog.jdo;
 
-import org.apache.isis.applib.services.exceprecog.ExceptionRecognizerForType;
+import org.apache.isis.applib.services.exceprecog.ExceptionRecognizerForType2;
 
-public class ExceptionRecognizerForJDOObjectNotFoundException
-        extends ExceptionRecognizerForType {
+public class ExceptionRecognizerForJDOObjectNotFoundException extends ExceptionRecognizerForType2 {
 
     public ExceptionRecognizerForJDOObjectNotFoundException() {
         super(Category.NOT_FOUND,
diff --git a/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/jdo/ExceptionRecognizerForSQLIntegrityConstraintViolationUniqueOrIndexException.java b/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/jdo/ExceptionRecognizerForSQLIntegrityConstraintViolationUniqueOrIndexException.java
index eee0462..cf74abe 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/jdo/ExceptionRecognizerForSQLIntegrityConstraintViolationUniqueOrIndexException.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/services/exceprecog/jdo/ExceptionRecognizerForSQLIntegrityConstraintViolationUniqueOrIndexException.java
@@ -18,14 +18,17 @@
  */
 package org.apache.isis.applib.services.exceprecog.jdo;
 
-import org.apache.isis.applib.services.exceprecog.ExceptionRecognizerForType;
+import org.apache.isis.applib.services.exceprecog.ExceptionRecognizerForType2;
 
-public class ExceptionRecognizerForSQLIntegrityConstraintViolationUniqueOrIndexException extends ExceptionRecognizerForType {
+public class ExceptionRecognizerForSQLIntegrityConstraintViolationUniqueOrIndexException 
+extends ExceptionRecognizerForType2 {
 
-    public ExceptionRecognizerForSQLIntegrityConstraintViolationUniqueOrIndexException() {
-        super(Category.CONSTRAINT_VIOLATION,
-              ofTypeIncluding(java.sql.SQLIntegrityConstraintViolationException.class, "unique constraint or index violation"),
-                prefix("Data already exists"));
-    }
+	public ExceptionRecognizerForSQLIntegrityConstraintViolationUniqueOrIndexException() {
+		super(Category.CONSTRAINT_VIOLATION,
+				ofTypeIncluding(
+						java.sql.SQLIntegrityConstraintViolationException.class, 
+						"unique constraint or index violation"),
+				prefix("Data already exists"));
+	}
 
 }

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