forked from akka/alpakka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.scala
166 lines (140 loc) · 5.07 KB
/
model.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright (C) 2016-2020 Lightbend Inc. <https://www.lightbend.com>
*/
package akka.stream.alpakka.reference
import java.util.{Optional, OptionalInt}
import akka.annotation.InternalApi
import akka.util.ByteString
import scala.collection.immutable
import scala.collection.JavaConverters._
import scala.compat.java8.OptionConverters._
import scala.util.{Success, Try}
/**
* Use "Read" in message data types to signify that the message was read from outside.
*
* The constructor is INTERNAL API, but you may construct instances for testing by using
* [[akka.stream.alpakka.reference.testkit.MessageFactory]].
*/
final class ReferenceReadResult @InternalApi private[reference] (
val data: immutable.Seq[ByteString] = immutable.Seq.empty,
val bytesRead: Try[Int] = Success(0)
) {
/**
* Java API
*
* If the model class is meant to be also consumed from the user API,
* but the attribute class is Scala specific, create getter for Java API.
*/
def getData(): java.util.List[ByteString] =
data.asJava
/**
* Java API
*
* If the model class is scala.util.Try, then two getters should be created.
* One for getting the value, and another for getting the exception.
*
* Return bytes read wrapped in OptionalInt if the Try contains a value,
* otherwise return empty Optional.
*/
def getBytesRead(): OptionalInt =
bytesRead.toOption.asPrimitive
/**
* Java API
*
* Return the exception wrapped in Optional if the Try contains a Failure,
* otherwise return empty Optional.
*/
def getBytesReadFailure(): Optional[Throwable] =
bytesRead.failed.toOption.asJava
override def toString: String =
s"ReferenceReadMessage(data=$data, bytesRead=$bytesRead)"
}
/**
* Use "Write" in message data types to signify that the messages is to be written to outside.
*/
final class ReferenceWriteMessage private (
val data: immutable.Seq[ByteString] = immutable.Seq.empty,
val metrics: Map[String, Long] = Map.empty
) {
def withData(data: immutable.Seq[ByteString]): ReferenceWriteMessage =
copy(data = data)
def withMetrics(metrics: Map[String, Long]): ReferenceWriteMessage =
copy(metrics = metrics)
/**
* Java API
*
* When settings class has an attribute of Scala collection type,
* create a setter that takes a corresponding Java collection type.
*/
def withData(data: java.util.List[ByteString]): ReferenceWriteMessage =
copy(data = data.asScala.toIndexedSeq)
/**
* Java API
*
* When settings class has an attribute of Scala Long class,
* Java setter needs to take Java Long class and convert to Scala Long.
*/
def withMetrics(metrics: java.util.Map[String, java.lang.Long]): ReferenceWriteMessage =
copy(metrics = metrics.asScala.mapValues(Long.unbox).toMap)
/**
* Java API
*
* If the model class is meant to be also consumed from the user API,
* but the attribute class is Scala specific, create getter for Java API.
*/
def getData(): java.util.List[ByteString] =
data.asJava
/**
* Java API
*
* Java getter needs to return Java Long classes which is converted from Scala Long.
*/
def getMetrics(): java.util.Map[String, java.lang.Long] =
metrics.map {
case (key, value) => key -> java.lang.Long.valueOf(value)
}.asJava
private def copy(data: immutable.Seq[ByteString] = data, metrics: Map[String, Long] = metrics) =
new ReferenceWriteMessage(data, metrics)
override def toString: String =
s"ReferenceWriteMessage(data=$data, metrics=$metrics)"
}
object ReferenceWriteMessage {
def apply(): ReferenceWriteMessage = new ReferenceWriteMessage()
def create(): ReferenceWriteMessage = ReferenceWriteMessage()
}
/**
* The result returned by the flow for each [[ReferenceWriteMessage]].
*
* As this class is not meant to be instantiated outside of this connector
* the constructor is marked as INTERNAL API.
*
* The constructor is INTERNAL API, but you may construct instances for testing by using
* [[akka.stream.alpakka.reference.testkit.MessageFactory]].
*/
final class ReferenceWriteResult @InternalApi private[reference] (val message: ReferenceWriteMessage,
val metrics: Map[String, Long],
val status: Int) {
/** Java API */
def getMessage: ReferenceWriteMessage = message
/**
* Java API
*
* Java getter needs to return Java Long classes which is converted from Scala Long.
*/
def getMetrics(): java.util.Map[String, java.lang.Long] =
metrics.map {
case (key, value) => key -> java.lang.Long.valueOf(value)
}.asJava
/** Java API */
def getStatus: Int = status
override def toString =
s"""ReferenceWriteResult(message=$message,status=$status)"""
override def equals(other: Any): Boolean = other match {
case that: ReferenceWriteResult =>
java.util.Objects.equals(this.message, that.message) &&
java.util.Objects.equals(this.status, that.status)
case _ => false
}
override def hashCode(): Int =
java.util.Objects.hash(message, Int.box(status))
}