Skip to content

Commit

Permalink
Added EqualsHelper.identityDifferent
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Aug 28, 2024
1 parent 589799a commit 8618083
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public void forEach (@Nonnull final IConsumer <T> aConsumer)
if (nKey != FREE_KEY)
{
final T aValue = m_aValues[i];
if (!EqualsHelper.identityEqual (aValue, m_aNoValue))
if (EqualsHelper.identityDifferent (aValue, m_aNoValue))
aConsumer.accept (nKey, aValue);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,7 @@ public static <T> T isSame (final T aValue,
@Nullable final T aExpectedValue)
{
if (isEnabled ())
if (!EqualsHelper.identityEqual (aValue, aExpectedValue))
if (EqualsHelper.identityDifferent (aValue, aExpectedValue))
throw new IllegalArgumentException ("The value of '" +
aName.get () +
"' does not match the expected value. Passed value: " +
Expand Down Expand Up @@ -3070,7 +3070,6 @@ private static void _isArrayOfsLen (@Nonnegative final int nArrayLen,
") exceeds array length (" +
nArrayLen +
")");

}

public static void isArrayOfsLen (final Object [] aArray, @Nonnegative final int nOfs, @Nonnegative final int nLen)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public final ICommonsList <DATATYPE> drainQueue ()
// Change data type
final ICommonsList <DATATYPE> ret = new CommonsArrayList <> ();
for (final Object aObj : aDrainedToList)
if (!EqualsHelper.identityEqual (aObj, STOP_QUEUE_OBJECT))
if (EqualsHelper.identityDifferent (aObj, STOP_QUEUE_OBJECT))
ret.add (GenericReflection.uncheckedCast (aObj));
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.BiPredicate;

Expand Down Expand Up @@ -68,6 +69,24 @@ public static <T> boolean identityEqual (@Nullable final T aObj1, @Nullable fina
return aObj1 == aObj2;
}

/**
* The only place where objects are compared by identity.
*
* @param aObj1
* First object. May be <code>null</code>.
* @param aObj2
* Second object. May be <code>null</code>.
* @return <code>true</code> if one object is <code>null</code> or if they
* reference a different object.
* @param <T>
* Type to check.
* @since 11.1.7
*/
public static <T> boolean identityDifferent (@Nullable final T aObj1, @Nullable final T aObj2)
{
return aObj1 != aObj2;
}

/**
* Check if two values are equal. This method only exists, so that no type
* differentiation is needed.
Expand Down Expand Up @@ -236,17 +255,16 @@ private static boolean _areChildrenEqual (@Nullable final Object aObj1, @Nullabl
return EqualsImplementationRegistry.areEqual (aObj1, aObj2);
}

public static <T> boolean equalsCollectionOnly (@Nonnull final Collection <T> aCont1, @Nonnull final Collection <?> aCont2)
public static <T> boolean equalsCollectionOnly (@Nonnull final Collection <T> aCont1,
@Nonnull final Collection <?> aCont2)
{
if (aCont1.isEmpty () && aCont2.isEmpty ())
return true;
if (aCont1.size () != aCont2.size ())
return false;
final Iterator <T> aIter1 = aCont1.iterator ();
final Iterator <?> aIter2 = aCont2.iterator ();
while (aIter1.hasNext ())
for (T aChildObj1 : aCont1)
{
final T aChildObj1 = aIter1.next ();
final Object aChildObj2 = aIter2.next ();
if (!_areChildrenEqual (aChildObj1, aChildObj2))
return false;
Expand All @@ -259,10 +277,8 @@ public static <K, V> boolean equalsMap (@Nonnull final Map <K, V> aCont1, @Nonnu
if (aCont1.size () != aCont2.size ())
return false;

final Iterator <Map.Entry <K, V>> it = aCont1.entrySet ().iterator ();
while (it.hasNext ())
for (Entry <K, V> aEntry : aCont1.entrySet ())
{
final Map.Entry <K, V> aEntry = it.next ();
final K aKey = aEntry.getKey ();
final V aValue = aEntry.getValue ();
if (aValue == null)
Expand Down Expand Up @@ -396,7 +412,8 @@ public static boolean equalsCollection (@Nullable final Object aObj1, @Nullable
// Check if it is an array of collections (e.g. List<String>[])
final Class <?> aComponentClass1 = aObj1.getClass ().getComponentType ();
final Class <?> aComponentClass2 = aObj2.getClass ().getComponentType ();
if (CollectionHelper.isCollectionClass (aComponentClass1) && CollectionHelper.isCollectionClass (aComponentClass2))
if (CollectionHelper.isCollectionClass (aComponentClass1) &&
CollectionHelper.isCollectionClass (aComponentClass2))
{
// Special handling for arrays of containers
final Object [] aArray1 = (Object []) aObj1;
Expand Down Expand Up @@ -489,7 +506,9 @@ public static boolean equalsAsList (@Nullable final Object aObj1, @Nullable fina
* otherwise
* @since 9.4.5
*/
public static <T> boolean equalsCustom (@Nullable final T aObj1, @Nullable final T aObj2, @Nonnull final BiPredicate <T, T> aPredicate)
public static <T> boolean equalsCustom (@Nullable final T aObj1,
@Nullable final T aObj2,
@Nonnull final BiPredicate <T, T> aPredicate)
{
ValueEnforcer.notNull (aPredicate, "Predicate");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public static EqualsImplementationRegistry getInstance ()
return ret;
}

public <T> void registerEqualsImplementation (@Nonnull final Class <T> aClass, @Nonnull final IEqualsImplementation <T> aImpl)
public <T> void registerEqualsImplementation (@Nonnull final Class <T> aClass,
@Nonnull final IEqualsImplementation <T> aImpl)
{
ValueEnforcer.notNull (aClass, "Class");
ValueEnforcer.notNull (aImpl, "Implementation");
Expand All @@ -125,7 +126,7 @@ public <T> void registerEqualsImplementation (@Nonnull final Class <T> aClass, @
{
// Avoid the warning when the passed implementation equals the stored
// implementation
if (!EqualsHelper.identityEqual (aOldImpl, aImpl))
if (EqualsHelper.identityDifferent (aOldImpl, aImpl))
LOGGER.warn ("Another equals implementation for class " +
aClass +
" is already registered (" +
Expand Down Expand Up @@ -215,7 +216,10 @@ public <T> IEqualsImplementation <T> getBestMatchingEqualsImplementation (@Nulla
aMatchingImplementation = GenericReflection.uncheckedCast (aImpl);
aMatchingClass = aCurClass;
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Found hierarchical match with class " + aMatchingClass + " when searching for " + aClass);
LOGGER.debug ("Found hierarchical match with class " +
aMatchingClass +
" when searching for " +
aClass);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public static HashCodeImplementationRegistry getInstance ()
return ret;
}

public <T> void registerHashCodeImplementation (@Nonnull final Class <T> aClass, @Nonnull final IHashCodeImplementation <T> aImpl)
public <T> void registerHashCodeImplementation (@Nonnull final Class <T> aClass,
@Nonnull final IHashCodeImplementation <T> aImpl)
{
ValueEnforcer.notNull (aClass, "Class");
ValueEnforcer.notNull (aImpl, "Implementation");
Expand All @@ -106,7 +107,7 @@ public <T> void registerHashCodeImplementation (@Nonnull final Class <T> aClass,
if (aOldImpl == null)
m_aMap.put (aClass, aImpl);
else
if (!EqualsHelper.identityEqual (aOldImpl, aImpl))
if (EqualsHelper.identityDifferent (aOldImpl, aImpl))
{
// Avoid the warning when the passed implementation equals the stored
// implementation
Expand Down Expand Up @@ -197,7 +198,10 @@ public <T> IHashCodeImplementation <T> getBestMatchingHashCodeImplementation (@N
aMatchingImplementation = GenericReflection.uncheckedCast (aImpl);
aMatchingClass = aCurClass;
if (LOGGER.isDebugEnabled ())
LOGGER.debug ("Found hierarchical match with class " + aMatchingClass + " when searching for " + aClass);
LOGGER.debug ("Found hierarchical match with class " +
aMatchingClass +
" when searching for " +
aClass);
break;
}
}
Expand Down

0 comments on commit 8618083

Please sign in to comment.