Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[util-core, inject-utils]: Move c.t.inject.conversions.tuple to util-…
…core Problem Finatra's inject-utils/conversions contains useful tuple conversion methods that would fit better in util-core/conversions. Solution Deprecate everything in `c.t.inject.conversions.tuple`, and move the functionality to util-core, along with the corresponding tests. JIRA Issues: CSL-10370 Differential Revision: https://phabricator.twitter.biz/D578804
- Loading branch information
Showing
3 changed files
with
34 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 31 additions & 50 deletions
81
inject/inject-utils/src/main/scala/com/twitter/inject/conversions/tuple.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,40 @@ | ||
package com.twitter.inject.conversions | ||
|
||
import scala.collection.{SortedMap, immutable, mutable} | ||
import com.twitter.conversions.TupleOps | ||
import scala.collection.SortedMap | ||
import scala.math.Ordering | ||
|
||
@deprecated("Use com.twitter.conversions.TupleOps instead", "2020-11-16") | ||
object tuple { | ||
|
||
implicit class RichTuples[A, B](val self: Iterable[(A, B)]) extends AnyVal { | ||
def toKeys: Seq[A] = { | ||
self.toSeq map { case (key, value) => key } | ||
} | ||
|
||
def toKeySet: Set[A] = { | ||
toKeys.toSet | ||
} | ||
|
||
def toValues: Seq[B] = { | ||
self.toSeq map { case (key, value) => value } | ||
} | ||
|
||
def mapValues[C](func: B => C): Seq[(A, C)] = { | ||
self.toSeq map { | ||
case (key, value) => | ||
key -> func(value) | ||
} | ||
} | ||
|
||
def groupByKey: Map[A, Seq[B]] = { | ||
val mutableMapBuilder = mutable.Map.empty[A, mutable.Builder[B, Seq[B]]] | ||
for ((a, b) <- self) { | ||
val seqBuilder = mutableMapBuilder.getOrElseUpdate(a, immutable.Seq.newBuilder[B]) | ||
seqBuilder += b | ||
} | ||
|
||
val mapBuilder = immutable.Map.newBuilder[A, Seq[B]] | ||
for ((k, v) <- mutableMapBuilder) { | ||
mapBuilder += ((k, v.result())) | ||
} | ||
|
||
mapBuilder.result() | ||
} | ||
|
||
def groupByKeyAndReduce(reduceFunc: (B, B) => B): Map[A, B] = { | ||
// use map instead of mapValues(deprecated since 2.13.0) for cross-building. | ||
groupByKey.map { | ||
case (k, values) => | ||
k -> values.reduce(reduceFunc) | ||
} | ||
} | ||
|
||
def sortByKey(implicit ord: Ordering[A]): Seq[(A, B)] = { | ||
self.toSeq sortBy { case (key, value) => key } | ||
} | ||
|
||
def toSortedMap(implicit ord: Ordering[A]): SortedMap[A, B] = { | ||
SortedMap(self.toSeq: _*) | ||
} | ||
@deprecated("Use com.twitter.conversions.TupleOps#toKeys instead", "2020-11-16") | ||
def toKeys: Seq[A] = TupleOps.toKeys(self) | ||
|
||
@deprecated("Use com.twitter.conversions.TupleOps#toKeys instead", "2020-11-16") | ||
def toKeySet: Set[A] = TupleOps.toKeySet(self) | ||
|
||
@deprecated("Use com.twitter.conversions.TupleOps#toValues instead", "2020-11-16") | ||
def toValues: Seq[B] = TupleOps.toValues(self) | ||
|
||
@deprecated("Use com.twitter.conversions.TupleOps#mapValues instead", "2020-11-16") | ||
def mapValues[C](func: B => C): Seq[(A, C)] = | ||
TupleOps.mapValues(self, func) | ||
|
||
@deprecated("Use com.twitter.conversions.TupleOps#groupByKey instead", "2020-11-16") | ||
def groupByKey: Map[A, Seq[B]] = | ||
TupleOps.groupByKey(self) | ||
|
||
@deprecated("Use com.twitter.conversions.TupleOps#groupByKeyAndReduce instead", "2020-11-16") | ||
def groupByKeyAndReduce(reduceFunc: (B, B) => B): Map[A, B] = | ||
TupleOps.groupByKeyAndReduce(self, reduceFunc) | ||
|
||
@deprecated("Use com.twitter.conversions.TupleOps#sortByKey instead", "2020-11-16") | ||
def sortByKey(implicit ord: Ordering[A]): Seq[(A, B)] = | ||
TupleOps.sortByKey(self) | ||
|
||
@deprecated("Use com.twitter.conversions.TupleOps#toSortedMap instead", "2020-11-16") | ||
def toSortedMap(implicit ord: Ordering[A]): SortedMap[A, B] = | ||
TupleOps.toSortedMap(self) | ||
} | ||
} |
30 changes: 0 additions & 30 deletions
30
...ect-utils/src/test/scala/com/twitter/inject/tests/conversions/TuplesConversionsTest.scala
This file was deleted.
Oops, something went wrong.