Skip to content

Commit 7806006

Browse files
authored
SCALA-706: Code companion for the article (#1413)
* SCALA-706: Code companion for the article * SCALA-706: Code companion for the article
1 parent 3b17d0f commit 7806006

File tree

10 files changed

+216
-0
lines changed

10 files changed

+216
-0
lines changed

build.sbt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ lazy val scala_core_oop = (project in file("scala-core-modules/scala-core-oop"))
143143
Seq(catsEffect, jUnitInterface) ++ scalaTestDeps
144144
)
145145

146+
lazy val scala_strings_2 = (project in file("scala-core-modules/scala-strings-2"))
147+
.settings(
148+
name := "scala-core-strings",
149+
libraryDependencies ++= scalaTestDeps,
150+
libraryDependencies += jUnitInterface,
151+
scalaVersion := scala3Version
152+
)
153+
154+
146155
lazy val scala_core_fp = (project in file("scala-core-modules/scala-core-fp"))
147156
.settings(
148157
name := "scala-core-fp",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Core Scala Strings
2+
3+
This module contains articles about Scala's Strings.
4+
5+
### Relevant Articles:
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.scala.strings.checkempty
2+
3+
object EmptyPatternMatchingExtensions {
4+
extension (seq: Seq[?])
5+
def isNullOrEmpty: Boolean = seq match {
6+
case null => true
7+
case Seq() => true
8+
case s => false
9+
}
10+
11+
extension (seq: Seq[Char])
12+
def isNullOrEmptyOrWhitespace: Boolean = seq match {
13+
case null => true
14+
case Seq() => true
15+
case s => s.forall(_.isWhitespace)
16+
}
17+
18+
extension (str: String)
19+
def isNullOrEmptyOrWhitespace: Boolean = str match {
20+
case null => true
21+
case "" => true
22+
case s => s.trim.isEmpty
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.scala.strings.checkempty
2+
3+
object EmptySeqExtensions {
4+
extension (objs: Seq[?])
5+
def isNullOrEmpty: Boolean = objs == null || objs.isEmpty
6+
7+
extension (objs: Seq[Char])
8+
def isNullOrEmptyOrWhitespace: Boolean =
9+
objs.isNullOrEmpty || objs.forall(_.isWhitespace)
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.scala.strings.checkempty
2+
3+
object EmptyStringExtensions {
4+
extension (str: String)
5+
def isEmptyOrWhitespace: Boolean = str.trim.isEmpty
6+
def isNullOrEmptyOrWhitespace: Boolean =
7+
str == null || str.isEmptyOrWhitespace
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.scala.strings.checkempty
2+
3+
type Empty
4+
type NonEmpty
5+
6+
opaque type VerifiedString[T] = String
7+
8+
object VerifiedString:
9+
def apply[T](value: String): VerifiedString[T] = value
10+
11+
given Conversion[VerifiedString[NonEmpty], String] = _.value
12+
13+
extension (str: String)
14+
def asVerifiedString =
15+
if str.isNullOrEmptyOrWhitespace then VerifiedString[Empty](str)
16+
else VerifiedString[NonEmpty](str)
17+
18+
def isEmptyOrWhitespace: Boolean = str.trim.isEmpty
19+
def isNullOrEmptyOrWhitespace: Boolean =
20+
str == null || str.isEmptyOrWhitespace
21+
22+
extension (vstr: VerifiedString[NonEmpty]) def value: String = vstr
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.scala.strings.checkempty
2+
3+
import org.scalatest.flatspec.AnyFlatSpec
4+
import org.scalatest.matchers.should.Matchers
5+
6+
import EmptyPatternMatchingExtensions._
7+
8+
class EmptyPatternMatchingUnitTest extends AnyFlatSpec with Matchers {
9+
"isNullOrEmpty" should "return true for null reference" in {
10+
val seq: Seq[?] = null
11+
seq.isNullOrEmpty.shouldBe(true)
12+
}
13+
14+
it should "return true for empty sequences" in {
15+
val seq: Seq[Any] = Seq.empty
16+
seq.isNullOrEmpty.shouldBe(true)
17+
}
18+
19+
"isNullOrEmptyOrWhitespace" should "return true for null strings" in {
20+
val str: String = null
21+
str.isNullOrEmptyOrWhitespace.shouldBe(true)
22+
}
23+
24+
it should "return true for empty strings" in {
25+
val str: String = ""
26+
str.isNullOrEmptyOrWhitespace.shouldBe(true)
27+
}
28+
29+
it should "return true for strings with only whitespace" in {
30+
val str: String = " "
31+
str.isNullOrEmptyOrWhitespace.shouldBe(true)
32+
}
33+
34+
it should "return false for non-empty strings" in {
35+
val str: String = "Hello, Scala"
36+
str.isNullOrEmptyOrWhitespace.shouldBe(false)
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.scala.strings.checkempty
2+
3+
import org.scalatest.flatspec.AnyFlatSpec
4+
import org.scalatest.matchers.should.Matchers
5+
6+
import EmptySeqExtensions._
7+
8+
class EmptySeqUnitTest extends AnyFlatSpec with Matchers {
9+
"isNullOrEmpty" should "return true for null reference" in {
10+
val seq: Seq[?] = null
11+
seq.isNullOrEmpty.shouldBe(true)
12+
}
13+
14+
it should "return true for empty sequences" in {
15+
val seq: Seq[Any] = Seq.empty
16+
seq.isNullOrEmpty.shouldBe(true)
17+
}
18+
19+
"isNullOrEmptyOrWhitespace" should "return true for null strings" in {
20+
val str: String = null
21+
str.isNullOrEmptyOrWhitespace.shouldBe(true)
22+
}
23+
24+
it should "return true for empty strings" in {
25+
val str: String = ""
26+
str.isNullOrEmptyOrWhitespace.shouldBe(true)
27+
}
28+
29+
it should "return true for strings with only whitespace" in {
30+
val str: String = " "
31+
str.isNullOrEmptyOrWhitespace.shouldBe(true)
32+
}
33+
34+
it should "return false for non-empty strings" in {
35+
val str: String = "Hello, Scala"
36+
str.isNullOrEmptyOrWhitespace.shouldBe(false)
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.scala.strings.checkempty
2+
3+
import org.scalatest.flatspec.AnyFlatSpec
4+
import org.scalatest.matchers.should.Matchers
5+
import EmptyStringExtensions._
6+
7+
class EmptyStringUnitTest extends AnyFlatSpec with Matchers {
8+
9+
"isNullOrEmptyOrWhitespace" should "return true for null strings" in {
10+
val str: String = null
11+
str.isNullOrEmptyOrWhitespace.shouldBe(true)
12+
}
13+
14+
it should "return true for empty strings" in {
15+
val str: String = ""
16+
str.isNullOrEmptyOrWhitespace.shouldBe(true)
17+
}
18+
19+
it should "return true for strings with only whitespace" in {
20+
val str: String = " "
21+
str.isNullOrEmptyOrWhitespace.shouldBe(true)
22+
}
23+
24+
it should "return false for non-empty strings" in {
25+
val str: String = "Hello, Scala"
26+
str.isNullOrEmptyOrWhitespace.shouldBe(false)
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.scala.strings.checkempty
2+
3+
import com.baeldung.scala.strings.checkempty.VerifiedString.*
4+
import org.scalatest.flatspec.AnyFlatSpec
5+
import org.scalatest.matchers.should.Matchers
6+
7+
class VerifiedStringUnitTest extends AnyFlatSpec with Matchers {
8+
9+
def lenght(str: VerifiedString[NonEmpty]) = str.length
10+
11+
"a verified string" should "never be empty" in {
12+
val str: String = null
13+
str.isNullOrEmptyOrWhitespace.shouldBe(true)
14+
}
15+
16+
it should "return true for empty strings" in {
17+
val str: String = ""
18+
str.isNullOrEmptyOrWhitespace.shouldBe(true)
19+
}
20+
21+
it should "return true for strings with only whitespace" in {
22+
val str: String = " "
23+
str.isNullOrEmptyOrWhitespace.shouldBe(true)
24+
}
25+
26+
it should "return false for non-empty strings" in {
27+
val str: String = "Hello, Scala"
28+
str.isNullOrEmptyOrWhitespace.shouldBe(false)
29+
}
30+
}

0 commit comments

Comments
 (0)