Skip to content
This repository has been archived by the owner on Sep 17, 2019. It is now read-only.

Commit

Permalink
Synchronized the first part with workshop @web-demo
Browse files Browse the repository at this point in the history
  • Loading branch information
svtk committed Oct 23, 2015
1 parent 4bcd8a8 commit 283d1e4
Show file tree
Hide file tree
Showing 41 changed files with 228 additions and 212 deletions.
49 changes: 13 additions & 36 deletions src/i_introduction/_10_Object_Expressions/ObjectExpressions.kt
Original file line number Diff line number Diff line change
@@ -1,45 +1,22 @@
package i_introduction._10_Object_Expressions

import java.util.Comparator
import util.TODO
import java.io.File
import java.awt.event.MouseListener
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent

fun objectLiteral() {

abstract class Foo {
abstract fun foo()
}

// Anonymous object extending a class
val foo: Foo = object : Foo() {
override fun foo() {
// ...
}
}

// Anonymous object extending an interface
runInANewThread(object : Runnable {
override fun run() {
// ...
}
})
}

fun runInANewThread(runnable: Runnable) = Thread(runnable).start()
import java.util.*
import util.*

fun todoTask10(): Nothing = TODO(
"""
Task 10.
Add an object expression that extends MouseAdapter and counts the number of mouse clicks
as an argument to the function 'handleMouse()'.
"""
Read about object expressions that play the same role in Kotlin as anonymous classes do in Java.
Add an object expression that provides a comparator to sort a list in a descending order using java.util.Collections class.
In Kotlin you use Kotlin library extensions instead of java.util.Collections,
but this example is still a good demonstration of mixing Kotlin and Java code.
""",
documentation = doc10()
)

fun task10(handleMouse: (MouseListener) -> Unit): Int {
var mouseClicks = 0
handleMouse(todoTask10())
return mouseClicks
fun task10(): List<Int> {
val arrayList = arrayListOf(1, 5, 2)
Collections.sort(arrayList, todoTask10())
return arrayList
}
22 changes: 22 additions & 0 deletions src/i_introduction/_11_SAM_Conversions/SAMConversions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package i_introduction._11_SAM_Conversions

import util.TODO
import util.doc11_1
import java.util.*

fun todoTask11() = TODO(
"""
Task 11.
When an object implements a SAM interface (one with a Single Abstract Method), you can pass a lambda instead.
Read more about SAM conversions in the blog posts about Kotlin.
Rewrite the previous example changing an object expression to a lambda.
""",
documentation = doc11_1()
)

fun task11(): List<Int> {
val arrayList = arrayListOf(1, 5, 2)
Collections.sort(arrayList, { x, y -> todoTask11() })
return arrayList
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package i_introduction._12_Extensions_On_Collections

import util.*

fun todoTask12() = TODO(
"""
Task 12.
In Kotlin standard library there are lots of extension functions that make the work with collections more convenient.
Rewrite the previous example once more using an extension function 'sortedDescending'.
Kotlin code can be easily mixed with Java code.
Thus in Kotlin we don't introduce our own collections, but use standard Java ones (slightly improved).
Read about read-only and mutable views on Java collections.
""",
documentation = doc12_1()
)

fun task12(): List<Int> {
todoTask12()
return arrayListOf(1, 5, 2)
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package i_introduction._1_Functions;
package i_introduction._1_Java_To_Kotlin_Converter;

import util.JavaCode;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package i_introduction._1_Functions
package i_introduction._1_Java_To_Kotlin_Converter

import util.TODO

Expand Down

This file was deleted.

This file was deleted.

19 changes: 19 additions & 0 deletions src/i_introduction/_2_Named_Arguments/NamedArguments.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package i_introduction._2_Named_Arguments

import util.*
import i_introduction._1_Java_To_Kotlin_Converter.task1

fun todoTask2() = TODO(
"""
Task 2.
Implement the same logic as in 'task1' again through the library method 'joinToString()'.
Change values of some of the 'joinToString' arguments.
Use default and named arguments to improve the readability of the function invocation.
""",
documentation = doc2(),
references = { collection: Collection<Int> -> task1(collection); collection.joinToString() })

fun task2(collection: Collection<Int>): String {
todoTask2()
return collection.joinToString()
}
24 changes: 24 additions & 0 deletions src/i_introduction/_3_Default_Arguments/DefaultAndNamedParams.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package i_introduction._3_Default_Arguments

import util.*

fun todoTask3_() = TODO(
"""
Task 3.
Several overloads of 'JavaCode2.foo()' can be replaced with one function in Kotlin.
Change the declaration of the function 'foo' in a way that makes the code using 'foo' compile.
You have to add parameters and replace 'todoTask3()' with a real body.
Uncomment the commented code and make it compile.
""",
documentation = doc2(),
references = { name: String -> JavaCode2().foo(name); foo(name) })

fun foo(name: String): String = todoTask3_()

fun task3_(): String {
todoTask3_()
// return (foo("a") +
// foo("b", number = 1) +
// foo("c", toUpperCase = true) +
// foo(name = "d", number = 2, toUpperCase = true))
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package i_introduction._2_Default_And_Named_Params;
package i_introduction._3_Default_Arguments;

import util.JavaCode;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package i_introduction._3_Lambdas;
package i_introduction._4_Lambdas;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package i_introduction._3_Lambdas
package i_introduction._4_Lambdas

import util.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package i_introduction._4_String_Templates
package i_introduction._5_String_Templates

import java.util.regex.Pattern
import util.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package i_introduction._7_Data_Classes
package i_introduction._6_Data_Classes

import util.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package i_introduction._7_Data_Classes;
package i_introduction._6_Data_Classes;

import util.JavaCode;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package i_introduction._5_Nullable_Types;
package i_introduction._7_Nullable_Types;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package i_introduction._5_Nullable_Types
package i_introduction._7_Nullable_Types

import util.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package i_introduction._6_Smart_Casts;
package i_introduction._8_Smart_Casts;

import util.JavaCode;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package i_introduction._6_Smart_Casts
package i_introduction._8_Smart_Casts

import util.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package i_introduction._8_Extension_Functions.StringExtensions
package i_introduction._9_Extension_Functions

import util.*

Expand Down

This file was deleted.

3 changes: 0 additions & 3 deletions src/util/JavaCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
public class JavaCode {
public static Set<String> set = Sets.newHashSet();

// private final int i;

public JavaCode() {
// this.getClass()
set.add(this.getClass().getName());
}
}
18 changes: 18 additions & 0 deletions src/util/LinksToDocumentation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ fun doc7() {}
*/
fun doc8() {}

//https://kotlinlang.org/docs/reference/object-declarations.html

/**
* @see <a href="https://kotlinlang.org/docs/reference/object-declarations.html">Object expressions</a>
*/
fun doc10() {}

/**
* @see <a href="http://blog.jetbrains.com/kotlin/2013/04/kotlin-m5-2-intellij-idea-12-1-and-gradle/#SAM-constructors">SAM-constructors</a>,
* <a href="http://blog.jetbrains.com/kotlin/2013/06/kotlin-m5-3-idea-13-delegated-properties-and-more/#SAM-conversions">SAM-conversions</a>
*/
fun doc11_1() {}

/**
* @see <a href="http://blog.jetbrains.com/kotlin/2012/09/kotlin-m3-is-out/#Collections">Read-only and mutable views on Java collections</a>
*/
fun doc12_1() {}

/**
* @see <a href="http://kotlinlang.org/docs/reference/operator-overloading.html">Operator overloading</a>
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package i_introduction._9_Extensions_On_Collections;
package v_collections;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
Expand All @@ -8,7 +8,7 @@
import java.util.List;
import java.util.Map;

public class JavaCode9 extends JavaCode {
public class LJavaCode9 extends JavaCode {
public Collection<String> doSomethingStrangeWithCollection(Collection<String> collection) {
Map<Integer, List<String>> groupsByLength = Maps.newHashMap();
for (String s : collection) {
Expand Down
Loading

0 comments on commit 283d1e4

Please sign in to comment.