From b96df92cfd9e93cd2604d0f2c9b236dba6850cec Mon Sep 17 00:00:00 2001 From: Drew Short Date: Wed, 8 Jan 2014 13:49:36 -0500 Subject: [PATCH] Updated Version to support comparisons --- .../com/sothr/imagetools/util/Version.scala | 52 +++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/src/main/scala/com/sothr/imagetools/util/Version.scala b/src/main/scala/com/sothr/imagetools/util/Version.scala index 126b0f7..6e67e6f 100644 --- a/src/main/scala/com/sothr/imagetools/util/Version.scala +++ b/src/main/scala/com/sothr/imagetools/util/Version.scala @@ -8,14 +8,60 @@ class Version(val versionString:String) { val (major,minor,revision,buildType) = { val splitVersion = versionString.split("""\.""") val splitType = splitVersion(splitVersion.length-1).split("""-""") - (splitVersion(0),splitVersion(1),splitType(0),splitType(1)) + (splitVersion(0).toInt,splitVersion(1).toInt,splitType(0).toInt,splitType(1)) } - def difference(otherVersion:Version):Integer = { - return -1 + /* + * -3 = this.revision < that.revision + * -2 = this.minor < that.minor + * -1 = this.major < that.major + * 0 = Identical Versions + * 1 = this.major > that.major + * 2 = this.minor > that.minor + * 3 = this.revision > that.revision + * 4 = this.buildType != that.buildType + */ + def difference(that:Version):Integer = { + if (this.hashCode == that.hashCode) return 0 + if (this.major > that.major) { + return 1 + } else if (this.major < that.major){ + return -1 + //major is the same + } else { + if (this.minor > that.minor) { + return 2 + } else if (this.minor < that.minor) { + return -2 + //major.minor are the same + } else { + if (this.revision > that.revision) { + return 3 + } else if (this.revision < that.revision) { + return -3 + //major.minor.revision are all the same + } else { + if (this.buildType != that.buildType) { + return 4 + } + //should be caught by the first if, but incase not + return 0 + } + } + } } override def toString():String = { return s"$major.$minor.$revision-$buildType" } + + override def hashCode(): Int = { + val prime:Int = 37; + val result:Int = 255 + var hash:Int = major + hash += minor + hash += revision + hash += buildType.hashCode + return prime * result + hash + } }