]> source.dussan.org Git - poi.git/commitdiff
replace CombinedIterable with a few lines of simple code
authorAndreas Beeker <kiwiwings@apache.org>
Sun, 23 May 2021 12:04:27 +0000 (12:04 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Sun, 23 May 2021 12:04:27 +0000 (12:04 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1890135 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/main/java/org/apache/poi/xdgf/usermodel/XDGFShape.java
poi-ooxml/src/main/java/org/apache/poi/xdgf/usermodel/section/CombinedIterable.java [deleted file]
poi-ooxml/src/main/java/org/apache/poi/xdgf/usermodel/section/GeometrySection.java
poi-ooxml/src/test/java/org/apache/poi/xdgf/usermodel/section/TestCombinedIterator.java

index 2f22ab805ff990a409b79ff0d4d3a47fb4a889e4..95084e2ed9386e40915807b52386268d0583be04 100644 (file)
@@ -17,6 +17,8 @@
 
 package org.apache.poi.xdgf.usermodel;
 
+import static org.apache.poi.xdgf.usermodel.section.GeometrySection.combineGeometries;
+
 import java.awt.BasicStroke;
 import java.awt.Color;
 import java.awt.Stroke;
@@ -33,7 +35,6 @@ import com.microsoft.schemas.office.visio.x2012.main.TextType;
 import org.apache.poi.ooxml.POIXMLException;
 import org.apache.poi.util.Internal;
 import org.apache.poi.xdgf.exceptions.XDGFException;
-import org.apache.poi.xdgf.usermodel.section.CombinedIterable;
 import org.apache.poi.xdgf.usermodel.section.GeometrySection;
 import org.apache.poi.xdgf.usermodel.section.XDGFSection;
 import org.apache.poi.xdgf.usermodel.shape.ShapeVisitor;
@@ -832,8 +833,7 @@ public class XDGFShape extends XDGFSheet {
     //
 
     public Iterable<GeometrySection> getGeometrySections() {
-        return new CombinedIterable<>(_geometry,
-                _masterShape != null ? _masterShape._geometry : null);
+        return combineGeometries(_geometry, _masterShape != null ? _masterShape._geometry : null);
     }
 
     /**
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xdgf/usermodel/section/CombinedIterable.java b/poi-ooxml/src/main/java/org/apache/poi/xdgf/usermodel/section/CombinedIterable.java
deleted file mode 100644 (file)
index a89e26c..0000000
+++ /dev/null
@@ -1,135 +0,0 @@
-/* ====================================================================
-   Licensed to the Apache Software Foundation (ASF) under one or more
-   contributor license agreements.  See the NOTICE file distributed with
-   this work for additional information regarding copyright ownership.
-   The ASF licenses this file to You under the Apache License, Version 2.0
-   (the "License"); you may not use this file except in compliance with
-   the License.  You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
-==================================================================== */
-
-package org.apache.poi.xdgf.usermodel.section;
-
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.Map.Entry;
-import java.util.NoSuchElementException;
-import java.util.SortedMap;
-
-/**
- * An iterator used to iterate over the base and master items
- */
-public class CombinedIterable<T> implements Iterable<T> {
-
-    final SortedMap<Long, T> _baseItems;
-    final SortedMap<Long, T> _masterItems;
-
-    public CombinedIterable(SortedMap<Long, T> baseItems,
-            SortedMap<Long, T> masterItems) {
-        _baseItems = baseItems;
-        _masterItems = masterItems;
-    }
-
-    @Override
-    public Iterator<T> iterator() {
-
-        final Iterator<Entry<Long, T>> vmasterI = (_masterItems == null)
-            ? Collections.emptyIterator() : _masterItems.entrySet().iterator();
-
-        return new Iterator<T>() {
-
-            Long lastI = Long.MIN_VALUE;
-
-            Entry<Long, T> currentBase;
-            Entry<Long, T> currentMaster;
-
-            // grab the iterator for both
-            final Iterator<Entry<Long, T>> baseI = _baseItems.entrySet().iterator();
-            final Iterator<Entry<Long, T>> masterI = vmasterI;
-
-            @Override
-            public boolean hasNext() {
-                return currentBase != null || currentMaster != null
-                        || baseI.hasNext() || masterI.hasNext();
-            }
-
-            @Override
-            public T next() {
-
-                // TODO: This seems far more complex than it needs to be
-
-                long baseIdx = Long.MAX_VALUE;
-                long masterIdx = Long.MAX_VALUE;
-
-                if (currentBase == null) {
-                    while (baseI.hasNext()) {
-                        currentBase = baseI.next();
-                        if (currentBase.getKey() > lastI) {
-                            baseIdx = currentBase.getKey();
-                            break;
-                        }
-                    }
-                } else {
-                    baseIdx = currentBase.getKey();
-                }
-
-                if (currentMaster == null) {
-                    while (masterI.hasNext()) {
-                        currentMaster = masterI.next();
-                        if (currentMaster.getKey() > lastI) {
-                            masterIdx = currentMaster.getKey();
-                            break;
-                        }
-                    }
-                } else {
-                    masterIdx = currentMaster.getKey();
-                }
-
-                T val;
-
-                if (currentBase != null) {
-
-                    if (baseIdx <= masterIdx) {
-                        lastI = baseIdx;
-                        val = currentBase.getValue();
-
-                        // discard master if same as base
-                        if (masterIdx == baseIdx) {
-                            currentMaster = null;
-                        }
-
-                        currentBase = null;
-
-                    } else {
-                        lastI = masterIdx;
-                        val = (currentMaster != null) ? currentMaster.getValue() : null;
-                        currentMaster = null;
-                    }
-
-                } else if (currentMaster != null) {
-                    lastI = currentMaster.getKey();
-                    val = currentMaster.getValue();
-
-                    currentMaster = null;
-                } else {
-                    throw new NoSuchElementException();
-                }
-
-                return val;
-            }
-
-            @Override
-            public void remove() {
-                throw new UnsupportedOperationException();
-            }
-        };
-    }
-
-}
index b9da05f1f542409f48ddb3a6182e0d71cc97a8c7..75e9b60365442b3e5cb1239be32a9ff65bda0fa7 100644 (file)
@@ -18,6 +18,7 @@
 package org.apache.poi.xdgf.usermodel.section;
 
 import java.awt.geom.Path2D;
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.Map.Entry;
 import java.util.SortedMap;
@@ -26,6 +27,7 @@ import java.util.TreeMap;
 import com.microsoft.schemas.office.visio.x2012.main.RowType;
 import com.microsoft.schemas.office.visio.x2012.main.SectionType;
 import org.apache.poi.ooxml.POIXMLException;
+import org.apache.poi.util.Internal;
 import org.apache.poi.xdgf.geom.SplineCollector;
 import org.apache.poi.xdgf.usermodel.XDGFCell;
 import org.apache.poi.xdgf.usermodel.XDGFShape;
@@ -86,9 +88,20 @@ public class GeometrySection extends XDGFSection {
         return noShow;
     }
 
+    @Internal
+    public static <T,S extends SortedMap<Long,T>> Collection<T> combineGeometries(S map1, S map2) {
+        SortedMap<Long,T> map;
+        if (map2 == null) {
+            map = map1;
+        } else {
+            map = new TreeMap<>(map2);
+            map.putAll(map1);
+        }
+        return map.values();
+    }
+
     public Iterable<GeometryRow> getCombinedRows() {
-        return new CombinedIterable<>(_rows,
-                _master == null ? null : _master._rows);
+        return combineGeometries(_rows, _master == null ? null : _master._rows);
     }
 
     public Path2D.Double getPath(XDGFShape parent) {
index 6232cfd62f21427816547a536e8daac39061af85..48cd8ce30ea8b584b76554cecd5615c116ecbe12 100644 (file)
@@ -17,6 +17,7 @@
 
 package org.apache.poi.xdgf.usermodel.section;
 
+import static org.apache.poi.xdgf.usermodel.section.GeometrySection.combineGeometries;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -49,7 +50,7 @@ class TestCombinedIterator {
         base.put(2L, "B2");
         base.put(3L, "B3");
 
-        testIteration(createIter(base, null), "B1", "B2", "B3");
+        testIteration(combineGeometries(base, null), "B1", "B2", "B3");
     }
 
     @Test
@@ -65,7 +66,7 @@ class TestCombinedIterator {
         master.put(5L, "M5");
         master.put(6L, "M6");
 
-        testIteration(createIter(base, master), "B1", "B2", "B3", "M4", "M5", "M6");
+        testIteration(combineGeometries(base, master), "B1", "B2", "B3", "M4", "M5", "M6");
     }
 
     @Test
@@ -81,7 +82,7 @@ class TestCombinedIterator {
         master.put(2L, "M2");
         master.put(3L, "M3");
 
-        testIteration(createIter(base, master), "M1", "M2", "M3", "B4", "B5", "B6");
+        testIteration(combineGeometries(base, master), "M1", "M2", "M3", "B4", "B5", "B6");
     }
 
     @Test
@@ -97,7 +98,7 @@ class TestCombinedIterator {
         master.put(4L, "M4");
         master.put(6L, "M6");
 
-        testIteration(createIter(base, master), "B1", "M2", "B3", "M4", "B5", "M6");
+        testIteration(combineGeometries(base, master), "B1", "M2", "B3", "M4", "B5", "M6");
     }
 
     @Test
@@ -115,7 +116,7 @@ class TestCombinedIterator {
         master.put(7L, "M7");
         master.put(8L, "M8");
 
-        testIteration(createIter(base, master), "B1", "B2", "M3", "M4", "B5", "B6", "M7", "M8");
+        testIteration(combineGeometries(base, master), "B1", "B2", "M3", "M4", "B5", "B6", "M7", "M8");
     }
 
     @Test
@@ -131,7 +132,7 @@ class TestCombinedIterator {
         master.put(2L, "M2");
         master.put(3L, "M3");
 
-        testIteration(createIter(base, master), "B1", "B2", "B3");
+        testIteration(combineGeometries(base, master), "B1", "B2", "B3");
     }
 
     @Test
@@ -148,12 +149,6 @@ class TestCombinedIterator {
         master.put(3L, "M3");
         master.put(4L, "M4");
 
-        testIteration(createIter(base, master), "B1", "B2", "B3", "M4");
-    }
-
-
-    private static <T> Iterable<T> createIter(SortedMap<Long, T> map1, SortedMap<Long, T> map2) {
-        // TODO: try to use commons collection and remove CombinedIterable
-        return new CombinedIterable<>(map1, map2);
+        testIteration(combineGeometries(base, master), "B1", "B2", "B3", "M4");
     }
 }