Skip to content

Commit

Permalink
refactor: revert ObjectEqualityComparator to class + companion
Browse files Browse the repository at this point in the history
  • Loading branch information
lppedd authored and ftomassetti committed Sep 20, 2024
1 parent 04abc4d commit 0bca7da
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright 2017-present Strumenta and contributors, licensed under Apache 2.0.
// Copyright 2024-present Strumenta and contributors, licensed under BSD 3-Clause.

package org.antlr.v4.kotlinruntime.atn

import org.antlr.v4.kotlinruntime.misc.MurmurHash
Expand Down Expand Up @@ -89,7 +88,7 @@ public class LexerATNConfig : ATNConfig {
return false
}

if (!ObjectEqualityComparator.equals(lexerActionExecutor, other.lexerActionExecutor)) {
if (!ObjectEqualityComparator.INSTANCE.equals(lexerActionExecutor, other.lexerActionExecutor)) {
return false
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright 2017-present Strumenta and contributors, licensed under Apache 2.0.
// Copyright 2024-present Strumenta and contributors, licensed under BSD 3-Clause.

package org.antlr.v4.kotlinruntime.atn

import org.antlr.v4.kotlinruntime.misc.ObjectEqualityComparator
Expand All @@ -9,7 +8,7 @@ import org.antlr.v4.kotlinruntime.misc.ObjectEqualityComparator
* @author Sam Harwell
*/
public class OrderedATNConfigSet : ATNConfigSet() {
public class LexerConfigHashSet : AbstractConfigHashSet(ObjectEqualityComparator)
public class LexerConfigHashSet : AbstractConfigHashSet(ObjectEqualityComparator.INSTANCE)

init {
configLookup = LexerConfigHashSet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlin.math.floor
*/
@Suppress("MemberVisibilityCanBePrivate")
public open class Array2DHashSet<T>(
protected val comparator: AbstractEqualityComparator<T> = ObjectEqualityComparator,
protected val comparator: AbstractEqualityComparator<T> = ObjectEqualityComparator.INSTANCE,
protected val initialCapacity: Int = INITIAL_CAPACITY,
protected val initialBucketCapacity: Int = INITIAL_BUCKET_CAPACITY,
) : MutableSet<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlin.math.floor
*/
@Suppress("MemberVisibilityCanBePrivate", "CanBeParameter")
public open class FlexibleHashMap<K, V>(
protected val comparator: AbstractEqualityComparator<K> = ObjectEqualityComparator,
protected val comparator: AbstractEqualityComparator<K> = ObjectEqualityComparator.INSTANCE,
public val initialCapacity: Int = INITIAL_CAPACITY,
public val initialBucketCapacity: Int = INITIAL_BUCKET_CAPACITY,
) : MutableMap<K, V> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@
// Copyright 2024-present Strumenta and contributors, licensed under BSD 3-Clause.
package org.antlr.v4.kotlinruntime.misc

import kotlin.jvm.JvmField

/**
* This default implementation of [EqualityComparator] uses object equality
* for comparisons by calling [Any.hashCode] and [Any.equals].
*
* @author Sam Harwell
*/
public object ObjectEqualityComparator : AbstractEqualityComparator<Any?>() {
override fun hashCode(obj: Any?): Int =
public class ObjectEqualityComparator<in T> : AbstractEqualityComparator<T>() {
public companion object {
@JvmField
public val INSTANCE: ObjectEqualityComparator<Any?> = ObjectEqualityComparator()
}

override fun hashCode(obj: T): Int =
obj?.hashCode() ?: 0

/**
Expand All @@ -21,6 +28,6 @@ public object ObjectEqualityComparator : AbstractEqualityComparator<Any?>() {
*
* Otherwise, this method returns the result of `a == b`.
*/
override fun equals(a: Any?, b: Any?): Boolean =
override fun equals(a: T?, b: T?): Boolean =
(a == null && b == null) || a == b
}

0 comments on commit 0bca7da

Please sign in to comment.