You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ma...@apache.org on 2021/11/20 22:53:21 UTC

[commons-geometry] branch master updated: GEOMETRY-141: add GeometryInternalUtils.sameInstance() method to document reference equality checks

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

mattjuntunen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-geometry.git


The following commit(s) were added to refs/heads/master by this push:
     new d0479b8  GEOMETRY-141: add GeometryInternalUtils.sameInstance() method to document reference equality checks
d0479b8 is described below

commit d0479b80fa22b9a411cb475b628ce1395ac0c7a0
Author: Matt Juntunen <ma...@apache.org>
AuthorDate: Sat Nov 20 17:40:10 2021 -0500

    GEOMETRY-141: add GeometryInternalUtils.sameInstance() method to document reference equality checks
---
 .../core/internal/GeometryInternalUtils.java       | 37 ++++++++++++++++++++++
 .../AbstractConvexHyperplaneBoundedRegion.java     |  3 +-
 .../core/partitioning/bsp/AbstractBSPTree.java     |  9 +++---
 .../euclidean/internal/AbstractPathConnector.java  |  4 ++-
 .../twod/path/AbstractLinePathConnector.java       |  3 +-
 .../spherical/twod/AbstractGreatArcConnector.java  |  3 +-
 6 files changed, 51 insertions(+), 8 deletions(-)

diff --git a/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/internal/GeometryInternalUtils.java b/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/internal/GeometryInternalUtils.java
new file mode 100644
index 0000000..8ba46f3
--- /dev/null
+++ b/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/internal/GeometryInternalUtils.java
@@ -0,0 +1,37 @@
+/*
+ * 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.commons.geometry.core.internal;
+
+/** Internal utility methods for <em>commons-geometry</em>.
+ */
+public final class GeometryInternalUtils {
+
+    /** Utility class; no instantiation. */
+    private GeometryInternalUtils() {}
+
+    /** Return {@code true} if {@code a} is the same instance as {@code b}, as
+     * determined by the {@code ==} operator. This method exists primarily to
+     * document the fact that reference equality was intended and is not a
+     * programming error.
+     * @param a first instance
+     * @param b second instance
+     * @return {@code true} if the arguments are the exact same instance
+     */
+    public static boolean sameInstance(final Object a, final Object b) {
+        return a == b;
+    }
+}
diff --git a/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/partitioning/AbstractConvexHyperplaneBoundedRegion.java b/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/partitioning/AbstractConvexHyperplaneBoundedRegion.java
index 2ea2ca9..c789288 100644
--- a/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/partitioning/AbstractConvexHyperplaneBoundedRegion.java
+++ b/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/partitioning/AbstractConvexHyperplaneBoundedRegion.java
@@ -25,6 +25,7 @@ import java.util.function.Function;
 import org.apache.commons.geometry.core.Point;
 import org.apache.commons.geometry.core.RegionLocation;
 import org.apache.commons.geometry.core.Transform;
+import org.apache.commons.geometry.core.internal.GeometryInternalUtils;
 
 /** Base class for convex hyperplane-bounded regions. This class provides generic implementations of many
  * algorithms related to convex regions.
@@ -457,7 +458,7 @@ public abstract class AbstractConvexHyperplaneBoundedRegion<P extends Point<P>,
                 splitter = boundsIt.next();
                 ++splitterIdx;
 
-                if (currentBound == splitter) {
+                if (GeometryInternalUtils.sameInstance(currentBound, splitter)) {
                     // do not split the bound with itself
 
                     if (currentBoundIdx > splitterIdx) {
diff --git a/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/partitioning/bsp/AbstractBSPTree.java b/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/partitioning/bsp/AbstractBSPTree.java
index fc3c37b..9c7687f 100644
--- a/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/partitioning/bsp/AbstractBSPTree.java
+++ b/commons-geometry-core/src/main/java/org/apache/commons/geometry/core/partitioning/bsp/AbstractBSPTree.java
@@ -23,6 +23,7 @@ import java.util.NoSuchElementException;
 
 import org.apache.commons.geometry.core.Point;
 import org.apache.commons.geometry.core.Transform;
+import org.apache.commons.geometry.core.internal.GeometryInternalUtils;
 import org.apache.commons.geometry.core.partitioning.Hyperplane;
 import org.apache.commons.geometry.core.partitioning.HyperplaneConvexSubset;
 import org.apache.commons.geometry.core.partitioning.HyperplaneLocation;
@@ -243,7 +244,7 @@ public abstract class AbstractBSPTree<P extends Point<P>, N extends AbstractBSPT
      */
     protected N copySubtree(final N src, final N dst) {
         // only copy if we're actually switching nodes
-        if (src != dst) {
+        if (!GeometryInternalUtils.sameInstance(src, dst)) {
             // copy non-structural properties
             copyNodeProperties(src, dst);
 
@@ -279,7 +280,7 @@ public abstract class AbstractBSPTree<P extends Point<P>, N extends AbstractBSPT
      */
     protected N importSubtree(final N src) {
         // create a copy of the node if it's not already in this tree
-        if (src.getTree() != this) {
+        if (!GeometryInternalUtils.sameInstance(src.getTree(), this)) {
             return copySubtree(src, createNode());
         }
 
@@ -949,13 +950,13 @@ public abstract class AbstractBSPTree<P extends Point<P>, N extends AbstractBSPT
         /** {@inheritDoc} */
         @Override
         public boolean isPlus() {
-            return parent != null && parent.getPlus() == this;
+            return parent != null && GeometryInternalUtils.sameInstance(parent.getPlus(), this);
         }
 
         /** {@inheritDoc} */
         @Override
         public boolean isMinus() {
-            return parent != null && parent.getMinus() == this;
+            return parent != null && GeometryInternalUtils.sameInstance(parent.getMinus(), this);
         }
 
         /** {@inheritDoc} */
diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/internal/AbstractPathConnector.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/internal/AbstractPathConnector.java
index 2fbe770..bb83390 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/internal/AbstractPathConnector.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/internal/AbstractPathConnector.java
@@ -21,6 +21,8 @@ import java.util.List;
 import java.util.NavigableSet;
 import java.util.TreeSet;
 
+import org.apache.commons.geometry.core.internal.GeometryInternalUtils;
+
 /** Abstract base class for joining unconnected path elements into connected, directional
  * paths. The connection algorithm is exposed as a set of protected methods, allowing subclasses
  * to define their own public API. Implementations must supply their own subclass of {@link ConnectableElement}
@@ -191,7 +193,7 @@ public abstract class AbstractPathConnector<E extends AbstractPathConnector.Conn
      * @return true if the candidate is a possible connection
      */
     private boolean addPossibleConnection(final E element, final E candidate) {
-        if (element != candidate &&
+        if (!GeometryInternalUtils.sameInstance(element, candidate) &&
                 !candidate.hasPrevious() &&
                 candidate.hasStart() &&
                 element.canConnectTo(candidate)) {
diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/path/AbstractLinePathConnector.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/path/AbstractLinePathConnector.java
index e5f6e35..1593940 100644
--- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/path/AbstractLinePathConnector.java
+++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/twod/path/AbstractLinePathConnector.java
@@ -20,6 +20,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
 
+import org.apache.commons.geometry.core.internal.GeometryInternalUtils;
 import org.apache.commons.geometry.euclidean.internal.AbstractPathConnector;
 import org.apache.commons.geometry.euclidean.twod.LineConvexSubset;
 import org.apache.commons.geometry.euclidean.twod.Vector2D;
@@ -123,7 +124,7 @@ public abstract class AbstractLinePathConnector
 
         ConnectableLineSubset current = root.getNext();
 
-        while (current != null && current != root) {
+        while (current != null && !GeometryInternalUtils.sameInstance(current, root)) {
             builder.append(current.getLineSubset());
             current = current.getNext();
         }
diff --git a/commons-geometry-spherical/src/main/java/org/apache/commons/geometry/spherical/twod/AbstractGreatArcConnector.java b/commons-geometry-spherical/src/main/java/org/apache/commons/geometry/spherical/twod/AbstractGreatArcConnector.java
index 2f56b4c..9e2a907 100644
--- a/commons-geometry-spherical/src/main/java/org/apache/commons/geometry/spherical/twod/AbstractGreatArcConnector.java
+++ b/commons-geometry-spherical/src/main/java/org/apache/commons/geometry/spherical/twod/AbstractGreatArcConnector.java
@@ -20,6 +20,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
 
+import org.apache.commons.geometry.core.internal.GeometryInternalUtils;
 import org.apache.commons.geometry.euclidean.internal.AbstractPathConnector;
 import org.apache.commons.geometry.euclidean.threed.Vector3D;
 
@@ -121,7 +122,7 @@ public abstract class AbstractGreatArcConnector
 
         ConnectableGreatArc current = root.getNext();
 
-        while (current != null && current != root) {
+        while (current != null && !GeometryInternalUtils.sameInstance(current, root)) {
             builder.append(current.getArc());
             current = current.getNext();
         }