diff --git a/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala b/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala index 86a737c9..1857f8f0 100644 --- a/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala +++ b/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala @@ -305,6 +305,14 @@ private[compat] trait PackageShared { fact: Sorted[K, V]): SortedExtensionMethods[K, V] = new SortedExtensionMethods[K, V](fact) + implicit def toSortedMapExtensionMethods[K, V]( + fact: collection.SortedMap[K, V]): SortedMapExtensionMethods[K, V] = + new SortedMapExtensionMethods[K, V](fact) + + implicit def toSortedSetExtensionMethods[A]( + fact: collection.SortedSet[A]): SortedSetExtensionMethods[A] = + new SortedSetExtensionMethods[A](fact) + implicit def toIteratorExtensionMethods[A](self: Iterator[A]): IteratorExtensionMethods[A] = new IteratorExtensionMethods[A](self) @@ -401,6 +409,24 @@ class SortedExtensionMethods[K, T <: Sorted[K, T]](private val fact: Sorted[K, T def rangeUntil(until: K): T = fact.until(until) } +class SortedMapExtensionMethods[K, V]( + private val self: collection.SortedMap[K, V] +) extends AnyVal { + + def minAfter(key: K): Option[(K, V)] = self.from(key).headOption + + def maxBefore(key: K): Option[(K, V)] = self.until(key).lastOption +} + +class SortedSetExtensionMethods[A]( + private val self: collection.SortedSet[A] +) extends AnyVal { + + def minAfter(key: A): Option[A] = self.from(key).headOption + + def maxBefore(key: A): Option[A] = self.until(key).lastOption +} + class IteratorExtensionMethods[A](private val self: c.Iterator[A]) extends AnyVal { def sameElements[B >: A](that: c.TraversableOnce[B]): Boolean = { self.sameElements(that.iterator) diff --git a/compat/src/test/scala/test/scala/collection/SortedTest.scala b/compat/src/test/scala/test/scala/collection/SortedTest.scala index dd221a0b..93a7a07c 100644 --- a/compat/src/test/scala/test/scala/collection/SortedTest.scala +++ b/compat/src/test/scala/test/scala/collection/SortedTest.scala @@ -64,4 +64,48 @@ class SortedTest { } */ } + + @Test + def sortedSetMinAfter(): Unit = { + val values = 1 to 10 + assertEquals(values.to(collection.SortedSet).minAfter(8), Some(8)) + assertEquals(values.to(collection.immutable.SortedSet).minAfter(9), Some(9)) + assertEquals(values.to(collection.mutable.SortedSet).minAfter(10), Some(10)) + + assertEquals(values.to(collection.SortedSet).minAfter(11), None) + assertEquals(values.to(collection.immutable.SortedSet).minAfter(12), None) + assertEquals(values.to(collection.mutable.SortedSet).minAfter(13), None) + } + + @Test + def sortedSetMaxBefore(): Unit = { + val values = 1 to 10 + assertEquals(values.to(collection.SortedSet).maxBefore(4), Some(3)) + assertEquals(values.to(collection.immutable.SortedSet).maxBefore(3), Some(2)) + assertEquals(values.to(collection.mutable.SortedSet).maxBefore(2), Some(1)) + + assertEquals(values.to(collection.SortedSet).maxBefore(1), None) + assertEquals(values.to(collection.immutable.SortedSet).maxBefore(0), None) + assertEquals(values.to(collection.mutable.SortedSet).maxBefore(-1), None) + } + + @Test + def sortedMapMinAfter(): Unit = { + val values = (1 to 10).map(x => x -> x.toString) + assertEquals(collection.SortedMap(values: _*).minAfter(9), Some(9 -> "9")) + assertEquals(collection.immutable.SortedMap(values: _*).minAfter(10), Some(10 -> "10")) + + assertEquals(collection.SortedMap(values: _*).minAfter(11), None) + assertEquals(collection.immutable.SortedMap(values: _*).minAfter(12), None) + } + + @Test + def sortedMapMaxBefore(): Unit = { + val values = (1 to 10).map(x => x -> x.toString) + assertEquals(collection.SortedMap(values: _*).maxBefore(3), Some(2 -> "2")) + assertEquals(collection.immutable.SortedMap(values: _*).maxBefore(2), Some(1 -> "1")) + + assertEquals(collection.SortedMap(values: _*).maxBefore(1), None) + assertEquals(collection.immutable.SortedMap(values: _*).maxBefore(0), None) + } }