Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Equality To Be Consistent With Compare In Version #12

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions versions/shared/src/main/scala/coursier/version/Version.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,57 @@ import scala.annotation.tailrec
*
* Same kind of ordering as aether-util/src/main/java/org/eclipse/aether/util/version/GenericVersion.java
*/
@data class Version(repr: String) extends Ordered[Version] {
final class Version(val repr: String) extends Ordered[Version] with Product with Serializable {
lazy val items: Vector[Version.Item] = Version.items(repr)
def compare(other: Version) = Version.listCompare(items, other.items)

def isEmpty = items.forall(_.isEmpty)

// We can't use @data here or the equals method will be inconsistent with
// the compare method. This means we have to hand write these methods.

def withRepr(value: String): Version =
new Version(value)

override def compare(other: Version) = Version.listCompare(items, other.items)

override def canEqual(that: Any): Boolean =
that match {
case _: Version =>
true
case _ =>
false
}

override def equals(that: Any): Boolean =
this.canEqual(that) && (that match {
case that: Version =>
this.compare(that) == 0
case _ => false
})

override val productArity: Int = 1

override def productElement(n: Int): Any =
if (n == 0) {
repr
} else {
throw new IndexOutOfBoundsException(n)
}

override def toString: String =
s"Version(${repr})"

override def hashCode: Int =
Version.items(repr).filterNot(Version.isBuildMetadata).filterNot(_.isEmpty).hashCode
}

object Version {

private[version] val zero = Version("0")

def apply(repr: String): Version =
new Version(repr)

sealed abstract class Item extends Ordered[Item] {
def compare(other: Item): Int =
(this, other) match {
Expand Down
17 changes: 17 additions & 0 deletions versions/shared/src/test/scala/coursier/version/VersionTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ object VersionTests extends TestSuite {
def compare(first: String, second: String) =
Version(first).compare(Version(second))

def compareEqualsConsistent(first: String, second: String): Boolean = {
val firstV: Version = Version(first)
val secondV: Version = Version(second)

if (firstV.compare(secondV) == 0) {
firstV == secondV && firstV.hashCode == secondV.hashCode
} else {
firstV != secondV
}
}

def increasing(versions: String*): Boolean =
versions.iterator.sliding(2).withPartial(false).forall{case Seq(a, b) => compare(a, b) < 0 }

Expand Down Expand Up @@ -86,6 +97,12 @@ object VersionTests extends TestSuite {
assert(compare("0", "" ) == 0)
}

"compareIsConsistentWithEquals" - {
assert(compareEqualsConsistent("1.0.0", "1.0.0"))
assert(compareEqualsConsistent("1.0.0", "1.0.0.0"))
assert(compareEqualsConsistent("1.0.0", "1"))
assert(compareEqualsConsistent("1.0.0", "2.0.0"))
}

"numericOrdering" - {
assert(compare("2", "10" ) < 0)
Expand Down