File tree Expand file tree Collapse file tree 10 files changed +216
-0
lines changed
scala-core-modules/scala-strings-2
main/scala/com/baeldung/scala/strings/checkempty
test/scala/com/baeldung/scala/strings/checkempty Expand file tree Collapse file tree 10 files changed +216
-0
lines changed Original file line number Diff line number Diff 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+
146155lazy val scala_core_fp = (project in file(" scala-core-modules/scala-core-fp" ))
147156 .settings(
148157 name := " scala-core-fp" ,
Original file line number Diff line number Diff line change 1+ ## Core Scala Strings
2+
3+ This module contains articles about Scala's Strings.
4+
5+ ### Relevant Articles:
6+
Original file line number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments