Skip to content
Permalink
Browse files
GEOMETRY-141: add GeometryInternalUtils.sameInstance() method to docu…
…ment reference equality checks
  • Loading branch information
darkma773r committed Nov 20, 2021
1 parent 630d9b6 commit d0479b80fa22b9a411cb475b628ce1395ac0c7a0
Showing 6 changed files with 51 additions and 8 deletions.
@@ -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;
}
}
@@ -25,6 +25,7 @@
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 @@ private HyperplaneConvexSubset<P> splitBound(final Hyperplane<P> currentBound,
splitter = boundsIt.next();
++splitterIdx;

if (currentBound == splitter) {
if (GeometryInternalUtils.sameInstance(currentBound, splitter)) {
// do not split the bound with itself

if (currentBoundIdx > splitterIdx) {
@@ -23,6 +23,7 @@

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 @@ protected N copyNode(final N src) {
*/
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 @@ protected N copySubtree(final N src, final N dst) {
*/
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 boolean isInternal() {
/** {@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} */
@@ -21,6 +21,8 @@
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 @@ private void findPossibleConnections(final E element) {
* @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)) {
@@ -20,6 +20,7 @@
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 @@ private LinePath toPath(final ConnectableLineSubset root) {

ConnectableLineSubset current = root.getNext();

while (current != null && current != root) {
while (current != null && !GeometryInternalUtils.sameInstance(current, root)) {
builder.append(current.getLineSubset());
current = current.getNext();
}
@@ -20,6 +20,7 @@
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 @@ private GreatArcPath toPath(final ConnectableGreatArc root) {

ConnectableGreatArc current = root.getNext();

while (current != null && current != root) {
while (current != null && !GeometryInternalUtils.sameInstance(current, root)) {
builder.append(current.getArc());
current = current.getNext();
}

0 comments on commit d0479b8

Please sign in to comment.