Fix equals and hashCode for empty ranges and progressions

Empty range is equal to any other empty range
This commit is contained in:
Alexander Udalov
2014-06-16 19:12:00 +04:00
parent 751f062f23
commit ad23a2d05b
6 changed files with 134 additions and 38 deletions
+27 -13
View File
@@ -32,9 +32,11 @@ public class ByteProgression(
public fun isEmpty(): Boolean = if (increment > 0) start > end else start < end
override fun equals(other: Any?): Boolean =
other is ByteProgression && start == other.start && end == other.end && increment == other.increment
other is ByteProgression && (isEmpty() && other.isEmpty() ||
start == other.start && end == other.end && increment == other.increment)
override fun hashCode(): Int = 31 * (31 * start.toInt() + end) + increment
override fun hashCode(): Int =
if (isEmpty()) -1 else (31 * (31 * start.toInt() + end) + increment)
override fun toString(): String = if (increment > 0) "$start..$end step $increment" else "$start downTo $end step ${-increment}"
}
@@ -53,9 +55,11 @@ public class CharProgression(
public fun isEmpty(): Boolean = if (increment > 0) start > end else start < end
override fun equals(other: Any?): Boolean =
other is CharProgression && start == other.start && end == other.end && increment == other.increment
other is CharProgression && (isEmpty() && other.isEmpty() ||
start == other.start && end == other.end && increment == other.increment)
override fun hashCode(): Int = 31 * (31 * start.toInt() + end) + increment
override fun hashCode(): Int =
if (isEmpty()) -1 else (31 * (31 * start.toInt() + end) + increment)
override fun toString(): String = if (increment > 0) "$start..$end step $increment" else "$start downTo $end step ${-increment}"
}
@@ -74,9 +78,11 @@ public class ShortProgression(
public fun isEmpty(): Boolean = if (increment > 0) start > end else start < end
override fun equals(other: Any?): Boolean =
other is ShortProgression && start == other.start && end == other.end && increment == other.increment
other is ShortProgression && (isEmpty() && other.isEmpty() ||
start == other.start && end == other.end && increment == other.increment)
override fun hashCode(): Int = 31 * (31 * start.toInt() + end) + increment
override fun hashCode(): Int =
if (isEmpty()) -1 else (31 * (31 * start.toInt() + end) + increment)
override fun toString(): String = if (increment > 0) "$start..$end step $increment" else "$start downTo $end step ${-increment}"
}
@@ -95,9 +101,11 @@ public class IntProgression(
public fun isEmpty(): Boolean = if (increment > 0) start > end else start < end
override fun equals(other: Any?): Boolean =
other is IntProgression && start == other.start && end == other.end && increment == other.increment
other is IntProgression && (isEmpty() && other.isEmpty() ||
start == other.start && end == other.end && increment == other.increment)
override fun hashCode(): Int = 31 * (31 * start + end) + increment
override fun hashCode(): Int =
if (isEmpty()) -1 else (31 * (31 * start + end) + increment)
override fun toString(): String = if (increment > 0) "$start..$end step $increment" else "$start downTo $end step ${-increment}"
}
@@ -116,9 +124,11 @@ public class LongProgression(
public fun isEmpty(): Boolean = if (increment > 0) start > end else start < end
override fun equals(other: Any?): Boolean =
other is LongProgression && start == other.start && end == other.end && increment == other.increment
other is LongProgression && (isEmpty() && other.isEmpty() ||
start == other.start && end == other.end && increment == other.increment)
override fun hashCode(): Int = (31 * (31 * (start xor (start ushr 32)) + (end xor (end ushr 32))) + (increment xor (increment ushr 32))).toInt()
override fun hashCode(): Int =
if (isEmpty()) -1 else (31 * (31 * (start xor (start ushr 32)) + (end xor (end ushr 32))) + (increment xor (increment ushr 32))).toInt()
override fun toString(): String = if (increment > 0) "$start..$end step $increment" else "$start downTo $end step ${-increment}"
}
@@ -138,9 +148,11 @@ public class FloatProgression(
public fun isEmpty(): Boolean = if (increment > 0) start > end else start < end
override fun equals(other: Any?): Boolean =
other is FloatProgression && java.lang.Float.compare(start, other.start) == 0 && java.lang.Float.compare(end, other.end) == 0 && java.lang.Float.compare(increment, other.increment) == 0
other is FloatProgression && (isEmpty() && other.isEmpty() ||
java.lang.Float.compare(start, other.start) == 0 && java.lang.Float.compare(end, other.end) == 0 && java.lang.Float.compare(increment, other.increment) == 0)
override fun hashCode(): Int = 31 * (31 * java.lang.Float.floatToIntBits(start) + java.lang.Float.floatToIntBits(end)) + java.lang.Float.floatToIntBits(increment)
override fun hashCode(): Int =
if (isEmpty()) -1 else (31 * (31 * java.lang.Float.floatToIntBits(start) + java.lang.Float.floatToIntBits(end)) + java.lang.Float.floatToIntBits(increment))
override fun toString(): String = if (increment > 0) "$start..$end step $increment" else "$start downTo $end step ${-increment}"
}
@@ -160,9 +172,11 @@ public class DoubleProgression(
public fun isEmpty(): Boolean = if (increment > 0) start > end else start < end
override fun equals(other: Any?): Boolean =
other is DoubleProgression && java.lang.Double.compare(start, other.start) == 0 && java.lang.Double.compare(end, other.end) == 0 && java.lang.Double.compare(increment, other.increment) == 0
other is DoubleProgression && (isEmpty() && other.isEmpty() ||
java.lang.Double.compare(start, other.start) == 0 && java.lang.Double.compare(end, other.end) == 0 && java.lang.Double.compare(increment, other.increment) == 0)
override fun hashCode(): Int {
if (isEmpty()) return -1
var temp = java.lang.Double.doubleToLongBits(start)
var result = (temp xor (temp ushr 32))
temp = java.lang.Double.doubleToLongBits(end)
+27 -13
View File
@@ -29,9 +29,11 @@ public class ByteRange(public override val start: Byte, public override val end:
override fun isEmpty(): Boolean = start > end
override fun equals(other: Any?): Boolean =
other is ByteRange && start == other.start && end == other.end
other is ByteRange && (isEmpty() && other.isEmpty() ||
start == other.start && end == other.end)
override fun hashCode(): Int = 31 * start.toInt() + end
override fun hashCode(): Int =
if (isEmpty()) -1 else (31 * start.toInt() + end)
class object {
public val EMPTY: ByteRange = ByteRange(1, 0)
@@ -49,9 +51,11 @@ public class CharRange(public override val start: Char, public override val end:
override fun isEmpty(): Boolean = start > end
override fun equals(other: Any?): Boolean =
other is CharRange && start == other.start && end == other.end
other is CharRange && (isEmpty() && other.isEmpty() ||
start == other.start && end == other.end)
override fun hashCode(): Int = 31 * start.toInt() + end
override fun hashCode(): Int =
if (isEmpty()) -1 else (31 * start.toInt() + end)
class object {
public val EMPTY: CharRange = CharRange(1.toChar(), 0.toChar())
@@ -69,9 +73,11 @@ public class ShortRange(public override val start: Short, public override val en
override fun isEmpty(): Boolean = start > end
override fun equals(other: Any?): Boolean =
other is ShortRange && start == other.start && end == other.end
other is ShortRange && (isEmpty() && other.isEmpty() ||
start == other.start && end == other.end)
override fun hashCode(): Int = 31 * start.toInt() + end
override fun hashCode(): Int =
if (isEmpty()) -1 else (31 * start.toInt() + end)
class object {
public val EMPTY: ShortRange = ShortRange(1, 0)
@@ -89,9 +95,11 @@ public class IntRange(public override val start: Int, public override val end: I
override fun isEmpty(): Boolean = start > end
override fun equals(other: Any?): Boolean =
other is IntRange && start == other.start && end == other.end
other is IntRange && (isEmpty() && other.isEmpty() ||
start == other.start && end == other.end)
override fun hashCode(): Int = 31 * start + end
override fun hashCode(): Int =
if (isEmpty()) -1 else (31 * start + end)
class object {
public val EMPTY: IntRange = IntRange(1, 0)
@@ -109,9 +117,11 @@ public class LongRange(public override val start: Long, public override val end:
override fun isEmpty(): Boolean = start > end
override fun equals(other: Any?): Boolean =
other is LongRange && start == other.start && end == other.end
other is LongRange && (isEmpty() && other.isEmpty() ||
start == other.start && end == other.end)
override fun hashCode(): Int = (31 * (start xor (start ushr 32)) + (end xor (end ushr 32))).toInt()
override fun hashCode(): Int =
if (isEmpty()) -1 else (31 * (start xor (start ushr 32)) + (end xor (end ushr 32))).toInt()
class object {
public val EMPTY: LongRange = LongRange(1, 0)
@@ -129,9 +139,11 @@ public class FloatRange(public override val start: Float, public override val en
override fun isEmpty(): Boolean = start > end
override fun equals(other: Any?): Boolean =
other is FloatRange && java.lang.Float.compare(start, other.start) == 0 && java.lang.Float.compare(end, other.end) == 0
other is FloatRange && (isEmpty() && other.isEmpty() ||
java.lang.Float.compare(start, other.start) == 0 && java.lang.Float.compare(end, other.end) == 0)
override fun hashCode(): Int = 31 * java.lang.Float.floatToIntBits(start) + java.lang.Float.floatToIntBits(end)
override fun hashCode(): Int =
if (isEmpty()) -1 else (31 * java.lang.Float.floatToIntBits(start) + java.lang.Float.floatToIntBits(end))
class object {
public val EMPTY: FloatRange = FloatRange(1.0f, 0.0f)
@@ -149,9 +161,11 @@ public class DoubleRange(public override val start: Double, public override val
override fun isEmpty(): Boolean = start > end
override fun equals(other: Any?): Boolean =
other is DoubleRange && java.lang.Double.compare(start, other.start) == 0 && java.lang.Double.compare(end, other.end) == 0
other is DoubleRange && (isEmpty() && other.isEmpty() ||
java.lang.Double.compare(start, other.start) == 0 && java.lang.Double.compare(end, other.end) == 0)
override fun hashCode(): Int {
if (isEmpty()) return -1
var temp = java.lang.Double.doubleToLongBits(start)
val result = (temp xor (temp ushr 32))
temp = java.lang.Double.doubleToLongBits(end)