Skip to content

Commit 5816727

Browse files
author
Cristian Popovici
committed
add scalafmt
1 parent 9781a38 commit 5816727

File tree

5 files changed

+148
-68
lines changed

5 files changed

+148
-68
lines changed

.scalafmt.conf

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
version = "2.2.2"
2+
3+
#
4+
# See http://scalameta.org/scalafmt/#Configuration for details
5+
#
6+
project {
7+
includeFilters = [
8+
".*.\\.scala$"
9+
".*\\..sbt$"
10+
]
11+
}
12+
13+
maxColumn = 120
14+
15+
# Vertical alignment, options: none, some, more
16+
#
17+
# This awkward, self-contradictory, configuration ensures that only
18+
# the common sbt tokens get aligned, and not "production" code.
19+
#
20+
align = none
21+
align {
22+
openParenCallSite = false
23+
openParenDefnSite = false
24+
tokens = ["%", ":=", "~=", "<-"]
25+
}
26+
27+
# If true, the margin character | is aligned with the opening triple quote string literals
28+
assumeStandardLibraryStripMargin = true
29+
30+
#From scalafmt website:
31+
#see: http://scalameta.org/scalafmt/#includeCurlyBraceInSelectChains
32+
includeCurlyBraceInSelectChains = false
33+
34+
continuationIndent {
35+
callSite = 2
36+
defnSite = 2
37+
extendSite = 2
38+
}
39+
40+
danglingParentheses = false
41+
42+
43+
newlines {
44+
alwaysBeforeTopLevelStatements = false
45+
sometimesBeforeColonInMethodReturnType = true
46+
penalizeSingleSelectMultiArgList = false
47+
alwaysBeforeElseAfterCurlyIf = false
48+
neverInResultType = false
49+
}
50+
51+
spaces {
52+
afterKeywordBeforeParen = true
53+
}
54+
55+
binPack {
56+
parentConstructors = true
57+
literalArgumentLists = true
58+
}
59+
60+
optIn {
61+
breaksInsideChains = false
62+
breakChainOnFirstMethodDot = true
63+
configStyleArguments = true
64+
}
65+
66+
runner {
67+
optimizer {
68+
# Set to -1 to disable. Number of characters needed to trigger "config-style" formatting
69+
# see: http://scalameta.org/scalafmt/#runner.optimizer.forceConfigStyleOnOffset
70+
forceConfigStyleOnOffset = 150
71+
72+
# minimum number of func arguments before config-style (look at top of file) is enabled
73+
forceConfigStyleMinArgCount = 2
74+
}
75+
}
76+
77+
rewrite {
78+
rules = [
79+
SortImports
80+
# if your for has more than one single <- then it gets transformed into a multit-line curly brace one
81+
# PreferCurlyFors
82+
]
83+
}
84+
85+
optIn {
86+
blankLineBeforeDocstring = true
87+
}
88+
89+
lineEndings=preserve

project/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=0.13.16
1+
sbt.version=1.2.8

project/plugins.sbt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "2.0.0")
1+
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "2.0.0")
2+
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.2.1")
3+

src/main/scala/util/retry/blocking/Retry.scala

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,72 +7,71 @@ import scala.util.Try
77
import scala.util.control.NonFatal
88

