cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From skolbac...@apache.org
Subject cayenne git commit: CAY-2029 | Allow out-of-order insertion for ListBuilder.addAll(Collection<T> objects)
Date Mon, 26 Oct 2015 09:50:56 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master 28c7db937 -> 9ddd0c92b


CAY-2029 | Allow out-of-order insertion for ListBuilder.addAll(Collection<T> objects)


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

Branch: refs/heads/master
Commit: 9ddd0c92b9441d88a8543b43c16c30f6b1518f34
Parents: 28c7db9
Author: Savva Kolbachev <s.kolbachev@gmail.com>
Authored: Mon Oct 26 12:50:21 2015 +0300
Committer: Savva Kolbachev <s.kolbachev@gmail.com>
Committed: Mon Oct 26 12:50:21 2015 +0300

----------------------------------------------------------------------
 .../java/org/apache/cayenne/di/spi/DIGraph.java | 10 ++++++
 .../cayenne/di/spi/DefaultListBuilder.java      | 10 +++---
 .../org/apache/cayenne/di/spi/ListProvider.java | 35 +++++++++++++++---
 .../di/spi/DefaultInjectorInjectionTest.java    | 37 ++++++++++++++++++++
 4 files changed, 84 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/9ddd0c92/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DIGraph.java
----------------------------------------------------------------------
diff --git a/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DIGraph.java b/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DIGraph.java
index 8277d90..5de1cd8 100644
--- a/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DIGraph.java
+++ b/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DIGraph.java
@@ -21,6 +21,7 @@ package org.apache.cayenne.di.spi;
 
 import java.util.ArrayDeque;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Deque;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
@@ -56,6 +57,15 @@ class DIGraph<V> {
 		neighbors.put(vertex, new ArrayList<V>());
 	}
 
