├── .gitignore
├── .travis.yml
├── CNAME
├── LICENSE.txt
├── README.md
├── android-chrome-192x192.png
├── android-chrome-512x512.png
├── apple-touch-icon.png
├── browserconfig.xml
├── cirru
├── classes.cirru
├── collections.cirru
├── concurrency.cirru
├── delegated-properties.cirru
├── dsl.cirru
├── error-prone.cirru
├── footer.cirru
├── functions.cirru
├── generated
│ └── .gitignore
├── header.cirru
├── index.cirru
├── io.cirru
├── scoping-functions.cirru
└── spring-framework.cirru
├── classes.html
├── code
├── java
│ ├── basic
│ │ ├── apply.java
│ │ ├── arrays.java
│ │ ├── bits-operations.java
│ │ ├── cast.java
│ │ ├── default-parameters.java
│ │ ├── elvis-operator.java
│ │ ├── hello-world.java
│ │ ├── null-i.java
│ │ ├── null-ii.java
│ │ ├── operations.java
│ │ ├── print.java
│ │ ├── strings-i.java
│ │ ├── strings-ii.java
│ │ ├── switch.java
│ │ ├── ternary-operator.java
│ │ ├── use.java
│ │ ├── variables-i.java
│ │ └── variables-ii.java
│ ├── classes
│ │ ├── classes-00.java
│ │ ├── classes-01.java
│ │ ├── classes-010.java
│ │ ├── classes-011.java
│ │ ├── classes-012.java
│ │ ├── classes-02.java
│ │ ├── classes-03.java
│ │ ├── classes-04.java
│ │ ├── classes-05.java
│ │ ├── classes-06.java
│ │ ├── classes-07.java
│ │ ├── classes-08.java
│ │ └── classes-09.java
│ ├── collections
│ │ ├── 01_ListOf.java
│ │ ├── 02_Filter.java
│ │ ├── 03_GroupBy.java
│ │ ├── 04_PartitionBy.java
│ │ ├── 05_SortBy.java
│ │ ├── 06_Fold.java
│ │ ├── 07_AssociateBy.java
│ │ ├── 08_Zip.java
│ │ ├── 09_InfixFunctions.java
│ │ ├── 10_CollectingAndThen.java
│ │ ├── 11_For.java
│ │ ├── 12_Repeat.java
│ │ └── 13_Comparators.java
│ ├── concurrency
│ │ ├── 01.java
│ │ ├── 02.java
│ │ └── 03.java
│ ├── delegated-properties
│ │ ├── lazy.java
│ │ ├── map.java
│ │ └── observable.java
│ ├── dsl
│ │ ├── 01.java
│ │ ├── 02.java
│ │ ├── 03.java
│ │ ├── 04.java
│ │ └── 05.java
│ ├── error-prone
│ │ └── 01-equals.java
│ ├── functions
│ │ ├── functions-02.java
│ │ ├── functions-03.java
│ │ ├── functions-04.java
│ │ ├── functions-05.java
│ │ ├── functions-07.java
│ │ ├── functions-08.java
│ │ ├── functions-09.java
│ │ └── functions-10.java
│ ├── io
│ │ └── read-file-to-string.java
│ ├── scoping-functions
│ │ ├── also.java
│ │ ├── apply.java
│ │ ├── let-apply.java
│ │ ├── let.java
│ │ ├── run.java
│ │ ├── takeIf.java
│ │ └── with.java
│ └── spring-framework
│ │ └── spring-framework-00.java
└── kotlin
│ ├── basic
│ ├── apply.kt
│ ├── arrays.kt
│ ├── bits-operations.kt
│ ├── cast.kt
│ ├── default-parameters.kt
│ ├── elvis-operator.kt
│ ├── hello-world.kt
│ ├── null-i.kt
│ ├── null-ii.kt
│ ├── operations.kt
│ ├── print.kt
│ ├── read-file.kt
│ ├── strings-i.kt
│ ├── strings-ii.kt
│ ├── ternary-operator.kt
│ ├── use.kt
│ ├── variables-i.kt
│ ├── variables-ii.kt
│ └── when.kt
│ ├── classes
│ ├── classes-00.kt
│ ├── classes-01.kt
│ ├── classes-010.kt
│ ├── classes-011.kt
│ ├── classes-012.kt
│ ├── classes-02.kt
│ ├── classes-03.kt
│ ├── classes-04.kt
│ ├── classes-05.kt
│ ├── classes-06.kt
│ ├── classes-07.kt
│ ├── classes-08.kt
│ └── classes-09.kt
│ ├── collections
│ ├── 01_ListOf.kt
│ ├── 02_Filter.kt
│ ├── 03_GroupBy.kt
│ ├── 04_PartitionBy.kt
│ ├── 05_SortBy.kt
│ ├── 06_Fold.kt
│ ├── 07_AssociateBy.kt
│ ├── 08_Zip.kt
│ ├── 09_InfixFunctions.kt
│ ├── 10_CollectingAndThen.kt
│ ├── 11_For.kt
│ ├── 12_Repeat.kt
│ └── 13_Comparators.kt
│ ├── concurrency
│ ├── 01.kt
│ ├── 02.kt
│ └── 03.kt
│ ├── delegated-properties
│ ├── lazy.kt
│ ├── map.kt
│ └── observable.kt
│ ├── dsl
│ ├── 01.kt
│ ├── 02.kt
│ ├── 03.kt
│ ├── 04.kt
│ └── 05.kt
│ ├── error-prone
│ └── 01-equals.kt
│ ├── functions
│ ├── functions-02.kt
│ ├── functions-03.kt
│ ├── functions-04.kt
│ ├── functions-05.kt
│ ├── functions-07.kt
│ ├── functions-08.kt
│ ├── functions-09.kt
│ └── functions-10.kt
│ ├── io
│ └── read-file-to-string.kt
│ ├── scoping-functions
│ ├── also.kt
│ ├── apply.kt
│ ├── let-apply.kt
│ ├── let.kt
│ ├── run.kt
│ ├── takeIf.kt
│ └── with.kt
│ └── spring-framework
│ └── spring-framework-00.kt
├── collections.html
├── concurrency.html
├── css
├── bootstrap.min.css
├── highlightjs-night-owl.css
└── style.css
├── delegated-properties.html
├── dsl.html
├── end.html
├── error-prone.html
├── favicon-16x16.png
├── favicon-32x32.png
├── favicon.ico
├── functions.html
├── index.html
├── io.html
├── js
├── bootstrap.min.js
└── highlight.9.16.2.js
├── make.coffee
├── mstile-150x150.png
├── package-lock.json
├── package.json
├── safari-pinned-tab.svg
├── scoping-functions.html
├── site.webmanifest
├── spring-framework.html
├── start-body.html
└── start.html
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | build
3 | .DS_Store
4 | *~
5 | \#*#
6 | .idea
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | dist: trusty
2 | language: node_js
3 | node_js:
4 | - stable
5 | skip_cleanup: true
6 | stages:
7 | - build
8 | - name: deploy
9 | if: branch = master AND type = push
10 | jobs:
11 | include:
12 | - stage: build
13 | before_install:
14 | - npm install -g npm@latest
15 | - npm install -g node-gyp
16 | - npm install -g coffeescript
17 | install:
18 | - npm install
19 | script:
20 | - ./make.coffee dev
21 | - stage: deploy
22 | script:
23 | - git checkout master
24 | - ./make.coffee dev
25 | - git add .
26 | - >
27 | if [ ! -z "$(git status --untracked-files=no --porcelain)" ]; then
28 | git commit -m "HTML updated";
29 | git push https://${GITHUB_TOKEN}@github.com/driver733/kotlin-vs-java.git;
30 | else
31 | echo "No changes in generated HTML files";
32 | fi
33 |
--------------------------------------------------------------------------------
/CNAME:
--------------------------------------------------------------------------------
1 | www.kotlinvsjava.com
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 Fabio Santana
4 | Copyright (c) 2019 Mikhail Yakushin
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Kotlin vs Java
2 |
3 | [](https://travis-ci.com/driver733/kotlin-vs-java)
4 |
5 | [](https://dependabot.com)
6 |
7 | [](https://github.com/driver733/kotlin-vs-java/blob/master/LICENSE.txt)
8 |
9 | Web page: [https://www.kotlinvsjava.com](https://www.kotlinvsjava.com)
10 |
11 | Based on: [fabiomsr/from-java-to-kotlin](https://github.com/fabiomsr/from-java-to-kotlin)
12 |
13 | ## Develop
14 |
15 | 1. Install npm
16 | 2. Install coffeescript
17 |
18 | `npm install -g coffeescript`
19 | 3. Install node-gyp
20 |
21 | `npm install -g node-gyp`
22 | 4. Install dependencies
23 |
24 | `npm install`
25 | 5. Generate HTML
26 |
27 | `./make.coffee dev`
28 |
29 | ## HTML generation
30 |
31 | First, the cirru templates (in `./cirru`) are combined with the header
32 | and footer (`./cirru/header.cirru` + `./cirru/{FILE}.cirru` + `./cirru/footer.cirru`).
33 | Next, the generated cirru templates from the first step (in `./cirru/generated`)
34 | are converted into HTML (in `./`).
35 |
36 | ## CI/CD
37 |
38 | TravisCI automatically regenerates HTML files on each merge commit made to the master branch.
39 | Therefore, HTML files must not be committed manually (e.g. in pull requests).
40 |
41 | ## Adding new code snippets
42 |
43 | The code snippets reside in the `code/java` and `code/kotlin` folders.
44 | They are referenced in cirru (`./cirru`) templates this way:
45 | ```
46 | .lang Java
47 | pre.code $ code (@insert ../../code/java/dsl/04.java) $ :class java
48 | ```
49 |
50 | ## How to contribute
51 |
52 | Fork repository, make changes, send a pull request. I will review
53 | your changes and apply them to the `master` branch shortly, provided
54 | they don't violate the quality standards. Before
55 | sending your pull request please check that the HTML is generated correctly:
56 |
57 | ```
58 | ./make.coffee dev
59 | ```
60 |
61 | ## Got questions?
62 |
63 | If you have questions or general suggestions, don't hesitate to submit
64 | a new [Github issue](https://github.com/driver733/kotlin-vs-java/issues/new).
65 |
--------------------------------------------------------------------------------
/android-chrome-192x192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/driver733/kotlin-vs-java/7bfda57b1db1ff8b3b59a73f9169ac200f3abbd5/android-chrome-192x192.png
--------------------------------------------------------------------------------
/android-chrome-512x512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/driver733/kotlin-vs-java/7bfda57b1db1ff8b3b59a73f9169ac200f3abbd5/android-chrome-512x512.png
--------------------------------------------------------------------------------
/apple-touch-icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/driver733/kotlin-vs-java/7bfda57b1db1ff8b3b59a73f9169ac200f3abbd5/apple-touch-icon.png
--------------------------------------------------------------------------------
/browserconfig.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | #da532c
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/cirru/classes.cirru:
--------------------------------------------------------------------------------
1 | .section
2 | .list-group-flush
3 | div (:class "list-group-item border-0")
4 | .title "Constructor Call"
5 | .container-fluid
6 | .row
7 | .col
8 | .lang Java
9 | pre.code $ code (@insert ../../code/java/classes/classes-00.java) $ :class java
10 | .col
11 | .lang Kotlin
12 | pre.code $ code (@insert ../../code/kotlin/classes/classes-00.kt) $ :class kotlin
13 | div (:class "list-group-item border-0")
14 | .title "Class"
15 | .container-fluid
16 | .row
17 | .col
18 | .lang Java
19 | pre.code $ code (@insert ../../code/java/classes/classes-01.java) $ :class java
20 | .col
21 | .lang Kotlin
22 | pre.code $ code (@insert ../../code/kotlin/classes/classes-01.kt) $ :class kotlin
23 | div (:class "list-group-item border-0")
24 | .title "Open Class"
25 | .container-fluid
26 | .row
27 | .col
28 | .lang Java
29 | pre.code $ code (@insert ../../code/java/classes/classes-02.java) $ :class java
30 | .col
31 | .lang Kotlin
32 | pre.code $ code (@insert ../../code/kotlin/classes/classes-02.kt) $ :class kotlin
33 | div (:class "list-group-item border-0")
34 | .title "Final Attributes"
35 | .container-fluid
36 | .row
37 | .col
38 | .lang Java
39 | pre.code $ code (@insert ../../code/java/classes/classes-03.java) $ :class java
40 | .col
41 | .lang Kotlin
42 | pre.code $ code (@insert ../../code/kotlin/classes/classes-03.kt) $ :class kotlin
43 | div (:class "list-group-item border-0")
44 | .title "Primary Constructor"
45 | .container-fluid
46 | .row
47 | .col
48 | .lang Java
49 | pre.code $ code (@insert ../../code/java/classes/classes-04.java) $ :class java
50 | .col
51 | .lang Kotlin
52 | pre.code $ code (@insert ../../code/kotlin/classes/classes-04.kt) $ :class kotlin
53 | div (:class "list-group-item border-0")
54 | .title "Optional Arguments in Constructors"
55 | .container-fluid
56 | .row
57 | .col
58 | .lang Java
59 | pre.code $ code (@insert ../../code/java/classes/classes-05.java) $ :class java
60 | .col
61 | .lang Kotlin
62 | pre.code $ code (@insert ../../code/kotlin/classes/classes-05.kt) $ :class kotlin
63 | div (:class "list-group-item border-0")
64 | .title "Properties"
65 | .container-fluid
66 | .row
67 | .col
68 | .lang Java
69 | pre.code $ code (@insert ../../code/java/classes/classes-06.java) $ :class java
70 | .col
71 | .lang Kotlin
72 | pre.code $ code (@insert ../../code/kotlin/classes/classes-06.kt) $ :class kotlin
73 |
74 | .section
75 | .list-group-flush
76 | div (:class "list-group-item border-0")
77 | .title "Abstract Class"
78 | .container-fluid
79 | .row
80 | .col
81 | .lang Java
82 | pre.code $ code (@insert ../../code/java/classes/classes-07.java) $ :class java
83 | .col
84 | .lang Kotlin
85 | pre.code $ code (@insert ../../code/kotlin/classes/classes-07.kt) $ :class kotlin
86 | div (:class "list-group-item border-0")
87 | .title "Singleton"
88 | .container-fluid
89 | .row
90 | .col
91 | .lang Java
92 | pre.code $ code (@insert ../../code/java/classes/classes-08.java) $ :class java
93 | .col
94 | .lang Kotlin
95 | pre.code $ code (@insert ../../code/kotlin/classes/classes-08.kt) $ :class kotlin
96 | div (:class "list-group-item border-0")
97 | .title "Extensions"
98 | .container-fluid
99 | .row
100 | .col
101 | .lang Java
102 | pre.code $ code (@insert ../../code/java/classes/classes-09.java) $ :class java
103 | .col
104 | .lang Kotlin
105 | pre.code $ code (@insert ../../code/kotlin/classes/classes-09.kt) $ :class kotlin
106 | div (:class "list-group-item border-0")
107 | .title "Inner Class"
108 | .container-fluid
109 | .row
110 | .col
111 | .lang Java
112 | pre.code $ code (@insert ../../code/java/classes/classes-010.java) $ :class java
113 | .col
114 | .lang Kotlin
115 | pre.code $ code (@insert ../../code/kotlin/classes/classes-010.kt) $ :class kotlin
116 | div (:class "list-group-item border-0")
117 | .title "Nested Class"
118 | .container-fluid
119 | .row
120 | .col
121 | .lang Java
122 | pre.code $ code (@insert ../../code/java/classes/classes-011.java) $ :class java
123 | .col
124 | .lang Kotlin
125 | pre.code $ code (@insert ../../code/kotlin/classes/classes-011.kt) $ :class kotlin
126 | div (:class "list-group-item border-0")
127 | .title "Interface"
128 | .container-fluid
129 | .row
130 | .col
131 | .lang Java
132 | pre.code $ code (@insert ../../code/java/classes/classes-012.java) $ :class java
133 | .col
134 | .lang Kotlin
135 | pre.code $ code (@insert ../../code/kotlin/classes/classes-012.kt) $ :class kotlin
136 |
--------------------------------------------------------------------------------
/cirru/collections.cirru:
--------------------------------------------------------------------------------
1 | .section
2 | .list-group-flush
3 | div (:class "list-group-item border-0")
4 | .title "ListOf"
5 | .container-fluid
6 | .row
7 | .col
8 | .lang Java
9 | pre.code $ code (@insert ../../code/java/collections/01_ListOf.java) $ :class java
10 | .col
11 | .lang Kotlin
12 | pre.code $ code (@insert ../../code/kotlin/collections/01_ListOf.kt) $ :class kotlin
13 | div (:class "list-group-item border-0")
14 | .title "Filter"
15 | .container-fluid
16 | .row
17 | .col
18 | .lang Java
19 | pre.code $ code (@insert ../../code/java/collections/02_Filter.java) $ :class java
20 | .col
21 | .lang Kotlin
22 | pre.code $ code (@insert ../../code/kotlin/collections/02_Filter.kt) $ :class kotlin
23 | div (:class "list-group-item border-0")
24 | .title "GroupBy"
25 | .container-fluid
26 | .row
27 | .col
28 | .lang Java
29 | pre.code $ code (@insert ../../code/java/collections/03_GroupBy.java) $ :class java
30 | .col
31 | .lang Kotlin
32 | pre.code $ code (@insert ../../code/kotlin/collections/03_GroupBy.kt) $ :class kotlin
33 | div (:class "list-group-item border-0")
34 | .title "PartitionBy"
35 | .container-fluid
36 | .row
37 | .col
38 | .lang Java
39 | pre.code $ code (@insert ../../code/java/collections/04_PartitionBy.java) $ :class java
40 | .col
41 | .lang Kotlin
42 | pre.code $ code (@insert ../../code/kotlin/collections/04_PartitionBy.kt) $ :class kotlin
43 | div (:class "list-group-item border-0")
44 | .title "SortBy"
45 | .container-fluid
46 | .row
47 | .col
48 | .lang Java
49 | pre.code $ code (@insert ../../code/java/collections/05_SortBy.java) $ :class java
50 | .col
51 | .lang Kotlin
52 | pre.code $ code (@insert ../../code/kotlin/collections/05_SortBy.kt) $ :class kotlin
53 | div (:class "list-group-item border-0")
54 | .title "Fold"
55 | .container-fluid
56 | .row
57 | .col
58 | .lang Java
59 | pre.code $ code (@insert ../../code/java/collections/06_Fold.java) $ :class java
60 | .col
61 | .lang Kotlin
62 | pre.code $ code (@insert ../../code/kotlin/collections/06_Fold.kt) $ :class kotlin
63 | div (:class "list-group-item border-0")
64 | .title "AssociateBy"
65 | .container-fluid
66 | .row
67 | .col
68 | .lang Java
69 | pre.code $ code (@insert ../../code/java/collections/07_AssociateBy.java) $ :class java
70 | .col
71 | .lang Kotlin
72 | pre.code $ code (@insert ../../code/kotlin/collections/07_AssociateBy.kt) $ :class kotlin
73 | div (:class "list-group-item border-0")
74 | .title "Zip"
75 | .container-fluid
76 | .row
77 | .col
78 | .lang Java
79 | pre.code $ code (@insert ../../code/java/collections/08_Zip.java) $ :class java
80 | .col
81 | .lang Kotlin
82 | pre.code $ code (@insert ../../code/kotlin/collections/08_Zip.kt) $ :class kotlin
83 | div (:class "list-group-item border-0")
84 | .title "Infix functions"
85 | .container-fluid
86 | .row
87 | .col
88 | .lang Java
89 | pre.code $ code (@insert ../../code/java/collections/09_InfixFunctions.java) $ :class java
90 | .col
91 | .lang Kotlin
92 | pre.code $ code (@insert ../../code/kotlin/collections/09_InfixFunctions.kt) $ :class kotlin
93 | div (:class "list-group-item border-0")
94 | .title "CollectingAndThen"
95 | .container-fluid
96 | .row
97 | .col
98 | .lang Java
99 | pre.code $ code (@insert ../../code/java/collections/10_CollectingAndThen.java) $ :class java
100 | .col
101 | .lang Kotlin
102 | pre.code $ code (@insert ../../code/kotlin/collections/10_CollectingAndThen.kt) $ :class kotlin
103 | div (:class "list-group-item border-0")
104 | .title "For"
105 | .container-fluid
106 | .row
107 | .col
108 | .lang Java
109 | pre.code $ code (@insert ../../code/java/collections/11_For.java) $ :class java
110 | .col
111 | .lang Kotlin
112 | pre.code $ code (@insert ../../code/kotlin/collections/11_For.kt) $ :class kotlin
113 | div (:class "list-group-item border-0")
114 | .title "Repeat"
115 | .container-fluid
116 | .row
117 | .col
118 | .lang Java
119 | pre.code $ code (@insert ../../code/java/collections/12_Repeat.java) $ :class java
120 | .col
121 | .lang Kotlin
122 | pre.code $ code (@insert ../../code/kotlin/collections/12_Repeat.kt) $ :class kotlin
123 | div (:class "list-group-item border-0")
124 | .title "Comparators"
125 | .container-fluid
126 | .row
127 | .col
128 | .lang Java
129 | pre.code $ code (@insert ../../code/java/collections/13_Comparators.java) $ :class java
130 | .col
131 | .lang Kotlin
132 | pre.code $ code (@insert ../../code/kotlin/collections/13_Comparators.kt) $ :class kotlin
133 |
134 |
--------------------------------------------------------------------------------
/cirru/concurrency.cirru:
--------------------------------------------------------------------------------
1 | .section
2 | .list-group-flush
3 | div (:class "list-group-item border-0")
4 | .title "I"
5 | .container-fluid
6 | .row
7 | .col
8 | .lang Java
9 | pre.code $ code (@insert ../../code/java/concurrency/01.java) $ :class java
10 | .col
11 | .lang Kotlin
12 | pre.code $ code (@insert ../../code/kotlin/concurrency/01.kt) $ :class kotlin
13 | div (:class "list-group-item border-0")
14 | .title "II"
15 | .container-fluid
16 | .row
17 | .col
18 | .lang Java
19 | pre.code $ code (@insert ../../code/java/concurrency/02.java) $ :class java
20 | .col
21 | .lang Kotlin
22 | pre.code $ code (@insert ../../code/kotlin/concurrency/02.kt) $ :class kotlin
23 | div (:class "list-group-item border-0")
24 | .title "III"
25 | .container-fluid
26 | .row
27 | .col
28 | .lang Java
29 | pre.code $ code (@insert ../../code/java/concurrency/03.java) $ :class java
30 | .col
31 | .lang Kotlin
32 | pre.code $ code (@insert ../../code/kotlin/concurrency/03.kt) $ :class kotlin
33 |
--------------------------------------------------------------------------------
/cirru/delegated-properties.cirru:
--------------------------------------------------------------------------------
1 | .section
2 | .list-group-flush
3 | div (:class "list-group-item border-0")
4 | .title "Lazy"
5 | .container-fluid
6 | .row
7 | .col
8 | .lang Java
9 | pre.code $ code (@insert ../../code/java/delegated-properties/lazy.java) $ :class java
10 | .col
11 | .lang Kotlin
12 | pre.code $ code (@insert ../../code/kotlin/delegated-properties/lazy.kt) $ :class kotlin
13 | div (:class "list-group-item border-0")
14 | .title "Map"
15 | .container-fluid
16 | .row
17 | .col
18 | .lang Java
19 | pre.code $ code (@insert ../../code/java/delegated-properties/map.java) $ :class java
20 | .col
21 | .lang Kotlin
22 | pre.code $ code (@insert ../../code/kotlin/delegated-properties/map.kt) $ :class kotlin
23 | div (:class "list-group-item border-0")
24 | .title "Observable"
25 | .container-fluid
26 | .row
27 | .col
28 | .lang Java
29 | pre.code $ code (@insert ../../code/java/delegated-properties/observable.java) $ :class java
30 | .col
31 | .lang Kotlin
32 | pre.code $ code (@insert ../../code/kotlin/delegated-properties/observable.kt) $ :class kotlin
33 |
--------------------------------------------------------------------------------
/cirru/dsl.cirru:
--------------------------------------------------------------------------------
1 | .section
2 | .list-group-flush
3 | div (:class "list-group-item border-0")
4 | .title "I"
5 | .container-fluid
6 | .row
7 | .col
8 | .lang Java
9 | pre.code $ code (@insert ../../code/java/dsl/01.java) $ :class java
10 | .col
11 | .lang Kotlin
12 | pre.code $ code (@insert ../../code/kotlin/dsl/01.kt) $ :class kotlin
13 | div (:class "list-group-item border-0")
14 | .title "II"
15 | .container-fluid
16 | .row
17 | .col
18 | .lang Java
19 | pre.code $ code (@insert ../../code/java/dsl/02.java) $ :class java
20 | .col
21 | .lang Kotlin
22 | pre.code $ code (@insert ../../code/kotlin/dsl/02.kt) $ :class kotlin
23 | div (:class "list-group-item border-0")
24 | .title "III"
25 | .container-fluid
26 | .row
27 | .col
28 | .lang Java
29 | pre.code $ code (@insert ../../code/java/dsl/03.java) $ :class java
30 | .col
31 | .lang Kotlin
32 | pre.code $ code (@insert ../../code/kotlin/dsl/03.kt) $ :class kotlin
33 | div (:class "list-group-item border-0")
34 | .title "IV"
35 | .container-fluid
36 | .row
37 | .col
38 | .lang Java
39 | pre.code $ code (@insert ../../code/java/dsl/04.java) $ :class java
40 | .col
41 | .lang Kotlin
42 | pre.code $ code (@insert ../../code/kotlin/dsl/04.kt) $ :class kotlin
43 | div (:class "list-group-item border-0")
44 | .title "V"
45 | .container-fluid
46 | .row
47 | .col
48 | .lang Java
49 | pre.code $ code (@insert ../../code/java/dsl/05.java) $ :class java
50 | .col
51 | .lang Kotlin
52 | pre.code $ code (@insert ../../code/kotlin/dsl/05.kt) $ :class kotlin
53 |
--------------------------------------------------------------------------------
/cirru/error-prone.cirru:
--------------------------------------------------------------------------------
1 | .section
2 | .list-group-flush
3 | div (:class "list-group-item border-0")
4 | .title "Equals"
5 | .container-fluid
6 | .row
7 | .col
8 | .lang Java
9 | pre.code $ code (@insert ../../code/java/error-prone/01-equals.java) $ :class java
10 | .col
11 | .lang Kotlin
12 | pre.code $ code (@insert ../../code/kotlin/error-prone/01-equals.kt) $ :class kotlin
13 |
14 |
--------------------------------------------------------------------------------
/cirru/footer.cirru:
--------------------------------------------------------------------------------
1 | footer
2 | div
3 | :class
4 | = "container text-center"
5 | div (:class "row justify-content-center pt-4")
6 | .col
7 | span
8 | = "Java 13"
9 | div (:class "row justify-content-center")
10 | .col
11 | span
12 | = "Kotlin 1.3.60"
13 | div (:class "row justify-content-center pt-4 pb-2")
14 | .col
15 | span
16 | = "Created by: "
17 | a (:href https://www.driver733.com)
18 | = @driver733
19 | div (:class "row justify-content-center")
20 | .col
21 | a (:href "https://github.com/driver733/kotlin-vs-java")
22 | img (:src "//img.shields.io/github/stars/driver733/kotlin-vs-java.svg?style=social") (:alt "GitHub stars")
23 | div (:class "row justify-content-center pt-4 pb-4")
24 | .col
25 | span
26 | = "Based on: "
27 | a (:href https://github.com/fabiomsr/from-java-to-kotlin)
28 | = fabiomsr/from-java-to-kotlin
29 | @insert ../../end.html
30 |
--------------------------------------------------------------------------------
/cirru/functions.cirru:
--------------------------------------------------------------------------------
1 | .section
2 | .list-group-flush
3 | div (:class "list-group-item border-0")
4 | .title "Default and non-null parameters"
5 | .container-fluid
6 | .row
7 | .col
8 | .lang Java
9 | pre.code $ code (@insert ../../code/java/functions/functions-02.java) $ :class java
10 | .col
11 | .lang Kotlin
12 | pre.code $ code (@insert ../../code/kotlin/functions/functions-02.kt) $ :class kotlin
13 | div (:class "list-group-item border-0")
14 | .title "Single-statement functions"
15 | .container-fluid
16 | .row
17 | .col
18 | .lang Java
19 | pre.code $ code (@insert ../../code/java/functions/functions-04.java) $ :class java
20 | .col
21 | .lang Kotlin
22 | pre.code $ code (@insert ../../code/kotlin/functions/functions-04.kt) $ :class kotlin
23 |
24 | .section
25 | .list-group-flush
26 | div (:class "list-group-item border-0")
27 | .title "Vararg"
28 | .container-fluid
29 | .row
30 | .col
31 | .lang Java
32 | pre.code $ code (@insert ../../code/java/functions/functions-05.java) $ :class java
33 | .col
34 | .lang Kotlin
35 | pre.code $ code (@insert ../../code/kotlin/functions/functions-05.kt) $ :class kotlin
36 | div (:class "list-group-item border-0")
37 | .title "Public functions"
38 | .container-fluid
39 | .row
40 | .col
41 | .lang Java
42 | pre.code $ code (@insert ../../code/java/functions/functions-07.java) $ :class java
43 | .col
44 | .lang Kotlin
45 | pre.code $ code (@insert ../../code/kotlin/functions/functions-07.kt) $ :class kotlin
46 | div (:class "list-group-item border-0")
47 | .title "Default parameters"
48 | .container-fluid
49 | .row
50 | .col
51 | .lang Java
52 | pre.code $ code (@insert ../../code/java/functions/functions-08.java) $ :class java
53 | .col
54 | .lang Kotlin
55 | pre.code $ code (@insert ../../code/kotlin/functions/functions-08.kt) $ :class kotlin
56 | div (:class "list-group-item border-0")
57 | .title "Generic methods"
58 | .container-fluid
59 | .row
60 | .col
61 | .lang Java
62 | pre.code $ code (@insert ../../code/java/functions/functions-09.java) $ :class java
63 | .col
64 | .lang Kotlin
65 | pre.code $ code (@insert ../../code/kotlin/functions/functions-09.kt) $ :class kotlin
66 | div (:class "list-group-item border-0")
67 | .title "Data classes"
68 | .container-fluid
69 | .row
70 | .col
71 | .lang Java
72 | pre.code $ code (@insert ../../code/java/functions/functions-10.java) $ :class java
73 | .col
74 | .lang Kotlin
75 | pre.code $ code (@insert ../../code/kotlin/functions/functions-10.kt) $ :class kotlin
76 |
--------------------------------------------------------------------------------
/cirru/generated/.gitignore:
--------------------------------------------------------------------------------
1 | *
2 | !.gitignore
--------------------------------------------------------------------------------
/cirru/header.cirru:
--------------------------------------------------------------------------------
1 | @insert ../../start.html
2 | script (:async) (:src "https://www.googletagmanager.com/gtag/js?id=UA-127781126-2")
3 | script = "window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-127781126-2');"
4 | script (:src "https://code.jquery.com/jquery-3.3.1.slim.min.js") (:integrity "sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo") (:crossorigin "anonymous")
5 | script (:src "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js") (:integrity "sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1") (:crossorigin "anonymous")
6 | script (:src "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js") (:integrity "sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM") (:crossorigin "anonymous")
7 | script (:src js/highlight.9.16.2.js)
8 | script (:src js/bootstrap.min.js)
9 | script "hljs.initHighlightingOnLoad();"
10 | link (:rel "stylesheet") (:href "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css") (:integrity "sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T") (:crossorigin "anonymous")
11 | link (:rel stylesheet) (:href css/style.css)
12 | link (:rel stylesheet) (:href css/highlightjs-night-owl.css)
13 | link (:rel apple-touch-icon) (:sizes 180x180) (:href /apple-touch-icon.png)
14 | link (:rel "icon") (:type image/png) (:sizes 32x32) (:href /favicon-32x32.png)
15 | link (:rel "icon") (:type image/png) (:sizes 16x16) (:href /favicon-16x16.png)
16 | link (:rel "manifest") (:href /site.webmanifest)
17 | link (:rel "mask-icon") (:href /safari-pinned-tab.svg) (:color #5bbad5)
18 | title "Kotlin vs Java"
19 | meta (:charset utf-8)
20 | meta (:name viewport) (:content "width=device-width") (:initial-scale 1)
21 | meta (:name msapplication-TileColor) (:content #da532c)
22 | meta (:name theme-color) (:content #ffffff)
23 |
24 | @insert ../../start-body.html
25 |
26 | div (:class "container-fluid text-center header-title")
27 | div (:class "row text-center")
28 | .col
29 | p
30 | = "Kotlin vs Java"
31 |
32 | nav (:class "navbar navbar-dark navbar-expand-lg")
33 | button (:class navbar-toggler) (:type button) (:data-toggle collapse) (:data-target #navbarNav) (:aria-controls navbarNav) (:aria-expanded false) (:aria-label "Toggle navigation")
34 | span (:class navbar-toggler-icon)
35 | div (:class "collapse navbar-collapse") (:id "navbarNav")
36 | ul (:class "navbar-nav")
37 | @if (@ index)
38 | li (:class "nav-item px-2 active")
39 | a (:class nav-link) (:href /index.html)
40 | = Basics
41 | span (:class sr-only)
42 | = (current)
43 | li (:class "nav-item px-2")
44 | a (:class nav-link) (:href /index.html)
45 | = Basics
46 | @if (@ classes)
47 | li (:class "nav-item px-2 active")
48 | a (:class nav-link) (:href /classes.html)
49 | = Classes
50 | span (:class sr-only)
51 | = (current)
52 | li (:class "nav-item px-2")
53 | a (:class nav-link) (:href /classes.html)
54 | = Classes
55 | @if (@ collections)
56 | li (:class "nav-item px-2 active")
57 | a (:class nav-link) (:href /collections.html)
58 | = Collections
59 | span (:class sr-only)
60 | = (current)
61 | li (:class "nav-item px-2")
62 | a (:class nav-link) (:href /collections.html)
63 | = Collections
64 | @if (@ concurrency)
65 | li (:class "nav-item px-2 active")
66 | a (:class nav-link) (:href /concurrency.html)
67 | = Concurrency
68 | span (:class sr-only)
69 | = (current)
70 | li (:class "nav-item px-2")
71 | a (:class nav-link) (:href /concurrency.html)
72 | = Concurrency
73 | @if (@ delegated_properties)
74 | li (:class "nav-item px-2 active")
75 | a (:class nav-link) (:href /delegated-properties.html)
76 | = Delegated Properties
77 | span (:class sr-only)
78 | = (current)
79 | li (:class "nav-item px-2")
80 | a (:class nav-link) (:href /delegated-properties.html)
81 | = Delegated Properties
82 | @if (@ dsl)
83 | li (:class "nav-item px-2 active")
84 | a (:class nav-link) (:href /dsl.html)
85 | = DSL
86 | span (:class sr-only)
87 | = (current)
88 | li (:class "nav-item px-2")
89 | a (:class nav-link) (:href /dsl.html)
90 | = DSL
91 | @if (@ functions)
92 | li (:class "nav-item px-2 active")
93 | a (:class nav-link) (:href /functions.html)
94 | = Functions
95 | span (:class sr-only)
96 | = (current)
97 | li (:class "nav-item px-2")
98 | a (:class nav-link) (:href /functions.html)
99 | = Functions
100 | @if (@ io)
101 | li (:class "nav-item px-2 active")
102 | a (:class nav-link) (:href /io.html)
103 | = "I/O"
104 | span (:class sr-only)
105 | = (current)
106 | li (:class "nav-item px-2")
107 | a (:class nav-link) (:href /io.html)
108 | = "I/O"
109 | @if (@ scoping_functions)
110 | li (:class "nav-item px-2 active")
111 | a (:class nav-link) (:href /scoping-functions.html)
112 | = Scoping Functions
113 | span (:class sr-only)
114 | = (current)
115 | li (:class "nav-item px-2")
116 | a (:class nav-link) (:href /scoping-functions.html)
117 | = Scoping Functions
118 | @if (@ spring_framework)
119 | li (:class "nav-item px-2 active")
120 | a (:class nav-link) (:href /spring-framework.html)
121 | = Spring Framework
122 | span (:class sr-only)
123 | = (current)
124 | li (:class "nav-item px-2")
125 | a (:class nav-link) (:href /spring-framework.html)
126 | = Spring Framework
127 | @if (@ error_prone)
128 | li (:class "nav-item px-2 active")
129 | a (:class nav-link) (:href /error-prone.html)
130 | = Error prone
131 | span (:class sr-only)
132 | = (current)
133 | li (:class "nav-item px-2")
134 | a (:class nav-link) (:href /error-prone.html)
135 | = Error prone
136 |
137 | a
138 | :href
139 | = https://github.com/driver733/kotlin-vs-java
140 | :class
141 | = github-corner
142 | :aria-label
143 | = View source on Github
144 | svg (:width 80) (:height 80) (:viewBox 0 0 250 250) (:style fill:#70B7FD; color:#fff; position: absolute; top: 0; border: 0; right: 0;) (:aria-hidden true)
145 | path (:d "M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z")
146 | path (:d "M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2") (:fill currentColor) (:style transform-origin: 130px 106px;) (:class octo-arm)
147 | path (:d "M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z") (:fill currentColor) (:class octo-body)
148 | style
149 | = ".github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}"
150 |
--------------------------------------------------------------------------------
/cirru/index.cirru:
--------------------------------------------------------------------------------
1 | .section
2 | .list-group-flush
3 | div (:class "list-group-item border-0")
4 | .title "Hello world"
5 | .container-fluid
6 | .row
7 | .col
8 | .lang Java
9 | pre.code $ code (@insert ../../code/java/basic/hello-world.java) $ :class java
10 | .col
11 | .lang Kotlin
12 | pre.code $ code (@insert ../../code/kotlin/basic/hello-world.kt) $ :class kotlin
13 | div (:class "list-group-item border-0")
14 | .title "Variables I"
15 | .container-fluid
16 | .row
17 | .col
18 | .lang Java
19 | pre.code $ code (@insert ../../code/java/basic/variables-i.java) $ :class java
20 | .col
21 | .lang Kotlin
22 | pre.code $ code (@insert ../../code/kotlin/basic/variables-i.kt) $ :class kotlin
23 | div (:class "list-group-item border-0")
24 | .title "Variables II"
25 | .container-fluid
26 | .row
27 | .col
28 | .lang Java
29 | pre.code $ code (@insert ../../code/java/basic/variables-ii.java) $ :class java
30 | .col
31 | .lang Kotlin
32 | pre.code $ code (@insert ../../code/kotlin/basic/variables-ii.kt) $ :class kotlin
33 | div (:class "list-group-item border-0")
34 | .title "Null I"
35 | .container-fluid
36 | .row
37 | .col
38 | .lang Java
39 | pre.code $ code (@insert ../../code/java/basic/null-i.java) $ :class java
40 | .col
41 | .lang Kotlin
42 | pre.code $ code (@insert ../../code/kotlin/basic/null-i.kt) $ :class kotlin
43 | div (:class "list-group-item border-0")
44 | .title "Null II"
45 | .container-fluid
46 | .row
47 | .col
48 | .lang Java
49 | pre.code $ code (@insert ../../code/java/basic/null-ii.java) $ :class java
50 | .col
51 | .lang Kotlin
52 | pre.code $ code (@insert ../../code/kotlin/basic/null-ii.kt) $ :class kotlin
53 | div (:class "list-group-item border-0")
54 | .title "Elvis operator"
55 | .container-fluid
56 | .row
57 | .col
58 | .lang Java
59 | pre.code $ code (@insert ../../code/java/basic/elvis-operator.java) $ :class java
60 | .col
61 | .lang Kotlin
62 | pre.code $ code (@insert ../../code/kotlin/basic/elvis-operator.kt) $ :class kotlin
63 | div (:class "list-group-item border-0")
64 | .title "Strings I"
65 | .container-fluid
66 | .row
67 | .col
68 | .lang Java
69 | pre.code $ code (@insert ../../code/java/basic/strings-i.java) $ :class java
70 | .col
71 | .lang Kotlin
72 | pre.code $ code (@insert ../../code/kotlin/basic/strings-i.kt) $ :class kotlin
73 | div (:class "list-group-item border-0")
74 | .title "Strings II"
75 | .container-fluid
76 | .row
77 | .col
78 | .lang Java
79 | pre.code $ code (@insert ../../code/java/basic/strings-ii.java) $ :class java
80 | .col
81 | .lang Kotlin
82 | pre.code $ code (@insert ../../code/kotlin/basic/strings-ii.kt) $ :class kotlin
83 | div (:class "list-group-item border-0")
84 | .title "Ternary operator"
85 | .container-fluid
86 | .row
87 | .col
88 | .lang Java
89 | pre.code $ code (@insert ../../code/java/basic/ternary-operator.java) $ :class java
90 | .col
91 | .lang Kotlin
92 | pre.code $ code (@insert ../../code/kotlin/basic/ternary-operator.kt) $ :class kotlin
93 | div (:class "list-group-item border-0")
94 | .title "Arrays"
95 | .container-fluid
96 | .row
97 | .col
98 | .lang Java
99 | pre.code $ code (@insert ../../code/java/basic/arrays.java) $ :class java
100 | .col
101 | .lang Kotlin
102 | pre.code $ code (@insert ../../code/kotlin/basic/arrays.kt) $ :class kotlin
103 | div (:class "list-group-item border-0")
104 | .title "Use"
105 | .container-fluid
106 | .row
107 | .col
108 | .lang Java
109 | pre.code $ code (@insert ../../code/java/basic/use.java) $ :class java
110 | .col
111 | .lang Kotlin
112 | pre.code $ code (@insert ../../code/kotlin/basic/use.kt) $ :class kotlin
113 |
--------------------------------------------------------------------------------
/cirru/io.cirru:
--------------------------------------------------------------------------------
1 | .section
2 | .list-group-flush
3 | div (:class "list-group-item border-0")
4 | .title "Read file contents into a string"
5 | .container-fluid
6 | .row
7 | .col
8 | .lang Java
9 | pre.code $ code (@insert ../../code/java/io/read-file-to-string.java) $ :class java
10 | .col
11 | .lang Kotlin
12 | pre.code $ code (@insert ../../code/kotlin/io/read-file-to-string.kt) $ :class kotlin
13 |
--------------------------------------------------------------------------------
/cirru/scoping-functions.cirru:
--------------------------------------------------------------------------------
1 | .section
2 | .list-group-flush
3 | div (:class "list-group-item border-0")
4 | .title "Also"
5 | .container-fluid
6 | .row
7 | .col
8 | .lang Java
9 | pre.code $ code (@insert ../../code/java/scoping-functions/also.java) $ :class java
10 | .col
11 | .lang Kotlin
12 | pre.code $ code (@insert ../../code/kotlin/scoping-functions/also.kt) $ :class kotlin
13 | div (:class "list-group-item border-0")
14 | .title "Apply"
15 | .container-fluid
16 | .row
17 | .col
18 | .lang Java
19 | pre.code $ code (@insert ../../code/java/scoping-functions/apply.java) $ :class java
20 | .col
21 | .lang Kotlin
22 | pre.code $ code (@insert ../../code/kotlin/scoping-functions/apply.kt) $ :class kotlin
23 | div (:class "list-group-item border-0")
24 | .title "Let"
25 | .container-fluid
26 | .row
27 | .col
28 | .lang Java
29 | pre.code $ code (@insert ../../code/java/scoping-functions/let.java) $ :class java
30 | .col
31 | .lang Kotlin
32 | pre.code $ code (@insert ../../code/kotlin/scoping-functions/let.kt) $ :class kotlin
33 | div (:class "list-group-item border-0")
34 | .title "Let and apply"
35 | .container-fluid
36 | .row
37 | .col
38 | .lang Java
39 | pre.code $ code (@insert ../../code/java/scoping-functions/let-apply.java) $ :class java
40 | .col
41 | .lang Kotlin
42 | pre.code $ code (@insert ../../code/kotlin/scoping-functions/let-apply.kt) $ :class kotlin
43 | div (:class "list-group-item border-0")
44 | .title "Run"
45 | .container-fluid
46 | .row
47 | .col
48 | .lang Java
49 | pre.code $ code (@insert ../../code/java/scoping-functions/run.java) $ :class java
50 | .col
51 | .lang Kotlin
52 | pre.code $ code (@insert ../../code/kotlin/scoping-functions/run.kt) $ :class kotlin
53 | div (:class "list-group-item border-0")
54 | .title "TakeIf"
55 | .container-fluid
56 | .row
57 | .col
58 | .lang Java
59 | pre.code $ code (@insert ../../code/java/scoping-functions/takeIf.java) $ :class java
60 | .col
61 | .lang Kotlin
62 | pre.code $ code (@insert ../../code/kotlin/scoping-functions/takeIf.kt) $ :class kotlin
63 | div (:class "list-group-item border-0")
64 | .title "With"
65 | .container-fluid
66 | .row
67 | .col
68 | .lang Java
69 | pre.code $ code (@insert ../../code/java/scoping-functions/with.java) $ :class java
70 | .col
71 | .lang Kotlin
72 | pre.code $ code (@insert ../../code/kotlin/scoping-functions/with.kt) $ :class kotlin
73 |
--------------------------------------------------------------------------------
/cirru/spring-framework.cirru:
--------------------------------------------------------------------------------
1 | .section
2 | .list-group-flush
3 | div (:class "list-group-item border-0")
4 | .title "Immutable Configuration properties"
5 | .container-fluid
6 | .row
7 | .col
8 | .lang Java
9 | pre.code $ code (@insert ../../code/java/spring-framework/spring-framework-00.java) $ :class java
10 | .col
11 | .lang Kotlin
12 | pre.code $ code (@insert ../../code/kotlin/spring-framework/spring-framework-00.kt) $ :class kotlin
13 |
--------------------------------------------------------------------------------
/classes.html:
--------------------------------------------------------------------------------
1 |
2 |
Kotlin vs Java Constructor Call
Java
final var file = new File("file.txt");
3 |
Kotlin
val file = File("file.txt")
4 |
Class
Java
public final class User {
5 | }
6 |
Open Class
Java
public class User {
8 | }
9 |
Kotlin
open class User
10 |
Final Attributes
Java
final class User {
11 | private final String name;
12 |
13 | public User(String name) {
14 | this.name = name;
15 | }
16 |
17 | public String getName() {
18 | return name;
19 | }
20 | }
21 |
Kotlin
class User(val name: String)
22 |
Primary Constructor
Java
final class User {
23 | private String name;
24 |
25 | public User(String name) {
26 | this.name = name;
27 | }
28 |
29 | public String getName() {
30 | return name;
31 | }
32 |
33 | public void setName(String name) {
34 | this.name = name;
35 | }
36 | }
37 |
Kotlin
class User(var name: String)
38 |
Optional Arguments in Constructors
Java
final class User {
39 | private String name;
40 | private String lastName;
41 |
42 | public User(String name) {
43 | this(name, "");
44 | }
45 |
46 | public User(String name, String lastName) {
47 | this.name = name;
48 | this.lastName = lastName;
49 | }
50 |
51 | // And Getters & Setters
52 | }
53 |
Kotlin
class User(var name: String, var lastName: String = "")
54 |
Properties
Java
public class Document {
55 |
56 | private String id = "00x";
57 |
58 | public String getId() {
59 | return id;
60 | }
61 |
62 | public void setId(String id) {
63 | if (id != null && !id.isEmpty()) {
64 | this.id = id;
65 | }
66 | }
67 |
68 | }
69 |
Kotlin
class Document{
70 | var id : String = "00x"
71 | set(value) {
72 | if(value.isNotEmpty()) field = value
73 | }
74 | }
75 |
Abstract Class
Java
public abstract class Document{
76 | public abstract int calculateSize();
77 | }
78 |
79 | public class Photo extends Document{
80 | @Override
81 | public int calculateSize() {
82 |
83 | }
84 | }
85 |
Kotlin
abstract class Document {
86 | abstract fun calculateSize(): Int
87 | }
88 |
89 | class Photo : Document() {
90 | override fun calculateSize(): Int {
91 |
92 | }
93 | }
94 |
Singleton
Java
public class Document {
95 | private static final Document INSTANCE = new Document();
96 |
97 | public static Document getInstance(){
98 | return INSTANCE;
99 | }
100 |
101 | }
102 |
103 |
Kotlin
object Document {
104 |
105 | }
106 |
Extensions
Java
public class ByteArrayUtils {
107 | public static String toHexString(byte[] data) {
108 |
109 | }
110 | }
111 |
112 | final var dummyData = new byte[10];
113 | final var hexValue = ByteArrayUtils.toHexString(dummyData);
114 |
Kotlin
fun ByteArray.toHex() : String {
115 |
116 | }
117 |
118 | val dummyData = byteArrayOf()
119 | val hexValue = dummyData.toHex()
120 |
Inner Class
Java
public class Documment {
121 |
122 | class InnerClass {
123 |
124 | }
125 |
126 | }
127 |
Kotlin
class Document {
128 | inner class InnerClass
129 | }
130 |
Nested Class
Java
public class Documment {
131 |
132 | public static class InnerClass {
133 |
134 | }
135 |
136 | }
137 |
Kotlin
class Document {
138 |
139 | class InnerClass
140 |
141 | }
142 |
Interface
Java
public interface Printable {
143 | void print();
144 | }
145 |
146 | public class Document implements Printable {
147 | @Override
148 | public void print() {
149 |
150 | }
151 | }
152 |
Kotlin
interface Printable {
153 | fun print()
154 | }
155 |
156 | class Document : Printable {
157 | override fun print() {
158 |
159 | }
160 | }
161 |
162 |
--------------------------------------------------------------------------------
/code/java/basic/apply.java:
--------------------------------------------------------------------------------
1 | File file = new File("file.txt");
2 |
3 | file.setExecutable(true);
4 | file.setReadable(true);
5 | file.setWritable(true);
6 |
--------------------------------------------------------------------------------
/code/java/basic/arrays.java:
--------------------------------------------------------------------------------
1 | var size = 1;
2 |
3 | var numbers = new int[size];
4 | numbers[0] = 1;
5 |
6 | var bigNumbers = new long[size];
7 |
8 | var strings = new String[size];
9 |
--------------------------------------------------------------------------------
/code/java/basic/bits-operations.java:
--------------------------------------------------------------------------------
1 | final int andResult = a & b;
2 | final int orResult = a | b;
3 | final int xorResult = a ^ b;
4 | final int rightShift = a >> 2;
5 | final int leftShift = a << 2;
6 |
--------------------------------------------------------------------------------
/code/java/basic/cast.java:
--------------------------------------------------------------------------------
1 | if (a instanceof String) {
2 | final var result = ((String) a).substring(1);
3 | }
4 |
--------------------------------------------------------------------------------
/code/java/basic/default-parameters.java:
--------------------------------------------------------------------------------
1 | public void greeting(String name, String greet) {
2 | System.out.println(greet + " " + greet + "!");
3 | }
4 |
5 | public void greeting(String name) {
6 | greeting(name, "hello");
7 | }
--------------------------------------------------------------------------------
/code/java/basic/elvis-operator.java:
--------------------------------------------------------------------------------
1 | final var result = Optional.ofNullable(nullableVariable)
2 | .flatMap(v -> Optional.ofNullable(v.someNullableMethodCall()))
3 | .orElseGet(() -> fallbackIfNullMethodCall())
4 |
--------------------------------------------------------------------------------
/code/java/basic/hello-world.java:
--------------------------------------------------------------------------------
1 | public static void main(final String[] args) {
2 | System.out.println("Hello world!");
3 | }
--------------------------------------------------------------------------------
/code/java/basic/null-i.java:
--------------------------------------------------------------------------------
1 | final String name = null;
2 |
3 | String lastName;
4 | lastName = null;
5 |
--------------------------------------------------------------------------------
/code/java/basic/null-ii.java:
--------------------------------------------------------------------------------
1 | final Integer length =
2 | bob != null
3 | && bob.department != null
4 | && bob.department.text != null
5 | ? bob.department.text.length()
6 | : null;
7 |
--------------------------------------------------------------------------------
/code/java/basic/operations.java:
--------------------------------------------------------------------------------
1 | if (x instanceof Integer) { }
2 | final var text = (String) other;
3 | if (x >= 0 && x <= 10 ) { }
4 |
--------------------------------------------------------------------------------
/code/java/basic/print.java:
--------------------------------------------------------------------------------
1 | System.out.print("Hello, World!");
2 | System.out.println("Hello, World!");
3 |
--------------------------------------------------------------------------------
/code/java/basic/strings-i.java:
--------------------------------------------------------------------------------
1 | final var name = "John";
2 | final var lastName = "Smith";
3 | final var text = "My name is: " + name + " " + lastName;
4 | final var otherText = "My name is: " + name.substring(2);
5 |
--------------------------------------------------------------------------------
/code/java/basic/strings-ii.java:
--------------------------------------------------------------------------------
1 | final var text = "First Line\n" +
2 | "Second Line\n" +
3 | "Third Line";
4 |
--------------------------------------------------------------------------------
/code/java/basic/switch.java:
--------------------------------------------------------------------------------
1 | final int x = // value;
2 | final String xResult;
3 |
4 | switch (x){
5 | case 0:
6 | case 11:
7 | xResult = "0 or 11";
8 | break;
9 | case 1:
10 | case 2:
11 | //...
12 | case 10:
13 | xResult = "from 1 to 10";
14 | break;
15 | default:
16 | if(x < 12 && x > 14) {
17 | xResult = "not from 12 to 14";
18 | break;
19 | }
20 |
21 | if(isOdd(x)) {
22 | xResult = "is odd";
23 | break;
24 | }
25 |
26 | xResult = "otherwise";
27 | }
28 |
29 |
30 |
31 | final int y = // value;
32 | final String yResult;
33 |
34 | if (isNegative(y)) {
35 | yResult = "is Negative";
36 | } else if(isZero(y)){
37 | yResult = "is Zero";
38 | } else if(isOdd(y)){
39 | yResult = "is Odd";
40 | } else {
41 | yResult = "otherwise";
42 | }
43 |
--------------------------------------------------------------------------------
/code/java/basic/ternary-operator.java:
--------------------------------------------------------------------------------
1 | final var text = x > 5 ? "x > 5" : "x <= 5";
2 |
--------------------------------------------------------------------------------
/code/java/basic/use.java:
--------------------------------------------------------------------------------
1 | var file = new File("contents.txt");
2 | try {
3 | var fileReader = new FileReader(file);
4 | try (var bufferedReader = new BufferedReader(fileReader)) {
5 | // the bufferedReader is closed automatically
6 | }
7 | } catch (IOException ex) {
8 | // handle the exception
9 | }
--------------------------------------------------------------------------------
/code/java/basic/variables-i.java:
--------------------------------------------------------------------------------
1 | var x; // compile-time error
2 | final var xx; // compile-time error
3 | final var y = 1;
4 |
--------------------------------------------------------------------------------
/code/java/basic/variables-ii.java:
--------------------------------------------------------------------------------
1 | int w;
2 | int z = 2;
3 | z = 3;
4 | w = 1;
5 |
--------------------------------------------------------------------------------
/code/java/classes/classes-00.java:
--------------------------------------------------------------------------------
1 | final var file = new File("file.txt");
2 |
--------------------------------------------------------------------------------
/code/java/classes/classes-01.java:
--------------------------------------------------------------------------------
1 | public final class User {
2 | }
3 |
--------------------------------------------------------------------------------
/code/java/classes/classes-010.java:
--------------------------------------------------------------------------------
1 | public class Documment {
2 |
3 | class InnerClass {
4 |
5 | }
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/code/java/classes/classes-011.java:
--------------------------------------------------------------------------------
1 | public class Documment {
2 |
3 | public static class InnerClass {
4 |
5 | }
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/code/java/classes/classes-012.java:
--------------------------------------------------------------------------------
1 | public interface Printable {
2 | void print();
3 | }
4 |
5 | public class Document implements Printable {
6 | @Override
7 | public void print() {
8 |
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/code/java/classes/classes-02.java:
--------------------------------------------------------------------------------
1 | public class User {
2 | }
3 |
--------------------------------------------------------------------------------
/code/java/classes/classes-03.java:
--------------------------------------------------------------------------------
1 | final class User {
2 | private final String name;
3 |
4 | public User(String name) {
5 | this.name = name;
6 | }
7 |
8 | public String getName() {
9 | return name;
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/code/java/classes/classes-04.java:
--------------------------------------------------------------------------------
1 | final class User {
2 | private String name;
3 |
4 | public User(String name) {
5 | this.name = name;
6 | }
7 |
8 | public String getName() {
9 | return name;
10 | }
11 |
12 | public void setName(String name) {
13 | this.name = name;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/code/java/classes/classes-05.java:
--------------------------------------------------------------------------------
1 | final class User {
2 | private String name;
3 | private String lastName;
4 |
5 | public User(String name) {
6 | this(name, "");
7 | }
8 |
9 | public User(String name, String lastName) {
10 | this.name = name;
11 | this.lastName = lastName;
12 | }
13 |
14 | // And Getters & Setters
15 | }
16 |
--------------------------------------------------------------------------------
/code/java/classes/classes-06.java:
--------------------------------------------------------------------------------
1 | public class Document {
2 |
3 | private String id = "00x";
4 |
5 | public String getId() {
6 | return id;
7 | }
8 |
9 | public void setId(String id) {
10 | if (id != null && !id.isEmpty()) {
11 | this.id = id;
12 | }
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/code/java/classes/classes-07.java:
--------------------------------------------------------------------------------
1 | public abstract class Document{
2 | public abstract int calculateSize();
3 | }
4 |
5 | public class Photo extends Document{
6 | @Override
7 | public int calculateSize() {
8 |
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/code/java/classes/classes-08.java:
--------------------------------------------------------------------------------
1 | public class Document {
2 | private static final Document INSTANCE = new Document();
3 |
4 | public static Document getInstance(){
5 | return INSTANCE;
6 | }
7 |
8 | }
9 |
10 |
--------------------------------------------------------------------------------
/code/java/classes/classes-09.java:
--------------------------------------------------------------------------------
1 | public class ByteArrayUtils {
2 | public static String toHexString(byte[] data) {
3 |
4 | }
5 | }
6 |
7 | final var dummyData = new byte[10];
8 | final var hexValue = ByteArrayUtils.toHexString(dummyData);
9 |
--------------------------------------------------------------------------------
/code/java/collections/01_ListOf.java:
--------------------------------------------------------------------------------
1 | final var map = Map.of(
2 | 1, "One",
3 | 2, "Two",
4 | 3, "Three"
5 | );
6 |
--------------------------------------------------------------------------------
/code/java/collections/02_Filter.java:
--------------------------------------------------------------------------------
1 | var filtered = numbers
2 | .stream()
3 | .filter(num -> num > 5)
4 | .collect(Collectors.toList())
--------------------------------------------------------------------------------
/code/java/collections/03_GroupBy.java:
--------------------------------------------------------------------------------
1 | final var groups = items.stream()
2 | .collect(
3 | Collectors.groupingBy(
4 | item -> (item % 2) == 0 ? "even" : "odd"
5 | )
6 | );
7 |
--------------------------------------------------------------------------------
/code/java/collections/04_PartitionBy.java:
--------------------------------------------------------------------------------
1 | final var partitioned = numbers.stream()
2 | .collect(
3 | Collectors.partitioningBy(num -> num % 2 == 0)
4 | );
5 |
6 | final var evens = partitioned.get(true);
7 | final var odds = partitioned.get(false);
8 |
--------------------------------------------------------------------------------
/code/java/collections/05_SortBy.java:
--------------------------------------------------------------------------------
1 | final var users = getUsers();
2 | users.sort(Comparator.comparing(user -> user.lastname));
3 |
--------------------------------------------------------------------------------
/code/java/collections/06_Fold.java:
--------------------------------------------------------------------------------
1 | private <T> Double calculateTotals(final Grid<T> grid) {
2 | return grid.getRows()
3 | .stream()
4 | .flatMap(e -> e.getColumns().stream())
5 | .collect(Collectors.groupingBy(Column::getTitle))
6 | .entrySet()
7 | .stream()
8 | .flatMap(e -> e.getValue().stream())
9 | .map(Column::getValue)
10 | .reduce(
11 | 0.0,
12 | (accumulator, value) ->
13 | value instanceof Number
14 | ? accumulator + ((Number) (value)).doubleValue()
15 | : accumulator,
16 | Double::sum
17 | );
18 |
19 | @Data // Lombok
20 | class Grid<T> {
21 | private final List rows;
22 | }
23 |
24 | @Data // Lombok
25 | class Row<T> {
26 | private final List columns;
27 | }
28 |
29 | @Data // Lombok
30 | class Column<T> {
31 | private final String title;
32 | private final T value;
33 | }
--------------------------------------------------------------------------------
/code/java/collections/07_AssociateBy.java:
--------------------------------------------------------------------------------
1 | final var map = currencies.stream()
2 | .collect(
3 | Collectors.toMap(
4 | Currency::getCode,
5 | Function.identity()
6 | )
7 | );
--------------------------------------------------------------------------------
/code/java/collections/08_Zip.java:
--------------------------------------------------------------------------------
1 | final var numbers = List.of(1, 2, 3);
2 | final var names = List.of("Jack", "Michael", "Bob");
3 | IntStream
4 | .range(0, Math.min(names.size(), ages.size()))
5 | .mapToObj(i -> names.get(i) + ":" + ages.get(i))
6 | .collect(Collectors.toList());
--------------------------------------------------------------------------------
/code/java/collections/09_InfixFunctions.java:
--------------------------------------------------------------------------------
1 | var list = List.of("a", "b", "c")
2 | list.add("d")
3 | list.remove("d")
--------------------------------------------------------------------------------
/code/java/collections/10_CollectingAndThen.java:
--------------------------------------------------------------------------------
1 | final var topPaidEmployee = Map.of(
2 | "Alex", 250,
3 | "Boss", 999,
4 | "Chris", 400
5 | ).entrySet()
6 | .stream()
7 | .filter(employee -> !employee.getKey().equals("Boss"))
8 | .collect(
9 | Collectors.collectingAndThen(
10 | Collectors.maxBy(Comparator.comparing(Map.Entry::getValue)),
11 | employee -> {
12 | if (employee.isPresent()) {
13 | return employee.get().getKey();
14 | }
15 | throw new IllegalStateException("No employees");
16 | }
17 | )
18 | );
19 |
--------------------------------------------------------------------------------
/code/java/collections/11_For.java:
--------------------------------------------------------------------------------
1 | for (int i = 1; i < 11; i++) { }
2 |
3 | for (int i = 1; i < 11; i += 2) { }
4 |
5 | for (String item : collection) { }
6 |
7 | for (var entry: map.entrySet()) { }
8 |
--------------------------------------------------------------------------------
/code/java/collections/12_Repeat.java:
--------------------------------------------------------------------------------
1 | for (int i = 0; i < 3; i++) {
2 | System.out.println("Hello")
3 | }
--------------------------------------------------------------------------------
/code/java/collections/13_Comparators.java:
--------------------------------------------------------------------------------
1 | var persons = List.of(
2 | new Person("Bob C.", 5), new Person("Alex G.", 10), new Person("Alex G.", 12)
3 | );
4 | persons.sort(
5 | Comparator
6 | .comparing(Person::getScore, Comparator.reverseOrder())
7 | .thenComparing((Person::getName), Comparator.naturalOrder())
8 | );
9 | // [(Alex G., 12), (Alex G., 10), (Bob C., 5)]
10 |
11 | @Data // Lombok
12 | class Person {
13 | private final String name;
14 | private final Integer score;
15 | }
16 |
17 |
--------------------------------------------------------------------------------
/code/java/concurrency/01.java:
--------------------------------------------------------------------------------
1 | public CompletableFuture<SomeOtherResult> loadAndCombine(String name1, String name2){
2 | loadImageAsync(name1)
3 | .thenAcceptBoth(loadImageAsync(name2)) ((one, two) -> {
4 | combineImages(one, two)
5 | )}
6 | }
7 |
--------------------------------------------------------------------------------
/code/java/concurrency/02.java:
--------------------------------------------------------------------------------
1 | public <T> CompletableFuture<T> inTransaction(DatabaseFunction<T> fun) {
2 | return CompletableFuture
3 | .runAsync(() -> this.sendQuery("BEGIN"))
4 | .thenApply(ignored -> fun.execute(this))
5 | .thenApply(s -> {
6 | this.sendQuery("COMMIT");
7 | return s;
8 | }).exceptionally(throwable -> {
9 | this.sendQuery("ROLLBACK");
10 | return null;
11 | });
12 | }
13 |
--------------------------------------------------------------------------------
/code/java/concurrency/03.java:
--------------------------------------------------------------------------------
1 | final var callables = Stream.of("Service A", "Service B", "Service C")
2 | .map(DummyService::new)
3 | .map(service -> (Callable<ContentDuration>) service::getContent)
4 | .collect(Collectors.toList());
5 |
6 | final var executor = Executors.newWorkStealingPool();
7 | final var results = executor.invokeAll(callables).stream()
8 | .map(
9 | future -> {
10 | try {
11 | return future.get();
12 | } catch (InterruptedException | ExecutionException e) {
13 | throw new RuntimeException(e);
14 | }
15 | }
16 | ).collect(Collectors.toList());
17 |
--------------------------------------------------------------------------------
/code/java/delegated-properties/lazy.java:
--------------------------------------------------------------------------------
1 | private String value;
2 |
3 | public synchronized String getValue() {
4 | if (value == null) {
5 | value = "Something";
6 | }
7 | return value;
8 | }
--------------------------------------------------------------------------------
/code/java/delegated-properties/map.java:
--------------------------------------------------------------------------------
1 | final ObjectMapper mapper = new ObjectMapper(); // jackson's objectmapper
2 | final MyPojo pojo = mapper.convertValue(map, MyPojo.class);
3 |
4 | // https://stackoverflow.com/a/16430704/2441104
--------------------------------------------------------------------------------
/code/java/delegated-properties/observable.java:
--------------------------------------------------------------------------------
1 | public class PCLNewsAgency {
2 | private String news;
3 |
4 | private PropertyChangeSupport support;
5 |
6 | public PCLNewsAgency() {
7 | support = new PropertyChangeSupport(this);
8 | }
9 |
10 | public void addPropertyChangeListener(PropertyChangeListener pcl) {
11 | support.addPropertyChangeListener(pcl);
12 | }
13 |
14 | public void removePropertyChangeListener(PropertyChangeListener pcl) {
15 | support.removePropertyChangeListener(pcl);
16 | }
17 |
18 | public void setNews(String value) {
19 | support.firePropertyChange("news", this.news, value);
20 | this.news = value;
21 | }
22 | }
--------------------------------------------------------------------------------
/code/java/dsl/01.java:
--------------------------------------------------------------------------------
1 | final var order = Sandwich()
2 | .type("toastie")
3 | .bread("white")
4 | .fillings(List.of("Cheese", "Ham"))
5 | .dressings(List.of("Pepper", "Worcestershire sauce"))
6 | .side("Crisps")
7 | .makeMeASandwich()
8 |
9 | System.out.println(ooSandwich.receipt())
10 |
11 | // https://www.kotlindays.com/2019/12/02/days-of-kotlin-a-dsl-for-everyone/index.html
--------------------------------------------------------------------------------
/code/java/dsl/02.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/driver733/kotlin-vs-java/7bfda57b1db1ff8b3b59a73f9169ac200f3abbd5/code/java/dsl/02.java
--------------------------------------------------------------------------------
/code/java/dsl/03.java:
--------------------------------------------------------------------------------
1 | final var act = this;
2 | final var layout = new LinearLayout(act);
3 | layout.setOrientation(LinearLayout.VERTICAL);
4 | final var name = new EditText(act);
5 | final var button = new Button(act);
6 | button.setText("Say Hello");
7 | button.setOnClickListener(
8 | () -> {
9 | Toast.makeText(act, "Hello, " + name.text + "!", Toast.LENGTH_SHORT)
10 | .show()
11 | }
12 | layout.addView(name);
13 | layout.addView(button);
--------------------------------------------------------------------------------
/code/java/dsl/04.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/driver733/kotlin-vs-java/7bfda57b1db1ff8b3b59a73f9169ac200f3abbd5/code/java/dsl/04.java
--------------------------------------------------------------------------------
/code/java/dsl/05.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/driver733/kotlin-vs-java/7bfda57b1db1ff8b3b59a73f9169ac200f3abbd5/code/java/dsl/05.java
--------------------------------------------------------------------------------
/code/java/error-prone/01-equals.java:
--------------------------------------------------------------------------------
1 | var first = new String("abc");
2 | var second = new String("abc");
3 |
4 | var eq = first.equals(second) // true
5 | var refEq = first == second // false
6 |
--------------------------------------------------------------------------------
/code/java/functions/functions-02.java:
--------------------------------------------------------------------------------
1 | public void hello(String name) {
2 | if (name == null) {
3 | name = "World";
4 | }
5 |
6 | System.out.print("Hello, " + name + "!");
7 | }
8 |
--------------------------------------------------------------------------------
/code/java/functions/functions-03.java:
--------------------------------------------------------------------------------
1 | public boolean hasItems() {
2 | return true;
3 | }
4 |
--------------------------------------------------------------------------------
/code/java/functions/functions-04.java:
--------------------------------------------------------------------------------
1 | public double cube(double x) {
2 | return x * x * x;
3 | }
4 |
--------------------------------------------------------------------------------
/code/java/functions/functions-05.java:
--------------------------------------------------------------------------------
1 | public int sum(int... numbers) { }
2 |
--------------------------------------------------------------------------------
/code/java/functions/functions-07.java:
--------------------------------------------------------------------------------
1 | public class Main {
2 | public static void main(final String[] args){
3 | openFile("file.txt", true);
4 | }
5 | }
6 |
7 | class FileUtils {
8 | public static File openFile(String filename, boolean readOnly) { }
9 | }
10 |
--------------------------------------------------------------------------------
/code/java/functions/functions-08.java:
--------------------------------------------------------------------------------
1 | public static void main(String[]args){
2 | createFile("file.txt");
3 |
4 | createFile("file.txt", true);
5 |
6 | createFile("file.txt", true, false);
7 |
8 | createExecutableFile("file.txt");
9 | }
10 |
11 | public static File createFile(String filename) { }
12 |
13 | public static File createFile(String filename, boolean appendDate) { }
14 |
15 | public static File createFile(String filename, boolean appendDate,
16 | boolean executable) { }
17 |
18 | public static File createExecutableFile(String filename) { }
19 |
--------------------------------------------------------------------------------
/code/java/functions/functions-09.java:
--------------------------------------------------------------------------------
1 | public void init() {
2 | var moduleInferred = createList("net");
3 | }
4 |
5 | public <T> List<T> createList(T item) { }
6 |
--------------------------------------------------------------------------------
/code/java/functions/functions-10.java:
--------------------------------------------------------------------------------
1 | public static void main(String[]args) {
2 | val book = createBook();
3 | System.out.println(book);
4 | System.out.println("Title: " + book.title);
5 | }
6 |
7 | public static Book createBook(){
8 | return new Book("title_01", "author_01");
9 | }
10 |
11 | @Data // Lombok
12 | public class Book {
13 | private final String title;
14 | private final String author;
15 | }
16 |
--------------------------------------------------------------------------------
/code/java/io/read-file-to-string.java:
--------------------------------------------------------------------------------
1 | String content = null;
2 | try {
3 | content = Files.readString(filePath);
4 | } catch (IOException e) {
5 | e.printStackTrace();
6 | }
7 |
--------------------------------------------------------------------------------
/code/java/scoping-functions/also.java:
--------------------------------------------------------------------------------
1 | final var original = "abc"
2 |
3 | System.out.println(original);
4 | System.out.println(new StringBuilder(input).reverse().toString());
5 | System.out.println(original.length());
--------------------------------------------------------------------------------
/code/java/scoping-functions/apply.java:
--------------------------------------------------------------------------------
1 | public MyFragment createInstance(Bundle args) {
2 | final var fragment = new MyFragment();
3 | fragment.setArguments(args);
4 | return fragment;
5 | }
6 |
--------------------------------------------------------------------------------
/code/java/scoping-functions/let-apply.java:
--------------------------------------------------------------------------------
1 | public File makeDir(String path) {
2 | final var result = new File(path);
3 | result.mkdirs();
4 | return result;
5 | }
--------------------------------------------------------------------------------
/code/java/scoping-functions/let.java:
--------------------------------------------------------------------------------
1 | final var original = "abc"
2 | // Map the value and send to the next chain
3 |
4 | Optional.of(original)
5 | .map(
6 | str -> {
7 | System.out.println("The original String is " + str); // "abc"
8 | val result = new StringBuilder(input).reverse().toString();
9 | return result;
10 | }
11 | ).map(
12 | reversed -> {
13 | System.out.println("The reverse String is " + reversed); // "cba"
14 | val result = reversed.length();
15 | return result;
16 | }
17 | ).map(
18 | length -> {
19 | System.out.println("The length of the String is " + length); // 3
20 | return length;
21 | }
22 | )
23 |
--------------------------------------------------------------------------------
/code/java/scoping-functions/run.java:
--------------------------------------------------------------------------------
1 | if (webview.settings != null) {
2 | final var settings = webview.settings;
3 | settings.javaScriptEnabled = true;
4 | settings.databaseEnabled = true;
5 | }
--------------------------------------------------------------------------------
/code/java/scoping-functions/takeIf.java:
--------------------------------------------------------------------------------
1 | public void displaySubstringPosition(final String input, final String sub) {
2 | final var index = input.indexOf(sub);
3 | if (index >= 0) {
4 | System.out.println("The substring " + sub + "is found in " + input + ".");
5 | System.out.println("Its start position is " + index + ".");
6 | }
7 | }
--------------------------------------------------------------------------------
/code/java/scoping-functions/with.java:
--------------------------------------------------------------------------------
1 | val numbers = List.of("one", "two", "three");
2 | with(numbers) {
3 | final var firstItem = numbers.get(0);
4 | final var lastItem = numbers.get(numbers.size() - 1);
5 | System.out.println("First item: " + firstItem + ", last item: " + lastItem);
6 | }
--------------------------------------------------------------------------------
/code/java/spring-framework/spring-framework-00.java:
--------------------------------------------------------------------------------
1 | @ImmutableConfigurationProperties(prefix = "dependency.updates")
2 | @Validated
3 | @RequiredArgsConstructor // Lombok
4 | @Data // Lombok
5 | public class AppProperties {
6 | private final List<Location> location;
7 | private final Git git;
8 | private final Integer limit;
9 | @NotBlank
10 | private final String cron;
11 | @NotBlank
12 | private final String priority;
13 | @NotBlank
14 | private final String dirPath;
15 | }
16 |
17 | @Validated
18 | @Data // Lombok
19 | public class Location {
20 | @NotBlank
21 | private final String project;
22 | @NotBlank
23 | private final String repository;
24 | }
25 |
26 | @Validated
27 | @Data // Lombok
28 | public class Git {
29 | @NotBlank
30 | private final String domain;
31 | private final Credentials credentials;
32 | }
33 |
34 | @Validated
35 | @Data // Lombok
36 | public class Credentials {
37 | @NotBlank
38 | private final String username;
39 | @NotBlank
40 | private final String password;
41 | }
--------------------------------------------------------------------------------
/code/kotlin/basic/apply.kt:
--------------------------------------------------------------------------------
1 | val file = File("file.txt").apply {
2 | setExecutable(true)
3 | setReadable(true)
4 | setWritable(true)
5 | }
6 |
--------------------------------------------------------------------------------
/code/kotlin/basic/arrays.kt:
--------------------------------------------------------------------------------
1 | val size = 1
2 |
3 | val numbers = IntArray(size)
4 | numbers[0] = 1
5 |
6 | val bigNumbers = LongArray(size)
7 |
8 | val strings = Array(size) { String() }
9 |
--------------------------------------------------------------------------------
/code/kotlin/basic/bits-operations.kt:
--------------------------------------------------------------------------------
1 | val andResult = a and b
2 | val orResult = a or b
3 | val xorResult = a xor b
4 | val rightShift = a shr 2
5 | val leftShift = a shl 2
6 |
--------------------------------------------------------------------------------
/code/kotlin/basic/cast.kt:
--------------------------------------------------------------------------------
1 | if (a is String) {
2 | val result = a.substring(1)
3 | }
4 |
--------------------------------------------------------------------------------
/code/kotlin/basic/default-parameters.kt:
--------------------------------------------------------------------------------
1 | fun greeting(name: String, greet: String = "Hello") {
2 | println("$greet $name!")
3 | }
--------------------------------------------------------------------------------
/code/kotlin/basic/elvis-operator.kt:
--------------------------------------------------------------------------------
1 | val result = nullableVariable?.someNullableMethodCall() ?: fallbackIfNullMethodCall()
--------------------------------------------------------------------------------
/code/kotlin/basic/hello-world.kt:
--------------------------------------------------------------------------------
1 | fun main() {
2 | println("Hello world!")
3 | }
4 |
5 | fun main(args: Array<String>) {
6 | println("Hello World!")
7 | }
--------------------------------------------------------------------------------
/code/kotlin/basic/null-i.kt:
--------------------------------------------------------------------------------
1 | val name: String? = null
2 |
3 | var lastName: String?
4 | lastName = null
5 |
6 | var firstName: String
7 | firstName = null // Compilation error!!
8 |
--------------------------------------------------------------------------------
/code/kotlin/basic/null-ii.kt:
--------------------------------------------------------------------------------
1 | val length = bob?.department?.text?.length
2 |
--------------------------------------------------------------------------------
/code/kotlin/basic/operations.kt:
--------------------------------------------------------------------------------
1 | if (x is Int) { }
2 |
3 | val text = other as String
4 |
5 | if (x in 0..10) { }
6 |
--------------------------------------------------------------------------------
/code/kotlin/basic/print.kt:
--------------------------------------------------------------------------------
1 | print("Hello, World!")
2 | println("Hello, World!")
3 |
--------------------------------------------------------------------------------
/code/kotlin/basic/read-file.kt:
--------------------------------------------------------------------------------
1 | val text = File("something.txt").readText()
--------------------------------------------------------------------------------
/code/kotlin/basic/strings-i.kt:
--------------------------------------------------------------------------------
1 | val name = "John"
2 | val lastName = "Smith"
3 | val text = "My name is: $name $lastName"
4 | val otherText = "My name is: ${name.substring(2)}"
5 |
--------------------------------------------------------------------------------
/code/kotlin/basic/strings-ii.kt:
--------------------------------------------------------------------------------
1 | val text = """
2 | |First Line
3 | |Second Line
4 | |Third Line
5 | """.trimMargin()
6 |
--------------------------------------------------------------------------------
/code/kotlin/basic/ternary-operator.kt:
--------------------------------------------------------------------------------
1 | val text = if (x > 5) "x > 5" else "x <= 5"
2 |
--------------------------------------------------------------------------------
/code/kotlin/basic/use.kt:
--------------------------------------------------------------------------------
1 | File("contents.txt")
2 | .bufferedReader()
3 | .use {
4 | // the bufferedReader is closed automatically
5 | }
--------------------------------------------------------------------------------
/code/kotlin/basic/variables-i.kt:
--------------------------------------------------------------------------------
1 | val x: Int // compile-time error
2 | val y // compile-time error
3 | val z = 2
4 |
--------------------------------------------------------------------------------
/code/kotlin/basic/variables-ii.kt:
--------------------------------------------------------------------------------
1 | var q // compile-time error
2 | var w: Int // compile-time error
3 | var z = 2
4 |
--------------------------------------------------------------------------------
/code/kotlin/basic/when.kt:
--------------------------------------------------------------------------------
1 | val x = // value
2 | val xResult = when (x) {
3 | 0, 11 -> "0 or 11"
4 | in 1..10 -> "from 1 to 10"
5 | !in 12..14 -> "not from 12 to 14"
6 | else -> if (isOdd(x)) { "is odd" } else { "otherwise" }
7 | }
8 |
9 | val y = // value
10 | val yResult = when {
11 | isNegative(y) -> "is Negative"
12 | isZero(y) -> "is Zero"
13 | isOdd(y) -> "is odd"
14 | else -> "otherwise"
15 | }
16 |
--------------------------------------------------------------------------------
/code/kotlin/classes/classes-00.kt:
--------------------------------------------------------------------------------
1 | val file = File("file.txt")
2 |
--------------------------------------------------------------------------------
/code/kotlin/classes/classes-01.kt:
--------------------------------------------------------------------------------
1 | class User
2 |
--------------------------------------------------------------------------------
/code/kotlin/classes/classes-010.kt:
--------------------------------------------------------------------------------
1 | class Document {
2 | inner class InnerClass
3 | }
4 |
--------------------------------------------------------------------------------
/code/kotlin/classes/classes-011.kt:
--------------------------------------------------------------------------------
1 | class Document {
2 |
3 | class InnerClass
4 |
5 | }
6 |
--------------------------------------------------------------------------------
/code/kotlin/classes/classes-012.kt:
--------------------------------------------------------------------------------
1 | interface Printable {
2 | fun print()
3 | }
4 |
5 | class Document : Printable {
6 | override fun print() {
7 |
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/code/kotlin/classes/classes-02.kt:
--------------------------------------------------------------------------------
1 | open class User
2 |
--------------------------------------------------------------------------------
/code/kotlin/classes/classes-03.kt:
--------------------------------------------------------------------------------
1 | class User(val name: String)
2 |
--------------------------------------------------------------------------------
/code/kotlin/classes/classes-04.kt:
--------------------------------------------------------------------------------
1 | class User(var name: String)
2 |
--------------------------------------------------------------------------------
/code/kotlin/classes/classes-05.kt:
--------------------------------------------------------------------------------
1 | class User(var name: String, var lastName: String = "")
2 |
--------------------------------------------------------------------------------
/code/kotlin/classes/classes-06.kt:
--------------------------------------------------------------------------------
1 | class Document{
2 | var id : String = "00x"
3 | set(value) {
4 | if(value.isNotEmpty()) field = value
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/code/kotlin/classes/classes-07.kt:
--------------------------------------------------------------------------------
1 | abstract class Document {
2 | abstract fun calculateSize(): Int
3 | }
4 |
5 | class Photo : Document() {
6 | override fun calculateSize(): Int {
7 |
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/code/kotlin/classes/classes-08.kt:
--------------------------------------------------------------------------------
1 | object Document {
2 |
3 | }
4 |
--------------------------------------------------------------------------------
/code/kotlin/classes/classes-09.kt:
--------------------------------------------------------------------------------
1 | fun ByteArray.toHex() : String {
2 |
3 | }
4 |
5 | val dummyData = byteArrayOf()
6 | val hexValue = dummyData.toHex()
7 |
--------------------------------------------------------------------------------
/code/kotlin/collections/01_ListOf.kt:
--------------------------------------------------------------------------------
1 | val numbers = listOf(1, 2, 3)
2 |
3 | val map = mapOf(1 to "One",
4 | 2 to "Two",
5 | 3 to "Three")
6 |
--------------------------------------------------------------------------------
/code/kotlin/collections/02_Filter.kt:
--------------------------------------------------------------------------------
1 | val filtered = numbers.filter { it > 5 }
2 |
--------------------------------------------------------------------------------
/code/kotlin/collections/03_GroupBy.kt:
--------------------------------------------------------------------------------
1 | val groups = numbers.groupBy {
2 | if (it % 2 == 0) "even" else "odd"
3 | }
4 |
--------------------------------------------------------------------------------
/code/kotlin/collections/04_PartitionBy.kt:
--------------------------------------------------------------------------------
1 | val (evens, odds) = numbers.partition { it % 2 == 0 }
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/code/kotlin/collections/05_SortBy.kt:
--------------------------------------------------------------------------------
1 | val users = getUsers()
2 | users.sortBy { it.lastname }
3 |
--------------------------------------------------------------------------------
/code/kotlin/collections/06_Fold.kt:
--------------------------------------------------------------------------------
1 | fun <T> calculateTotals(grid: Grid<T>) =
2 | grid.rows
3 | .flatMap(Row<T>::columns)
4 | .groupingBy(Column<T>::title)
5 | .fold(0.0) {
6 | accumulator,
7 | (_, value) ->
8 | accumulator +
9 | when (value) {
10 | is Number -> value.toDouble()
11 | else -> 0.0
12 | }
13 | }
14 |
15 | data class Grid<T>(val rows: List)
16 | data class Row<T>(val columns: List)
17 | data class Column<T>(val title: String, val value: T)
18 |
19 | // https://kotlinexpertise.com/kotlin-productivity/
--------------------------------------------------------------------------------
/code/kotlin/collections/07_AssociateBy.kt:
--------------------------------------------------------------------------------
1 | val map = currencies.associateBy { it.code }
--------------------------------------------------------------------------------
/code/kotlin/collections/08_Zip.kt:
--------------------------------------------------------------------------------
1 | val pairs = listOf(1, 2, 3).zip(listOf("one", "two", "three")) // List>
--------------------------------------------------------------------------------
/code/kotlin/collections/09_InfixFunctions.kt:
--------------------------------------------------------------------------------
1 | val list = mutableListOf("a", "b", "c")
2 | list += "d"
3 | list -= "d"
--------------------------------------------------------------------------------
/code/kotlin/collections/10_CollectingAndThen.kt:
--------------------------------------------------------------------------------
1 | val topPaidEmployee = listOf("Alex" to 250, "Boss" to 999, "Chris" to 400)
2 | .filter { it.first != "Boss" }
3 | .maxBy { it.second }
4 | ?.first
5 | ?: throw IllegalStateException("No employees")
--------------------------------------------------------------------------------
/code/kotlin/collections/11_For.kt:
--------------------------------------------------------------------------------
1 | for (i in 1 until 11) { }
2 |
3 | for (i in 1..10 step 2) {}
4 |
5 | for (item in collection) {}
6 | for ((index, item) in collection.withIndex()) {}
7 |
8 | for ((key, value) in map) {}
9 |
--------------------------------------------------------------------------------
/code/kotlin/collections/12_Repeat.kt:
--------------------------------------------------------------------------------
1 | repeat(3) {
2 | println("Hello")
3 | }
--------------------------------------------------------------------------------
/code/kotlin/collections/13_Comparators.kt:
--------------------------------------------------------------------------------
1 | val personsSorted = listOf(
2 | Pair("Bob C.", 5), Pair("Alex G.", 10), Pair("Alex G.", 12)
3 | ).sortedWith(
4 | compareByDescending<Pair<String, Int>> { it.second }
5 | .thenBy { it. first }
6 | )
--------------------------------------------------------------------------------
/code/kotlin/concurrency/01.kt:
--------------------------------------------------------------------------------
1 | suspend fun loadAndCombine(name1: String, name2: String): Image =
2 | coroutineScope {
3 | val deferred1 = async { loadImage(name1) }
4 | val deferred2 = async { loadImage(name2) }
5 | combineImages(deferred1.await(), deferred2.await())
6 | }
--------------------------------------------------------------------------------
/code/kotlin/concurrency/02.kt:
--------------------------------------------------------------------------------
1 | suspend fun <T> inTransaction(
2 | f: suspend (Connection) -> T): T {
3 | try {
4 | this.sendQuery("BEGIN")
5 | val result = f(this)
6 | this.sendQuery("COMMIT")
7 | return result
8 | } catch (e: Throwable) {
9 | this.sendQuery("ROLLBACK")
10 | throw e
11 | }
12 | }
13 |
14 |
--------------------------------------------------------------------------------
/code/kotlin/concurrency/03.kt:
--------------------------------------------------------------------------------
1 | val DummyService.asyncContent: Deferred<ContentDuration>
2 | get() = async(CommonPool) { content }
3 |
4 | var results = runBlocking {
5 | arrayOf("Service A", "Service B", "Service C")
6 | .map { DummyService(it) }
7 | .map { it.asyncContent }
8 | .map { it.await() }
9 | }
10 |
--------------------------------------------------------------------------------
/code/kotlin/delegated-properties/lazy.kt:
--------------------------------------------------------------------------------
1 | val lazyValue: String by lazy {
2 | "Something"
3 | }
--------------------------------------------------------------------------------
/code/kotlin/delegated-properties/map.kt:
--------------------------------------------------------------------------------
1 | class User(val map: Map<String, Any?>) {
2 | val name: String by map
3 | val age: Int by map
4 | }
--------------------------------------------------------------------------------
/code/kotlin/delegated-properties/observable.kt:
--------------------------------------------------------------------------------
1 | class User {
2 | var name: String by Delegates.observable("") {
3 | prop, old, new ->
4 | println("$old -> $new")
5 | }
6 | }
--------------------------------------------------------------------------------
/code/kotlin/dsl/01.kt:
--------------------------------------------------------------------------------
1 | val dslSandwich = sandwich {
2 | with type "toasted"
3 | bread = "baguette"
4 |
5 | filling("cheese")
6 | filling("ham")
7 | filling("tomato")
8 |
9 | dressings {
10 | +"Basil"
11 | +"Pepper"
12 | }
13 |
14 | sideOrders {
15 | side("French Fries")
16 | }
17 |
18 | }
19 |
20 | println(dslSandwich.receipt())
21 |
22 | // https://www.kotlindays.com/2019/12/02/days-of-kotlin-a-dsl-for-everyone/index.html
--------------------------------------------------------------------------------
/code/kotlin/dsl/02.kt:
--------------------------------------------------------------------------------
1 | val result = html {
2 | head {
3 | title { +"HTML encoding with Kotlin" }
4 | }
5 | body {
6 | h1 { +"HTML encoding with Kotlin" }
7 | p {
8 | +"this format can be used as an"
9 | +"alternative markup to HTML"
10 | }
11 |
12 | // an element with attributes and text content
13 | a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
14 |
15 | // mixed content
16 | p {
17 | +"This is some"
18 | b { +"mixed" }
19 | +"text. For more see the"
20 | a(href = "http://jetbrains.com/kotlin") {
21 | +"Kotlin"
22 | }
23 | +"project"
24 | }
25 | p {
26 | +"some text"
27 | ul {
28 | for (i in 1..5)
29 | li { +"${i}*2 = ${i*2}" }
30 | }
31 | }
32 | }
33 | }
--------------------------------------------------------------------------------
/code/kotlin/dsl/03.kt:
--------------------------------------------------------------------------------
1 | verticalLayout {
2 | val name = editText()
3 | button("Say Hello") {
4 | onClick { toast("Hello, ${name.text}!") }
5 | }
6 | }
--------------------------------------------------------------------------------
/code/kotlin/dsl/04.kt:
--------------------------------------------------------------------------------
1 | override fun onCreate(savedInstanceState: Bundle?) {
2 | super.onCreate(savedInstanceState)
3 |
4 | verticalLayout {
5 | padding = dip(30)
6 | editText {
7 | hint = "Name"
8 | textSize = 24f
9 | }
10 | editText {
11 | hint = "Password"
12 | textSize = 24f
13 | }
14 | button("Login") {
15 | textSize = 26f
16 | }
17 | }
18 | }
--------------------------------------------------------------------------------
/code/kotlin/dsl/05.kt:
--------------------------------------------------------------------------------
1 | application.install(Routing) {
2 | get("/") {
3 | call.respondText("Hello, World!")
4 | }
5 | get("/bye") {
6 | call.respondText("Good bye, World!")
7 | }
8 | }
--------------------------------------------------------------------------------
/code/kotlin/error-prone/01-equals.kt:
--------------------------------------------------------------------------------
1 | var first = String() + "abc"
2 | var second = String() + "abc"
3 |
4 | var eq = first.equals(second) // true
5 | var refEq = first === second // false
--------------------------------------------------------------------------------
/code/kotlin/functions/functions-02.kt:
--------------------------------------------------------------------------------
1 | fun hello(name: String = "World") {
2 | println("Hello, $name!")
3 | }
4 |
--------------------------------------------------------------------------------
/code/kotlin/functions/functions-03.kt:
--------------------------------------------------------------------------------
1 | fun hasItems() = true
2 |
--------------------------------------------------------------------------------
/code/kotlin/functions/functions-04.kt:
--------------------------------------------------------------------------------
1 | fun cube(x: Double) : Double = x * x * x
2 |
--------------------------------------------------------------------------------
/code/kotlin/functions/functions-05.kt:
--------------------------------------------------------------------------------
1 | fun sum(vararg x: Int) { }
2 |
--------------------------------------------------------------------------------
/code/kotlin/functions/functions-07.kt:
--------------------------------------------------------------------------------
1 | fun main() {
2 | openFile("file.txt", true)
3 | }
4 |
5 | fun openFile(filename: String, readOnly: Boolean) : File { }
6 |
--------------------------------------------------------------------------------
/code/kotlin/functions/functions-08.kt:
--------------------------------------------------------------------------------
1 | fun main(args: Array<String>) {
2 | createFile("file.txt")
3 |
4 | createFile("file.txt", true)
5 | createFile("file.txt", appendDate = true)
6 |
7 | createFile("file.txt", true, false)
8 | createFile("file.txt", appendDate = true, executable = true)
9 |
10 | createFile("file.txt", executable = true)
11 | }
12 |
13 | fun createFile(filename: String, appendDate: Boolean = false,
14 | executable: Boolean = false): File { }
15 |
--------------------------------------------------------------------------------
/code/kotlin/functions/functions-09.kt:
--------------------------------------------------------------------------------
1 | fun init() {
2 | val module = createList<String>("net")
3 | val moduleInferred = createList("net")
4 | }
5 |
6 | fun <T> createList(item: T): List<T> { }
7 |
--------------------------------------------------------------------------------
/code/kotlin/functions/functions-10.kt:
--------------------------------------------------------------------------------
1 | fun main(args: Array<String>) {
2 | val book = createBook();
3 | // or
4 | val (title, author) = createBook()
5 |
6 | println(book)
7 | println("Title: $title")
8 | }
9 |
10 | fun createBook() : Book{
11 | return Book("title_01", "author_01")
12 | }
13 |
14 | data class Book(val title: String, val author: String)
15 |
--------------------------------------------------------------------------------
/code/kotlin/io/read-file-to-string.kt:
--------------------------------------------------------------------------------
1 | File("doc.txt").readText()
--------------------------------------------------------------------------------
/code/kotlin/scoping-functions/also.kt:
--------------------------------------------------------------------------------
1 | val original = "abc"
2 |
3 | original.also {
4 | println("The original String is $it") // "abc"
5 | }.also {
6 | println("The reverse String is ${it.reversed()}") // "cba"
7 | }.also {
8 | println("The length of the String is ${it.length}") // 3
9 | }
--------------------------------------------------------------------------------
/code/kotlin/scoping-functions/apply.kt:
--------------------------------------------------------------------------------
1 | fun createInstance(args: Bundle) = MyFragment().apply { arguments = args }
--------------------------------------------------------------------------------
/code/kotlin/scoping-functions/let-apply.kt:
--------------------------------------------------------------------------------
1 | fun makeDir(path: String) =
2 | path.let { File(it) }
3 | .also { it.mkdirs() }
--------------------------------------------------------------------------------
/code/kotlin/scoping-functions/let.kt:
--------------------------------------------------------------------------------
1 | val original = "abc"
2 | // Map the value and send to the next chain
3 | original.let {
4 | println("The original String is $it") // "abc"
5 | it.reversed() // map it as parameter to send to next let
6 | }.let {
7 | println("The reverse String is $it") // "cba"
8 | it.length // can be evolved to other type
9 | }.let {
10 | println("The length of the String is $it") // 3
11 | }
--------------------------------------------------------------------------------
/code/kotlin/scoping-functions/run.kt:
--------------------------------------------------------------------------------
1 | webview.settings?.run {
2 | javaScriptEnabled = true
3 | databaseEnabled = true
4 | }
--------------------------------------------------------------------------------
/code/kotlin/scoping-functions/takeIf.kt:
--------------------------------------------------------------------------------
1 | fun displaySubstringPosition(input: String, sub: String) {
2 | input.indexOf(sub).takeIf { it >= 0 }?.let {
3 | println("The substring $sub is found in $input.")
4 | println("Its start position is $it.")
5 | }
6 | }
--------------------------------------------------------------------------------
/code/kotlin/scoping-functions/with.kt:
--------------------------------------------------------------------------------
1 | val numbers = mutableListOf("one", "two", "three")
2 | with(numbers) {
3 | val firstItem = first()
4 | val lastItem = last()
5 | println("First item: $firstItem, last item: $lastItem")
6 | }
--------------------------------------------------------------------------------
/code/kotlin/spring-framework/spring-framework-00.kt:
--------------------------------------------------------------------------------
1 | @ImmutableConfigurationProperties(prefix = "dependency.updates")
2 | @Validated
3 | data class AppProperties(
4 | val locations: List<Location>,
5 | val git: Git,
6 | val limit: Int,
7 | @field:NotBlank
8 | val cron: String,
9 | @field:NotBlank
10 | val priority: String,
11 | @field:NotBlank
12 | val dirPath: String
13 | )
14 |
15 | @Validated
16 | data class Location(
17 | @field:NotBlank
18 | val project: String,
19 | @field:NotBlank
20 | val repository: String
21 | )
22 |
23 | @Validated
24 | data class Git(
25 | @field:NotBlank
26 | val domain: String,
27 | val credentials: Credentials
28 | )
29 |
30 | @Validated
31 | data class Credentials(
32 | @field:NotBlank
33 | val username: String,
34 | @field:NotBlank
35 | val password: String
36 | )
--------------------------------------------------------------------------------
/concurrency.html:
--------------------------------------------------------------------------------
1 |
2 | Kotlin vs Java I
Java
public CompletableFuture<SomeOtherResult> loadAndCombine(String name1, String name2){
3 | loadImageAsync(name1)
4 | .thenAcceptBoth(loadImageAsync(name2)) ((one, two) -> {
5 | combineImages(one, two)
6 | )}
7 | }
8 |
Kotlin
suspend fun loadAndCombine(name1: String, name2: String): Image =
9 | coroutineScope {
10 | val deferred1 = async { loadImage(name1) }
11 | val deferred2 = async { loadImage(name2) }
12 | combineImages(deferred1.await(), deferred2.await())
13 | }
II
Java
public <T> CompletableFuture<T> inTransaction(DatabaseFunction<T> fun) {
14 | return CompletableFuture
15 | .runAsync(() -> this.sendQuery("BEGIN"))
16 | .thenApply(ignored -> fun.execute(this))
17 | .thenApply(s -> {
18 | this.sendQuery("COMMIT");
19 | return s;
20 | }).exceptionally(throwable -> {
21 | this.sendQuery("ROLLBACK");
22 | return null;
23 | });
24 | }
25 |
Kotlin
suspend fun <T> inTransaction(
26 | f: suspend (Connection) -> T): T {
27 | try {
28 | this.sendQuery("BEGIN")
29 | val result = f(this)
30 | this.sendQuery("COMMIT")
31 | return result
32 | } catch (e: Throwable) {
33 | this.sendQuery("ROLLBACK")
34 | throw e
35 | }
36 | }
37 |
38 |
III
Java
final var callables = Stream.of("Service A", "Service B", "Service C")
39 | .map(DummyService::new)
40 | .map(service -> (Callable<ContentDuration>) service::getContent)
41 | .collect(Collectors.toList());
42 |
43 | final var executor = Executors.newWorkStealingPool();
44 | final var results = executor.invokeAll(callables).stream()
45 | .map(
46 | future -> {
47 | try {
48 | return future.get();
49 | } catch (InterruptedException | ExecutionException e) {
50 | throw new RuntimeException(e);
51 | }
52 | }
53 | ).collect(Collectors.toList());
54 |
Kotlin
val DummyService.asyncContent: Deferred<ContentDuration>
55 | get() = async(CommonPool) { content }
56 |
57 | var results = runBlocking {
58 | arrayOf("Service A", "Service B", "Service C")
59 | .map { DummyService(it) }
60 | .map { it.asyncContent }
61 | .map { it.await() }
62 | }
63 |
64 |
--------------------------------------------------------------------------------
/css/highlightjs-night-owl.css:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | Night Owl for highlight.js (c) Carl Baxter
4 |
5 | An adaptation of Sarah Drasner's Night Owl VS Code Theme
6 | https://github.com/sdras/night-owl-vscode-theme
7 |
8 | Copyright (c) 2018 Sarah Drasner
9 |
10 | Permission is hereby granted, free of charge, to any person obtaining a copy
11 | of this software and associated documentation files (the "Software"), to deal
12 | in the Software without restriction, including without limitation the rights
13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 | copies of the Software, and to permit persons to whom the Software is
15 | furnished to do so, subject to the following conditions:
16 |
17 | The above copyright notice and this permission notice shall be included in all
18 | copies or substantial portions of the Software.
19 |
20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 | SOFTWARE.
27 |
28 | */
29 |
30 | .hljs {
31 | display: block;
32 | overflow-x: auto;
33 | padding: 0.5em;
34 | background: #011627;
35 | color: #d6deeb;
36 | }
37 |
38 | /* General Purpose */
39 | .hljs-keyword {
40 | color: #c792ea;
41 | font-style: italic;
42 | }
43 | .hljs-built_in {
44 | color: #addb67;
45 | font-style: italic;
46 | }
47 | .hljs-type {
48 | color: #82aaff;
49 | }
50 | .hljs-literal {
51 | color: #ff5874;
52 | }
53 | .hljs-number {
54 | color: #F78C6C;
55 | }
56 | .hljs-regexp {
57 | color: #5ca7e4;
58 | }
59 | .hljs-string {
60 | color: #ecc48d;
61 | }
62 | .hljs-subst {
63 | color: #d3423e;
64 | }
65 | .hljs-symbol {
66 | color: #82aaff;
67 | }
68 | .hljs-class {
69 | color: #ffcb8b;
70 | }
71 | .hljs-function {
72 | color: #82AAFF;
73 | }
74 | .hljs-title {
75 | color: #DCDCAA;
76 | font-style: italic;
77 | }
78 | .hljs-params {
79 | color: #7fdbca;
80 | }
81 |
82 | /* Meta */
83 | .hljs-comment {
84 | color: #637777;
85 | font-style: italic;
86 | }
87 | .hljs-doctag {
88 | color: #7fdbca;
89 | }
90 | .hljs-meta {
91 | color: #82aaff;
92 | }
93 | .hljs-meta-keyword {
94 | color: #82aaff;
95 | }
96 | .hljs-meta-string {
97 | color: #ecc48d;
98 | }
99 |
100 | /* Tags, attributes, config */
101 | .hljs-section {
102 | color: #82b1ff;
103 | }
104 | .hljs-tag,
105 | .hljs-name,
106 | .hljs-builtin-name {
107 | color: #7fdbca;
108 | }
109 | .hljs-attr {
110 | color: #7fdbca;
111 | }
112 | .hljs-attribute {
113 | color: #80cbc4;
114 | }
115 | .hljs-variable {
116 | color: #addb67;
117 | }
118 |
119 | /* Markup */
120 | .hljs-bullet {
121 | color: #d9f5dd;
122 | }
123 | .hljs-code {
124 | color: #80CBC4;
125 | }
126 | .hljs-emphasis {
127 | color: #c792ea;
128 | font-style: italic;
129 | }
130 | .hljs-strong {
131 | color: #addb67;
132 | font-weight: bold;
133 | }
134 | .hljs-formula {
135 | color: #c792ea;
136 | }
137 | .hljs-link {
138 | color: #ff869a;
139 | }
140 | .hljs-quote {
141 | color: #697098;
142 | font-style: italic;
143 | }
144 |
145 | /* CSS */
146 | .hljs-selector-tag {
147 | color: #ff6363;
148 | }
149 |
150 | .hljs-selector-id {
151 | color: #fad430;
152 | }
153 |
154 | .hljs-selector-class {
155 | color: #addb67;
156 | font-style: italic;
157 | }
158 |
159 | .hljs-selector-attr,
160 | .hljs-selector-pseudo {
161 | color: #c792ea;
162 | font-style: italic;
163 | }
164 |
165 | /* Templates */
166 | .hljs-template-tag {
167 | color: #c792ea;
168 | }
169 | .hljs-template-variable {
170 | color: #addb67;
171 | }
172 |
173 | /* diff */
174 | .hljs-addition {
175 | color: #addb67ff;
176 | font-style: italic;
177 | }
178 |
179 | .hljs-deletion {
180 | color: #EF535090;
181 | font-style: italic;
182 | }
183 |
--------------------------------------------------------------------------------
/css/style.css:
--------------------------------------------------------------------------------
1 | html {
2 | height: 100vh;
3 | }
4 |
5 | body {
6 | font-family: "Helvetica Neue", sans-serif;
7 | height: 100vh;
8 | background-color: #414141;
9 | }
10 |
11 | .list-group {
12 | color: #232323;
13 | }
14 |
15 | .list-group-item {
16 | color: #232323;
17 | }
18 |
19 | .section .list-group-item {
20 | background-color: #232323;
21 | }
22 |
23 | .title {
24 | font-size: 2em;
25 | color: white;
26 | font-weight: 100;
27 | letter-spacing: 0.2em;
28 | text-align: center;
29 | padding-bottom: 0.5em;
30 | }
31 |
32 | .case {}
33 |
34 | .name {
35 | font-size: 3em;
36 | color: white;
37 | font-weight: 300;
38 | text-align: center;
39 | padding-top: 0.5em;
40 | margin: 0.5em;
41 | }
42 |
43 | .lang {
44 | font-size: 1.25em;
45 | color: white;
46 | font-weight: 200;
47 | letter-spacing: 0.07em;
48 | }
49 |
50 | .code {
51 | font-size: 1em;
52 | }
53 |
54 | #fork-me {
55 | position: absolute;
56 | right: 0;
57 | }
58 |
59 | .header-title {
60 | font-size: 2em;
61 | color: #fff;
62 | background: #414141;
63 | font-weight: 300;
64 | letter-spacing: 0.05em;
65 | }
66 |
67 | .header {
68 | background-color: #414141;
69 | font-size: 1.5em;
70 | }
71 |
72 | .header a {
73 | color: white;
74 | }
75 |
76 | li a:hover {
77 | background-color: #3c3c3c;
78 | }
79 |
80 | .selected {
81 | background-color: #4e4e4e;
82 | }
83 |
84 | footer {
85 | background-color: #414141;
86 | font-size: 1em;
87 | color: white;
88 | }
89 |
90 | nav {
91 | background-color: #414141;
92 | font-size: 1em;
93 | }
94 |
95 | nav .active {
96 | background-color: #4e4e4e;
97 | }
--------------------------------------------------------------------------------
/delegated-properties.html:
--------------------------------------------------------------------------------
1 |
2 | Kotlin vs Java Lazy
Java
private String value;
3 |
4 | public synchronized String getValue() {
5 | if (value == null) {
6 | value = "Something";
7 | }
8 | return value;
9 | }
Kotlin
val lazyValue: String by lazy {
10 | "Something"
11 | }
Map
Java
final ObjectMapper mapper = new ObjectMapper(); // jackson's objectmapper
12 | final MyPojo pojo = mapper.convertValue(map, MyPojo.class);
13 |
14 | // https://stackoverflow.com/a/16430704/2441104
Kotlin
class User(val map: Map<String, Any?>) {
15 | val name: String by map
16 | val age: Int by map
17 | }
Observable
Java
public class PCLNewsAgency {
18 | private String news;
19 |
20 | private PropertyChangeSupport support;
21 |
22 | public PCLNewsAgency() {
23 | support = new PropertyChangeSupport(this);
24 | }
25 |
26 | public void addPropertyChangeListener(PropertyChangeListener pcl) {
27 | support.addPropertyChangeListener(pcl);
28 | }
29 |
30 | public void removePropertyChangeListener(PropertyChangeListener pcl) {
31 | support.removePropertyChangeListener(pcl);
32 | }
33 |
34 | public void setNews(String value) {
35 | support.firePropertyChange("news", this.news, value);
36 | this.news = value;
37 | }
38 | }
Kotlin
class User {
39 | var name: String by Delegates.observable("") {
40 | prop, old, new ->
41 | println("$old -> $new")
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/dsl.html:
--------------------------------------------------------------------------------
1 |
2 | Kotlin vs Java I
Java
final var order = Sandwich()
3 | .type("toastie")
4 | .bread("white")
5 | .fillings(List.of("Cheese", "Ham"))
6 | .dressings(List.of("Pepper", "Worcestershire sauce"))
7 | .side("Crisps")
8 | .makeMeASandwich()
9 |
10 | System.out.println(ooSandwich.receipt())
11 |
12 | // https://www.kotlindays.com/2019/12/02/days-of-kotlin-a-dsl-for-everyone/index.html
Kotlin
val dslSandwich = sandwich {
13 | with type "toasted"
14 | bread = "baguette"
15 |
16 | filling("cheese")
17 | filling("ham")
18 | filling("tomato")
19 |
20 | dressings {
21 | +"Basil"
22 | +"Pepper"
23 | }
24 |
25 | sideOrders {
26 | side("French Fries")
27 | }
28 |
29 | }
30 |
31 | println(dslSandwich.receipt())
32 |
33 | // https://www.kotlindays.com/2019/12/02/days-of-kotlin-a-dsl-for-everyone/index.html
II
Kotlin
val result = html {
34 | head {
35 | title { +"HTML encoding with Kotlin" }
36 | }
37 | body {
38 | h1 { +"HTML encoding with Kotlin" }
39 | p {
40 | +"this format can be used as an"
41 | +"alternative markup to HTML"
42 | }
43 |
44 | // an element with attributes and text content
45 | a(href = "http://jetbrains.com/kotlin") { +"Kotlin" }
46 |
47 | // mixed content
48 | p {
49 | +"This is some"
50 | b { +"mixed" }
51 | +"text. For more see the"
52 | a(href = "http://jetbrains.com/kotlin") {
53 | +"Kotlin"
54 | }
55 | +"project"
56 | }
57 | p {
58 | +"some text"
59 | ul {
60 | for (i in 1..5)
61 | li { +"${i}*2 = ${i*2}" }
62 | }
63 | }
64 | }
65 | }
III
Java
final var act = this;
66 | final var layout = new LinearLayout(act);
67 | layout.setOrientation(LinearLayout.VERTICAL);
68 | final var name = new EditText(act);
69 | final var button = new Button(act);
70 | button.setText("Say Hello");
71 | button.setOnClickListener(
72 | () -> {
73 | Toast.makeText(act, "Hello, " + name.text + "!", Toast.LENGTH_SHORT)
74 | .show()
75 | }
76 | layout.addView(name);
77 | layout.addView(button);
Kotlin
verticalLayout {
78 | val name = editText()
79 | button("Say Hello") {
80 | onClick { toast("Hello, ${name.text}!") }
81 | }
82 | }
IV
Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
83 | super.onCreate(savedInstanceState)
84 |
85 | verticalLayout {
86 | padding = dip(30)
87 | editText {
88 | hint = "Name"
89 | textSize = 24f
90 | }
91 | editText {
92 | hint = "Password"
93 | textSize = 24f
94 | }
95 | button("Login") {
96 | textSize = 26f
97 | }
98 | }
99 | }
V
Kotlin
application.install(Routing) {
100 | get("/") {
101 | call.respondText("Hello, World!")
102 | }
103 | get("/bye") {
104 | call.respondText("Good bye, World!")
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/end.html:
--------------------------------------------------------------------------------
1 |