99
/**
10-
* The `Retry` type represents a computation that is retrying itself in case of an exception. It uses a `RetryStrategy`
11-
* as a policy for the retry operation.
12-
*
13-
* The result may be successful consisting of the computation result value or a failure that
14-
* is wrapping the underlying exception. The type is similar to the scala [[scala.util.Try]] type.
15-
*
16-
* Example:
17-
* {{{
18-
* import scala.concurrent.duration._
19-
* import util.retry.blocking.{RetryStrategy, Failure, Retry, Success}
20-
*
21-
* implicit val retryStrategy =
22-
* RetryStrategy.fixedBackOff(retryDuration = 1.seconds, maxAttempts = 2)
23-
*
24-
* val r = Retry(1 / 1) match {
25-
* case Success(x) => x
26-
* case Failure(t) => log("Exception occurred", t)
27-
* }
28-
* }}}
29-
*/
10+
* The `Retry` type represents a computation that is retrying itself in case of an exception. It uses a `RetryStrategy`
11+
* as a policy for the retry operation.
12+
*
13+
* The result may be successful consisting of the computation result value or a failure that
14+
* is wrapping the underlying exception. The type is similar to the scala [[scala.util.Try]] type.
15+
*
16+
* Example:
17+
* {{{
18+
* import scala.concurrent.duration._
19+
* import util.retry.blocking.{RetryStrategy, Failure, Retry, Success}
20+
*
21+
* implicit val retryStrategy =
22+
* RetryStrategy.fixedBackOff(retryDuration = 1.seconds, maxAttempts = 2)
23+
*
24+
* val r = Retry(1 / 1) match {
25+
* case Success(x) => x
26+
* case Failure(t) => log("Exception occurred", t)
27+
* }
28+
* }}}
29+
*/
3030
sealed trait Retry[+T] {
31-
3231
/**
33-
* Returns `true` if the `Retry` is a `Failure` otherwise it returns `false`.
34-
*/
32+
* Returns `true` if the `Retry` is a `Failure` otherwise it returns `false`.
33+
*/
3534
def flatMap[S](f: T => Retry[S]): Retry[S]
3635

3736
/**
38-
* Maps the given function to the value from this `Success` or returns this if this is a `Failure`.
39-
*/
37+
* Maps the given function to the value from this `Success` or returns this if this is a `Failure`.
38+
*/
4039
def map[S](f: T => S)(implicit strategy: RetryStrategyProducer): Retry[S]
4140

4241
/**
43-
* Returns `true` if the `Retry` is a `Failure` otherwise it returns `false`.
44-
*/
42+
* Returns `true` if the `Retry` is a `Failure` otherwise it returns `false`.
43+
*/
4544
def isFailure: Boolean
4645

4746
/**
48-
* Returns `true` if the `Retry` is a `Success` otherwise it returns `false` otherwise.
49-
*/
47+
* Returns `true` if the `Retry` is a `Success` otherwise it returns `false` otherwise.
48+
*/
5049
def isSuccess: Boolean
5150

5251
/**
53-
* Returns the computation value in case of a `Success`.
54-
* In case of a `Failure` it throws the underlying exception.
55-
*/
52+
* Returns the computation value in case of a `Success`.
53+
* In case of a `Failure` it throws the underlying exception.
54+
*/
5655
def get: T
5756

5857
/**
59-
* Returns the computation value in case of a `Success`. Otherwise it is returning the provided default.
60-
*/
58+
* Returns the computation value in case of a `Success`. Otherwise it is returning the provided default.
59+
*/
6160
def getOrElse[U >: T](default: => U): U = if (isSuccess) get else default
6261

6362
/**
64-
* Applies the given function `f` if this is a `Success`, otherwise returns `Unit` if this is a `Failure`.
65-
*/
63+
* Applies the given function `f` if this is a `Success`, otherwise returns `Unit` if this is a `Failure`.
64+
*/
6665
def foreach[X](f: T => X)
6766

6867
/**
69-
* Applies the given function `f` if this is a `Failure`, otherwise returns this if this is a `Success`.
70-
*/
68+
* Applies the given function `f` if this is a `Failure`, otherwise returns this if this is a `Success`.
69+
*/
7170
def recover[X >: T](f: PartialFunction[Throwable, X]): Retry[X]
7271

7372
/**
74-
* Transforms the `Retry` value by applying a transformation function to its underlying value
75-
*/
73+
* Transforms the `Retry` value by applying a transformation function to its underlying value
74+
*/
7675
def transform[X](f: T => X): X
7776
}
7877

@@ -85,8 +84,7 @@ final case class Success[+T](value: T) extends Retry[T] {
8584
override def foreach[X](f: T => X): Unit = f(value)
8685
override def transform[X](f: T => X): X = f(value)
8786
override def flatMap[S](f: T => Retry[S]): Retry[S] = f(value)
88-
override def map[S](f: T => S)(
89-
implicit strategy: RetryStrategyProducer): Retry[S] = Retry(f(value))
87+
override def map[S](f: T => S)(implicit strategy: RetryStrategyProducer): Retry[S] = Retry(f(value))
9088
}
9189

