Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions core-scala/src/main/scala/com/baeldung/scala/Events.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.baeldung.scala

abstract class EventAbstract {
val eventType: String
var applicationId: Int = 99
def debug = { println(toJson) }
def toJson: String
}

trait EventTrait {
val eventType: String
var applicationId: Int = 99
def debug = { println(toJson) }
def toJson: String
}

class NewPostArrivedEventA extends EventAbstract {
val eventType: String = "NewPost"
def toJson: String = s"""{"type": "$eventType", "appId": $applicationId}"""
}

class NewPostArrivedEventT extends EventTrait {
val eventType: String = "NewPost"
def toJson: String = s"""{"type": "$eventType", "appId": $applicationId}"""
}


trait Event2 {
val eventType: String
var applicationId: Int = 99
}

trait Debug {
def debug = { println(toJson) }
def toJson: String
}

class SimpleEvent extends Event2 with Debug {
val eventType: String = "simpleType"
def toJson: String = s"""{"type": "$eventType", "appId": $applicationId}"""
}
34 changes: 34 additions & 0 deletions core-scala/src/main/scala/com/baeldung/scala/StackableChange.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.baeldung.scala

abstract class EventSender {
def envelope(payload: String): String
}

class SimpleEventSender extends EventSender {
def envelope(payload: String): String = {
s"""{ payload: $payload }"""
}
}

trait EventFirstSender extends EventSender {
abstract override def envelope(payload: String): String = {
s"""{ firstSender: ${super.envelope(payload)} }"""
}
}

trait EventSecondSender extends EventSender {
abstract override def envelope(payload: String): String = {
s"""{ secondSender: ${super.envelope(payload)} }"""
}
}

trait EventThirdSender extends EventSender {
abstract override def envelope(payload: String): String = {
s"""{ thirdSender: ${super.envelope(payload)} }"""
}
}





Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.baeldung.scala

import org.junit.Test

class MultipleInheritanceTest {

val expected: String = """{"type": "SimpleType"}"""

@Test
def whenMultipleTraitsInherited_thenFunctionalityExtended() = {

// Set up a an objects with abstract parent and Trait
val ev = new SimpleEvent() with Debug

// We expected object have extended functionality
ev.debug
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.baeldung.scala

import org.junit.Assert.assertFalse
import org.junit.Test

class StackableChangeTest {

@Test
def whenFirstTraitOrderDefined_thenOneResult() = {

// Set up a an object with stackable Traits
val sender = new SimpleEventSender with EventFirstSender with EventSecondSender with EventThirdSender

// Let's provide some payload to envelope

val enveloped = sender.envelope("data")

val expected = "{ thirdSender: { secondSender: { firstSender: { payload: data } } } }"
assert(enveloped == expected)
}

@Test
def whenSecondTraitOrderDefined_thenOtherResult() = {

// Set up a an object with stackable Traits with different order
val sender = new SimpleEventSender with EventThirdSender with EventSecondSender with EventFirstSender

// Let's provide some payload to envelope

val enveloped = sender.envelope("data")

assert(enveloped == "{ firstSender: { secondSender: { thirdSender: { payload: data } } } }")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.baeldung.scala

import org.junit.Test

class TraitVsAbstractClassTest {

@Test
def whenDifferentObjectsFromDifferentParents_thenBehaveTheSame() = {

// Set up a an objects with abstract parent and Trait
val eventFromAbstract = new NewPostArrivedEventA
val eventFromTrait = new NewPostArrivedEventT

// We expected the same behavior
assert(eventFromAbstract.toJson == eventFromTrait.toJson)
}
}