Skip to content

Commit

Permalink
[util-core, inject-utils]: Move c.t.inject.conversions.map.RichMap to…
Browse files Browse the repository at this point in the history
… util-core

Problem

Finatra's inject-utils/conversions contains useful map conversion methods that
would fit better in util-core/conversions.

Solution

Deprecate RichMap and its methods, move the functionality to util-core, along
with the corresponding tests.

JIRA Issues: CSL-10368

Differential Revision: https://phabricator.twitter.biz/D578819
  • Loading branch information
dotordogh authored and jenkins committed Nov 19, 2020
1 parent 2bd6dbf commit e765b5a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 75 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.rst
Expand Up @@ -15,11 +15,14 @@ Added
Changed
~~~~~~~

* inject-utils: Deprecate all methods in `c.t.inject.conversions.map.RichMap`, and move
functionality to `c.t.conversions.MapOps` in the util/util-core project. ``PHAB_ID=D578819``

* inject-utils: Deprecate all methods in `c.t.inject.conversions.tuple`, and move functionality
to `c.t.conversions.TupleOps` in the util/util-core project. ``PHAB_ID=D578804``

* inject-utils: Deprecate all methods in `c.t.inject.conversions.seq`, and move functionality
to `c.t.conversions.SeqOps` in the util/util-core project.. ``PHAB_ID=D578605``
to `c.t.conversions.SeqOps` in the util/util-core project. ``PHAB_ID=D578605``

* inject-utils: Remove deprecated `camelify`, `pascalify`, and `snakify` from
`c.t.inject.conversions.string.RichString`. Additionally, deprecate `toOption` and
Expand Down
@@ -1,57 +1,39 @@
package com.twitter.inject.conversions

import com.twitter.conversions.MapOps
import com.twitter.inject.conversions.tuple._
import java.util.concurrent.ConcurrentHashMap
import scala.collection.compat.immutable.ArraySeq
import scala.collection.{SortedMap, immutable, mutable}
import scala.collection.{SortedMap, immutable}

@deprecated("Use com.twitter.conversions.MapOps instead", "2020-11-16")
object map {

implicit class RichMap[K, V](val self: Map[K, V]) extends AnyVal {
def mapKeys[T](func: K => T): Map[T, V] = {
for ((k, v) <- self) yield {
func(k) -> v
}
}

def invert: Map[V, Seq[K]] = {
val invertedMapWithBuilderValues = mutable.Map.empty[V, mutable.Builder[K, Seq[K]]]
for ((k, v) <- self) {
val valueBuilder = invertedMapWithBuilderValues.getOrElseUpdate(v, Seq.newBuilder[K])
valueBuilder += k
}
@deprecated("Use com.twitter.conversions.MapOps#mapKeys instead", "2020-11-16")
def mapKeys[T](func: K => T): Map[T, V] = MapOps.mapKeys(self, func)

val invertedMap = immutable.Map.newBuilder[V, Seq[K]]
for ((k, valueBuilder) <- invertedMapWithBuilderValues) {
invertedMap += (k -> valueBuilder.result)
}

invertedMap.result()
}
@deprecated("Use com.twitter.conversions.MapOps#invert instead", "2020-11-16")
def invert: Map[V, Seq[K]] = MapOps.invert(self)

def invertSingleValue: Map[V, K] = {
self map { _.swap }
}
@deprecated("Use com.twitter.conversions.MapOps#invertSingleValue instead", "2020-11-16")
def invertSingleValue: Map[V, K] = MapOps.invertSingleValue(self)

def filterValues(func: V => Boolean): Map[K, V] = {
self filter {
case (_, value) =>
func(value)
}
}
@deprecated("Use com.twitter.conversions.MapOps#filterValues instead", "2020-11-16")
def filterValues(func: V => Boolean): Map[K, V] =
MapOps.filterValues(self, func)

def filterNotValues(func: V => Boolean): Map[K, V] = {
filterValues(!func(_))
}
@deprecated("Use com.twitter.conversions.MapOps#filterNotValues instead", "2020-11-16")
def filterNotValues(func: V => Boolean): Map[K, V] =
MapOps.filterNotValues(self, func)

def filterNotKeys(func: K => Boolean): Map[K, V] = {
// use filter instead of filterKeys(deprecated since 2.13.0) for cross-building.
self.filter { case (key, _) => !func(key) }
}
@deprecated("Use com.twitter.conversions.MapOps#filterNotKeys instead", "2020-11-16")
def filterNotKeys(func: K => Boolean): Map[K, V] =
MapOps.filterNotKeys(self, func)

def toSortedMap(implicit ordering: Ordering[K]): SortedMap[K, V] = {
SortedMap[K, V](self.toSeq: _*)
}
@deprecated("Use com.twitter.conversions.MapOps#toSortedMap instead", "2020-11-16")
def toSortedMap(implicit ordering: Ordering[K]): SortedMap[K, V] =
MapOps.toSortedMap(self)

}

Expand Down
Expand Up @@ -28,41 +28,6 @@ class MapsConversionsTest extends Test {
Map("a" -> None, "b" -> Some(2)).flattenEntries should equal(Map("b" -> 2))
}

test("RichMap#mapKeys") {
Map(1 -> "a") mapKeys { _.toString } should
equal(Map("1" -> "a"))
}

test("RichMap#invert simple") {
Map(1 -> "a").invert should
equal(Map("a" -> Iterable(1)))
}

test("RichMap#invert complex") {
Map(1 -> "a", 2 -> "a", 3 -> "b").invert should
equal(Map("a" -> Iterable(1, 2), "b" -> Iterable(3)))
}

test("RichMap#invertSingleValue") {
Map(1 -> "a", 2 -> "a", 3 -> "b").invertSingleValue should
equal(Map("a" -> 2, "b" -> 3))
}

test("RichMap#filterValues") {
Map(1 -> "a", 2 -> "a", 3 -> "b") filterValues { _ == "b" } should
equal(Map(3 -> "b"))
}

test("RichMap#filterNotValues") {
Map(1 -> "a", 2 -> "a", 3 -> "b") filterNotValues { _ == "b" } should
equal(Map(1 -> "a", 2 -> "a"))
}

test("RichMap#filterNotKeys") {
Map(1 -> "a", 2 -> "a", 3 -> "b") filterNotKeys { _ == 3 } should
equal(Map(1 -> "a", 2 -> "a"))
}

test("RichJavaMap#toOrderedMap") {
val treeMap = new java.util.TreeMap[String, Int]()
treeMap.put("c", 3)
Expand Down

0 comments on commit e765b5a

Please sign in to comment.