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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ plugins {
id 'org.jetbrains.kotlin.jvm' version '1.7.21'
}

test {
useJUnitPlatform()
}

group 'ru.otus'
version '1.0-SNAPSHOT'

Expand All @@ -11,4 +15,5 @@ repositories {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
testImplementation "org.junit.jupiter:junit-jupiter:5.8.1"
}
38 changes: 38 additions & 0 deletions src/main/kotlin/ru/otus/homework/builder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ru.otus.homework

class Notification private constructor(
val iconRes: String?,
val title: String,
val subTitle: String?,
) {
data class Builder(
var iconRes: String? = null,
var title: String? = null,
var subTitle: String? = null,
) {
fun iconRes(iconRes: String) = apply {
this.iconRes = iconRes
}
fun title(title: String) = apply {
this.title = title
}
fun subTitle(subTitle: String) = apply {
this.subTitle = subTitle
}

fun build() = Notification(
iconRes = iconRes,
title = title ?: "",
subTitle = subTitle
)
}
}

fun main() {
val notification = Notification.Builder()
.title("test notification title")
.subTitle("test notification subtitle")
.build()

println("Result notification iconRes: ${notification.iconRes}, title: ${notification.title}, subTitle: ${notification.subTitle}")
}
66 changes: 66 additions & 0 deletions src/main/kotlin/ru/otus/homework/command.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package ru.otus.homework

interface Command {
fun execute()
fun undo()
}

data class Clipboard(var content: String = "")

class Editor(initialContent: String) {
private var content = initialContent

fun write(text: String) {
content += text
}

fun remove(text: String) {
content = content.removeSuffix(text)
}

fun read(): String = content
}

class PasteCommand(
private val editor: Editor,
private val clipboard: Clipboard,
) : Command {
override fun execute() {
editor.write(clipboard.content)
}

override fun undo() {
editor.remove(clipboard.content)
}
}

class EditorInvoker {
private val commandsCache = mutableListOf<Command>()

fun executeCommand(command: Command) {
commandsCache.add(command)
command.execute()
}

fun undoLastCommand() {
if (commandsCache.isNotEmpty()) {
commandsCache.removeLast().undo()
}
}
}

fun main() {
val editor = Editor("artem")
val invoker = EditorInvoker()

println("initial editor: ${editor.read()}")

invoker.executeCommand(PasteCommand(editor, Clipboard("test")))
invoker.executeCommand(PasteCommand(editor, Clipboard("boo")))

println("after paste: ${editor.read()}")

invoker.undoLastCommand()

println("after undo: ${editor.read()}")
}
31 changes: 31 additions & 0 deletions src/main/kotlin/ru/otus/homework/decorator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ru.otus.homework

interface Logger {
fun log(text: String)
}

class PrintLogger : Logger {
override fun log(text: String) {
println(text)
}
}

class MapLoggerDecorator<K, V>(
private val map: MutableMap<K, V> = mutableMapOf(),
private val logger: Logger,
): MutableMap<K, V> by map {
override fun put(key: K, value: V): V? {
return map.put(key, value).apply {
logger.log("MapLoggerDecorator putted key: $key, value: $value. Result map: $map")
}
}
}

fun main() {
val mapLoggerDecorator = MapLoggerDecorator<String, String>(
logger = PrintLogger(),
)
mapLoggerDecorator["artem"] = "tim"
mapLoggerDecorator["andrey"] = "drew"
mapLoggerDecorator["alexei"] = "alex"
}
21 changes: 21 additions & 0 deletions src/main/kotlin/ru/otus/homework/singleton.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ru.otus.homework

class Singleton private constructor() {
companion object {
val instance: Singleton by lazy {
Singleton()
}
}

private var i = 0

fun doSomething() = ++i
}

fun main() {
val instance1 = Singleton.instance
println("instance1 increment: ${instance1.doSomething()}")

val instance2 = Singleton.instance
println("instance2 increment: ${instance2.doSomething()}")
}
38 changes: 38 additions & 0 deletions src/test/kotlin/ru/otus/homework/BuilderTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ru.otus.homework

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test

private const val notificationTitle = "test notification title"
private const val notificationSubTitle = "test notification subtitle"

class BuilderTest {
private companion object {
val notification = Notification.Builder()
.title(notificationTitle)
.subTitle(notificationSubTitle)
.build()
}

@Test
fun notificationIconRes() {
assertNull(notification.iconRes)
}

@Test
fun notificationTitle() {
assertEquals(
notificationTitle,
notification.title
)
}

@Test
fun notificationSubTitle() {
assertEquals(
notificationSubTitle,
notification.subTitle
)
}
}
61 changes: 61 additions & 0 deletions src/test/kotlin/ru/otus/homework/CommandTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package ru.otus.homework

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.MethodOrderer
import org.junit.jupiter.api.Order
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestMethodOrder

private const val initialEditorContent = "artem"
private const val clipboardFirstContent = "test"
private const val clipboardSecondContent = "boo"

@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
class CommandTest {
private companion object {
private val editor: Editor = Editor(initialEditorContent)
private val invoker = EditorInvoker()
}

@Test
@Order(1)
fun shouldInitialValue() {
assertEquals(
initialEditorContent,
editor.read()
)
}

@Test
@Order(2)
fun shouldPasteFirst() {
invoker.executeCommand(PasteCommand(editor, Clipboard(clipboardFirstContent)))

assertEquals(
"$initialEditorContent$clipboardFirstContent",
editor.read()
)
}

@Test
@Order(3)
fun shouldPasteSecond() {
invoker.executeCommand(PasteCommand(editor, Clipboard(clipboardSecondContent)))

assertEquals(
"$initialEditorContent$clipboardFirstContent$clipboardSecondContent",
editor.read()
)
}

@Test
@Order(4)
fun shouldLastCommandUndo() {
invoker.undoLastCommand()

assertEquals(
"$initialEditorContent$clipboardFirstContent",
editor.read()
)
}
}
33 changes: 33 additions & 0 deletions src/test/kotlin/ru/otus/homework/SingletonTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ru.otus.homework

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.ClassOrderer.OrderAnnotation
import org.junit.jupiter.api.MethodOrderer
import org.junit.jupiter.api.Order
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestMethodOrder

@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
class SingletonTest {
@Test
@Order(1)
fun instance1Increment() {
val instance1: Singleton = Singleton.instance

assertEquals(
1,
instance1.doSomething()
)
}

@Test
@Order(2)
fun instance2Increment() {
val instance2: Singleton = Singleton.instance

assertEquals(
2,
instance2.doSomething()
)
}
}