+    /**
+     * Add vertexes to the graph.
+     */
+    public void addAll(Collection<V> vertexes) {
+        for (V vertex : vertexes) {
+            this.add(vertex);
+        }
+    }
+
 	/**
 	 * Add an edge to the graph; if either vertex does not exist, it's added.
 	 * This implementation allows the creation of multi-edges and self-loops.

http://git-wip-us.apache.org/repos/asf/cayenne/blob/9ddd0c92/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultListBuilder.java
----------------------------------------------------------------------
diff --git a/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultListBuilder.java b/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultListBuilder.java
index e3f741b..853bb5e 100644
--- a/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultListBuilder.java
+++ b/cayenne-di/src/main/java/org/apache/cayenne/di/spi/DefaultListBuilder.java
@@ -21,12 +21,14 @@ package org.apache.cayenne.di.spi;
 import org.apache.cayenne.di.DIRuntimeException;
 import org.apache.cayenne.di.Key;
 import org.apache.cayenne.di.ListBuilder;
-import org.apache.cayenne.di.UnorderedListBuilder;
 import org.apache.cayenne.di.Provider;
 import org.apache.cayenne.di.Scope;
+import org.apache.cayenne.di.UnorderedListBuilder;
 
 import java.util.Collection;
+import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Map;
 
 /**
  * @since 3.1
@@ -99,15 +101,15 @@ class DefaultListBuilder<T> implements ListBuilder<T> {
     @Override
     public ListBuilder<T> addAll(Collection<T> objects) throws DIRuntimeException
{
 
-        ListProvider listProvider = getListProvider();
-
+        Map<Key<?>, Provider<?>> keyProviderMap = new LinkedHashMap<>();
         for (T object : objects) {
             Provider<T> provider0 = new InstanceProvider<T>(object);
             Provider<T> provider1 = new FieldInjectingProvider<T>(provider0,
injector);
 
-            listProvider.add(Key.get(object.getClass(), String.valueOf(object.hashCode())),
provider1);
+            keyProviderMap.put(Key.get(object.getClass(), String.valueOf(object.hashCode())),
provider1);
         }
 
+        getListProvider().addAll(keyProviderMap);
         return this;
     }
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/9ddd0c92/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ListProvider.java
----------------------------------------------------------------------
diff --git a/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ListProvider.java b/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ListProvider.java
index 1ea4b50..2178635 100644
--- a/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ListProvider.java
+++ b/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ListProvider.java
@@ -23,6 +23,8 @@ import org.apache.cayenne.di.Key;
 import org.apache.cayenne.di.Provider;
 
 import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -35,10 +37,12 @@ class ListProvider implements Provider<List<?>> {
     private Map<Key<?>, Provider<?>> providers;
     private DIGraph<Key<?>> graph;
     private Key<?> lastKey;
+    private Collection<Key<?>> lastKeys;
 
     public ListProvider() {
         this.providers = new HashMap<>();
         this.graph = new DIGraph<>();
+        this.lastKeys = Collections.emptySet();
     }
 
     @Override
@@ -72,14 +76,37 @@ class ListProvider implements Provider<List<?>> {
         providers.put(key, provider);
         graph.add(key);
         lastKey = key;
+        lastKeys.clear();
     }
 
-    void after(Key<?> key) {
-        graph.add(lastKey, key);
+    void addAll(Map<Key<?>, Provider<?>> keyProviderMap) {
+        providers.putAll(keyProviderMap);
+        graph.addAll(keyProviderMap.keySet());
+        lastKeys = keyProviderMap.keySet();
     }
 
-    void before(Key<?> key) {
-        graph.add(key, lastKey);
+    void after(Key<?> after) {
+        if (!lastKeys.isEmpty()) {
+            for (Key<?> key : lastKeys) {
+                graph.add(key, after);
+            }
+
+            return;
+        }
+
+        graph.add(lastKey, after);
+    }
+
+    void before(Key<?> before) {
+        if (!lastKeys.isEmpty()) {
+            for (Key<?> key: lastKeys) {
+                graph.add(before, key);
+            }
+
+            return;
+        }
+
+        graph.add(before, lastKey);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/9ddd0c92/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorInjectionTest.java
----------------------------------------------------------------------
diff --git a/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorInjectionTest.java
b/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorInjectionTest.java
index 26f44b6..31b9cd3 100644
--- a/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorInjectionTest.java
+++ b/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorInjectionTest.java
@@ -43,6 +43,9 @@ import org.apache.cayenne.di.mock.MockInterface4;
 import org.apache.cayenne.di.mock.MockInterface5;
 import org.junit.Test;
 
+import java.util.ArrayList;
+import java.util.Collection;
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
@@ -298,6 +301,40 @@ public class DefaultInjectorInjectionTest {
     }
 
     @Test
+    public void testListInjection_addAllValues() {
+        Module module = new Module() {
+            @Override
+            public void configure(Binder binder) {
+                binder.bind(MockInterface1.class).to(
+                        MockImplementation1_ListConfiguration.class);
+
+                Collection firstList = new ArrayList<>();
+                firstList.add("1value");
+                firstList.add("2value");
+                firstList.add("3value");
+
+                Collection secondList = new ArrayList<>();
+                secondList.add("6value");
+                secondList.add("7value");
+                secondList.add("8value");
+                binder.bindList("xyz")
+                        .add(Key.get(Object.class, "4value"), "4value")
+                        .addAll(firstList)
+                        .before(Key.get(Object.class, "4value"))
+                        .addAll(secondList)
+                        .after(Key.get(Object.class, "5value"))
+                        .add(Key.get(Object.class, "5value"), "5value");
+            }
+        };
+
+        DefaultInjector injector = new DefaultInjector(module);
+
+        MockInterface1 service = injector.getInstance(MockInterface1.class);
+        assertNotNull(service);
+        assertEquals(";1value;2value;3value;4value;5value;6value;7value;8value", service.getName());
+    }
+
+    @Test
     public void testListInjection_addType() {
         Module module = new Module() {
 


Mime
View raw message