-
Notifications
You must be signed in to change notification settings - Fork 596
/
Collector.scala
122 lines (104 loc) · 4 KB
/
Collector.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (c) 2013 Functional Streams for Scala
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package fs2
import scala.reflect.ClassTag
import scodec.bits.ByteVector
/**
* Supports building a result of type `Out` from zero or more `Chunk[A]`.
*
* This is similar to the standard library collection builders but optimized for
* building a collection from a stream.
*
* The companion object provides implicit conversions (methods starting with `supports`),
* which adapts various collections to the `Collector` trait.
*/
trait Collector[-A] {
type Out
def newBuilder: Collector.Builder[A, Out]
}
object Collector extends CollectorPlatform {
type Aux[A, X] = Collector[A] { type Out = X }
def string: Collector.Aux[String, String] =
make(Builder.string)
implicit def supportsArray[A: ClassTag](a: Array.type): Collector.Aux[A, Array[A]] = {
val _ = a
make(implicitly[ClassTag[A]] match {
case ClassTag.Byte =>
Builder.byteArray.asInstanceOf[Builder[A, Array[A]]]
case _ => Builder.array[A]
})
}
implicit def supportsByteVector(b: ByteVector.type): Collector.Aux[Byte, ByteVector] = {
val _ = b
make(Builder.byteVector)
}
protected def make[A, X](nb: => Builder[A, X]): Collector.Aux[A, X] =
new Collector[A] {
type Out = X
def newBuilder = nb
}
/** Builds a value of type `X` from zero or more `Chunk[A]`. */
trait Builder[-A, +X] { self =>
def +=(c: Chunk[A]): Unit
def result: X
def mapResult[Y](f: X => Y): Builder[A, Y] =
new Builder[A, Y] {
def +=(c: Chunk[A]): Unit = self += c
def result: Y = f(self.result)
}
}
object Builder extends BuilderPlatform {
def byteArray: Builder[Byte, Array[Byte]] =
byteVector.mapResult(_.toArray)
def array[A: ClassTag]: Builder[A, Array[A]] =
Chunk.newBuilder.mapResult(_.toArray)
protected def fromBuilder[A, C[_], B](
builder: collection.mutable.Builder[A, C[B]]
): Builder[A, C[B]] =
new Builder[A, C[B]] {
def +=(c: Chunk[A]): Unit = builder ++= c.iterator
def result: C[B] = builder.result()
}
def string: Builder[String, String] =
new Builder[String, String] {
private[this] val builder = new StringBuilder
def +=(c: Chunk[String]): Unit = c.foreach(s => builder ++= s)
def result: String = builder.toString
}
def byteVector: Builder[Byte, ByteVector] =
new Builder[Byte, ByteVector] {
private[this] var acc = ByteVector.empty
def +=(c: Chunk[Byte]): Unit = acc = acc ++ c.toByteVector
def result: ByteVector = acc
}
}
}
/** Mixin trait for companions of collections that can build a `C[A]` for all `A`. */
trait CollectorK[+C[_]] {
def newBuilder[A]: Collector.Builder[A, C[A]]
}
object CollectorK {
implicit def toCollector[A, C[_]](c: CollectorK[C]): Collector.Aux[A, C[A]] =
new Collector[A] {
type Out = C[A]
def newBuilder: Collector.Builder[A, C[A]] = c.newBuilder[A]
}
}