You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by dk...@apache.org on 2015/06/05 20:46:41 UTC

[11/26] incubator-tinkerpop git commit: Refactor RangeLocalStep.applyRange for readability

Refactor RangeLocalStep.applyRange for readability


Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/c26b87ce
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/c26b87ce
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/c26b87ce

Branch: refs/heads/preprocessor
Commit: c26b87ce8c763f5d9cdcb735078b685a0af85b82
Parents: 25ed1fb
Author: mhfrantz <mf...@redsealnetworks.com>
Authored: Thu Jun 4 12:02:31 2015 -0700
Committer: mhfrantz <mf...@redsealnetworks.com>
Committed: Thu Jun 4 12:02:31 2015 -0700

----------------------------------------------------------------------
 .../traversal/step/map/RangeLocalStep.java      | 62 +++++++++++---------
 1 file changed, 35 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c26b87ce/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/RangeLocalStep.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/RangeLocalStep.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/RangeLocalStep.java
index 3e5b752..33673b8 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/RangeLocalStep.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/RangeLocalStep.java
@@ -64,39 +64,47 @@ public final class RangeLocalStep<S> extends MapStep<S, S> {
      * </li>
      * </ul>
      */
-    static <S> S applyRange(S start, long low, long high) {
+    static <S> S applyRange(final S start, final long low, final long high) {
         if (start instanceof Map) {
-            final Map map = (Map) start;
-            final long capacity = (high != -1 ? high : map.size()) - low;
-            final Map result = new LinkedHashMap((int) Math.min(capacity, map.size()));
-            long c = 0L;
-            for (final Object obj : map.entrySet()) {
-                final Map.Entry entry = (Map.Entry) obj;
-                if (c >= low) {
-                    if (c < high || high == -1) {
-                        result.put(entry.getKey(), entry.getValue());
-                    } else break;
-                }
-                c++;
-            }
-            return (S) result;
+            return (S) applyRangeMap((Map) start, low, high);
         } else if (start instanceof Collection) {
-            final Collection collection = (Collection) start;
-            final Collection result = (collection instanceof Set) ? new LinkedHashSet() : new LinkedList();
-            long c = 0L;
-            for (final Object item : collection) {
-                if (c >= low) {
-                    if (c < high || high == -1) {
-                        result.add(item);
-                    } else break;
-                }
-                c++;
-            }
-            return (S) result;
+            return (S) applyRangeCollection((Collection) start, low, high);
         }
         return start;
     }
 
+    /** Extracts specified range of elements from a Map. */
+    private static Map applyRangeMap(final Map map, final long low, final long high) {
+        final long capacity = (high != -1 ? high : map.size()) - low;
+        final Map result = new LinkedHashMap((int) Math.min(capacity, map.size()));
+        long c = 0L;
+        for (final Object obj : map.entrySet()) {
+            final Map.Entry entry = (Map.Entry) obj;
+            if (c >= low) {
+                if (c < high || high == -1) {
+                    result.put(entry.getKey(), entry.getValue());
+                } else break;
+            }
+            c++;
+        }
+        return result;
+    }
+
+    /** Extracts specified range of elements from a Collection. */
+    private static Collection applyRangeCollection(final Collection collection, final long low, final long high) {
+        final Collection result = (collection instanceof Set) ? new LinkedHashSet() : new LinkedList();
+        long c = 0L;
+        for (final Object item : collection) {
+            if (c >= low) {
+                if (c < high || high == -1) {
+                    result.add(item);
+                } else break;
+            }
+            c++;
+        }
+        return result;
+    }
+
     @Override
     public String toString() {
         return StringFactory.stepString(this, this.low, this.high);