9290
final case class Failure[+T](exception: Throwable) extends Retry[T] {
@@ -106,8 +104,7 @@ final case class Failure[+T](exception: Throwable) extends Retry[T] {
106104
override def foreach[X](f: T => X): Unit = ()
107105
override def transform[X](f: T => X): X = throw exception
108106
override def flatMap[S](f: T => Retry[S]): Retry[S] = Failure(exception)
109-
override def map[S](f: T => S)(
110-
implicit strategy: () => RetryStrategy): Retry[S] = Failure(exception)
107+
override def map[S](f: T => S)(implicit strategy: () => RetryStrategy): Retry[S] = Failure(exception)
111108
}
112109

113110
object Retry {

src/main/scala/util/retry/blocking/RetryStrategy.scala

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import scala.concurrent.blocking
99
* Interface defining a retry strategy
1010
*/
1111
sealed trait RetryStrategy {
12-
1312
/**
1413
* Returns `true` if the retry should be performed
1514
*/
@@ -45,39 +44,34 @@ class RetryForever(val waitTime: Int) extends RetryStrategy {
4544
}
4645

4746
class FixedWaitRetryStrategy(val millis: Long, override val maxAttempts: Int)
48-
extends MaxNumberOfRetriesStrategy(maxAttempts) with Sleep {
49-
47+
extends MaxNumberOfRetriesStrategy(maxAttempts) with Sleep {
5048
override def update(): RetryStrategy = {
5149
sleep(millis)
5250
new FixedWaitRetryStrategy(millis, maxAttempts - 1)
5351
}
5452
}
5553

56-
class RandomWaitRetryStrategy(val minimumWaitTime: Long,
57-
val maximumWaitTime: Long,
58-
override val maxAttempts: Int)
59-
extends MaxNumberOfRetriesStrategy(maxAttempts) with Sleep {
60-
54+
class RandomWaitRetryStrategy(val minimumWaitTime: Long, val maximumWaitTime: Long, override val maxAttempts: Int)
55+
extends MaxNumberOfRetriesStrategy(maxAttempts) with Sleep {
6156
private[this] final val random: Random = new Random()
6257

6358
override def update(): RetryStrategy = {
6459
val millis: Long =
6560
math.abs(random.nextLong) % (maximumWaitTime - minimumWaitTime)
6661
sleep(millis)
6762
new RandomWaitRetryStrategy(
68-
minimumWaitTime,
69-
maximumWaitTime,
70-
maxAttempts - 1
63+
minimumWaitTime,
64+
maximumWaitTime,
65+
maxAttempts - 1
7166
)
7267
}
7368
}
7469

75-
class FibonacciBackOffStrategy(
76-
waitTime: Long, step: Long, override val maxAttempts: Int)
77-
extends MaxNumberOfRetriesStrategy(maxAttempts) with Sleep {
70+
class FibonacciBackOffStrategy(waitTime: Long, step: Long, override val maxAttempts: Int)
71+
extends MaxNumberOfRetriesStrategy(maxAttempts) with Sleep {
7872
def fibonacci(n: Long): Long = {
7973
n match {
80-
case x@(0L | 1L) => x
74+
case x @ (0L | 1L) => x
8175
case _ =>
8276
var prevPrev: Long = 0L
8377
var prev: Long = 1L
@@ -111,7 +105,6 @@ sealed trait Sleep {
111105
}
112106

113107
object RetryStrategy {
114-
115108
type RetryStrategyProducer = () => RetryStrategy
116109

117110
val noRetry: RetryStrategyProducer = () => NoRetry
@@ -125,12 +118,11 @@ object RetryStrategy {
125118
def fixedBackOff(retryDuration: FiniteDuration, maxAttempts: Int): RetryStrategyProducer =
126119
() => new FixedWaitRetryStrategy(retryDuration.toMillis, maxAttempts)
127120

128-
def randomBackOff(minimumWaitDuration: FiniteDuration,
129-
maximumWaitDuration: FiniteDuration,
130-
maxAttempts: Int):RetryStrategyProducer =
131-
() => new RandomWaitRetryStrategy(minimumWaitDuration.toMillis,
132-
maximumWaitDuration.toMillis,
133-
maxAttempts)
121+
def randomBackOff(
122+
minimumWaitDuration: FiniteDuration,
123+
maximumWaitDuration: FiniteDuration,
124+
maxAttempts: Int): RetryStrategyProducer =
125+
() => new RandomWaitRetryStrategy(minimumWaitDuration.toMillis, maximumWaitDuration.toMillis, maxAttempts)
134126

135127
def fibonacciBackOff(initialWaitDuration: FiniteDuration, maxAttempts: Int): RetryStrategyProducer =
136128
() => new FibonacciBackOffStrategy(initialWaitDuration.toMillis, 1, maxAttempts)

0 commit comments

Comments
 (0)