CAY-2413 swap inner classes for lambdas

Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/87d7e89b
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/87d7e89b
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/87d7e89b

Branch: refs/heads/master
Commit: 87d7e89b90f70a71d740dd22ff3d7c7064289de3
Parents: bd8f5c0
Author: Nikita Timofeev <stari...@gmail.com>
Authored: Wed Mar 7 15:00:31 2018 +0300
Committer: Nikita Timofeev <stari...@gmail.com>
Committed: Wed Mar 7 15:00:31 2018 +0300

----------------------------------------------------------------------
 .../dbsync/naming/CallbackNameBuilder.java      |  29 ++--
 .../dbsync/naming/DeduplicationVisitor.java     | 161 ++++++-------------
 2 files changed, 65 insertions(+), 125 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/87d7e89b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/naming/CallbackNameBuilder.java
----------------------------------------------------------------------
diff --git 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/naming/CallbackNameBuilder.java
 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/naming/CallbackNameBuilder.java
index 7f01eb8..a37df53 100644
--- 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/naming/CallbackNameBuilder.java
+++ 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/naming/CallbackNameBuilder.java
@@ -39,28 +39,25 @@ class CallbackNameBuilder extends NameBuilder {
                 ? this.baseName
                 : "onEvent";
 
-        return new DeduplicationVisitor(parent, baseName, 
dupesPattern).resolve(new DeduplicationVisitor.Predicate() {
-            @Override
-            public boolean isNameInUse(String name) {
+        return new DeduplicationVisitor(parent, baseName, 
dupesPattern).resolve(name -> {
 
-                ObjEntity entity = (ObjEntity) parent;
+            ObjEntity entity = (ObjEntity) parent;
 
-                if (entity.getCallbackMethods().contains(name)) {
-                    return true;
-                }
+            if (entity.getCallbackMethods().contains(name)) {
+                return true;
+            }
 
-                if (name.startsWith("get")) {
-                    String conflictingProperty = 
NameUtil.uncapitalize(name.substring(3));
+            if (name.startsWith("get")) {
+                String conflictingProperty = 
NameUtil.uncapitalize(name.substring(3));
 
-                    // check if either attribute or relationship name 
matches...
-                    if (entity.getAttribute(conflictingProperty) != null
-                            || entity.getRelationship(conflictingProperty) != 
null) {
-                        return true;
-                    }
+                // check if either attribute or relationship name matches...
+                if (entity.getAttribute(conflictingProperty) != null
+                        || entity.getRelationship(conflictingProperty) != 
null) {
+                    return true;
                 }
-
-                return false;
             }
+
+            return false;
         });
     }
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/87d7e89b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/naming/DeduplicationVisitor.java
----------------------------------------------------------------------
diff --git 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/naming/DeduplicationVisitor.java
 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/naming/DeduplicationVisitor.java
index cd42d32..4a59067 100644
--- 
a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/naming/DeduplicationVisitor.java
+++ 
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/naming/DeduplicationVisitor.java
@@ -36,11 +36,11 @@ import org.apache.cayenne.map.ProcedureParameter;
 import org.apache.cayenne.map.QueryDescriptor;
 
 import java.util.Objects;
+import java.util.function.Predicate;
 
 /**
  * @since 4.0
  */
-// TODO: swap inner classes for lambdas when we are on java 8
 class DeduplicationVisitor implements ConfigurationNodeVisitor<String> {
 
     private ConfigurationNode parent;
@@ -61,85 +61,56 @@ class DeduplicationVisitor implements 
ConfigurationNodeVisitor<String> {
 
     @Override
     public String visitDataNodeDescriptor(DataNodeDescriptor nodeDescriptor) {
-        return resolve(new Predicate() {
-            @Override
-            public boolean isNameInUse(String name) {
-
-                DataChannelDescriptor dataChannelDescriptor = 
(DataChannelDescriptor) parent;
-                for (DataNodeDescriptor dataNodeDescriptor : 
dataChannelDescriptor.getNodeDescriptors()) {
-                    if (dataNodeDescriptor.getName().equals(name)) {
-                        return true;
-                    }
+        return resolve(name -> {
+            DataChannelDescriptor dataChannelDescriptor = 
(DataChannelDescriptor) parent;
+            for (DataNodeDescriptor dataNodeDescriptor : 
dataChannelDescriptor.getNodeDescriptors()) {
+                if (dataNodeDescriptor.getName().equals(name)) {
+                    return true;
                 }
-
-                return false;
             }
+
+            return false;
         });
     }
 
     @Override
     public String visitDataMap(DataMap dataMap) {
-        return resolve(new Predicate() {
-            @Override
-            public boolean isNameInUse(String name) {
-
-                // null context is a situation when DataMap is a
-                // top level object of the project
-                if (parent == null) {
-                    return false;
-                }
-
-                if (parent instanceof DataChannelDescriptor) {
-                    DataChannelDescriptor domain = (DataChannelDescriptor) 
parent;
-                    return domain.getDataMap(name) != null;
-                }
+        return resolve(name -> {
+            // null context is a situation when DataMap is a
+            // top level object of the project
+            if (parent == null) {
                 return false;
             }
+
+            if (parent instanceof DataChannelDescriptor) {
+                DataChannelDescriptor domain = (DataChannelDescriptor) parent;
+                return domain.getDataMap(name) != null;
+            }
+            return false;
         });
     }
 
     @Override
     public String visitObjEntity(ObjEntity entity) {
-        return resolve(new Predicate() {
-            @Override
-            public boolean isNameInUse(String name) {
-                DataMap map = (DataMap) parent;
-                return map.getObjEntity(name) != null;
-            }
-        });
+        return resolve(name -> ((DataMap) parent).getObjEntity(name) != null);
     }
 
     @Override
     public String visitDbEntity(DbEntity entity) {
-        return resolve(new Predicate() {
-            @Override
-            public boolean isNameInUse(String name) {
-                DataMap map = (DataMap) parent;
-                return map.getDbEntity(name) != null;
-            }
-        });
+        return resolve(name -> ((DataMap) parent).getDbEntity(name) != null);
     }
 
     @Override
     public String visitEmbeddable(Embeddable embeddable) {
-        return resolve(new Predicate() {
-            @Override
-            public boolean isNameInUse(String name) {
-                DataMap map = (DataMap) parent;
-                return map.getEmbeddable(map.getNameWithDefaultPackage(name)) 
!= null;
-            }
+        return resolve(name -> {
+            DataMap map = (DataMap) parent;
+            return map.getEmbeddable(map.getNameWithDefaultPackage(name)) != 
null;
         });
     }
 
     @Override
     public String visitEmbeddableAttribute(EmbeddableAttribute attribute) {
-        return resolve(new Predicate() {
-            @Override
-            public boolean isNameInUse(String name) {
-                Embeddable emb = (Embeddable) parent;
-                return emb.getAttribute(name) != null;
-            }
-        });
+        return resolve(name -> ((Embeddable) parent).getAttribute(name) != 
null);
     }
 
     @Override
@@ -164,51 +135,36 @@ class DeduplicationVisitor implements 
ConfigurationNodeVisitor<String> {
 
     @Override
     public String visitProcedure(Procedure procedure) {
-        return resolve(new Predicate() {
-            @Override
-            public boolean isNameInUse(String name) {
-                DataMap map = (DataMap) parent;
-                return map.getProcedure(name) != null;
-            }
-        });
+        return resolve(name -> ((DataMap) parent).getProcedure(name) != null);
     }
 
     @Override
     public String visitProcedureParameter(ProcedureParameter parameter) {
-        return resolve(new Predicate() {
-            @Override
-            public boolean isNameInUse(String name) {
-
-                // it doesn't matter if we create a parameter with a duplicate 
name.. parameters are positional anyway..
-                // still try to use unique names for visual consistency
-
-                Procedure procedure = (Procedure) parent;
-                for (ProcedureParameter parameter : 
procedure.getCallParameters()) {
-                    if (name.equals(parameter.getName())) {
-                        return true;
-                    }
-                }
+        return resolve(name -> {
 
-                return false;
+            // it doesn't matter if we create a parameter with a duplicate 
name.. parameters are positional anyway..
+            // still try to use unique names for visual consistency
+
+            Procedure procedure = (Procedure) parent;
+            for (ProcedureParameter parameter1 : 
procedure.getCallParameters()) {
+                if (name.equals(parameter1.getName())) {
+                    return true;
+                }
             }
+
+            return false;
         });
     }
 
     @Override
     public String visitQuery(QueryDescriptor query) {
-        return resolve(new Predicate() {
-            @Override
-            public boolean isNameInUse(String name) {
-                DataMap map = (DataMap) parent;
-                return map.getQueryDescriptor(name) != null;
-            }
-        });
+        return resolve(name -> ((DataMap) parent).getQueryDescriptor(name) != 
null);
     }
 
-    String resolve(Predicate nameChecker) {
+    String resolve(Predicate<String> nameChecker) {
         int c = 1;
         String name = baseName;
-        while (nameChecker.isNameInUse(name)) {
+        while (nameChecker.test(name)) {
             name = String.format(dupesPattern, baseName, c++);
         }
 
@@ -216,38 +172,25 @@ class DeduplicationVisitor implements 
ConfigurationNodeVisitor<String> {
     }
 
     private String resolveDbEntityProperty() {
-        return resolve(new Predicate() {
-            @Override
-            public boolean isNameInUse(String name) {
-
-                DbEntity entity = (DbEntity) parent;
-
-                // check if either attribute or relationship name matches...
-                return entity.getAttribute(name) != null || 
entity.getRelationship(name) != null;
-            }
+        return resolve(name -> {
+            DbEntity entity = (DbEntity) parent;
+            // check if either attribute or relationship name matches...
+            return entity.getAttribute(name) != null || 
entity.getRelationship(name) != null;
         });
     }
 
     private String resolveObjEntityProperty() {
-        return resolve(new Predicate() {
-            @Override
-            public boolean isNameInUse(String name) {
-
-                ObjEntity entity = (ObjEntity) parent;
+        return resolve(name -> {
+            ObjEntity entity = (ObjEntity) parent;
 
-                // check if either attribute or relationship name matches...
-                if (entity.getAttribute(name) != null || 
entity.getRelationship(name) != null) {
-                    return true;
-                }
-
-                //  check if there's a callback method that shadows attribute 
getter (unlikely, but still)
-                String conflictingCallback = "get" + NameUtil.capitalize(name);
-                return 
entity.getCallbackMethods().contains(conflictingCallback);
+            // check if either attribute or relationship name matches...
+            if (entity.getAttribute(name) != null || 
entity.getRelationship(name) != null) {
+                return true;
             }
-        });
-    }
 
-    interface Predicate {
-        boolean isNameInUse(String name);
+            //  check if there's a callback method that shadows attribute 
getter (unlikely, but still)
+            String conflictingCallback = "get" + NameUtil.capitalize(name);
+            return entity.getCallbackMethods().contains(conflictingCallback);
+        });
     }
 }

Reply via email to