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

Port Item.java from JSR-166 #3311

Merged
merged 2 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/publicdomain/zero/1.0/
*/
package org.scalanative.testsuite.javalib.util.concurrent

import java.util.Comparator
import java.io.Serializable

/** A simple element class for collections etc
*/
final class Item extends Number with Comparable[Item] with Serializable {
final var value = 0

def this(v: Int) = {
this()
value = v
}

def this(i: Item) = {
this()
value = i.value
}

def this(i: Integer) = {
this()
value = i.intValue
}

override def intValue: Int = value

override def longValue: Long = value.toLong

override def floatValue: Float = value.toFloat

override def doubleValue: Double = value.toDouble

override def equals(x: Any): Boolean =
x.isInstanceOf[Item] && x.asInstanceOf[Item].value == value

def equals(b: Int): Boolean = value == b

override def compareTo(x: Item): Int = Integer.compare(this.value, x.value)

def compareTo(b: Int): Int = Integer.compare(this.value, b)

override def hashCode: Int = value

override def toString: String = Integer.toString(value)
}

object Item {
def valueOf(i: Int) = new Item(i)

def compare(x: Item, y: Item): Int = Integer.compare(x.value, y.value)

def compare(x: Item, b: Int): Int = Integer.compare(x.value, b)

def comparator = new Item.Cpr

class Cpr extends Comparator[Item] {
override def compare(x: Item, y: Item): Int =
Integer.compare(x.value, y.value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,30 @@ object JSR166Test {
*/
final val SIZE = 20

def seqItems(size: Int) = {
val s = new Array[Item](size)
for (i <- 0 until size) {
s(i) = new Item(i)
}
s
}

def negativeSeqItems(size: Int) = {
val s = new Array[Item](size)
for (i <- 0 until size) {
s(i) = new Item(-i)
}
s
}

val defaultItems: Array[Item] = seqItems(SIZE);

def itemFor(i: Int) = { // check cache for defaultItems
val items = defaultItems
if (i >= 0 && i < items.length) items(i)
else new Item(i)
}

// Some convenient Integer constants
final val zero = Integer.valueOf(0)
final val one = Integer.valueOf(1)
Expand All @@ -1153,6 +1177,75 @@ object JSR166Test {
final val m6 = Integer.valueOf(-6)
final val m10 = Integer.valueOf(-10)

def mustEqual(x: Item, y: Item): Unit = {
if (x ne y) assertEquals(x.value, y.value)
}
def mustEqual(x: Item, y: Int): Unit = {
assertEquals(x.value, y)
}
def mustEqual(x: Int, y: Item): Unit = {
assertEquals(x, y.value)
}
def mustEqual(x: Int, y: Int): Unit = {
assertEquals(x, y)
}
def mustEqual(x: Any, y: Any): Unit = {
if (x != y) assertEquals(x, y)
}
def mustEqual(x: Int, y: Any): Unit = {
if (y.isInstanceOf[Item]) assertEquals(x, y.asInstanceOf[Item].value)
else fail()
}
def mustEqual(x: Any, y: Int): Unit = {
if (x.isInstanceOf[Item]) assertEquals(x.asInstanceOf[Item].value, y)
else fail()
}
def mustEqual(x: Boolean, y: Boolean): Unit = {
assertEquals(x, y)
}
def mustEqual(x: Long, y: Long): Unit = {
assertEquals(x, y)
}
def mustEqual(x: Double, y: Double): Unit = {
assertEquals(x, y)
}
def mustContain(c: Collection[Item], i: Int): Unit = {
assertTrue(c.contains(itemFor(i)))
}
def mustContain(c: Collection[Item], i: Item): Unit = {
assertTrue(c.contains(i))
}
def mustNotContain(c: Collection[Item], i: Int): Unit = {
assertFalse(c.contains(itemFor(i)))
}
def mustNotContain(c: Collection[Item], i: Item): Unit = {
assertFalse(c.contains(i))
}
def mustRemove(c: Collection[Item], i: Int): Unit = {
assertTrue(c.remove(itemFor(i)))
}
def mustRemove(c: Collection[Item], i: Item): Unit = {
assertTrue(c.remove(i))
}
def mustNotRemove(c: Collection[Item], i: Int): Unit = {
assertFalse(c.remove(itemFor(i)))
}
def mustNotRemove(c: Collection[Item], i: Item): Unit = {
assertFalse(c.remove(i))
}
def mustAdd(c: Collection[Item], i: Int): Unit = {
assertTrue(c.add(itemFor(i)))
}
def mustAdd(c: Collection[Item], i: Item): Unit = {
assertTrue(c.add(i))
}
def mustOffer(c: Queue[Item], i: Int): Unit = {
assertTrue(c.offer(itemFor(i)))
}
def mustOffer(c: Queue[Item], i: Item): Unit = {
assertTrue(c.offer(i))
}

/** Returns the number of milliseconds since time given by startNanoTime,
* which must have been previously returned from a call to {@link
* System#nanoTime()}.
Expand Down