├── .gitignore ├── README.md ├── code └── Kotlin-Sample │ ├── .gitignore │ ├── Kotlin-mpp-sample │ ├── build.gradle.kts │ └── src │ │ └── mingwX64Main │ │ └── kotlin │ │ └── com │ │ └── bennyhuo │ │ └── kotlin │ │ └── sample │ │ └── Main.kt │ ├── build.gradle.kts │ ├── buildSrc │ ├── build.gradle.kts │ └── src │ │ └── main │ │ └── kotlin │ │ ├── TestPlugin.kt │ │ ├── Versions.kt │ │ └── test-plugin.gradle.kts │ ├── deprecated-sample │ ├── build.gradle.kts │ └── src │ │ └── main │ │ └── java │ │ └── com │ │ └── bennyhuo │ │ └── kotlin │ │ └── deprecatedsample │ │ ├── Api.kt │ │ └── v2 │ │ └── NewApi.kt │ ├── gradle.properties │ ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── kclass-issue │ ├── build.gradle.kts │ └── src │ │ └── main │ │ └── java │ │ └── com │ │ └── bennyhuo │ │ └── kotlin │ │ └── kclassvalue │ │ └── Main.kt │ ├── kotlin-android-sample │ ├── .gitignore │ ├── build.gradle.kts │ ├── gradle.properties │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── bennyhuo │ │ │ └── kotlin │ │ │ └── android │ │ │ └── sample │ │ │ └── ExampleInstrumentedTest.kt │ │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── assets │ │ │ └── logback.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── bennyhuo │ │ │ │ └── kotlin │ │ │ │ └── android │ │ │ │ └── sample │ │ │ │ ├── KaeTestActivity.kt │ │ │ │ └── MainActivity.kt │ │ └── res │ │ │ ├── drawable-v24 │ │ │ └── ic_launcher_foreground.xml │ │ │ ├── drawable │ │ │ └── ic_launcher_background.xml │ │ │ ├── layout │ │ │ └── activity_main.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── values-night │ │ │ └── themes.xml │ │ │ └── values │ │ │ ├── colors.xml │ │ │ ├── strings.xml │ │ │ └── themes.xml │ │ └── test │ │ └── java │ │ └── com │ │ └── bennyhuo │ │ └── kotlin │ │ └── android │ │ └── sample │ │ └── ExampleUnitTest.kt │ ├── kotlin-js-sample │ ├── build.gradle.kts │ └── src │ │ └── main │ │ └── kotlin │ │ └── com │ │ └── bennyhuo │ │ └── kotlin │ │ └── js │ │ ├── Main.kt │ │ ├── deepcopy │ │ └── TestDeepCopy.kt │ │ └── kclassvalue │ │ └── KClassIssue.kt │ ├── opt-in-sample │ ├── build.gradle.kts │ └── src │ │ └── main │ │ └── java │ │ └── com │ │ └── bennyhuo │ │ └── kotlin │ │ └── optinsample │ │ ├── Annotations.kt │ │ └── SampleApi.kt │ ├── settings.gradle.kts │ └── src │ └── main │ └── java │ └── com │ └── bennyhuo │ └── kotlin │ ├── api │ └── UpdateApi.kt │ ├── basics │ ├── BackTicks.kt │ ├── FunctionType.kt │ ├── History.java │ └── Main.java │ ├── coroutines │ ├── CallbackToCoroutines.kt │ ├── HowSuspendFunctionWork.kt │ ├── Interceptors.kt │ ├── JavaMain.java │ └── SuspendToBlocking.kt │ ├── coroutinesupdate │ ├── DelicateGlobalScope.kt │ ├── FlowUpdates.kt │ ├── NewChannelApis.kt │ └── widgets │ │ └── Button.kt │ ├── deprecated │ └── DeprecatedSample.kt │ ├── ide │ ├── Main.kt │ ├── Sample.kt │ └── SampleJava.java │ ├── measuretime │ └── Main.kt │ ├── nonfinalfunction │ └── Base.kt │ ├── optin │ └── OptInSample.kt │ ├── other │ └── currying │ │ ├── CurryingSample.kt │ │ └── tursomcurrying │ │ ├── currying.kt │ │ ├── curyingMethod.kt │ │ └── uncurrying.kt │ ├── samissue │ ├── Java8Sam.java │ ├── KotlinSam.kt │ ├── View.java │ └── sub │ │ ├── Java8Sam.java │ │ └── SubSam.kt │ ├── scheduledtask │ ├── CoroutineUpdateTask.kt │ ├── HandlerUpdateTask.kt │ ├── Main.kt │ ├── RxJavaUpdateTask.kt │ ├── TimerUpdateTask.kt │ └── UpdateTask.kt │ ├── tailrec │ └── Main.kt │ ├── update15 │ ├── jvmrecord │ │ ├── JavaRecordSample.java │ │ └── JavaRecordSupport.kt │ ├── sealedinterfaces │ │ ├── javasealed │ │ │ ├── Cons.java │ │ │ ├── List.java │ │ │ └── Nil.java │ │ ├── since1_0 │ │ │ └── List.kt │ │ ├── since1_1 │ │ │ └── List.kt │ │ └── since1_5 │ │ │ ├── samepackage │ │ │ ├── Cons.kt │ │ │ ├── List.kt │ │ │ ├── Main.kt │ │ │ ├── Nil.kt │ │ │ └── X.java │ │ │ └── useinterface │ │ │ └── List.kt │ ├── sealedwhen │ │ └── Main.kt │ └── valueclass │ │ └── ValueClass.kt │ ├── update16 │ ├── annotations │ │ ├── Annotations.kt │ │ └── JavaAnnotationSample.java │ ├── builderinterfaces │ │ └── Main.kt │ ├── sealedwhen │ │ └── Sample.kt │ ├── suspendingtype │ │ └── SuspendingFunctionType.kt │ └── typeinferring │ │ └── Main.kt │ └── url │ └── Main.kt └── legacy ├── README.md ├── articles ├── 2016.12.26 Kotlin 如何优雅的实现『多继承』.md ├── 2016.12.26.jpg ├── 2017.1.16 │ ├── cover.jpg │ └── 用 Kotlin 写 Android 01 难道只有环境搭建这么简单?.md ├── 2017.1.2 │ ├── 17.1.2.jpg │ ├── 17.1.2b.jpg │ ├── Kotlin 1.0.6.md │ ├── args.gif │ ├── try.gif │ └── 像写文章一样使用 Kotlin.md ├── 2017.1.21 │ ├── 1.1-Beta-Banner-2-01.png │ ├── check.png │ ├── choose.png │ ├── houzi.png │ └── 喜大普奔!Kotlin 1.1 Beta 降临~.md ├── 2017.1.23 │ ├── [用 Kotlin 写 Android] 02 说说 Anko.md │ ├── cover.jpg │ ├── linear.png │ └── relative.png ├── 2017.1.30 │ ├── coroutine_cover.jpg │ ├── coroutine_lua.png │ ├── cr0.png │ ├── cr1.png │ ├── cr2.png │ ├── mdhere.gif │ ├── 勘误:15 Kotlin 与 Java 共存 (2) .md │ ├── 如何优雅的在微信公众号中编辑代码.jpg │ ├── 如何优雅的在微信公众号中编辑代码.md │ └── 深入理解 Kotlin Coroutine.md ├── 2017.1.9 │ ├── action.gif │ ├── array.jpg │ ├── comment.gif │ ├── comment2.gif │ ├── comment3.gif │ ├── enter.gif │ ├── findaction.png │ ├── intellij.jpg │ ├── iter_smart.gif │ ├── iter_stupid.gif │ ├── livetemplate.png │ ├── lt10.png │ ├── lt11.png │ ├── lt12.png │ ├── lt13.png │ ├── lt14.png │ ├── lt15.png │ ├── lt2.png │ ├── lt3.png │ ├── lt4.png │ ├── lt5.png │ ├── lt6.png │ ├── lt8.png │ ├── lt9.png │ ├── main.gif │ ├── multiline.gif │ ├── multiline2.gif │ ├── multiline_action.png │ ├── newline.gif │ ├── newline2.gif │ ├── newline3.gif │ ├── newline_action.png │ ├── posix.png │ ├── search.png │ ├── search2.png │ ├── swap_action.png │ ├── swap_fun.gif │ ├── swap_smart.gif │ ├── swap_stupid.gif │ ├── tab.gif │ ├── 为什么要为 Int 专门定义数组类型 IntArray.md │ └── 高效地使用你的 IntelliJ.md ├── 2017.2.13 │ ├── lambda.jpg │ └── 细说 Lambda 表达式.md ├── 2017.2.20 │ ├── 11RC-01.png │ ├── Kotlin 1.1:我们都路上.md │ ├── cover.jpg │ ├── embarassed.png │ ├── growup.png │ ├── high_order_fun.jpg │ ├── right.jpg │ ├── road.JPG │ ├── wangzhe.jpg │ ├── zhiyin.jpg │ └── 高阶函数(一).md ├── 2017.2.27 │ └── 高阶函数(二).md ├── 2017.2.6 │ ├── Kotlin 1.1 Beta 2 发布~.jpg │ ├── Kotlin 1.1 Beta 2 发布~.md │ └── 深入理解 Kotlin coroutine (二).md ├── 2017.3.13 │ ├── Kotlin Script 介绍.jpg │ ├── Kotlin Script 介绍.md │ ├── 快速上手 Kotlin 11招.jpg │ └── 快速上手 Kotlin 11招.md ├── 2017.3.20 │ ├── cover.jpg │ └── wechat_group.jpg ├── 2017.3.27 │ ├── Kotlin 遇到 MyBatis:到底是 Int 的错,还是 data class 的错?.md │ └── cover.jpg ├── 2017.3.4 │ ├── Kotlin 1.1.md │ └── Kotlin11blogbanner1.jpg └── 2017.4.3 │ └── Kotlin 兼容 Java 遇到的最大的坑 .md ├── arts ├── Kotlin-logo.png ├── Kotlin.jpg ├── contributes.jpg ├── e_group.png └── kotlin扫码关注.png ├── code ├── Kt01 │ └── src │ │ └── net │ │ └── println │ │ └── kt01 │ │ └── Main.kt ├── Kt02 │ └── src │ │ └── net │ │ └── println │ │ └── kt02 │ │ └── Main.kt ├── Kt03 │ ├── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── settings.gradle │ └── src │ │ └── main │ │ ├── java │ │ └── net │ │ │ └── println │ │ │ └── kt03 │ │ │ ├── Hello.java │ │ │ ├── HelloJava.kt │ │ │ └── HelloKotlin.kt │ │ └── kotlin │ │ └── net │ │ └── println │ │ └── kt03 │ │ └── User.kt ├── Kt04 │ ├── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── settings.gradle │ └── src │ │ └── main │ │ └── java │ │ └── net │ │ └── println │ │ └── kt04 │ │ ├── ConsoleParam.kt │ │ └── ConsoleParamInJava.java ├── Kt05 │ ├── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── settings.gradle │ └── src │ │ └── main │ │ └── java │ │ └── net │ │ └── println │ │ └── kt05 │ │ ├── ConsoleParam2.kt │ │ └── ConsoleParamInJava.java ├── Kt06 │ ├── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── settings.gradle │ └── src │ │ └── main │ │ └── java │ │ └── net │ │ └── println │ │ └── kt06 │ │ ├── EnumInJava.java │ │ ├── Main.java │ │ └── SayHello.kt ├── Kt08 │ ├── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── settings.gradle │ └── src │ │ └── main │ │ └── java │ │ └── net │ │ └── println │ │ └── kt08 │ │ ├── Service.kt │ │ └── User.kt ├── Kt09 │ ├── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ └── src │ │ └── main │ │ └── java │ │ └── net │ │ └── println │ │ └── kt09 │ │ └── Tailrec.kt ├── Kt10 │ ├── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── settings.gradle │ └── src │ │ └── main │ │ ├── java │ │ └── net │ │ │ └── println │ │ │ └── kt10 │ │ │ └── java │ │ │ ├── LazyNotThreadSafe.java │ │ │ ├── LazyThreadSafeDoubleCheck.java │ │ │ ├── LazyThreadSafeStaticInnerClass.java │ │ │ ├── LazyThreadSafeSynchronized.java │ │ │ └── PlainOldSingleton.java │ │ └── kotlin │ │ └── net │ │ └── println │ │ └── kt10 │ │ └── kotlin │ │ ├── LazyNotThreadSafe.kt │ │ ├── LazyThreadSafeDoubleCheck.kt │ │ ├── LazyThreadSafeStaticInnerObject.kt │ │ ├── LazyThreadSafeSynchronized.kt │ │ └── PlainOldSingleton.kt ├── Kt11 │ ├── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── settings.gradle │ └── src │ │ └── main │ │ └── kotlin │ │ └── net │ │ └── println │ │ └── kt11 │ │ ├── Main.kt │ │ ├── Player.kt │ │ ├── PlayerCmd.kt │ │ └── State.kt ├── Kt12 │ ├── Program.cs │ ├── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── result_singer.json │ ├── result_singer_field_loss.json │ ├── result_songs.json │ ├── settings.gradle │ ├── singer.json │ └── src │ │ └── main │ │ ├── java │ │ └── net │ │ │ └── println │ │ │ └── kt12 │ │ │ └── Main.java │ │ └── kotlin │ │ └── net │ │ └── println │ │ └── kt12 │ │ ├── Api.kt │ │ ├── Data.kt │ │ ├── GsonExt.kt │ │ └── Main.kt ├── Kt13 │ ├── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── settings.gradle │ └── src │ │ └── main │ │ └── kotlin │ │ └── net │ │ └── println │ │ └── kt13 │ │ ├── RESTFulComponent.kt │ │ ├── Service.kt │ │ ├── User.kt │ │ ├── config │ │ └── Settings.kt │ │ └── module │ │ ├── BaseUrlModule.kt │ │ ├── CacheModule.kt │ │ ├── GsonConverterModule.kt │ │ ├── GsonModule.kt │ │ ├── OkHttpClientModule.kt │ │ ├── RetrofitModule.kt │ │ └── RxAdapterModule.kt ├── Kt14 │ ├── Kt14 │ │ ├── build.gradle │ │ └── src │ │ │ └── main │ │ │ ├── java │ │ │ └── net │ │ │ │ └── println │ │ │ │ └── kt14 │ │ │ │ ├── AccessToObject.java │ │ │ │ ├── AccessToOverloads.java │ │ │ │ ├── CallExtenstionMethod.java │ │ │ │ ├── CallInternalClass.java │ │ │ │ ├── CallPackageMethod.java │ │ │ │ └── PersonMain.java │ │ │ └── kotlin │ │ │ └── net │ │ │ └── println │ │ │ └── kt14 │ │ │ ├── CallInternalClass.kt │ │ │ ├── ExtensionMethod.kt │ │ │ ├── InternalClass.kt │ │ │ ├── Overloads.kt │ │ │ ├── Package.kt │ │ │ ├── Person.kt │ │ │ └── Singleton.kt │ ├── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── kt14internal │ │ ├── build.gradle │ │ └── src │ │ │ └── main │ │ │ ├── java │ │ │ └── net │ │ │ │ └── println │ │ │ │ └── kt14internal │ │ │ │ └── CallInternalClass.java │ │ │ └── kotlin │ │ │ └── net │ │ │ └── println │ │ │ └── kt14internal │ │ │ └── CallInternalClassKt.kt │ └── settings.gradle ├── Kt15 │ ├── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── settings.gradle │ └── src │ │ └── main │ │ ├── java │ │ └── net │ │ │ └── println │ │ │ └── kt15 │ │ │ ├── DataClass.java │ │ │ ├── Generics.java │ │ │ ├── NullSafetyAbsClass.java │ │ │ ├── NullSafetyJava.java │ │ │ └── SAMInJava.java │ │ └── kotlin │ │ └── net │ │ └── println │ │ └── kt15 │ │ ├── AccessDataClass.kt │ │ ├── ConcurrentRelated.kt │ │ ├── GenericsInKt.kt │ │ ├── NullSafety.kt │ │ ├── NullSafetySubClass.kt │ │ └── SAMConversion.kt ├── MyBatisIssue │ ├── build.gradle │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── libs │ │ └── mysql-connector-java-5.1.40-bin.jar │ ├── settings.gradle │ └── src │ │ └── main │ │ ├── kotlin │ │ └── net │ │ │ └── println │ │ │ └── kotlin │ │ │ └── mybatis │ │ │ ├── Main.java │ │ │ ├── Main.kt │ │ │ ├── TestUnsafe.kt │ │ │ ├── User.kt │ │ │ ├── UserMapper.java │ │ │ └── annotations │ │ │ └── PoKo.kt │ │ └── resources │ │ └── net │ │ └── println │ │ └── kotlin │ │ └── mybatis │ │ ├── UserMapper.xml │ │ ├── config.xml │ │ ├── db.properties │ │ └── userinfo.sql ├── RealmIssue │ ├── .gitignore │ ├── app │ │ ├── .gitignore │ │ ├── build.gradle │ │ ├── proguard-rules.pro │ │ └── src │ │ │ └── main │ │ │ ├── AndroidManifest.xml │ │ │ ├── java │ │ │ └── net │ │ │ │ └── println │ │ │ │ └── kotlin │ │ │ │ └── realm │ │ │ │ ├── App.kt │ │ │ │ ├── MainActivity.kt │ │ │ │ ├── PoKo.kt │ │ │ │ └── User.kt │ │ │ └── res │ │ │ ├── layout │ │ │ └── activity_main.xml │ │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ └── values │ │ │ ├── colors.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ ├── build.gradle │ ├── gradle.properties │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ └── settings.gradle └── kt07 │ ├── build.gradle │ ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── settings.gradle │ └── src │ └── main │ ├── Resources │ └── input │ └── java │ └── net │ └── println │ └── kt07 │ └── Lambda.kt └── script ├── 14 Kotlin 与 Java 共存(1).md ├── 15 Kotlin 与 Java 共存 (2).md ├── Kt01 简介.pptx ├── Kt12 解析Json数据.key ├── Kt12 面向对象的几个概念.key └── Kt14 Kotlin 与 Java 共存(一).potx /.gitignore: -------------------------------------------------------------------------------- 1 | videos 2 | build 3 | .gradle 4 | *.iml 5 | .idea 6 | out 7 | raw 8 | cache 9 | beta -------------------------------------------------------------------------------- /code/Kotlin-Sample/.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .gradle 3 | *.iml 4 | local.properties 5 | build -------------------------------------------------------------------------------- /code/Kotlin-Sample/Kotlin-mpp-sample/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | kotlin("multiplatform") 3 | } 4 | 5 | group = "com.bennyhuo.kotlin.hello" 6 | version = "1.0-SNAPSHOT" 7 | 8 | kotlin { 9 | /* Targets configuration omitted. 10 | * To find out how to configure the targets, please follow the link: 11 | * https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#setting-up-targets */ 12 | 13 | sourceSets { 14 | val commonMain by getting { 15 | dependencies { 16 | implementation(kotlin("stdlib-common")) 17 | } 18 | } 19 | val commonTest by getting { 20 | dependencies { 21 | implementation(kotlin("test-common")) 22 | implementation(kotlin("test-annotations-common")) 23 | } 24 | } 25 | 26 | jvm() 27 | js{ 28 | nodejs() 29 | } 30 | mingwX64 { 31 | binaries { 32 | executable { 33 | // Binary configuration. 34 | entryPoint = "com.bennyhuo.kotlin.sample.main" 35 | } 36 | } 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/Kotlin-mpp-sample/src/mingwX64Main/kotlin/com/bennyhuo/kotlin/sample/Main.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.sample 2 | 3 | /** 4 | * Created by benny. 5 | */ 6 | 7 | interface DeepCopyable 8 | 9 | data class District(var name: String) 10 | 11 | data class Location(var lat: Double, var lng: Double) 12 | 13 | data class Company(var name: String, var location: Location, var district: District): DeepCopyable 14 | 15 | data class Speaker(var name: String, var age: Int, var company: Company): DeepCopyable 16 | 17 | data class Talk(var name: String, var speaker: Speaker): DeepCopyable 18 | 19 | 20 | fun main() { 21 | println("Hello") 22 | 23 | val district = District("x") 24 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/build.gradle.kts: -------------------------------------------------------------------------------- 1 | import org.jetbrains.kotlin.gradle.tasks.KotlinCompile 2 | 3 | buildscript { 4 | repositories { 5 | maven("https://mirrors.tencent.com/nexus/repository/maven-public/") 6 | } 7 | dependencies { 8 | classpath("com.android.tools.build:gradle:4.1.3") 9 | classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion") 10 | } 11 | } 12 | 13 | plugins { 14 | java 15 | kotlin("jvm") version kotlinVersion 16 | id("test-plugin") 17 | id("com.bennyhuo.test-plugin") 18 | } 19 | 20 | group = "com.bennyhuo.kotlin.hello" 21 | version = "1.0-SNAPSHOT" 22 | 23 | allprojects { 24 | repositories { 25 | maven("https://mirrors.tencent.com/nexus/repository/maven-public/") 26 | } 27 | } 28 | 29 | dependencies { 30 | implementation(kotlin("stdlib")) 31 | implementation(kotlin("reflect")) 32 | 33 | implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinCoroutineVersion") 34 | implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:$kotlinCoroutineVersion") 35 | 36 | implementation("org.jetbrains.kotlinx:kotlinx-datetime:$kotlinDateTimeVersion") 37 | 38 | implementation("com.squareup.retrofit2:retrofit:2.9.0") 39 | implementation("com.squareup.retrofit2:adapter-rxjava3:2.9.0") 40 | implementation("com.squareup.retrofit2:converter-gson:2.9.0") 41 | implementation("com.bennyhuo:portable-android-handler:1.0") 42 | implementation("io.reactivex.rxjava3:rxjava:3.0.13") 43 | 44 | implementation("org.apache.logging.log4j:log4j-core:2.14.1") 45 | implementation("org.apache.logging.log4j:log4j-api:2.14.1") 46 | 47 | implementation(project(":opt-in-sample")) 48 | implementation(project(":deprecated-sample")) 49 | 50 | testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0") 51 | testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine") 52 | } 53 | 54 | val compileKotlin: KotlinCompile by tasks 55 | compileKotlin.kotlinOptions { 56 | freeCompilerArgs = listOf( 57 | "-Xopt-in=kotlin.RequiresOptIn", 58 | "-Xextended-compiler-checks", 59 | "-Xenable-builder-inference" 60 | ) 61 | jvmTarget = "17" 62 | } 63 | 64 | java { 65 | targetCompatibility = JavaVersion.VERSION_17 66 | sourceCompatibility = JavaVersion.VERSION_17 67 | } 68 | 69 | //tasks.withType().forEach { 70 | // it.options.compilerArgs.add("--enable-preview") 71 | //} 72 | 73 | tasks.getByName("test") { 74 | useJUnitPlatform() 75 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/buildSrc/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | `kotlin-dsl` 3 | //region 4 | //id("org.gradle.kotlin.kotlin-dsl") version "2.1.4" 5 | //endregion 6 | } 7 | 8 | repositories { 9 | maven("https://mirrors.tencent.com/nexus/repository/maven-public/") 10 | } 11 | 12 | gradlePlugin { 13 | plugins { 14 | create("TestPlugin") { 15 | id = "com.bennyhuo.test-plugin" 16 | implementationClass = "TestPlugin" 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/buildSrc/src/main/kotlin/TestPlugin.kt: -------------------------------------------------------------------------------- 1 | import org.gradle.api.Plugin 2 | import org.gradle.api.Project 3 | 4 | /** 5 | * Created by benny. 6 | */ 7 | class TestPlugin: Plugin { 8 | override fun apply(target: Project) { 9 | println("Test Plugin applied!") 10 | } 11 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/buildSrc/src/main/kotlin/Versions.kt: -------------------------------------------------------------------------------- 1 | val kotlinVersion = "1.6.0" 2 | val kotlinCoroutineVersion = "1.6.0-RC" 3 | val kotlinDateTimeVersion = "0.3.1" -------------------------------------------------------------------------------- /code/Kotlin-Sample/buildSrc/src/main/kotlin/test-plugin.gradle.kts: -------------------------------------------------------------------------------- 1 | tasks.register("hello") { 2 | println("Register task hello.") 3 | group = "test" 4 | doLast { 5 | println("Hello Task from precompiled task.") 6 | } 7 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/deprecated-sample/build.gradle.kts: -------------------------------------------------------------------------------- 1 | import org.jetbrains.kotlin.gradle.tasks.KotlinCompile 2 | 3 | plugins { 4 | java 5 | kotlin("jvm") 6 | } 7 | 8 | group = "com.bennyhuo" 9 | version = "1.0-SNAPSHOT" 10 | 11 | repositories { 12 | maven("https://mirrors.tencent.com/nexus/repository/maven-public/") 13 | } 14 | 15 | dependencies { 16 | implementation("org.jetbrains.kotlin:kotlin-stdlib") 17 | testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.0") 18 | testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.0") 19 | } 20 | 21 | tasks.getByName("compileKotlin") { 22 | kotlinOptions.freeCompilerArgs = listOf("-Xopt-in=kotlin.RequiresOptIn") 23 | } 24 | 25 | tasks.getByName("test") { 26 | useJUnitPlatform() 27 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/deprecated-sample/src/main/java/com/bennyhuo/kotlin/deprecatedsample/Api.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.deprecatedsample 2 | 3 | object Api { 4 | 5 | fun newApiForDeprecated(key: String, value: String) { 6 | 7 | } 8 | 9 | @Deprecated( 10 | message = "Use newApiForDeprecated instead.", 11 | replaceWith = ReplaceWith( 12 | expression = "NewApi.newApiForDeprecated(key, value, default)", 13 | imports = ["com.bennyhuo.kotlin.deprecatedsample.v2.NewApi"] 14 | ) 15 | ) 16 | fun deprecatedDontUse(key: String, value: String) { 17 | 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/deprecated-sample/src/main/java/com/bennyhuo/kotlin/deprecatedsample/v2/NewApi.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.deprecatedsample.v2 2 | 3 | object NewApi { 4 | 5 | fun newApiForDeprecated(key: String, value: String, default: String) { 6 | 7 | } 8 | 9 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/gradle.properties: -------------------------------------------------------------------------------- 1 | kotlin.code.style=official 2 | org.gradle.daemon=true -------------------------------------------------------------------------------- /code/Kotlin-Sample/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/code/Kotlin-Sample/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /code/Kotlin-Sample/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-all.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/kclass-issue/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | java 3 | kotlin("jvm") 4 | } 5 | 6 | group = "com.bennyhuo.kotlin.hello" 7 | version = "1.0-SNAPSHOT" 8 | 9 | dependencies { 10 | implementation(kotlin("stdlib")) 11 | implementation(kotlin("reflect")) 12 | testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0") 13 | testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine") 14 | } 15 | 16 | tasks.getByName("test") { 17 | useJUnitPlatform() 18 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/kclass-issue/src/main/java/com/bennyhuo/kotlin/kclassvalue/Main.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.kclassvalue 2 | 3 | import kotlin.concurrent.thread 4 | 5 | /** 6 | * Created by benny. 7 | */ 8 | fun main() { 9 | // val anyClass = Any::class 10 | // val anyClass2 = Any::class 11 | // println(anyClass == anyClass2) 12 | // println(anyClass === anyClass2) 13 | 14 | testConcurrentIssue() 15 | } 16 | 17 | //region concurrent issue. 18 | var concurrentValue = 0 19 | 20 | fun safeIncrement() { 21 | synchronized(Any::class) { 22 | concurrentValue++ 23 | } 24 | } 25 | 26 | fun testConcurrentIssue() { 27 | List(100) { 28 | thread(start = false) { 29 | for (i in 0 until 1000) { 30 | safeIncrement() 31 | } 32 | } 33 | }.onEach(Thread::start) 34 | .forEach(Thread::join) 35 | 36 | println(concurrentValue) 37 | } 38 | //endregion -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | id("com.android.application") 3 | id("kotlin-android") 4 | id("kotlin-android-extensions") 5 | } 6 | 7 | android { 8 | compileSdkVersion(30) 9 | buildToolsVersion ("30.0.3") 10 | 11 | defaultConfig { 12 | applicationId = "com.bennyhuo.kotlin.android.sample" 13 | minSdkVersion(16) 14 | targetSdkVersion(30) 15 | versionCode = 1 16 | versionName = "1.0" 17 | 18 | testInstrumentationRunner("android.support.test.runner.AndroidJUnitRunner") 19 | } 20 | 21 | buildTypes { 22 | val release by getting { 23 | isMinifyEnabled = false 24 | proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro") 25 | } 26 | } 27 | compileOptions { 28 | sourceCompatibility = JavaVersion.VERSION_1_8 29 | targetCompatibility = JavaVersion.VERSION_1_8 30 | } 31 | kotlinOptions { 32 | jvmTarget = "1.8" 33 | useOldBackend = true 34 | } 35 | } 36 | 37 | dependencies { 38 | implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion") 39 | implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinCoroutineVersion") 40 | implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlinCoroutineVersion") 41 | 42 | implementation("androidx.appcompat:appcompat:1.3.0") 43 | implementation("androidx.constraintlayout:constraintlayout:2.0.4") 44 | implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.3.1") 45 | 46 | api("org.slf4j:slf4j-api:1.7.30") 47 | api("com.github.tony19:logback-android:2.0.0") 48 | 49 | testImplementation("junit:junit:4.+") 50 | androidTestImplementation("com.android.support.test:runner:1.0.2") 51 | androidTestImplementation("com.android.support.test.espresso:espresso-core:3.0.2") 52 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/gradle.properties: -------------------------------------------------------------------------------- 1 | android.useAndroidX=true 2 | android.enableJetifier=true 3 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/androidTest/java/com/bennyhuo/kotlin/android/sample/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.android.sample 2 | 3 | import android.support.test.InstrumentationRegistry 4 | import android.support.test.runner.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.bennyhuo.kotlin.android.sample", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/assets/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | [%thread](%file:%line\)%M: %msg%n 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/java/com/bennyhuo/kotlin/android/sample/KaeTestActivity.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.android.sample 2 | 3 | import android.os.Bundle 4 | import androidx.appcompat.app.AppCompatActivity 5 | import kotlinx.android.synthetic.main.activity_main.* 6 | import org.slf4j.LoggerFactory 7 | 8 | class KaeTestActivity : AppCompatActivity() { 9 | private val logger = LoggerFactory.getLogger(KaeTestActivity::class.java) 10 | 11 | override fun onCreate(savedInstanceState: Bundle?) { 12 | super.onCreate(savedInstanceState) 13 | setContentView(R.layout.activity_main) 14 | 15 | textView.text = "hello" 16 | textView.setOnClickListener { 17 | logger.debug("clicked.") 18 | } 19 | 20 | textView?.setOnClickListener { 21 | 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/java/com/bennyhuo/kotlin/android/sample/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.android.sample 2 | 3 | import android.animation.ObjectAnimator 4 | import android.os.Bundle 5 | import android.view.View 6 | import androidx.appcompat.app.AppCompatActivity 7 | import androidx.lifecycle.lifecycleScope 8 | import kotlinx.android.synthetic.main.activity_main.* 9 | import kotlinx.coroutines.channels.awaitClose 10 | import kotlinx.coroutines.channels.trySendBlocking 11 | import kotlinx.coroutines.flow.Flow 12 | import kotlinx.coroutines.flow.callbackFlow 13 | import kotlinx.coroutines.flow.collect 14 | import kotlinx.coroutines.launch 15 | import org.slf4j.LoggerFactory 16 | 17 | class MainActivity : AppCompatActivity() { 18 | private val logger = LoggerFactory.getLogger(MainActivity::class.java) 19 | 20 | override fun onCreate(savedInstanceState: Bundle?) { 21 | super.onCreate(savedInstanceState) 22 | setContentView(R.layout.activity_main) 23 | 24 | lifecycleScope.launch { 25 | getTextViewPositionFlow() 26 | .collect { 27 | logger.debug(it.toString()) 28 | } 29 | } 30 | 31 | textView.setOnClickListener { 32 | ObjectAnimator.ofFloat(textView, "x", 0f, 1000f) 33 | .setDuration(1000) 34 | .start() 35 | 36 | ObjectAnimator.ofFloat(textView, "y", 0f, 500f) 37 | .setDuration(1000) 38 | .start() 39 | } 40 | } 41 | 42 | private fun getTextViewPositionFlow(): Flow> { 43 | return callbackFlow> { 44 | val listener = 45 | View.OnLayoutChangeListener { v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom -> 46 | logger.debug("left: $left, top: $top") 47 | trySendBlocking(top to left) 48 | } 49 | textView.addOnLayoutChangeListener(listener) 50 | 51 | awaitClose { 52 | textView.removeOnLayoutChangeListener(listener) 53 | } 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 4 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 14 | 18 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 18 | 19 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/code/Kotlin-Sample/kotlin-android-sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/res/values-night/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFBB86FC 4 | #FF6200EE 5 | #FF3700B3 6 | #FF03DAC5 7 | #FF018786 8 | #FF000000 9 | #FFFFFFFF 10 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Kotlin-Android-Sample 3 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-android-sample/src/test/java/com/bennyhuo/kotlin/android/sample/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.android.sample 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-js-sample/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | id("org.jetbrains.kotlin.js") 3 | } 4 | 5 | group = "com.bennyhuo.kotlin.hello" 6 | version = "1.0-SNAPSHOT" 7 | 8 | dependencies { 9 | implementation(kotlin("stdlib-js")) 10 | } 11 | 12 | kotlin { 13 | // js(IR) { 14 | // moduleName = "kotlin-js-ir" 15 | // binaries.executable() 16 | // nodejs() 17 | // } 18 | 19 | js { 20 | moduleName = "kotlin-js-legacy" 21 | binaries.executable() 22 | nodejs() 23 | } 24 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-js-sample/src/main/kotlin/com/bennyhuo/kotlin/js/Main.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.js 2 | 3 | import com.bennyhuo.kotlin.js.deepcopy.DeepCopyable 4 | import com.bennyhuo.kotlin.js.deepcopy.deepCopy 5 | import com.bennyhuo.kotlin.js.kclassvalue.testKClass 6 | 7 | /** 8 | * Created by benny at 2021/6/26 8:36. 9 | */ 10 | fun main() { 11 | console.log("Hello Js") 12 | 13 | val duration = Duration(10) 14 | println(duration) 15 | 16 | testKClass() 17 | 18 | val talk = Talk( 19 | "如何优雅地使用数据类", 20 | Speaker( 21 | "bennyhuo 不是算命的", 22 | 1, 23 | Company( 24 | "猿辅导", 25 | Location(39.9, 116.3), 26 | District("北京郊区") 27 | ) 28 | ) 29 | ) 30 | 31 | val copiedTalk = talk.deepCopy() 32 | // copiedTalk.name = "Kotlin 编译器插件:我们不期待" 33 | // copiedTalk.speaker.company = Company( 34 | // "猿辅导", 35 | // Location(39.9, 116.3), 36 | // District("华鼎世家对面") 37 | // ) 38 | 39 | println(talk.speaker !== copiedTalk.speaker) 40 | println(talk.speaker.company !== copiedTalk.speaker.company) 41 | println(talk.speaker.company.location === copiedTalk.speaker.company.location) 42 | } 43 | 44 | data class District(var name: String) 45 | 46 | data class Location(var lat: Double, var lng: Double) 47 | 48 | data class Company(var name: String, var location: Location, var district: District): DeepCopyable 49 | 50 | data class Speaker(var name: String, var age: Int, var company: Company): DeepCopyable 51 | 52 | data class Talk(var name: String, var speaker: Speaker): DeepCopyable 53 | 54 | 55 | inline class Span(val value: Long) 56 | value class Duration(val value: Long) -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-js-sample/src/main/kotlin/com/bennyhuo/kotlin/js/deepcopy/TestDeepCopy.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.js.deepcopy 2 | 3 | /** 4 | * Created by benny. 5 | */ 6 | interface DeepCopyable 7 | 8 | fun T.deepCopy(): T { 9 | val constructor = this::class.js.asDynamic() 10 | val newInstance = js("{}") 11 | newInstance.__proto__ = constructor.prototype 12 | val parameters = (1..Int.MAX_VALUE).takeWhile { 13 | constructor.prototype["component$it"] !== undefined 14 | }.map { 15 | constructor.prototype["component$it"].call(this).unsafeCast() 16 | .let { 17 | (it as? DeepCopyable)?.deepCopy() ?: it 18 | } 19 | }.toTypedArray() 20 | constructor.apply(newInstance, parameters) 21 | return newInstance as T 22 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/kotlin-js-sample/src/main/kotlin/com/bennyhuo/kotlin/js/kclassvalue/KClassIssue.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.js.kclassvalue 2 | 3 | /** 4 | * Created by benny. 5 | */ 6 | fun testKClass() { 7 | val anyClass = Any::class 8 | val anyClass2 = Any::class 9 | 10 | println(anyClass === anyClass2) 11 | 12 | val personClass = Person::class 13 | val personClass2 = Person::class 14 | println(personClass === personClass2) 15 | } 16 | 17 | class Person -------------------------------------------------------------------------------- /code/Kotlin-Sample/opt-in-sample/build.gradle.kts: -------------------------------------------------------------------------------- 1 | import org.jetbrains.kotlin.gradle.tasks.KotlinCompile 2 | 3 | plugins { 4 | java 5 | kotlin("jvm") 6 | } 7 | 8 | group = "com.bennyhuo" 9 | version = "1.0-SNAPSHOT" 10 | 11 | repositories { 12 | maven("https://mirrors.tencent.com/nexus/repository/maven-public/") 13 | } 14 | 15 | dependencies { 16 | implementation("org.jetbrains.kotlin:kotlin-stdlib") 17 | testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.0") 18 | testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.0") 19 | } 20 | 21 | tasks.getByName("compileKotlin") { 22 | kotlinOptions.freeCompilerArgs = listOf("-Xopt-in=kotlin.RequiresOptIn") 23 | } 24 | 25 | tasks.getByName("test") { 26 | useJUnitPlatform() 27 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/opt-in-sample/src/main/java/com/bennyhuo/kotlin/optinsample/Annotations.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.optinsample 2 | 3 | @RequiresOptIn("This api is not stable!!", RequiresOptIn.Level.WARNING) 4 | annotation class UnstableApi 5 | 6 | @RequiresOptIn("This api is removed.", RequiresOptIn.Level.ERROR) 7 | annotation class UnsupportedApi 8 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/opt-in-sample/src/main/java/com/bennyhuo/kotlin/optinsample/SampleApi.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.optinsample 2 | 3 | class SampleApi { 4 | 5 | fun stableAndFreeToUse() { 6 | 7 | } 8 | 9 | @UnstableApi 10 | fun unstableAndPayAttention() { 11 | 12 | } 13 | 14 | @Deprecated( 15 | message = "Use stableAndFreeToUse instead.", 16 | ReplaceWith("stableAndFreeToUse()") 17 | ) 18 | @UnsupportedApi 19 | fun removedApiDontUse(): Unit = error("removed.") 20 | 21 | fun newApiForDeprecated(key: String, value: String) { 22 | 23 | } 24 | 25 | @Deprecated( 26 | message = "Use newApiForDeprecated instead.", 27 | replaceWith = ReplaceWith("newApiForDeprecated(key, value)") 28 | ) 29 | fun deprecatedDontUse(key: String, value: String) { 30 | 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/settings.gradle.kts: -------------------------------------------------------------------------------- 1 | rootProject.name = "Kotlin-Sample" 2 | 3 | include(":opt-in-sample",":deprecated-sample") 4 | include(":kotlin-android-sample") 5 | include("kotlin-js-sample") 6 | include("kclass-issue") 7 | include("Kotlin-mpp-sample") 8 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/api/UpdateApi.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.api 2 | 3 | import io.reactivex.rxjava3.core.Observable 4 | import okhttp3.Dispatcher 5 | import okhttp3.OkHttpClient 6 | import retrofit2.Call 7 | import retrofit2.Retrofit 8 | import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory 9 | import retrofit2.converter.gson.GsonConverterFactory 10 | import retrofit2.http.GET 11 | import java.util.concurrent.Executors 12 | 13 | data class Config(val bannerId: String, val bannerUrl: String) 14 | 15 | interface UpdateApi { 16 | 17 | @GET("/") 18 | suspend fun getConfigSuspend(): List 19 | 20 | @GET("/") 21 | fun getConfig(): Call> 22 | 23 | @GET("/") 24 | fun getConfigObservable(): Observable> 25 | } 26 | 27 | val updateApi by lazy { 28 | Retrofit.Builder() 29 | .client( 30 | OkHttpClient.Builder() 31 | .dispatcher(Dispatcher(Executors.newFixedThreadPool(3) { 32 | Thread(it).also { it.isDaemon = true } 33 | })) 34 | .build() 35 | ) 36 | .addCallAdapterFactory(RxJava3CallAdapterFactory.create()) 37 | .addConverterFactory(GsonConverterFactory.create()) 38 | .baseUrl("http://localhost:8080") 39 | .build() 40 | .create(UpdateApi::class.java) 41 | } 42 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/basics/BackTicks.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.basics 2 | 3 | import kotlinx.datetime.toInstant 4 | 5 | /** 6 | * Created by benny. 7 | */ 8 | fun main() { 9 | val result = 2 `**` 3 10 | println(result) 11 | val history = History("2016-02-19T00:00:00.000Z".toInstant().toEpochMilliseconds(), "Kotlin 1.0 release!") 12 | println(history.`when`()) 13 | } 14 | 15 | fun `fun`() { 16 | 17 | } 18 | 19 | @JvmName("classInJava") 20 | fun `class`() { 21 | 22 | } 23 | 24 | @JvmName("This_is_a_test") 25 | fun `This is a test`() { 26 | 27 | } 28 | 29 | infix fun Number.`**`(power: Number): Double { 30 | return Math.pow(this.toDouble(), power.toDouble()) 31 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/basics/FunctionType.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.coroutines 2 | 3 | import kotlinx.coroutines.runBlocking 4 | 5 | /** 6 | * Created by benny. 7 | */ 8 | fun a0() { 9 | 10 | } 11 | 12 | suspend fun a1() { 13 | 14 | } 15 | 16 | fun b0(): Int { 17 | TODO() 18 | } 19 | 20 | suspend fun b1(): Int { 21 | TODO() 22 | } 23 | 24 | fun Int.c0(block: (Int) -> String): String { 25 | TODO() 26 | } 27 | 28 | fun c00(i: Int, block: (Int) -> String): String { 29 | TODO() 30 | } 31 | 32 | suspend fun Int.c1(block: (Int) -> String): String { 33 | TODO() 34 | } 35 | 36 | suspend fun c11(i: Int, block: (Int) -> String): String { 37 | TODO() 38 | } 39 | 40 | fun x(block: suspend Int.((Int) -> String) -> String) { 41 | runBlocking { 42 | block(1) { 43 | it.toString() 44 | } 45 | } 46 | } 47 | 48 | fun Int.println() { 49 | 50 | } 51 | 52 | fun main() { 53 | val typeA0 = ::a0 // () -> Unit 54 | val typeA1 = ::a1 // suspend () -> Unit 55 | 56 | val typeB0 = ::b0 // () -> Int 57 | val typeB1 = ::b1 // suspend () -> Int 58 | 59 | val typeC0 = Int::c0 // Int.((Int) -> String) -> String 60 | val typeC0x = 1::c0 // ((Int) -> String) -> String 61 | val typeC00 = ::c00 // (Int, (Int) -> String) -> String 62 | val typeC1 = Int::c1 // suspend Int.((Int) -> String) -> String 63 | val typeC11 = ::c11 // suspend (Int, (Int) -> String) -> String 64 | 65 | x(typeC1) 66 | x(typeC11) 67 | 68 | listOf(1,2,3).forEach(Int::println) 69 | listOf(1,2,3).forEach(::println) 70 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/basics/History.java: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.basics; 2 | 3 | /** 4 | * Created by benny. 5 | */ 6 | public class History { 7 | 8 | private long when; 9 | private String eventName; 10 | 11 | public History(long when, String eventName) { 12 | this.when = when; 13 | this.eventName = eventName; 14 | } 15 | 16 | public long when() { 17 | return when; 18 | } 19 | 20 | public String eventName() { 21 | return eventName; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/basics/Main.java: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.basics; 2 | 3 | /** 4 | * Created by benny. 5 | */ 6 | public class Main { 7 | public static void main(String[] args) { 8 | BackTicksKt.fun(); 9 | BackTicksKt.classInJava(); 10 | BackTicksKt.This_is_a_test(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/coroutines/Interceptors.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.coroutines 2 | 3 | import kotlinx.coroutines.* 4 | import kotlin.concurrent.thread 5 | import kotlin.coroutines.Continuation 6 | import kotlin.coroutines.ContinuationInterceptor 7 | import kotlin.coroutines.CoroutineContext 8 | 9 | /** 10 | * Created by benny. 11 | */ 12 | suspend fun main() { 13 | 14 | log(1) 15 | 16 | val job = GlobalScope.launch( 17 | context = CoroutineName("Hello") + MyNewContinuationInterceptor2() + MyNewContinuationInterceptor1(), 18 | start = CoroutineStart.UNDISPATCHED 19 | ) { 20 | log(2) 21 | delay(1000) 22 | log(3) 23 | log(coroutineContext[ContinuationInterceptor]) 24 | } 25 | 26 | log(4) 27 | 28 | job.join() 29 | 30 | log(5) 31 | } 32 | 33 | private class MyNewContinuationInterceptor1 : ContinuationInterceptor { 34 | 35 | companion object Key : CoroutineContext.Key 36 | 37 | override val key: CoroutineContext.Key<*> = MyNewContinuationInterceptor1 38 | 39 | override fun interceptContinuation(continuation: Continuation): Continuation { 40 | return object : Continuation { 41 | override val context: CoroutineContext = continuation.context 42 | 43 | override fun resumeWith(result: Result) { 44 | log(" $result") 45 | thread(name = "MyMain1") { 46 | continuation.resumeWith(result) 47 | } 48 | } 49 | } 50 | } 51 | } 52 | 53 | private class MyNewContinuationInterceptor2 : ContinuationInterceptor { 54 | 55 | companion object Key : CoroutineContext.Key 56 | 57 | override val key: CoroutineContext.Key<*> = MyNewContinuationInterceptor2 58 | 59 | override fun interceptContinuation(continuation: Continuation): Continuation { 60 | return object : Continuation { 61 | override val context: CoroutineContext = continuation.context 62 | 63 | override fun resumeWith(result: Result) { 64 | log(" $result") 65 | thread(name = "MyMain2") { 66 | continuation.resumeWith(result) 67 | } 68 | } 69 | } 70 | } 71 | } 72 | 73 | fun log(message: Any?) { 74 | println("[${Thread.currentThread().name}] $message") 75 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/coroutines/JavaMain.java: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.coroutines; 2 | 3 | import org.jetbrains.annotations.NotNull; 4 | 5 | import java.util.concurrent.CompletableFuture; 6 | 7 | import kotlin.coroutines.Continuation; 8 | import kotlin.coroutines.CoroutineContext; 9 | import kotlin.coroutines.EmptyCoroutineContext; 10 | import kotlin.coroutines.intrinsics.IntrinsicsKt; 11 | 12 | public class JavaMain { 13 | public static void main(String[] args) { 14 | CompletableFuture future = new CompletableFuture<>(); 15 | Object result = SuspendToBlockingKt.suspendableApi(new Continuation() { 16 | @NotNull 17 | @Override 18 | public CoroutineContext getContext() { 19 | return EmptyCoroutineContext.INSTANCE; 20 | } 21 | 22 | @Override 23 | public void resumeWith(@NotNull Object o) { 24 | if(o instanceof String) { 25 | future.complete((String) o); 26 | } else { 27 | future.completeExceptionally((Throwable) o); 28 | } 29 | } 30 | }); 31 | 32 | if (result != IntrinsicsKt.getCOROUTINE_SUSPENDED()) { 33 | future.complete((String) result); 34 | } 35 | 36 | System.out.println(future.join()); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/coroutines/SuspendToBlocking.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.coroutines 2 | 3 | import kotlinx.coroutines.* 4 | import kotlinx.coroutines.future.future 5 | import kotlin.coroutines.EmptyCoroutineContext 6 | 7 | suspend fun suspendableApi(): String { 8 | if(Math.random() > 0.5) delay(1000) 9 | 10 | return "bilibli 关注我:bennyhuo 不是算命的" 11 | } 12 | 13 | fun futureMain() { 14 | val scope = CoroutineScope(Dispatchers.IO) 15 | 16 | scope.future { 17 | suspendableApi() 18 | }.join() 19 | .let(::println) 20 | } 21 | 22 | fun main() { 23 | val value = runBlocking { 24 | suspendableApi() 25 | } 26 | println(value) 27 | 28 | futureMain() 29 | } 30 | 31 | //suspend fun main() { 32 | // val value = suspendableApi() 33 | // println(value) 34 | //} -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/coroutinesupdate/DelicateGlobalScope.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.coroutinesupdate 2 | 3 | import kotlinx.coroutines.* 4 | 5 | @OptIn(DelicateCoroutinesApi::class) 6 | suspend fun main() { 7 | val jobInGlobal = GlobalScope.launch() { 8 | println("扶我起来!我还能再战两年!") 9 | }.join() 10 | GlobalScope.cancel() 11 | 12 | val scope = CoroutineScope(Dispatchers.IO) 13 | val job = scope.launch { 14 | 15 | } 16 | 17 | job.cancel() 18 | scope.cancel() 19 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/coroutinesupdate/NewChannelApis.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.coroutinesupdate 2 | 3 | import kotlinx.coroutines.Dispatchers 4 | import kotlinx.coroutines.channels.* 5 | import kotlinx.coroutines.isActive 6 | import kotlinx.coroutines.launch 7 | import kotlinx.coroutines.selects.select 8 | import kotlinx.coroutines.withContext 9 | 10 | suspend fun main() { 11 | val channel = Channel() 12 | // channel.trySend(1).isSuccess // offer 13 | // channel.tryReceive().getOrNull() // poll 14 | // 15 | // channel.trySendBlocking(1) // sendBlocking 16 | 17 | withContext(Dispatchers.Default) { 18 | val producer = launch { 19 | (0 .. 5).forEach { 20 | channel.send(it) 21 | } 22 | channel.close() 23 | } 24 | 25 | val consumer = launch { 26 | while (isActive && !channel.isClosedForReceive) { 27 | select { 28 | channel.onReceiveCatching { 29 | println("$it - ${it.isClosed}") 30 | } 31 | } 32 | 33 | // or 34 | // val value = channel.onReceiveCatching() 35 | } 36 | } 37 | 38 | select { 39 | consumer.onJoin 40 | } 41 | 42 | 43 | val consumer1 = produce { 44 | 45 | } 46 | 47 | val producer1 = actor { 48 | 49 | } 50 | 51 | 52 | BroadcastChannel(1) 53 | ConflatedBroadcastChannel() 54 | 55 | } 56 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/coroutinesupdate/widgets/Button.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.coroutinesupdate.widgets 2 | 3 | fun interface OnClickListener { 4 | fun onClick(view: View) 5 | } 6 | 7 | open class View { 8 | 9 | private var onClickListener: OnClickListener? = null 10 | 11 | fun setOnClickListener(onClickListener: OnClickListener) { 12 | this.onClickListener = onClickListener 13 | } 14 | 15 | fun click() { 16 | onClickListener?.onClick(this) 17 | } 18 | } 19 | 20 | class Button: View() { 21 | 22 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/deprecated/DeprecatedSample.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.deprecated 2 | 3 | import com.bennyhuo.kotlin.deprecatedsample.Api 4 | 5 | fun main() { 6 | Api.deprecatedDontUse("name", "bennyhuo 不是算命的") 7 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/ide/Main.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.ide 2 | 3 | interface Api { 4 | val state: Int 5 | 6 | fun start() 7 | 8 | fun stop() 9 | } 10 | 11 | class ApiImpl : Api { 12 | override val state: Int 13 | get() = 0 14 | 15 | override fun start() { 16 | } 17 | 18 | override fun stop() { 19 | } 20 | 21 | 22 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/ide/Sample.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.ide 2 | 3 | /** 4 | * Created by benny at 2021/6/16 21:58. 5 | */ 6 | class Sample { 7 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/ide/SampleJava.java: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.ide; 2 | 3 | /** 4 | * Created by benny at 2021/6/16 21:59. 5 | */ 6 | public class SampleJava { 7 | } 8 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/measuretime/Main.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.measuretime 2 | 3 | import kotlinx.coroutines.CoroutineScope 4 | import kotlinx.coroutines.Dispatchers 5 | import kotlinx.coroutines.delay 6 | import kotlinx.coroutines.launch 7 | import kotlin.concurrent.fixedRateTimer 8 | import kotlin.time.* 9 | 10 | 11 | inline fun timeCost(block: ()-> Unit): Long { 12 | val startTime = System.currentTimeMillis() 13 | block() 14 | return System.currentTimeMillis() - startTime 15 | } 16 | 17 | inline fun timeCostReturns(block: () -> T): Pair { 18 | val startTime = System.currentTimeMillis() 19 | val result = block() 20 | return result to (System.currentTimeMillis() - startTime) 21 | } 22 | 23 | @OptIn(ExperimentalTime::class) 24 | suspend fun main() { 25 | 26 | val duration = measureTime { 27 | Thread.sleep(100) 28 | } 29 | println(duration) 30 | 31 | val timeCost = timeCost { 32 | Thread.sleep(100) 33 | } 34 | println(timeCost) 35 | 36 | // val (value, duration2) = measureTimedValue { 37 | // Thread.sleep(500) 38 | // 39 | // 1000 40 | // } 41 | 42 | val (value, duration2) = timeCostReturns { 43 | Thread.sleep(500) 44 | 45 | 1000 46 | } 47 | 48 | println(value) 49 | println(duration2) 50 | 51 | val scope = CoroutineScope(Dispatchers.IO) 52 | scope.launch { 53 | 54 | val duration = measureTime { 55 | delay(1000) 56 | } 57 | println(duration) 58 | 59 | val mark = TimeSource.Monotonic.markNow() 60 | delay(1000) 61 | println(mark.elapsedNow()) 62 | }.join() 63 | 64 | 65 | 66 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/nonfinalfunction/Base.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.nonfinalfunction 2 | 3 | import kotlin.random.Random 4 | 5 | /** 6 | * Created by benny. 7 | */ 8 | open class Base { 9 | 10 | open val name = "Base" 11 | 12 | val id = generateId() 13 | 14 | init { 15 | println("Base: id: $id, name: ${xyz()}") 16 | } 17 | 18 | open fun generateId(): Int { 19 | return Random(System.currentTimeMillis()).nextInt(10000000) 20 | } 21 | 22 | fun xyz(): String { 23 | generateId() 24 | return this.name 25 | } 26 | 27 | } 28 | 29 | class Derived: Base() { 30 | 31 | override val name = "Derived" 32 | 33 | init { 34 | println("Derived: id: $id, name: $name") 35 | } 36 | 37 | override fun generateId(): Int { 38 | return 1 39 | } 40 | 41 | } 42 | 43 | fun main() { 44 | val d = Derived() 45 | } 46 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/optin/OptInSample.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.optin 2 | 3 | import com.bennyhuo.kotlin.optinsample.SampleApi 4 | import com.bennyhuo.kotlin.optinsample.UnsupportedApi 5 | 6 | 7 | @UnsupportedApi 8 | fun main() { 9 | val sampleApi = SampleApi() 10 | sampleApi.stableAndFreeToUse() 11 | sampleApi.unstableAndPayAttention() 12 | sampleApi.removedApiDontUse() 13 | sampleApi.newApiForDeprecated("a", "b") 14 | } 15 | 16 | @UnsupportedApi 17 | fun main2() { 18 | main() 19 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/other/currying/CurryingSample.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.other.currying 2 | 3 | import cn.tursom.core.curry.currying 4 | 5 | /** 6 | * Created by benny. 7 | */ 8 | 9 | enum class LoggerLevel { 10 | DEBUG, 11 | INFO, 12 | WARN, 13 | ERROR 14 | } 15 | 16 | fun log(context: Any?, level: LoggerLevel, message: Any?) { 17 | println("$level - [$context]: $message") 18 | } 19 | 20 | fun main() { 21 | ::log.currying()(1234)(LoggerLevel.DEBUG)("Hello Curried from Kotlin !!!!!!!!!!") 22 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/samissue/Java8Sam.java: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.samissue; 2 | 3 | public class Java8Sam { 4 | public static void main(String[] args) { 5 | View view = new View(); 6 | view.setOnSizeChangedListener((width, height) -> { 7 | System.out.println("w: " + width + ", h: " + height); 8 | }); 9 | 10 | view.setOnSizeChangedListener(new View.OnSizeChangedListener() { 11 | @Override 12 | public void onSizeChanged(int width, int height) { 13 | System.out.println("w: " + width + ", h: " + height); 14 | } 15 | }); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/samissue/KotlinSam.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.samissue 2 | 3 | fun main() { 4 | View().setOnSizeChangedListener { width, height -> 5 | println("w: $width, h: $height") 6 | } 7 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/samissue/View.java: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.samissue; 2 | 3 | public class View { 4 | // Single Abstract method 5 | public interface OnSizeChangedListener { 6 | void onSizeChanged(int width, int height); 7 | } 8 | 9 | public void setOnSizeChangedListener(OnSizeChangedListener onSizeChangedListener) { 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/samissue/sub/Java8Sam.java: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.samissue.sub; 2 | 3 | import com.bennyhuo.kotlin.samissue.View; 4 | 5 | public class Java8Sam { 6 | public static void main(String[] args) { 7 | View view = new View(); 8 | view.setOnSizeChangedListener((width, height) -> { 9 | System.out.println("w: " + width + ", h: " + height); 10 | }); 11 | 12 | view.setOnSizeChangedListener(new View.OnSizeChangedListener() { 13 | @Override 14 | public void onSizeChanged(int width, int height) { 15 | System.out.println("w: " + width + ", h: " + height); 16 | } 17 | }); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/samissue/sub/SubSam.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.samissue.sub 2 | 3 | import com.bennyhuo.kotlin.samissue.View 4 | 5 | fun main() { 6 | View().setOnSizeChangedListener { width, height -> 7 | println("w: $width, h: $height") 8 | } 9 | 10 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/scheduledtask/CoroutineUpdateTask.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.scheduledtask 2 | 3 | import com.bennyhuo.kotlin.api.updateApi 4 | import kotlinx.coroutines.* 5 | 6 | class CoroutineUpdateTask : UpdateTask { 7 | 8 | private var scope: CoroutineScope? = null 9 | 10 | override fun scheduleUpdate(interval: Long) { 11 | cancel() 12 | val scope = CoroutineScope(Dispatchers.IO) 13 | scope.launch { 14 | while (isActive) { 15 | try { 16 | updateApi.getConfigSuspend().let(::println) 17 | } catch (e: Exception) { 18 | e.printStackTrace() 19 | if (e is CancellationException) throw e 20 | } 21 | delay(interval) 22 | } 23 | } 24 | 25 | this.scope = scope 26 | } 27 | 28 | override fun cancel() { 29 | scope?.cancel() 30 | scope = null 31 | } 32 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/scheduledtask/HandlerUpdateTask.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.scheduledtask 2 | 3 | import android.os.Handler 4 | import android.os.HandlerThread 5 | import android.os.Message 6 | import com.bennyhuo.kotlin.api.Config 7 | import com.bennyhuo.kotlin.api.updateApi 8 | import retrofit2.Call 9 | import retrofit2.Callback 10 | import retrofit2.Response 11 | 12 | class HandlerUpdateTask : UpdateTask { 13 | 14 | private var handlerThread: HandlerThread? = null 15 | private var handler: Handler? = null 16 | 17 | private fun Handler.triggerUpdateNext(delay: Long) { 18 | val message = Message.obtain(handler) { 19 | updateApi.getConfig().enqueue(object : Callback> { 20 | override fun onResponse( 21 | call: Call>, 22 | response: Response> 23 | ) { 24 | println(response.body()) 25 | } 26 | 27 | override fun onFailure(call: Call>, t: Throwable) { 28 | t.printStackTrace() 29 | } 30 | }) 31 | triggerUpdateNext(delay) 32 | } 33 | message.obj = this@HandlerUpdateTask 34 | sendMessageDelayed(message, delay) 35 | } 36 | 37 | override fun scheduleUpdate(interval: Long) { 38 | cancel() 39 | 40 | val handlerThread = HandlerThread("handler-thread") 41 | handlerThread.start() 42 | val handler = Handler(handlerThread.looper) 43 | handler.triggerUpdateNext(interval) 44 | 45 | this.handlerThread = handlerThread 46 | this.handler = handler 47 | } 48 | 49 | override fun cancel() { 50 | handler?.removeCallbacksAndMessages(this) 51 | handler = null 52 | handlerThread?.quit() 53 | handlerThread = null 54 | } 55 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/scheduledtask/Main.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.scheduledtask 2 | 3 | import kotlinx.coroutines.delay 4 | 5 | suspend fun main() { 6 | 7 | val taskClasses = listOf( 8 | TimerUpdateTask::class, 9 | RxJavaUpdateTask::class, 10 | HandlerUpdateTask::class, 11 | CoroutineUpdateTask::class 12 | ) 13 | 14 | taskClasses.forEach { 15 | val task = it.java.newInstance() 16 | println("${it.simpleName} start") 17 | task.scheduleUpdate(1000) 18 | delay(5000) 19 | task.cancel() 20 | println("${it.simpleName} cancel") 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/scheduledtask/RxJavaUpdateTask.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.scheduledtask 2 | 3 | import com.bennyhuo.kotlin.api.updateApi 4 | import io.reactivex.rxjava3.core.Observable 5 | import io.reactivex.rxjava3.disposables.Disposable 6 | import io.reactivex.rxjava3.schedulers.Schedulers 7 | import java.util.concurrent.TimeUnit 8 | 9 | class RxJavaUpdateTask : UpdateTask { 10 | 11 | private var disposable: Disposable? = null 12 | 13 | override fun scheduleUpdate(interval: Long) { 14 | cancel() 15 | 16 | disposable = Observable.interval(interval, TimeUnit.MILLISECONDS) 17 | .observeOn(Schedulers.io()) 18 | .flatMap { 19 | updateApi.getConfigObservable() 20 | }.onErrorResumeNext { 21 | Observable.just(emptyList()) 22 | }.subscribe { 23 | println(it) 24 | } 25 | } 26 | 27 | override fun cancel() { 28 | disposable?.dispose() 29 | disposable = null 30 | } 31 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/scheduledtask/TimerUpdateTask.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.scheduledtask 2 | 3 | import com.bennyhuo.kotlin.api.Config 4 | import com.bennyhuo.kotlin.api.updateApi 5 | import retrofit2.Call 6 | import retrofit2.Callback 7 | import retrofit2.Response 8 | import java.util.* 9 | 10 | class TimerUpdateTask : UpdateTask { 11 | 12 | private var timer: Timer? = null 13 | 14 | override fun scheduleUpdate(interval: Long) { 15 | cancel() 16 | 17 | val timer = Timer() 18 | timer.schedule(object : TimerTask() { 19 | override fun run() { 20 | updateApi.getConfig().enqueue(object : Callback> { 21 | override fun onResponse( 22 | call: Call>, 23 | response: Response> 24 | ) { 25 | println(response.body()) 26 | } 27 | 28 | override fun onFailure(call: Call>, t: Throwable) { 29 | t.printStackTrace() 30 | } 31 | }) 32 | } 33 | }, 0L, interval) 34 | 35 | this.timer = timer 36 | } 37 | 38 | override fun cancel() { 39 | timer?.cancel() 40 | timer = null 41 | } 42 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/scheduledtask/UpdateTask.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.scheduledtask 2 | 3 | interface UpdateTask { 4 | 5 | fun scheduleUpdate(interval: Long) 6 | 7 | fun cancel() 8 | 9 | } 10 | 11 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update15/jvmrecord/JavaRecordSample.java: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update15.jvmrecord; 2 | 3 | /** 4 | * Created by benny at 2021/6/18 8:06. 5 | */ 6 | public class JavaRecordSample { 7 | 8 | public record Person (String name, int age) { 9 | 10 | public Person { 11 | if (name == null) throw new IllegalArgumentException("name should not be null."); 12 | if (age < 0 || age > 100) throw new IllegalArgumentException(""); 13 | count++; 14 | } 15 | 16 | public static int count = 0; 17 | 18 | public static int count() { 19 | return count; 20 | } 21 | 22 | } 23 | 24 | public static void main(String[] args) { 25 | var person = new Person("bennyhuo", 2); 26 | System.out.println(person); 27 | //var ktPerson = new KotlinPerson("bennyhuo", 10); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update15/jvmrecord/JavaRecordSupport.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update15.jvmrecord 2 | 3 | /** 4 | * Created by benny at 2021/6/20 9:35. 5 | */ 6 | open class Super 7 | @JvmRecord 8 | data class KotlinPerson(val name: String, val age: Int) { 9 | 10 | } 11 | 12 | fun main() { 13 | val person = JavaRecordSample.Person("bennyhuo", 1) 14 | 15 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update15/sealedinterfaces/javasealed/Cons.java: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update15.sealedinterfaces.javasealed; 2 | 3 | /** 4 | * Created by benny. 5 | */ 6 | public final class Cons implements List{ 7 | } 8 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update15/sealedinterfaces/javasealed/List.java: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update15.sealedinterfaces.javasealed; 2 | 3 | /** 4 | * Created by benny. 5 | */ 6 | public sealed interface List permits Nil, Cons { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update15/sealedinterfaces/javasealed/Nil.java: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update15.sealedinterfaces.javasealed; 2 | 3 | /** 4 | * Created by benny. 5 | */ 6 | public final class Nil implements List { 7 | } 8 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update15/sealedinterfaces/since1_0/List.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update15.sealedinterfaces.since1_0 2 | 3 | /** 4 | * Created by benny. 5 | * For Kotlin 1.0 and before. 6 | */ 7 | sealed class List { 8 | object Nil: List() 9 | class Cons(val value: T, val next: List): List() 10 | } 11 | 12 | tailrec fun List.forEach(block: (T) -> Unit) { 13 | when(this) { 14 | List.Nil -> return 15 | is List.Cons -> { 16 | block(value) 17 | next.forEach(block) 18 | } 19 | } 20 | } 21 | 22 | fun listOf(vararg values: T): List { 23 | return values.reversedArray().fold(List.Nil as List) { acc, t -> 24 | List.Cons(t, acc) 25 | } 26 | } 27 | 28 | fun main() { 29 | listOf(1,2,3,4).forEach { 30 | println(it) 31 | } 32 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update15/sealedinterfaces/since1_1/List.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update15.sealedinterfaces.since1_1 2 | 3 | /** 4 | * Created by benny. 5 | * For Kotlin 1.1 to 1.4. 6 | */ 7 | sealed class List 8 | object Nil: List() 9 | class Cons(val value: T, val next: List): List() 10 | 11 | tailrec fun List.forEach(block: (T) -> Unit) { 12 | when(this) { 13 | Nil -> return 14 | is Cons -> { 15 | block(value) 16 | next.forEach(block) 17 | } 18 | } 19 | } 20 | 21 | fun listOf(vararg values: T): List { 22 | return values.reversedArray().fold(Nil as List) { acc, t -> 23 | Cons(t, acc) 24 | } 25 | } 26 | 27 | fun main() { 28 | listOf(1,2,3,4).forEach { 29 | println(it) 30 | } 31 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update15/sealedinterfaces/since1_5/samepackage/Cons.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update15.sealedinterfaces.since1_5.samepackage 2 | 3 | class Cons(val value: T, val next: List): List 4 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update15/sealedinterfaces/since1_5/samepackage/List.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update15.sealedinterfaces.since1_5.samepackage 2 | 3 | sealed interface List -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update15/sealedinterfaces/since1_5/samepackage/Main.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update15.sealedinterfaces.since1_5.samepackage 2 | 3 | /** 4 | * Created by benny. 5 | */ 6 | 7 | 8 | tailrec fun List.forEach(block: (T) -> Unit) { 9 | when(this) { 10 | Nil -> return 11 | is Cons -> { 12 | block(value) 13 | next.forEach(block) 14 | } 15 | } 16 | } 17 | 18 | fun listOf(vararg values: T): List { 19 | return values.reversedArray().fold(Nil as List) { acc, t -> 20 | Cons(t, acc) 21 | } 22 | } 23 | 24 | fun main() { 25 | listOf(1,2,3,4).forEach { 26 | println(it) 27 | } 28 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update15/sealedinterfaces/since1_5/samepackage/Nil.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update15.sealedinterfaces.since1_5.samepackage 2 | 3 | object Nil: List 4 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update15/sealedinterfaces/since1_5/samepackage/X.java: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update15.sealedinterfaces.since1_5.samepackage; 2 | 3 | /** 4 | * Created by benny. 5 | */ 6 | //public class X implements List { 7 | //} 8 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update15/sealedinterfaces/since1_5/useinterface/List.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update15.sealedinterfaces.since1_5.useinterface 2 | 3 | /** 4 | * Created by benny. 5 | */ 6 | sealed interface List 7 | object Nil: List 8 | class Cons(val value: T, val next: List): List 9 | 10 | tailrec fun List.forEach(block: (T) -> Unit) { 11 | when(this) { 12 | Nil -> return 13 | is Cons -> { 14 | block(value) 15 | next.forEach(block) 16 | } 17 | } 18 | } 19 | 20 | fun listOf(vararg values: T): List { 21 | return values.reversedArray().fold(Nil as List) { acc, t -> 22 | Cons(t, acc) 23 | } 24 | } 25 | 26 | fun main() { 27 | listOf(1,2,3,4).forEach { 28 | println(it) 29 | } 30 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update15/sealedwhen/Main.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update15.sealedwhen 2 | 3 | /** 4 | * Created by benny. 5 | */ 6 | 7 | enum class State { 8 | Idle, Playing, Error 9 | } 10 | 11 | class Song 12 | 13 | sealed interface PlayState { 14 | object Idle: PlayState 15 | class Playing(val song: Song): PlayState 16 | class Error(val throwable: Throwable): PlayState 17 | } 18 | 19 | fun main() { 20 | val state = State.Idle 21 | when (state) { 22 | State.Idle -> { 23 | 24 | } 25 | 26 | State.Playing -> { 27 | 28 | } 29 | } 30 | 31 | val playState: PlayState = PlayState.Idle 32 | when (playState) { 33 | PlayState.Idle -> { 34 | 35 | } 36 | } 37 | 38 | 39 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update15/valueclass/ValueClass.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update15.valueclass 2 | 3 | /** 4 | * Created by benny. 5 | */ 6 | 7 | inline class DurationForLegacy(val value: Long) 8 | 9 | @JvmInline 10 | value class DurationForNew(val value: Long) -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update16/annotations/Annotations.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update16.annotations 2 | 3 | import kotlin.reflect.full.findAnnotations 4 | 5 | /** 6 | * Created by benny. 7 | */ 8 | annotation class X 9 | 10 | @Target(AnnotationTarget.TYPE_PARAMETER) 11 | annotation class BoxContent 12 | 13 | class Box<@BoxContent T> {} 14 | 15 | @Repeatable 16 | annotation class Tag(val value: String) 17 | 18 | annotation class TagContainer(val value: Array) 19 | 20 | @JvmRepeatable(TagContainer::class) 21 | annotation class Tag1(val value: String) 22 | 23 | @Tag("hello") 24 | @Tag("world") 25 | @Tag("world") 26 | @Tag("world") 27 | @Tag("world") 28 | @Tag("world") 29 | class Abc 30 | 31 | @OptIn(ExperimentalStdlibApi::class) 32 | fun main() { 33 | val x = X() 34 | 35 | Abc::class.findAnnotations(Tag::class).forEach { println(it.value) } 36 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update16/annotations/JavaAnnotationSample.java: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update16.annotations; 2 | 3 | import java.lang.annotation.Annotation; 4 | import java.lang.annotation.Repeatable; 5 | 6 | /** 7 | * Created by benny. 8 | */ 9 | public class JavaAnnotationSample { 10 | 11 | public static void main(String[] args) { 12 | SuperAnnotation annotation = new SubAnnotation("HelloWorld"); 13 | System.out.println(annotation.value()[0]); 14 | } 15 | 16 | public @interface SuperAnnotation { 17 | String[] value(); 18 | } 19 | 20 | static class SubAnnotation implements SuperAnnotation { 21 | private String value; 22 | 23 | public SubAnnotation(String value) { 24 | this.value = value; 25 | } 26 | 27 | @Override 28 | public String[] value() { 29 | return new String[]{value}; 30 | } 31 | 32 | @Override 33 | public Class annotationType() { 34 | return SuperAnnotation.class; 35 | } 36 | } 37 | 38 | @Repeatable(Container.class) 39 | public @interface Tag { 40 | String value(); 41 | } 42 | 43 | 44 | public @interface Container { 45 | Tag[] value(); 46 | } 47 | 48 | @Tag("hello") 49 | @Tag("world") 50 | class Abc { 51 | } 52 | 53 | @Container({@Tag("hello"), @Tag("world")}) 54 | class Acb { 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update16/builderinterfaces/Main.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update16.builderinterfaces 2 | 3 | /** 4 | * Created by benny. 5 | */ 6 | class Container { 7 | fun setValue(value: T) { 8 | 9 | } 10 | 11 | fun getValue(): T = TODO() 12 | } 13 | 14 | fun buildContainer(builder: Container.() -> Unit) { 15 | 16 | } 17 | 18 | fun main() { 19 | buildContainer { 20 | setValue(1) 21 | } 22 | 23 | buildList { 24 | add("Hello") 25 | add("World") 26 | set(1, "World 1") 27 | } 28 | 29 | buildMap { 30 | put("Hello", 1) 31 | } 32 | 33 | buildSet { 34 | add(1) 35 | } 36 | 37 | sequence { 38 | yield(1) 39 | } 40 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update16/sealedwhen/Sample.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update16.sealedwhen 2 | 3 | /** 4 | * Created by benny. 5 | */ 6 | sealed class Contact { 7 | data class PhoneCall(val number: String) : Contact() 8 | data class TextMessage(val number: String) : Contact() 9 | } 10 | 11 | //fun Contact.messageCost(): Int = 12 | // when (this) { // Error: 'when' expression must be exhaustive 13 | // is Contact.PhoneCall -> 42 14 | // } 15 | 16 | fun sendMessage(contact: Contact, message: String) { 17 | // Starting with 1.6.0 18 | 19 | // Warning: Non exhaustive 'when' statements on Boolean will be 20 | // prohibited in 1.7, add 'false' branch or 'else' branch instead 21 | when (message.isEmpty()) { 22 | true -> return 23 | } 24 | // Warning: Non exhaustive 'when' statements on sealed class/interface will be 25 | // prohibited in 1.7, add 'is TextMessage' branch or 'else' branch instead 26 | when (contact) { 27 | is Contact.PhoneCall -> TODO() 28 | } 29 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update16/suspendingtype/SuspendingFunctionType.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update16.suspendingtype 2 | 3 | import kotlinx.coroutines.GlobalScope 4 | import kotlinx.coroutines.launch 5 | 6 | /** 7 | * Created by benny. 8 | */ 9 | class MyClickAction0 : () -> Unit { 10 | override fun invoke() { 11 | } 12 | 13 | } 14 | 15 | class MyClickAction1 : suspend () -> Unit { 16 | override suspend fun invoke() { 17 | TODO() 18 | } 19 | } 20 | 21 | fun launchOnClick(action: suspend () -> Unit) {} 22 | 23 | fun getSuspending(suspending: suspend () -> Unit) {} 24 | 25 | fun suspending() {} 26 | 27 | fun test(regular: () -> Unit) { 28 | getSuspending { } // OK 29 | getSuspending(::suspending) // 1.4 OK 30 | getSuspending(regular) // 1.6 OK 31 | 32 | val x: () -> Unit = { } 33 | val y: suspend () -> Unit = { } 34 | 35 | GlobalScope.launch { 36 | 37 | } 38 | 39 | suspend { 40 | 41 | } 42 | } 43 | 44 | fun main() { 45 | launchOnClick(MyClickAction0()) 46 | launchOnClick(MyClickAction1()) 47 | 48 | val clickAction1: suspend () -> Unit = MyClickAction1() 49 | // val clickAction0: suspend () -> Unit = MyClickAction0() 50 | 51 | val x: suspend () -> Unit = { } 52 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/update16/typeinferring/Main.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.update16.typeinferring 2 | 3 | /** 4 | * Created by benny. 5 | */ 6 | abstract class AbsBuilder> { 7 | val self: B = this as B 8 | 9 | private var id: Int? = null 10 | 11 | fun setId(id: Int): B { 12 | this.id = id 13 | return self 14 | } 15 | } 16 | 17 | open class MyBuilder1> : AbsBuilder() { 18 | 19 | private var name: String? = null 20 | 21 | fun setName(name: String): B { 22 | this.name = name 23 | return self 24 | } 25 | } 26 | 27 | class MyBuilder2 : MyBuilder1() { 28 | private var age: Int? = null 29 | 30 | fun setAge(age: Int): MyBuilder2 { 31 | this.age = age 32 | return this 33 | } 34 | } 35 | 36 | fun main() { 37 | MyBuilder1().setId(1).setName("Hello") // error < 1.6 38 | 39 | MyBuilder2().setId(2).setAge(10) 40 | } -------------------------------------------------------------------------------- /code/Kotlin-Sample/src/main/java/com/bennyhuo/kotlin/url/Main.kt: -------------------------------------------------------------------------------- 1 | package com.bennyhuo.kotlin.url 2 | 3 | import okhttp3.HttpUrl 4 | 5 | /** 6 | * Created by benny. 7 | */ 8 | fun appendQuery(url: String, name: String, value: String): String { 9 | val httpUrl = HttpUrl.parse(url)!! 10 | println(httpUrl.fragment()) 11 | if (httpUrl.queryParameter(name) == null) { 12 | return httpUrl.newBuilder().addQueryParameter(name, value) 13 | .build().toString() 14 | } else { 15 | return url 16 | } 17 | } 18 | 19 | fun getUrl(): String { 20 | return "http://local.bennyhuo.com:8000/#/index?c=4" 21 | } 22 | 23 | fun main() { 24 | println(appendQuery("http://www.baidu.com", "a", "1")) 25 | println(appendQuery(getUrl(), "c", "3")) 26 | } 27 | 28 | -------------------------------------------------------------------------------- /legacy/articles/2016.12.26.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2016.12.26.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.1.16/cover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.16/cover.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.1.2/17.1.2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.2/17.1.2.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.1.2/17.1.2b.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.2/17.1.2b.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.1.2/args.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.2/args.gif -------------------------------------------------------------------------------- /legacy/articles/2017.1.2/try.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.2/try.gif -------------------------------------------------------------------------------- /legacy/articles/2017.1.21/1.1-Beta-Banner-2-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.21/1.1-Beta-Banner-2-01.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.21/check.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.21/check.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.21/choose.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.21/choose.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.21/houzi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.21/houzi.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.23/cover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.23/cover.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.1.23/linear.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.23/linear.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.23/relative.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.23/relative.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.30/coroutine_cover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.30/coroutine_cover.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.1.30/coroutine_lua.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.30/coroutine_lua.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.30/cr0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.30/cr0.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.30/cr1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.30/cr1.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.30/cr2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.30/cr2.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.30/mdhere.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.30/mdhere.gif -------------------------------------------------------------------------------- /legacy/articles/2017.1.30/勘误:15 Kotlin 与 Java 共存 (2) .md: -------------------------------------------------------------------------------- 1 | # 勘误:15 Kotlin 与 Java 共存 (2) 2 | 3 | 在过去推送过的第15期视频中,涉及到一个泛型对比的点,当时文中提到“对于循环引用泛型参数的情况,Kotlin 无法实现”的结论是有问题的。感谢 @zhangdatou 指正,对应部分的内容修改如下: 4 | 5 | ## 3 泛型 6 | 7 | 整体上来讲,Kotlin 的泛型与 Java 的没什么大的差别,Java 的 ? 在 Kotlin 中变成了 *,毕竟 ? 在 Kotlin 当中用处大着呢。另外,Java 泛型的上限下限的表示法在 Kotlin 当中变成了 out 和 in。 8 | 9 | 不过,由于 Java 1.5 之前并没有泛型的概念,为了兼容旧版本,1.5 之后的 Java 仍然允许下面的写法存在: 10 | 11 | ```java 12 | List list = new ArrayList(); 13 | list.add("Hello"); 14 | list.add(1); 15 | for (Object o : list) { 16 | System.out.println(o); 17 | } 18 | ``` 19 | 而对应的,在 Kotlin 当中要用 List<*> 来表示 Java 中的 List —— 这本身没什么问题。那么我们现在再看一段代码: 20 | 21 | ```java 22 | public abstract class View

{ 23 | protected P presenter; 24 | } 25 | 26 | public abstract class Presenter{ 27 | protected V view; 28 | } 29 | ``` 30 | 这个其实是现在比较流行的 MVP 设计模式的一个简单的接口,我希望通过泛型来绑定 V-P,并且我可以通过泛型参数在运行时直接反射实例化一个 presenter 完成 V-P 的实例注入,这在 Java 当中是没有问题的。 31 | 32 | 那么在 Kotlin 当中呢? 因为我们知道 View 和 Presenter 都有泛型参数的,所以我们在 Kotlin 当中就要这么写: 33 | 34 | ```kotlin 35 | abstract class View

>{ 36 | protected abstract val presenter: P 37 | } 38 | 39 | abstract class Presenter>>>{ 40 | protected abstract val view: V 41 | } 42 | ``` 43 | 一直写下去,最终陷入了死循环。编译器给出的解释是: 44 | 45 | >This type parameter violates the Finite Bound Restriction. 46 | 47 | 在 @zhangdatou 给我发邮件之前,我曾一直对此耿耿于怀,Kotlin 这么优秀的语言怎么还会有做不到的事情呢。原来不是做不到,而是我没有想到: 48 | 49 | ```kotlin 50 | abstract class View>> 51 | abstract class Presenter>> 52 | 53 | class MyView: View() 54 | class MyPresenter: Presenter() 55 | ``` 56 | 实际上我们需要 View 的泛型参数 P 只要是 Presenter 的子类即可,并且要求这个泛型参数 P 本身对应的泛型参数也需要是 View 的子类,而这个 View 其实就是最初的那个 View,那么这个 View 的泛型参数当然就是 P 了。有点儿绕,但这么写出来明显感觉比 Java 安全靠谱多了。 57 | 58 | -------------------------------------------------------------------------------- /legacy/articles/2017.1.30/如何优雅的在微信公众号中编辑代码.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.30/如何优雅的在微信公众号中编辑代码.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/action.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/action.gif -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/array.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/array.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/comment.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/comment.gif -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/comment2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/comment2.gif -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/comment3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/comment3.gif -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/enter.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/enter.gif -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/findaction.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/findaction.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/intellij.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/intellij.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/iter_smart.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/iter_smart.gif -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/iter_stupid.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/iter_stupid.gif -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/livetemplate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/livetemplate.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/lt10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/lt10.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/lt11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/lt11.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/lt12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/lt12.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/lt13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/lt13.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/lt14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/lt14.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/lt15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/lt15.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/lt2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/lt2.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/lt3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/lt3.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/lt4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/lt4.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/lt5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/lt5.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/lt6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/lt6.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/lt8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/lt8.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/lt9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/lt9.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/main.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/main.gif -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/multiline.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/multiline.gif -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/multiline2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/multiline2.gif -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/multiline_action.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/multiline_action.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/newline.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/newline.gif -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/newline2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/newline2.gif -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/newline3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/newline3.gif -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/newline_action.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/newline_action.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/posix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/posix.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/search.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/search2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/search2.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/swap_action.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/swap_action.png -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/swap_fun.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/swap_fun.gif -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/swap_smart.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/swap_smart.gif -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/swap_stupid.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/swap_stupid.gif -------------------------------------------------------------------------------- /legacy/articles/2017.1.9/tab.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.1.9/tab.gif -------------------------------------------------------------------------------- /legacy/articles/2017.2.13/lambda.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.2.13/lambda.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.2.20/11RC-01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.2.20/11RC-01.png -------------------------------------------------------------------------------- /legacy/articles/2017.2.20/Kotlin 1.1:我们都路上.md: -------------------------------------------------------------------------------- 1 | # Kotlin 1.1:我们都路上 2 | 3 | ##►1.1 RC 来了,Release 还会远吗? 4 | 5 |

![](11RC-01.png)
6 | 7 | 去年 2 月 19 日 1.0 发版,我记得此前看到论坛上面开始讨论 1.0 API 趋于稳定的话题,想想都还挺兴奋呢,一不留神,Kotlin 1.0 在稳定奔跑一年之后,也终于迎来下一个版本:就在前天(17日),我留意到 Kotlin 官博发出了 1.1 RC(Release Candidate) 的消息,截止到目前,1.1 的所有开发工作都已经进入尾声,剩下的就只有我们期待的眼神了。 8 | 9 | ##►成长,一路有你 10 | 11 |
![](road.JPG)
12 | 13 | 2016 这一年发生了挺多的事情,最重要的就是 Kotlin 终于有一个正式的版本,开始了按部就班的日子:每两个月一个正式版本以及一个 1.1 的里程碑版本。接着,我们也很快的发现我们期待的功能都悄悄的走进了 Kotlin 的更新日志,Kotlin 正一天一天的成长起来。 14 | 15 | 不知道各位看官年龄几何,对我来说,C 和 C++ 太老,Java 虽然小了一些,可它诞生的时候我还在小学里面玩 Dos;而当我真正开始认识它们的时候却发现它们实在是太庞大了,有时候甚至觉得可怕,有时候更是没办法理解它们。它们是编程语言的长者,而我,只是一个年轻的程序员。 16 | 17 | Kotlin 则不一样了。它出现的不算早,大概 2010 年的样子吧,那个时候我刚刚闲来无事考了个软考中级的 “软件设计师”,一个在学校里面随处可见的“小黄书”背后的没有多大用处的考试,那个时候大概是我基础最好的时候吧。后来 Kotlin 逐渐成长起来,而我也逐渐脱离了象牙塔里的天真,冲向了努力干活历练自己的道路中间。 18 | 19 | ##►还记得,你与 Kotiln 的第一次相遇吗? 20 | 21 |
![](zhiyin.jpg)
22 | 23 | 最早认识 Kotlin 还是在 15 年初,那时候我正热衷于编写 IntelliJ 的插件,尽管后面成果空空,不过一些个莫名其妙的后缀为 kt 的文件却着实让我头疼不已。尽管照着说明安装好 Kotlin 的插件,可因为版本不一致,始终无法编译过去,真是无可奈何啊。当时我还想,又有新语言啊,这可当真是学海无涯咯。 24 | 25 | 不过,随着接触的机会逐渐增多,我发现 Kotlin 解决了很多我对 Java 不满的问题,而它呢,却又全力支持着 Java 中的一切,始终让我们感觉不到我用了另外一种语言,语法的那点儿差异说真的比起不同语言背后的编程思维的差异来说,简直不值得一提。我在同时尝试了 groovy 和 scala 之后,觉得 Kotlin 才是我想要的,于是在 Kotlin 1.0 发布之际,我向 Bugly 公众号投稿文章:[Android 必备技能:最有可能接替Java的语言——Kotlin](http://www.println.net/post/Android-A-Powerful-Substitution-Kotlin) ,把我眼中的 Kotlin 向大家展现了出来。 26 | 27 | 28 | ##​►Kotlin 给你带来了什么? 29 | 去年上半年有段时间经常奔波在北京和深圳之间,工作节奏“日新月异”,项目似乎进入了一种莫名其妙的状态,而我自己则犹如置身死水,所幸我也是上过王者的人了,也不枉费我那一段时间在上面投入的一个个不眠之夜吧。 30 | 31 |
![](wangzhe.jpg)
32 | 33 | 那段时间,我对项目的代码有我“自以为是的足够”的祸害的自由,于是我开始肆无忌惮的用 Kotlin 写一些模块,完全没有顾及合作开发的我们组唯一的妹子的感受。以及,这代码后来落入导师手中,我也算是坑他们不浅呐。不过,如果没有这段经历,或许我后来也不会有那么大的底气去在 10 月份斗胆录制视频,也自然不会去开公众号每周发几篇水文了。说来,得好好谢谢他们。 34 | 35 | 36 |
![](embarassed.png)
37 | 38 | 话说,自从摊上这么个事儿,我只好每天早晨 6 点起来或写写东西,或看看书,学点儿东西,每天节奏也极其规律,中午再也没没有精力跟小伙伴们组团王者了,因为我得睡一个小时。 39 | 40 | 年前我又在 Bugly 公众号发了一篇文章:[你为什么需要 Kotlin](http://mp.weixin.qq.com/s?__biz=MzA3NTYzODYzMg==&mid=2653578489&idx=1&sn=17d6e657d81c0daa345489271d305b36&chksm=84b3b6feb3c43fe8b0197850bf6ca8caefaa8045671f75ed6dec2171a681496365acfd0ebd33&mpshare=1&scene=23&srcid=0217VmFlkVB0LB4I2YRbvicL#rd),结果大家都说我是被代码耽误了的段子手,呃。。我想说你们说的很对! 41 | 42 |
![](right.jpg)
43 | 44 | 承蒙各位朋友厚爱,经常提及后续视频录制的问题,我正在尝试重新录制一套较为细致和基础的视频,目前讲义已经编写完毕,至于发布时间,那得看我啥时候录得完啦。 45 | 46 | 末了,建议大家有事儿没事儿也都写写,我最开始在公司内部写文章刷积分玩,后来发现自己写的东西经常需要复习;不仅如此,很多时候遇到一个问题,可能最终用某种方式解决了,你以为这个问题你是搞清楚了的,不过,一旦你企图将其形成文字,你就会发现问题的背后将会是更多的细枝末节。俗话说得好,好记性不如啪啪啪的机械键盘啊! 47 | 48 |
![](../../arts/kotlin扫码关注.png)
-------------------------------------------------------------------------------- /legacy/articles/2017.2.20/cover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.2.20/cover.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.2.20/embarassed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.2.20/embarassed.png -------------------------------------------------------------------------------- /legacy/articles/2017.2.20/growup.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.2.20/growup.png -------------------------------------------------------------------------------- /legacy/articles/2017.2.20/high_order_fun.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.2.20/high_order_fun.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.2.20/right.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.2.20/right.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.2.20/road.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.2.20/road.JPG -------------------------------------------------------------------------------- /legacy/articles/2017.2.20/wangzhe.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.2.20/wangzhe.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.2.20/zhiyin.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.2.20/zhiyin.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.2.6/Kotlin 1.1 Beta 2 发布~.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.2.6/Kotlin 1.1 Beta 2 发布~.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.2.6/Kotlin 1.1 Beta 2 发布~.md: -------------------------------------------------------------------------------- 1 | # Kotlin 1.1 Beta 2 重点更新 2 | 3 | ## 1 协程改包名风波 4 | 5 | 说真的,看到这个标题的时候我还挺兴奋,离 release 又近了一步。不过,看了这篇文章的时候,我就觉得也是醉醉的。发生了啥呢? 6 | 7 | 原来,协程相关的依赖统统被标记为 experimental 了,以前叫: 8 | 9 | ```kotlin 10 | package kotlin.coroutines 11 | ``` 12 | 现在呢? 13 | 14 | ```kotlin 15 | package kotlin.coroutines.experimental 16 | ``` 17 | 18 | 这意味着啥?意味着我们在这次更新之后,还得把原来的协程代码的包重新导入一遍,另外,如果你想使用协程,那么你还需要在配置当中呢启用它,例如 gradle 配置需要加入: 19 | 20 | ```gradle 21 | kotlin { 22 |     experimental { 23 |         coroutines 'enable' 24 |     } 25 | } 26 | ``` 27 | 28 | 你在升级所有的依赖的时候,确保它是兼容 1.1.0-beta-38 的,这一点很重要,不然等着报错吧! 29 | 30 | 话说,为啥要这么搞呢?按照官方的说法就是,协程这个特性目前已经实现的非常不错了,内置 API 非常少,灵活扩展性也强,不过他们觉得这个东西还有很大的潜力,也不能就这样作为最终版本给大家放出来,而作为实验特性交给大家使用呢,更多地还是希望大家能提提意见啥的。嗯,说实在的,协程这个特性真不是个小特性。 31 | 32 | ## 2 兼容 1.0 33 | 34 | 话说,1.1 的编译器终于声称兼容 1.0 的源码了,这表明我们再也不用搞两个 IntelliJ 分别装 1.0 稳定版的插件和 1.1 Beta 版的插件了。 35 | 36 | 是的,就算你不用 1.1 的特性,你装 1.1 的插件,用 1.1 的编译器,写 1.0 的代码毫无压力!! 37 | 38 | 什么?你问我试了没?当然,我一直用最新的插件,折腾地挺苦的 T T,劝诸君还是装稳定版吧,吃螃蟹要做好心理准备~! 39 | 40 | ## 3 小结 41 | 42 | 浏览了一下 1.1Beta 2 的主要特性,其实就是改改包名,修几个小 Bug,大的改动基本没有了。如果大家想要尽早上手 1.1 的特性,那么就从现在开始吧~ 43 | 44 | -------------------------------------------------------------------------------- /legacy/articles/2017.3.13/Kotlin Script 介绍.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.3.13/Kotlin Script 介绍.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.3.13/快速上手 Kotlin 11招.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.3.13/快速上手 Kotlin 11招.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.3.20/cover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.3.20/cover.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.3.20/wechat_group.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.3.20/wechat_group.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.3.27/cover.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.3.27/cover.jpg -------------------------------------------------------------------------------- /legacy/articles/2017.3.4/Kotlin11blogbanner1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/articles/2017.3.4/Kotlin11blogbanner1.jpg -------------------------------------------------------------------------------- /legacy/arts/Kotlin-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/arts/Kotlin-logo.png -------------------------------------------------------------------------------- /legacy/arts/Kotlin.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/arts/Kotlin.jpg -------------------------------------------------------------------------------- /legacy/arts/contributes.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/arts/contributes.jpg -------------------------------------------------------------------------------- /legacy/arts/e_group.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/arts/e_group.png -------------------------------------------------------------------------------- /legacy/arts/kotlin扫码关注.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/arts/kotlin扫码关注.png -------------------------------------------------------------------------------- /legacy/code/Kt01/src/net/println/kt01/Main.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt01 2 | 3 | /** 4 | * Created by benny on 10/11/16. 5 | */ 6 | fun main(args: Array){ 7 | println("Hello World!!") 8 | println(Main("Bennyhuo", 0)) 9 | } 10 | 11 | class Main(val title: String, val id: Int){ 12 | override fun toString(): String { 13 | return "$id - $title" 14 | } 15 | } -------------------------------------------------------------------------------- /legacy/code/Kt02/src/net/println/kt02/Main.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt02 2 | 3 | /** 4 | * Created by benny on 10/11/16. 5 | */ 6 | 7 | fun main(args : Array){ 8 | println("Hello World!!!") 9 | println(Main(0, "bennyhuo")) 10 | } 11 | 12 | data class Main(val id : Int, val name: String) -------------------------------------------------------------------------------- /legacy/code/Kt03/build.gradle: -------------------------------------------------------------------------------- 1 | group 'net.println' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.0.4' 6 | 7 | repositories { 8 | jcenter() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'kotlin' 17 | 18 | sourceCompatibility = 1.5 19 | 20 | repositories { 21 | jcenter() 22 | } 23 | 24 | dependencies { 25 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 26 | compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" 27 | testCompile group: 'junit', name: 'junit', version: '4.11' 28 | } -------------------------------------------------------------------------------- /legacy/code/Kt03/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/code/Kt03/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /legacy/code/Kt03/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Oct 11 23:18:44 CST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip 7 | -------------------------------------------------------------------------------- /legacy/code/Kt03/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /legacy/code/Kt03/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Kt03' 2 | 3 | -------------------------------------------------------------------------------- /legacy/code/Kt03/src/main/java/net/println/kt03/Hello.java: -------------------------------------------------------------------------------- 1 | package net.println.kt03; 2 | 3 | /** 4 | * Created by benny on 10/12/16. 5 | */ 6 | public class Hello { 7 | public static void main(String... args) { 8 | int a = 021; 9 | System.out.println(a); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /legacy/code/Kt03/src/main/java/net/println/kt03/HelloJava.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt03 2 | 3 | /** 4 | * Created by benny on 10/11/16. 5 | */ 6 | object HelloJava { 7 | @JvmStatic fun main(args: Array) { 8 | val user = User(0, "bennyhuo") 9 | println(user) 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /legacy/code/Kt03/src/main/java/net/println/kt03/HelloKotlin.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt03 2 | 3 | /** 4 | * Created by benny on 10/11/16. 5 | */ 6 | fun main(args: Array){ 7 | val user = User(0, "bennyhuo") 8 | println(user) 9 | 10 | HelloKotlin::class.constructors.map(::println) 11 | } 12 | 13 | class HelloKotlin{ 14 | fun hello(){ 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /legacy/code/Kt03/src/main/kotlin/net/println/kt03/User.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt03 2 | 3 | /** 4 | * Created by benny on 10/11/16. 5 | */ 6 | data class User(val id : Int, val name : String) { 7 | 8 | } -------------------------------------------------------------------------------- /legacy/code/Kt04/build.gradle: -------------------------------------------------------------------------------- 1 | group 'net.println' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.0.4' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'kotlin' 17 | 18 | sourceCompatibility = 1.5 19 | 20 | repositories { 21 | mavenCentral() 22 | } 23 | 24 | dependencies { 25 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 26 | compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" 27 | testCompile group: 'junit', name: 'junit', version: '4.11' 28 | } 29 | -------------------------------------------------------------------------------- /legacy/code/Kt04/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/code/Kt04/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /legacy/code/Kt04/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Oct 12 23:22:16 CST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip 7 | -------------------------------------------------------------------------------- /legacy/code/Kt04/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /legacy/code/Kt04/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Kt04' 2 | 3 | -------------------------------------------------------------------------------- /legacy/code/Kt04/src/main/java/net/println/kt04/ConsoleParam.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt04 2 | 3 | /** 4 | * Created by benny on 10/14/16. 5 | */ 6 | fun main(args: Array): Unit{ 7 | args.map(::println) 8 | } -------------------------------------------------------------------------------- /legacy/code/Kt04/src/main/java/net/println/kt04/ConsoleParamInJava.java: -------------------------------------------------------------------------------- 1 | package net.println.kt04; 2 | 3 | /** 4 | * Created by benny on 10/14/16. 5 | */ 6 | public class ConsoleParamInJava { 7 | public static void main(String... args) { 8 | for (String arg : args) { 9 | System.out.println(arg); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /legacy/code/Kt05/build.gradle: -------------------------------------------------------------------------------- 1 | group 'net.println' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.0.4' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'kotlin' 17 | 18 | sourceCompatibility = 1.5 19 | 20 | repositories { 21 | mavenCentral() 22 | } 23 | 24 | dependencies { 25 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 26 | testCompile group: 'junit', name: 'junit', version: '4.11' 27 | } 28 | -------------------------------------------------------------------------------- /legacy/code/Kt05/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/code/Kt05/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /legacy/code/Kt05/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Oct 14 22:31:22 CST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip 7 | -------------------------------------------------------------------------------- /legacy/code/Kt05/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /legacy/code/Kt05/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Kt05' 2 | 3 | -------------------------------------------------------------------------------- /legacy/code/Kt05/src/main/java/net/println/kt05/ConsoleParam2.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt05 2 | 3 | /** 4 | * a_b_c d_e f_g_h_j 5 | * a b c d e f g h j 6 | * Created by benny on 10/14/16. 7 | */ 8 | fun main(vararg args: String) { 9 | args.flatMap { 10 | it.split("_") 11 | }.map { 12 | print("$it ${it.length}") 13 | } 14 | } -------------------------------------------------------------------------------- /legacy/code/Kt05/src/main/java/net/println/kt05/ConsoleParamInJava.java: -------------------------------------------------------------------------------- 1 | package net.println.kt05; 2 | 3 | /** 4 | * Created by benny on 10/14/16. 5 | */ 6 | public class ConsoleParamInJava { 7 | public static void main(String... args) { 8 | for (String arg: args){ 9 | String[] splits = arg.split("_"); 10 | for (String split : splits) { 11 | System.out.print(split); 12 | System.out.print(" "); 13 | } 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /legacy/code/Kt06/build.gradle: -------------------------------------------------------------------------------- 1 | group 'net.println' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.0.4' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'kotlin' 17 | 18 | sourceCompatibility = 1.5 19 | 20 | repositories { 21 | mavenCentral() 22 | } 23 | 24 | dependencies { 25 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 26 | testCompile group: 'junit', name: 'junit', version: '4.11' 27 | } 28 | -------------------------------------------------------------------------------- /legacy/code/Kt06/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/code/Kt06/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /legacy/code/Kt06/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Oct 15 23:36:13 CST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip 7 | -------------------------------------------------------------------------------- /legacy/code/Kt06/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /legacy/code/Kt06/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Kt06' 2 | 3 | -------------------------------------------------------------------------------- /legacy/code/Kt06/src/main/java/net/println/kt06/EnumInJava.java: -------------------------------------------------------------------------------- 1 | package net.println.kt06; 2 | 3 | public enum EnumInJava { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /legacy/code/Kt06/src/main/java/net/println/kt06/Main.java: -------------------------------------------------------------------------------- 1 | package net.println.kt06; 2 | 3 | /** 4 | * Created by benny on 10/16/15. 5 | */ 6 | public class Main { 7 | public static void main(String... args) { 8 | Lang lang = Lang.ENGLISH; 9 | 10 | String bye; 11 | switch (lang){ 12 | case ENGLISH: 13 | bye = "Bye"; 14 | break; 15 | case CHINESE: 16 | bye = "再见"; 17 | break; 18 | case JAPANESE: 19 | bye = "さようなら"; 20 | break; 21 | case KOREAN: 22 | bye = "안녕히가세요"; 23 | break; 24 | default: 25 | bye = "Bye"; 26 | } 27 | System.out.println(bye); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /legacy/code/Kt06/src/main/java/net/println/kt06/SayHello.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt06 2 | 3 | /** 4 | * Created by benny on 10/15/16. 5 | */ 6 | 7 | enum class Lang(val hello: String){ 8 | ENGLISH("Hello"), 9 | CHINESE("你好"), 10 | JAPANESE("こんにちは"), 11 | KOREAN("안녕하새요"); 12 | 13 | fun sayHello(){ 14 | println(hello) 15 | } 16 | 17 | companion object{ 18 | fun parse(name: String):Lang{ 19 | return Lang.valueOf(name.toUpperCase()) 20 | } 21 | } 22 | } 23 | 24 | fun main(args: Array) { 25 | if(args.size == 0) return 26 | val lang = Lang.parse(args[0]) 27 | println(lang) 28 | lang.sayHello() 29 | lang.sayBye() 30 | } 31 | 32 | fun Lang.sayBye(){ 33 | val bye = when(this){ 34 | Lang.ENGLISH ->"Bye" 35 | Lang.CHINESE ->"再见" 36 | Lang.JAPANESE ->"さようなら" 37 | Lang.KOREAN -> "안녕히가세요" 38 | } 39 | println(bye) 40 | } 41 | -------------------------------------------------------------------------------- /legacy/code/Kt08/build.gradle: -------------------------------------------------------------------------------- 1 | group 'net.println' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.0.4' 6 | 7 | repositories { 8 | mavenLocal() 9 | 10 | jcenter() 11 | } 12 | dependencies { 13 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 14 | } 15 | } 16 | 17 | apply plugin: 'java' 18 | apply plugin: 'kotlin' 19 | 20 | sourceCompatibility = 1.5 21 | 22 | repositories { 23 | mavenLocal() 24 | 25 | jcenter() 26 | } 27 | 28 | dependencies { 29 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 30 | compile "com.squareup.retrofit2:retrofit:2.1.0" 31 | compile "com.squareup.retrofit2:converter-gson:2.1.0" 32 | testCompile group: 'junit', name: 'junit', version: '4.11' 33 | } 34 | -------------------------------------------------------------------------------- /legacy/code/Kt08/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/code/Kt08/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /legacy/code/Kt08/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Oct 27 21:43:09 CST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-bin.zip 7 | -------------------------------------------------------------------------------- /legacy/code/Kt08/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Kt08' 2 | 3 | -------------------------------------------------------------------------------- /legacy/code/Kt08/src/main/java/net/println/kt08/Service.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt08 2 | 3 | import retrofit2.Call 4 | import retrofit2.Retrofit 5 | import retrofit2.converter.gson.GsonConverterFactory 6 | import retrofit2.http.GET 7 | 8 | /** 9 | * Created by benny on 11/1/16. 10 | */ 11 | interface GitHubService{ 12 | 13 | @GET("/repos/enbandari/Kotlin-Tutorials/stargazers") 14 | fun getStarGazers(): Call> 15 | } 16 | 17 | object Service{ 18 | val gitHubService: GitHubService by lazy { 19 | Retrofit.Builder().baseUrl("https://api.github.com").addConverterFactory(GsonConverterFactory.create()).build().create(GitHubService::class.java) 20 | } 21 | } 22 | 23 | fun main(args: Array) { 24 | Service.gitHubService.getStarGazers().execute().body().map(::println) 25 | } -------------------------------------------------------------------------------- /legacy/code/Kt08/src/main/java/net/println/kt08/User.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt08 2 | 3 | /** 4 | * Created by benny on 11/1/16. 5 | */ 6 | data class User(val login: String, val id: Long, val avatar_url: String){ 7 | override fun toString(): String { 8 | return login 9 | } 10 | } -------------------------------------------------------------------------------- /legacy/code/Kt09/build.gradle: -------------------------------------------------------------------------------- 1 | group 'net.println' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.0.4' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'kotlin' 17 | 18 | sourceCompatibility = 1.5 19 | 20 | repositories { 21 | mavenCentral() 22 | } 23 | 24 | dependencies { 25 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 26 | compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" 27 | testCompile group: 'junit', name: 'junit', version: '4.11' 28 | } 29 | -------------------------------------------------------------------------------- /legacy/code/Kt09/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/code/Kt09/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /legacy/code/Kt09/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Nov 04 08:40:27 CST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip 7 | -------------------------------------------------------------------------------- /legacy/code/Kt09/src/main/java/net/println/kt09/Tailrec.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt09 2 | 3 | import java.math.BigInteger 4 | 5 | /** 6 | * Created by benny on 11/6/16. 7 | */ 8 | class Result(var value: BigInteger = BigInteger.valueOf(1L)) 9 | 10 | 11 | tailrec fun factorial(num: Int, result: Result){ 12 | if(num == 0) result.value = result.value.times(BigInteger.valueOf(1L)) 13 | else { 14 | result.value = result.value.times(BigInteger.valueOf(num.toLong())) 15 | factorial(num - 1, result) 16 | } 17 | } 18 | 19 | fun main(args: Array) { 20 | val result = Result() 21 | factorial(1000000, result) 22 | println(result.value) 23 | } -------------------------------------------------------------------------------- /legacy/code/Kt10/build.gradle: -------------------------------------------------------------------------------- 1 | group 'net.println' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.0.4' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'kotlin' 17 | 18 | sourceCompatibility = 1.5 19 | 20 | repositories { 21 | mavenCentral() 22 | } 23 | 24 | dependencies { 25 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 26 | testCompile group: 'junit', name: 'junit', version: '4.11' 27 | } 28 | -------------------------------------------------------------------------------- /legacy/code/Kt10/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/code/Kt10/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /legacy/code/Kt10/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Nov 12 22:19:31 CST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip 7 | -------------------------------------------------------------------------------- /legacy/code/Kt10/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Kt10' 2 | 3 | -------------------------------------------------------------------------------- /legacy/code/Kt10/src/main/java/net/println/kt10/java/LazyNotThreadSafe.java: -------------------------------------------------------------------------------- 1 | package net.println.kt10.java; 2 | 3 | /** 4 | * Created by benny on 11/13/16. 5 | */ 6 | public class LazyNotThreadSafe { 7 | private static LazyNotThreadSafe INSTANCE; 8 | 9 | private LazyNotThreadSafe(){} 10 | 11 | public static LazyNotThreadSafe getInstance(){ 12 | if(INSTANCE == null){ 13 | INSTANCE = new LazyNotThreadSafe(); 14 | } 15 | return INSTANCE; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /legacy/code/Kt10/src/main/java/net/println/kt10/java/LazyThreadSafeDoubleCheck.java: -------------------------------------------------------------------------------- 1 | package net.println.kt10.java; 2 | 3 | /** 4 | * Created by benny on 11/13/16. 5 | */ 6 | public class LazyThreadSafeDoubleCheck { 7 | private static volatile LazyThreadSafeDoubleCheck INSTANCE; 8 | 9 | private LazyThreadSafeDoubleCheck(){} 10 | 11 | public static LazyThreadSafeDoubleCheck getInstance(){ 12 | if(INSTANCE == null){ 13 | synchronized (LazyThreadSafeDoubleCheck.class){ 14 | if(INSTANCE == null) { 15 | //初始化时分为实例化和赋值两步, 尽管我们把这一步写成下面的语句, 16 | // 但Java虚拟机并不保证其他线程『眼中』这两步的顺序究竟是怎么样的 17 | INSTANCE = new LazyThreadSafeDoubleCheck(); 18 | } 19 | } 20 | } 21 | return INSTANCE; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /legacy/code/Kt10/src/main/java/net/println/kt10/java/LazyThreadSafeStaticInnerClass.java: -------------------------------------------------------------------------------- 1 | package net.println.kt10.java; 2 | 3 | /** 4 | * Created by benny on 11/13/16. 5 | */ 6 | public class LazyThreadSafeStaticInnerClass { 7 | 8 | private static class Holder{ 9 | private static LazyThreadSafeStaticInnerClass INSTANCE = new LazyThreadSafeStaticInnerClass(); 10 | } 11 | 12 | private LazyThreadSafeStaticInnerClass(){} 13 | 14 | public static LazyThreadSafeStaticInnerClass getInstance(){ 15 | return Holder.INSTANCE; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /legacy/code/Kt10/src/main/java/net/println/kt10/java/LazyThreadSafeSynchronized.java: -------------------------------------------------------------------------------- 1 | package net.println.kt10.java; 2 | 3 | /** 4 | * Created by benny on 11/13/16. 5 | */ 6 | public class LazyThreadSafeSynchronized { 7 | private static LazyThreadSafeSynchronized INSTANCE; 8 | 9 | private LazyThreadSafeSynchronized(){} 10 | 11 | public static synchronized LazyThreadSafeSynchronized getInstance(){ 12 | if(INSTANCE == null){ 13 | INSTANCE = new LazyThreadSafeSynchronized(); 14 | } 15 | return INSTANCE; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /legacy/code/Kt10/src/main/java/net/println/kt10/java/PlainOldSingleton.java: -------------------------------------------------------------------------------- 1 | package net.println.kt10.java; 2 | 3 | /** 4 | * Created by benny on 11/13/16. 5 | */ 6 | public class PlainOldSingleton { 7 | private static PlainOldSingleton INSTANCE = new PlainOldSingleton(); 8 | 9 | private PlainOldSingleton(){ 10 | System.out.println("PlainOldSingleton"); 11 | } 12 | 13 | public static PlainOldSingleton getInstance(){ 14 | return INSTANCE; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /legacy/code/Kt10/src/main/kotlin/net/println/kt10/kotlin/LazyNotThreadSafe.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt10.kotlin 2 | 3 | /** 4 | * Created by benny on 11/13/16. 5 | */ 6 | class LazyNotThreadSafe { 7 | 8 | companion object{ 9 | val instance by lazy(LazyThreadSafetyMode.NONE) { 10 | LazyNotThreadSafe() 11 | } 12 | 13 | //下面是另一种等价的写法, 获取单例使用 get 方法 14 | private var instance2: LazyNotThreadSafe? = null 15 | 16 | fun get() : LazyNotThreadSafe { 17 | if(instance2 == null){ 18 | instance2 = LazyNotThreadSafe() 19 | } 20 | return instance2!! 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /legacy/code/Kt10/src/main/kotlin/net/println/kt10/kotlin/LazyThreadSafeDoubleCheck.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt10.kotlin 2 | 3 | /** 4 | * Created by benny on 11/13/16. 5 | */ 6 | class LazyThreadSafeDoubleCheck private constructor(){ 7 | companion object{ 8 | val instance by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED){ 9 | LazyThreadSafeDoubleCheck() 10 | } 11 | 12 | private @Volatile var instance2: LazyThreadSafeDoubleCheck? = null 13 | 14 | fun get(): LazyThreadSafeDoubleCheck { 15 | if(instance2 == null){ 16 | synchronized(this){ 17 | if(instance2 == null) 18 | instance2 = LazyThreadSafeDoubleCheck() 19 | } 20 | } 21 | return instance2!! 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /legacy/code/Kt10/src/main/kotlin/net/println/kt10/kotlin/LazyThreadSafeStaticInnerObject.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt10.kotlin 2 | 3 | /** 4 | * Created by benny on 11/13/16. 5 | */ 6 | class LazyThreadSafeStaticInnerObject private constructor(){ 7 | companion object{ 8 | fun getInstance() = Holder.instance 9 | } 10 | 11 | private object Holder{ 12 | val instance = LazyThreadSafeStaticInnerObject() 13 | } 14 | } -------------------------------------------------------------------------------- /legacy/code/Kt10/src/main/kotlin/net/println/kt10/kotlin/LazyThreadSafeSynchronized.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt10.kotlin 2 | 3 | /** 4 | * Created by benny on 11/13/16. 5 | */ 6 | class LazyThreadSafeSynchronized private constructor() { 7 | companion object { 8 | private var instance: LazyThreadSafeSynchronized? = null 9 | 10 | @Synchronized 11 | fun get(): LazyThreadSafeSynchronized{ 12 | if(instance == null) instance = LazyThreadSafeSynchronized() 13 | return instance!! 14 | } 15 | } 16 | 17 | } -------------------------------------------------------------------------------- /legacy/code/Kt10/src/main/kotlin/net/println/kt10/kotlin/PlainOldSingleton.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt10.kotlin 2 | 3 | /** 4 | * Created by benny on 11/13/16. 5 | */ 6 | object PlainOldSingleton { 7 | 8 | } -------------------------------------------------------------------------------- /legacy/code/Kt11/build.gradle: -------------------------------------------------------------------------------- 1 | group 'net.println' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.0.4' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'kotlin' 17 | 18 | sourceCompatibility = 1.5 19 | 20 | repositories { 21 | jcenter() 22 | } 23 | 24 | dependencies { 25 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 26 | testCompile group: 'junit', name: 'junit', version: '4.11' 27 | } 28 | -------------------------------------------------------------------------------- /legacy/code/Kt11/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/code/Kt11/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /legacy/code/Kt11/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Nov 20 13:30:26 CST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip 7 | -------------------------------------------------------------------------------- /legacy/code/Kt11/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Kt11' 2 | 3 | -------------------------------------------------------------------------------- /legacy/code/Kt11/src/main/kotlin/net/println/kt11/Main.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt11 2 | 3 | /** 4 | * Created by benny on 11/27/16. 5 | */ 6 | fun main(args: Array) { 7 | val player: Player = Player() 8 | player.play("http://ws.stream.qqmusic.qq.com/C2000012Ppbd3hjGOK.m4a") 9 | player.pause() 10 | player.resume() 11 | player.seekTo(30000) 12 | player.stop() 13 | } -------------------------------------------------------------------------------- /legacy/code/Kt11/src/main/kotlin/net/println/kt11/Player.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt11 2 | 3 | import kotlin.properties.Delegates 4 | 5 | /** 6 | * Created by benny on 11/20/16. 7 | */ 8 | class Player { 9 | private var state: State by Delegates.observable(State.IDLE, { prop, old, new -> 10 | println("$old -> $new") 11 | onPlayerStateChangedListener?.onStateChanged(old, new) 12 | }) 13 | 14 | private fun sendCmd(cmd: PlayerCmd) { 15 | when (cmd) { 16 | is PlayerCmd.Play -> { 17 | println("\nPlay ${cmd.url} from ${cmd.position}ms") 18 | state = State.PLAYING 19 | doPlay(cmd.url, cmd.position) 20 | } 21 | is PlayerCmd.Resume -> { 22 | println("\nResume. ") 23 | state = State.PLAYING 24 | doResume() 25 | } 26 | is PlayerCmd.Pause -> { 27 | println("\nPause. ") 28 | state = State.PAUSED 29 | doPause() 30 | } 31 | is PlayerCmd.Stop -> { 32 | println("\nStop.") 33 | state = State.IDLE 34 | doStop() 35 | } 36 | is PlayerCmd.Seek -> { 37 | println("\nSeek to ${cmd.position}ms, state: $state") 38 | } 39 | } 40 | } 41 | 42 | private fun doPlay(url: String, position: Long) { 43 | TODO("Play function not yet implemented") 44 | } 45 | 46 | private fun doResume(){ 47 | //todo 48 | } 49 | 50 | private fun doPause() { 51 | //todo 52 | } 53 | 54 | private fun doStop() { 55 | //todo 56 | } 57 | 58 | //region api 59 | interface OnPlayerStateChangedListener { 60 | fun onStateChanged(oldState: State, new: State) 61 | } 62 | 63 | var onPlayerStateChangedListener: OnPlayerStateChangedListener? = null 64 | 65 | fun play(url: String, position: Long = 0) { 66 | sendCmd(PlayerCmd.Play(url, position)) 67 | } 68 | 69 | fun resume() { 70 | sendCmd(PlayerCmd.Resume) 71 | } 72 | 73 | fun pause() { 74 | sendCmd(PlayerCmd.Pause) 75 | } 76 | 77 | fun stop() { 78 | sendCmd(PlayerCmd.Stop) 79 | } 80 | 81 | fun seekTo(position: Long) { 82 | sendCmd(PlayerCmd.Seek(position)) 83 | } 84 | //endregion 85 | } 86 | -------------------------------------------------------------------------------- /legacy/code/Kt11/src/main/kotlin/net/println/kt11/PlayerCmd.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt11 2 | 3 | /** 4 | * Created by benny on 11/27/16. 5 | */ 6 | sealed class PlayerCmd { 7 | class Play(val url: String, val position: Long = 0): PlayerCmd() 8 | 9 | class Seek(val position: Long): PlayerCmd() 10 | 11 | object Pause: PlayerCmd() 12 | 13 | object Resume: PlayerCmd() 14 | 15 | object Stop: PlayerCmd() 16 | } -------------------------------------------------------------------------------- /legacy/code/Kt11/src/main/kotlin/net/println/kt11/State.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt11 2 | 3 | /** 4 | * Created by benny on 11/27/16. 5 | */ 6 | enum class State { 7 | IDLE, PAUSED, PLAYING 8 | } -------------------------------------------------------------------------------- /legacy/code/Kt12/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class Program 4 | { 5 | public static void Main(String[] args) 6 | { 7 | testGeneric(); 8 | } 9 | 10 | public static void testGeneric(){ 11 | Console.WriteLine(typeof(T)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /legacy/code/Kt12/build.gradle: -------------------------------------------------------------------------------- 1 | group 'net.println' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.0.4' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'kotlin' 17 | 18 | sourceCompatibility = 1.6 19 | targetCompatibility = 1.6 20 | repositories { 21 | jcenter() 22 | } 23 | 24 | dependencies { 25 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 26 | compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" 27 | compile "com.google.code.gson:gson:2.7" 28 | testCompile group: 'junit', name: 'junit', version: '4.11' 29 | } 30 | -------------------------------------------------------------------------------- /legacy/code/Kt12/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/code/Kt12/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /legacy/code/Kt12/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Nov 28 22:47:53 CST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip 7 | -------------------------------------------------------------------------------- /legacy/code/Kt12/result_singer.json: -------------------------------------------------------------------------------- 1 | { 2 | "code": 0, 3 | "message": "ok", 4 | "content": { 5 | "id": 1, 6 | "name": "Unknown", 7 | "songs": [ 8 | { 9 | "id": 0, 10 | "name": "Rada" 11 | }, 12 | { 13 | "id": 1, 14 | "name": "Olympic Dream" 15 | }, 16 | { 17 | "id": 2, 18 | "name": "The Escapist" 19 | } 20 | ] 21 | } 22 | } -------------------------------------------------------------------------------- /legacy/code/Kt12/result_singer_field_loss.json: -------------------------------------------------------------------------------- 1 | { 2 | "code": 0, 3 | "message": "ok", 4 | "content": { 5 | "id": 1 6 | } 7 | } -------------------------------------------------------------------------------- /legacy/code/Kt12/result_songs.json: -------------------------------------------------------------------------------- 1 | { 2 | "code": 0, 3 | "message": "ok", 4 | "content": [ 5 | { 6 | "id": 0, 7 | "name": "Rada" 8 | }, 9 | { 10 | "id": 1, 11 | "name": "Olympic Dream" 12 | }, 13 | { 14 | "id": 2, 15 | "name": "The Escapist" 16 | } 17 | ] 18 | } -------------------------------------------------------------------------------- /legacy/code/Kt12/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Kt12' 2 | 3 | -------------------------------------------------------------------------------- /legacy/code/Kt12/singer.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": 1, 3 | "name": "Unknown", 4 | "songs": [ 5 | { 6 | "id": 0, 7 | "name": "Rada" 8 | }, 9 | { 10 | "id": 1, 11 | "name": "Olympic Dream" 12 | }, 13 | { 14 | "id": 2, 15 | "name": "The Escapist" 16 | } 17 | ] 18 | } -------------------------------------------------------------------------------- /legacy/code/Kt12/src/main/java/net/println/kt12/Main.java: -------------------------------------------------------------------------------- 1 | package net.println.kt12; 2 | 3 | /** 4 | * Created by benny on 12/4/16. 5 | */ 6 | public class Main { 7 | public static void main(String... args) { 8 | 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /legacy/code/Kt12/src/main/kotlin/net/println/kt12/Api.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt12 2 | 3 | import com.google.gson.Gson 4 | import com.google.gson.reflect.TypeToken 5 | import java.lang.reflect.Proxy 6 | 7 | /** 8 | * Created by benny on 12/4/16. 9 | */ 10 | interface Api { 11 | fun getSingerFromJson(json: String): BaseResult 12 | } 13 | 14 | object ApiFactory { 15 | val api: Api by lazy { 16 | Proxy.newProxyInstance(ApiFactory.javaClass.classLoader, arrayOf(Api::class.java)) { 17 | proxy, method, args -> 18 | val responseType = method.genericReturnType 19 | val adapter = Gson().getAdapter(TypeToken.get(responseType)) 20 | adapter.fromJson(args[0].toString()) 21 | } as Api 22 | } 23 | } -------------------------------------------------------------------------------- /legacy/code/Kt12/src/main/kotlin/net/println/kt12/Data.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt12 2 | 3 | /** 4 | * Created by benny on 12/1/16. 5 | */ 6 | data class BaseResult(val code: Int, val message: String, val content: Content) 7 | 8 | data class Song(val id: Long, val name: String) 9 | 10 | data class Singer(val id: Long, val name: String, val songs: List) 11 | -------------------------------------------------------------------------------- /legacy/code/Kt12/src/main/kotlin/net/println/kt12/GsonExt.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt12 2 | 3 | import com.google.gson.Gson 4 | 5 | inline fun Gson.fromJson(json: String): T{ 6 | return fromJson(json, T::class.java) 7 | } -------------------------------------------------------------------------------- /legacy/code/Kt12/src/main/kotlin/net/println/kt12/Main.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt12 2 | 3 | import java.io.File 4 | 5 | /** 6 | * Created by benny on 12/1/16. 7 | */ 8 | fun main(args: Array) { 9 | val json = File("result_singer_field_loss.json").readText() 10 | val result : BaseResult = ApiFactory.api.getSingerFromJson(json) 11 | println(result.content.name.isEmpty()) 12 | } -------------------------------------------------------------------------------- /legacy/code/Kt13/build.gradle: -------------------------------------------------------------------------------- 1 | group 'net.println' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.0.5-2' 6 | ext.dagger_version = "2.8" 7 | ext.retrofit_version = "2.1.0" 8 | 9 | repositories { 10 | jcenter() 11 | } 12 | dependencies { 13 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 14 | } 15 | } 16 | 17 | apply plugin: 'kotlin' 18 | apply plugin: 'kotlin-kapt' 19 | 20 | sourceCompatibility = 1.5 21 | 22 | repositories { 23 | jcenter() 24 | } 25 | 26 | sourceSets { 27 | main.java.srcDirs += [file("$buildDir/generated/source/kapt2/main")] 28 | } 29 | 30 | dependencies { 31 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 32 | 33 | compile "com.squareup.retrofit2:retrofit:$retrofit_version" 34 | compile "com.squareup.retrofit2:converter-gson:$retrofit_version" 35 | compile "com.squareup.retrofit2:adapter-rxjava:$retrofit_version" 36 | 37 | compile "com.google.dagger:dagger:${dagger_version}" 38 | kapt "com.google.dagger:dagger-compiler:${dagger_version}" 39 | 40 | testCompile group: 'junit', name: 'junit', version: '4.11' 41 | } 42 | -------------------------------------------------------------------------------- /legacy/code/Kt13/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/code/Kt13/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /legacy/code/Kt13/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Nov 28 22:47:53 CST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip 7 | -------------------------------------------------------------------------------- /legacy/code/Kt13/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Kt13' 2 | 3 | -------------------------------------------------------------------------------- /legacy/code/Kt13/src/main/kotlin/net/println/kt13/RESTFulComponent.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt13 2 | 3 | import dagger.Component 4 | import net.println.kt13.module.RetrofitModule 5 | import retrofit2.Retrofit 6 | import javax.inject.Singleton 7 | 8 | /** 9 | * Created by benny on 12/11/16. 10 | */ 11 | @Singleton 12 | @Component(modules = arrayOf(RetrofitModule::class)) 13 | interface RESTFulComponent { 14 | fun retrofit(): Retrofit 15 | } -------------------------------------------------------------------------------- /legacy/code/Kt13/src/main/kotlin/net/println/kt13/Service.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt08 2 | 3 | import net.println.kt13.DaggerRESTFulComponent 4 | import retrofit2.Call 5 | import retrofit2.http.GET 6 | import retrofit2.http.Query 7 | 8 | /** 9 | * Created by benny on 11/1/16. 10 | */ 11 | interface GitHubService{ 12 | 13 | @GET("/repos/enbandari/Kotlin-Tutorials/stargazers") 14 | fun getStarGazers(@Query("page") page: Int = 1, @Query("per_page") pageSize: Int = 20): Call> 15 | } 16 | 17 | object Service { 18 | val api: GitHubService by lazy { 19 | DaggerRESTFulComponent 20 | .builder() 21 | .build() 22 | .retrofit() 23 | .create(GitHubService::class.java) 24 | } 25 | } 26 | 27 | fun main(args: Array) { 28 | Service.api.getStarGazers().execute().body().map(::println) 29 | } -------------------------------------------------------------------------------- /legacy/code/Kt13/src/main/kotlin/net/println/kt13/User.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt08 2 | 3 | /** 4 | * Created by benny on 11/1/16. 5 | */ 6 | data class User(val login: String, val id: Long, val avatar_url: String){ 7 | override fun toString(): String { 8 | return login 9 | } 10 | } -------------------------------------------------------------------------------- /legacy/code/Kt13/src/main/kotlin/net/println/kt13/config/Settings.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt13.config 2 | 3 | /** 4 | * Created by benny on 12/11/16. 5 | */ 6 | object Settings { 7 | 8 | const val DEBUG = true 9 | 10 | } -------------------------------------------------------------------------------- /legacy/code/Kt13/src/main/kotlin/net/println/kt13/module/BaseUrlModule.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt13.module 2 | 3 | import dagger.Module 4 | import dagger.Provides 5 | import net.println.kt13.config.Settings 6 | import javax.inject.Singleton 7 | 8 | /** 9 | * Created by benny on 12/11/16. 10 | */ 11 | @Module 12 | class BaseUrlModule { 13 | companion object{ 14 | //测试环境 15 | const val DEBUG_URL = "https://api.github.com" 16 | 17 | //线上环境 18 | const val RELEASE_URL = "https://api.github.com" 19 | } 20 | 21 | @Singleton @Provides fun baseUrl(): String = if(Settings.DEBUG) DEBUG_URL else RELEASE_URL 22 | } -------------------------------------------------------------------------------- /legacy/code/Kt13/src/main/kotlin/net/println/kt13/module/CacheModule.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt13.module 2 | 3 | import dagger.Module 4 | import dagger.Provides 5 | import okhttp3.Cache 6 | import java.io.File 7 | import javax.inject.Singleton 8 | 9 | /** 10 | * Created by benny on 12/11/16. 11 | */ 12 | @Module 13 | class CacheModule { 14 | companion object{ 15 | const val CACHE_DIR = "./cache" 16 | const val CACHE_SIZE = 10 * 1024 * 1024L 17 | } 18 | 19 | @Singleton @Provides fun cache(): Cache = Cache(File(CACHE_DIR), CACHE_SIZE) 20 | } -------------------------------------------------------------------------------- /legacy/code/Kt13/src/main/kotlin/net/println/kt13/module/GsonConverterModule.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt13.module 2 | 3 | import com.google.gson.Gson 4 | import dagger.Module 5 | import dagger.Provides 6 | import retrofit2.Converter 7 | import retrofit2.converter.gson.GsonConverterFactory 8 | import javax.inject.Singleton 9 | 10 | /** 11 | * Created by benny on 12/11/16. 12 | */ 13 | @Module(includes = arrayOf(GsonModule::class)) 14 | class GsonConverterModule { 15 | @Singleton @Provides fun converter(gson: Gson): Converter.Factory = GsonConverterFactory.create(gson) 16 | } -------------------------------------------------------------------------------- /legacy/code/Kt13/src/main/kotlin/net/println/kt13/module/GsonModule.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt13.module 2 | 3 | import com.google.gson.Gson 4 | import dagger.Module 5 | import dagger.Provides 6 | import javax.inject.Singleton 7 | 8 | /** 9 | * Created by benny on 12/11/16. 10 | */ 11 | @Module 12 | class GsonModule { 13 | 14 | @Singleton @Provides fun gson(): Gson = Gson() 15 | } -------------------------------------------------------------------------------- /legacy/code/Kt13/src/main/kotlin/net/println/kt13/module/OkHttpClientModule.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt13.module 2 | 3 | import dagger.Module 4 | import dagger.Provides 5 | import okhttp3.Cache 6 | import okhttp3.OkHttpClient 7 | import javax.inject.Singleton 8 | 9 | /** 10 | * Created by benny on 12/11/16. 11 | */ 12 | @Module(includes = arrayOf(CacheModule::class)) 13 | class OkHttpClientModule { 14 | @Singleton @Provides fun okHttpClient(cache: Cache): OkHttpClient 15 | = OkHttpClient.Builder().cache(cache).build() 16 | } -------------------------------------------------------------------------------- /legacy/code/Kt13/src/main/kotlin/net/println/kt13/module/RetrofitModule.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt13.module 2 | 3 | import dagger.Module 4 | import dagger.Provides 5 | import okhttp3.OkHttpClient 6 | import retrofit2.CallAdapter 7 | import retrofit2.Converter 8 | import retrofit2.Retrofit 9 | import javax.inject.Singleton 10 | 11 | /** 12 | * Created by benny on 12/11/16. 13 | */ 14 | @Module(includes = arrayOf(GsonConverterModule::class, OkHttpClientModule::class, RxAdapterModule::class, BaseUrlModule::class)) 15 | class RetrofitModule { 16 | @Singleton @Provides fun retrofit( 17 | baseUrl: String, 18 | okHttpClient: OkHttpClient, 19 | adapterFactory: CallAdapter.Factory, 20 | converterFactory: Converter.Factory): Retrofit 21 | = Retrofit.Builder() 22 | .baseUrl(baseUrl) 23 | .addCallAdapterFactory(adapterFactory) 24 | .addConverterFactory(converterFactory) 25 | .client(okHttpClient).build() 26 | } -------------------------------------------------------------------------------- /legacy/code/Kt13/src/main/kotlin/net/println/kt13/module/RxAdapterModule.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt13.module 2 | 3 | import dagger.Module 4 | import dagger.Provides 5 | import retrofit2.CallAdapter 6 | import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory 7 | import javax.inject.Singleton 8 | 9 | /** 10 | * Created by benny on 12/11/16. 11 | */ 12 | @Module 13 | class RxAdapterModule { 14 | @Singleton @Provides fun adapter(): CallAdapter.Factory = RxJavaCallAdapterFactory.create() 15 | } -------------------------------------------------------------------------------- /legacy/code/Kt14/Kt14/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'kotlin' 3 | 4 | sourceCompatibility = 1.5 5 | 6 | dependencies { 7 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 8 | testCompile group: 'junit', name: 'junit', version: '4.11' 9 | } 10 | -------------------------------------------------------------------------------- /legacy/code/Kt14/Kt14/src/main/java/net/println/kt14/AccessToObject.java: -------------------------------------------------------------------------------- 1 | package net.println.kt14; 2 | 3 | /** 4 | * Created by benny on 12/18/16. 5 | */ 6 | public class AccessToObject { 7 | public static void main(String... args) { 8 | Singleton.INSTANCE.printlnHello(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /legacy/code/Kt14/Kt14/src/main/java/net/println/kt14/AccessToOverloads.java: -------------------------------------------------------------------------------- 1 | package net.println.kt14; 2 | 3 | /** 4 | * Created by benny on 12/18/16. 5 | */ 6 | public class AccessToOverloads { 7 | public static void main(String... args) { 8 | Overloads overloads = new Overloads(); 9 | overloads.overloaded(1, 2, 3); 10 | overloads.overloaded(1); 11 | overloads.overloaded(1,3); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /legacy/code/Kt14/Kt14/src/main/java/net/println/kt14/CallExtenstionMethod.java: -------------------------------------------------------------------------------- 1 | package net.println.kt14; 2 | 3 | /** 4 | * Created by benny on 12/17/16. 5 | */ 6 | public class CallExtenstionMethod { 7 | public static void main(String... args) { 8 | System.out.println(ExtensionMethodKt.notEmpty("Hello")); 9 | } 10 | } -------------------------------------------------------------------------------- /legacy/code/Kt14/Kt14/src/main/java/net/println/kt14/CallInternalClass.java: -------------------------------------------------------------------------------- 1 | package net.println.kt14; 2 | 3 | /** 4 | * Created by benny on 12/17/16. 5 | */ 6 | 7 | public class CallInternalClass{ 8 | public static void main(String... args) { 9 | InternalClass internalClass = new InternalClass(); 10 | internalClass.printlnHello$production_sources_for_module_Kt14_Kt14_main(); 11 | } 12 | } -------------------------------------------------------------------------------- /legacy/code/Kt14/Kt14/src/main/java/net/println/kt14/CallPackageMethod.java: -------------------------------------------------------------------------------- 1 | package net.println.kt14; 2 | 3 | /** 4 | * Created by benny on 12/17/16. 5 | */ 6 | public class CallPackageMethod{ 7 | public static void main(String... args) { 8 | PackageKt.printlnHello(); 9 | } 10 | } -------------------------------------------------------------------------------- /legacy/code/Kt14/Kt14/src/main/java/net/println/kt14/PersonMain.java: -------------------------------------------------------------------------------- 1 | package net.println.kt14; 2 | 3 | /** 4 | * Created by benny on 12/17/16. 5 | */ 6 | public class PersonMain { 7 | public static void main(String... args) { 8 | Person person = new Person("benny", 27); 9 | System.out.println(person.getName() + " is " + person.age); 10 | person.setName("andy"); 11 | person.age = 26; 12 | System.out.println(person.getName() + " is " + person.age); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /legacy/code/Kt14/Kt14/src/main/kotlin/net/println/kt14/CallInternalClass.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt14 2 | 3 | /** 4 | * Created by benny on 12/17/16. 5 | */ 6 | fun main(args: Array) { 7 | val internalClass = InternalClass() 8 | internalClass.printlnHello() 9 | } -------------------------------------------------------------------------------- /legacy/code/Kt14/Kt14/src/main/kotlin/net/println/kt14/ExtensionMethod.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt14 2 | 3 | /** 4 | * Created by benny on 12/17/16. 5 | */ 6 | fun String.notEmpty(): Boolean{ 7 | return this != "" 8 | } -------------------------------------------------------------------------------- /legacy/code/Kt14/Kt14/src/main/kotlin/net/println/kt14/InternalClass.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt14 2 | 3 | /** 4 | * Created by benny on 12/17/16. 5 | */ 6 | internal class InternalClass{ 7 | internal fun printlnHello(){ 8 | println("Hello") 9 | } 10 | } -------------------------------------------------------------------------------- /legacy/code/Kt14/Kt14/src/main/kotlin/net/println/kt14/Overloads.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt14 2 | 3 | /** 4 | * Created by benny on 12/18/16. 5 | */ 6 | class Overloads { 7 | @JvmOverloads 8 | fun overloaded(a: Int, b: Int = 0, c: Int = 1){ 9 | println("$a, $b, $c") 10 | } 11 | } -------------------------------------------------------------------------------- /legacy/code/Kt14/Kt14/src/main/kotlin/net/println/kt14/Package.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt14 2 | 3 | /** 4 | * Created by benny on 12/17/16. 5 | */ 6 | fun printlnHello(){ 7 | println("Hello") 8 | } 9 | 10 | fun main(args: Array) { 11 | 12 | } -------------------------------------------------------------------------------- /legacy/code/Kt14/Kt14/src/main/kotlin/net/println/kt14/Person.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt14 2 | 3 | /** 4 | * Created by benny on 12/17/16. 5 | */ 6 | data class Person(var name:String, @JvmField var age: Int) -------------------------------------------------------------------------------- /legacy/code/Kt14/Kt14/src/main/kotlin/net/println/kt14/Singleton.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt14 2 | 3 | /** 4 | * Created by benny on 12/17/16. 5 | */ 6 | object Singleton { 7 | fun printlnHello(){ 8 | println("Hello") 9 | } 10 | } -------------------------------------------------------------------------------- /legacy/code/Kt14/build.gradle: -------------------------------------------------------------------------------- 1 | group 'net.println' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.0.5-2' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | mavenCentral() 18 | } 19 | } -------------------------------------------------------------------------------- /legacy/code/Kt14/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/code/Kt14/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /legacy/code/Kt14/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Dec 17 10:32:19 CST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip 7 | -------------------------------------------------------------------------------- /legacy/code/Kt14/kt14internal/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'kotlin' 3 | 4 | dependencies { 5 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 6 | compile project(":Kt14") 7 | testCompile group: 'junit', name: 'junit', version: '4.11' 8 | } 9 | -------------------------------------------------------------------------------- /legacy/code/Kt14/kt14internal/src/main/java/net/println/kt14internal/CallInternalClass.java: -------------------------------------------------------------------------------- 1 | package net.println.kt14internal; 2 | 3 | import net.println.kt14.InternalClass; 4 | 5 | /** 6 | * Created by benny on 12/17/16. 7 | */ 8 | public class CallInternalClass { 9 | public static void main(String... args) { 10 | InternalClass internalClass = new InternalClass(); 11 | internalClass.printlnHello$production_sources_for_module_Kt14_Kt14_main(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /legacy/code/Kt14/kt14internal/src/main/kotlin/net/println/kt14internal/CallInternalClassKt.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt14internal 2 | 3 | /** 4 | * Created by benny on 12/17/16. 5 | */ 6 | fun main(args: Array) { 7 | // val internalClass = InternalClass() 8 | // internalClass.printHello() 9 | } 10 | -------------------------------------------------------------------------------- /legacy/code/Kt14/settings.gradle: -------------------------------------------------------------------------------- 1 | include ':Kt14', ':kt14internal' -------------------------------------------------------------------------------- /legacy/code/Kt15/build.gradle: -------------------------------------------------------------------------------- 1 | group 'net.println' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.0.5-2' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | } 13 | } 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'kotlin' 17 | 18 | sourceCompatibility = 1.5 19 | 20 | repositories { 21 | mavenCentral() 22 | } 23 | 24 | dependencies { 25 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 26 | testCompile group: 'junit', name: 'junit', version: '4.11' 27 | } 28 | -------------------------------------------------------------------------------- /legacy/code/Kt15/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/code/Kt15/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /legacy/code/Kt15/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Dec 17 21:40:38 CST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip 7 | -------------------------------------------------------------------------------- /legacy/code/Kt15/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Kt15' 2 | 3 | -------------------------------------------------------------------------------- /legacy/code/Kt15/src/main/java/net/println/kt15/DataClass.java: -------------------------------------------------------------------------------- 1 | package net.println.kt15; 2 | 3 | /** 4 | * Created by benny on 12/17/16. 5 | */ 6 | public class DataClass { 7 | private int id; 8 | 9 | public int getId() { 10 | return id; 11 | } 12 | 13 | public void setId(int id) { 14 | this.id = id; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /legacy/code/Kt15/src/main/java/net/println/kt15/Generics.java: -------------------------------------------------------------------------------- 1 | package net.println.kt15; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by benny on 12/17/16. 8 | */ 9 | public class Generics { 10 | 11 | public abstract class View

{ 12 | P presenter; 13 | } 14 | 15 | public abstract class Presenter{ 16 | V view; 17 | } 18 | 19 | public static void main(String... args) { 20 | List list = new ArrayList(); 21 | list.add("Hello"); 22 | list.add(0); 23 | for (Object o : list) { 24 | System.out.println(o); 25 | } 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /legacy/code/Kt15/src/main/java/net/println/kt15/NullSafetyAbsClass.java: -------------------------------------------------------------------------------- 1 | package net.println.kt15; 2 | 3 | import org.jetbrains.annotations.NotNull; 4 | 5 | import java.text.SimpleDateFormat; 6 | import java.util.Date; 7 | 8 | /** 9 | * Created by benny on 12/17/16. 10 | */ 11 | public abstract class NullSafetyAbsClass { 12 | public abstract String formatDate(Date date); 13 | 14 | public @NotNull String formatTime(@NotNull Date date){ 15 | return new SimpleDateFormat("HH:mm:ss").format(date); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /legacy/code/Kt15/src/main/java/net/println/kt15/NullSafetyJava.java: -------------------------------------------------------------------------------- 1 | package net.println.kt15; 2 | 3 | /** 4 | * Created by benny on 12/17/16. 5 | */ 6 | public class NullSafetyJava { 7 | public String getData(){ 8 | return null; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /legacy/code/Kt15/src/main/java/net/println/kt15/SAMInJava.java: -------------------------------------------------------------------------------- 1 | package net.println.kt15; 2 | 3 | import java.util.ArrayList; 4 | 5 | /** 6 | * Created by benny on 12/17/16. 7 | */ 8 | public class SAMInJava { 9 | 10 | private ArrayList runnables = new ArrayList(); 11 | 12 | public void addTask(Runnable runnable){ 13 | runnables.add(runnable); 14 | System.out.println("After add: " + runnable + ", we have " + runnables.size() + " in all."); 15 | } 16 | 17 | public void removeTask(Runnable runnable){ 18 | runnables.remove(runnable); 19 | System.out.println("After remove: " + runnable + ", only " + runnables.size() + " left."); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /legacy/code/Kt15/src/main/kotlin/net/println/kt15/AccessDataClass.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt15 2 | 3 | /** 4 | * Created by benny on 12/17/16. 5 | */ 6 | fun main(args: Array) { 7 | val dataClass = DataClass() 8 | dataClass.id = 0 9 | println(dataClass.id) 10 | } -------------------------------------------------------------------------------- /legacy/code/Kt15/src/main/kotlin/net/println/kt15/ConcurrentRelated.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt15 2 | 3 | /** 4 | * Created by benny on 12/17/16. 5 | */ 6 | @Volatile var count: Int = 0 7 | 8 | fun count(){ 9 | synchronized(count){ 10 | count++ 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /legacy/code/Kt15/src/main/kotlin/net/println/kt15/GenericsInKt.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt15 2 | 3 | import java.util.* 4 | 5 | /** 6 | * Created by benny on 12/17/16. 7 | */ 8 | //Kotlin 无法写出这样的代码 9 | //abstract class View

>>{ 10 | // protected abstract val presenter: P 11 | //} 12 | // 13 | //abstract class Presenter>>{ 14 | // protected abstract val view: V 15 | //} 16 | 17 | fun main(args: Array) { 18 | val list = ArrayList() 19 | list.add("Hello") 20 | list.add(0) 21 | list.map(::println) 22 | } -------------------------------------------------------------------------------- /legacy/code/Kt15/src/main/kotlin/net/println/kt15/NullSafety.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt15 2 | 3 | /** 4 | * Created by benny on 12/17/16. 5 | */ 6 | fun main(args: Array) { 7 | val nullSafetyJava = NullSafetyJava() 8 | val data: String = nullSafetyJava.data 9 | val dataCanBeNull: String? = nullSafetyJava.data 10 | println(data) 11 | } -------------------------------------------------------------------------------- /legacy/code/Kt15/src/main/kotlin/net/println/kt15/NullSafetySubClass.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt15 2 | 3 | import java.util.* 4 | 5 | /** 6 | * Created by benny on 12/17/16. 7 | */ 8 | class NullSafetySubClass : NullSafetyAbsClass(){ 9 | override fun formatDate(date: Date): String { 10 | return date.toString() 11 | } 12 | } 13 | 14 | fun main(args: Array) { 15 | val nullSafetySubClass = NullSafetySubClass() 16 | println(nullSafetySubClass.formatDate(Date())) 17 | println(nullSafetySubClass.formatTime(Date())) 18 | } -------------------------------------------------------------------------------- /legacy/code/Kt15/src/main/kotlin/net/println/kt15/SAMConversion.kt: -------------------------------------------------------------------------------- 1 | package net.println.kt15 2 | 3 | /** 4 | * Created by benny on 12/17/16. 5 | */ 6 | fun main(args: Array) { 7 | val samInJava = SAMInJava() 8 | val lambda = { 9 | println("Hello") 10 | } 11 | samInJava.addTask(lambda) 12 | samInJava.addTask(lambda) 13 | samInJava.addTask(lambda) 14 | samInJava.addTask(lambda) 15 | samInJava.addTask(lambda) 16 | 17 | samInJava.removeTask(lambda) 18 | samInJava.removeTask(lambda) 19 | samInJava.removeTask(lambda) 20 | samInJava.removeTask(lambda) 21 | samInJava.removeTask(lambda) 22 | } -------------------------------------------------------------------------------- /legacy/code/MyBatisIssue/build.gradle: -------------------------------------------------------------------------------- 1 | group 'net.println.kotlin' 2 | version '1.0-SNAPSHOT' 3 | 4 | buildscript { 5 | ext.kotlin_version = '1.1.0' 6 | 7 | repositories { 8 | mavenCentral() 9 | } 10 | dependencies { 11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 12 | classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version" 13 | } 14 | } 15 | 16 | apply plugin: 'java' 17 | apply plugin: 'kotlin' 18 | apply plugin: "kotlin-noarg" 19 | 20 | noArg{ 21 | annotation("net.println.kotlin.mybatis.annotations.PoKo") 22 | } 23 | 24 | sourceCompatibility = 1.5 25 | 26 | repositories { 27 | mavenCentral() 28 | } 29 | 30 | dependencies { 31 | compile fileTree(include: ['*.jar'], dir: 'libs') 32 | compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version" 33 | compile "org.mybatis:mybatis:3.4.2" 34 | testCompile group: 'junit', name: 'junit', version: '4.11' 35 | } 36 | -------------------------------------------------------------------------------- /legacy/code/MyBatisIssue/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/code/MyBatisIssue/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /legacy/code/MyBatisIssue/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Mar 25 09:41:11 CST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.1-bin.zip 7 | -------------------------------------------------------------------------------- /legacy/code/MyBatisIssue/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /legacy/code/MyBatisIssue/libs/mysql-connector-java-5.1.40-bin.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bennyhuo/Kotlin-Tutorials/b5fde39a472a7a2ddcc3f4d6e2ae5dc6c48bad07/legacy/code/MyBatisIssue/libs/mysql-connector-java-5.1.40-bin.jar -------------------------------------------------------------------------------- /legacy/code/MyBatisIssue/settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'MyBatisIssue' 2 | 3 | -------------------------------------------------------------------------------- /legacy/code/MyBatisIssue/src/main/kotlin/net/println/kotlin/mybatis/Main.java: -------------------------------------------------------------------------------- 1 | package net.println.kotlin.mybatis; 2 | 3 | /** 4 | * Created by benny on 3/26/17. 5 | */ 6 | public class Main { 7 | public static void main(String... args) { 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /legacy/code/MyBatisIssue/src/main/kotlin/net/println/kotlin/mybatis/Main.kt: -------------------------------------------------------------------------------- 1 | package net.println.kotlin.mybatis 2 | 3 | import org.apache.ibatis.io.Resources 4 | import org.apache.ibatis.session.SqlSessionFactoryBuilder 5 | 6 | 7 | /** 8 | * Created by benny on 3/25/17. 9 | */ 10 | fun main(args: Array) { 11 | val resource = "net/println/kotlin/mybatis/config.xml" 12 | val inputStream = Resources.getResourceAsStream(resource) 13 | val sqlSessionFactory = SqlSessionFactoryBuilder().build(inputStream) 14 | val session = sqlSessionFactory.openSession() 15 | session.use { session -> 16 | val mapper = session.getMapper(UserMapper::class.java) 17 | val user = mapper.selectUser(1) 18 | println(user) 19 | } 20 | } -------------------------------------------------------------------------------- /legacy/code/MyBatisIssue/src/main/kotlin/net/println/kotlin/mybatis/TestUnsafe.kt: -------------------------------------------------------------------------------- 1 | package net.println.kotlin.mybatis 2 | 3 | import sun.misc.Unsafe 4 | 5 | /** 6 | * Created by benny on 3/26/17. 7 | */ 8 | 9 | class Test{ 10 | init { 11 | println("init") 12 | } 13 | 14 | companion object{ 15 | init { 16 | println("cinit") 17 | } 18 | } 19 | } 20 | 21 | fun main(args: Array) { 22 | val field = Unsafe::class.java.getDeclaredField("theUnsafe") 23 | field.isAccessible = true 24 | val unsafe = field.get(null) as Unsafe 25 | unsafe.allocateInstance(Test::class.java) 26 | 27 | } -------------------------------------------------------------------------------- /legacy/code/MyBatisIssue/src/main/kotlin/net/println/kotlin/mybatis/User.kt: -------------------------------------------------------------------------------- 1 | package net.println.kotlin.mybatis 2 | 3 | import net.println.kotlin.mybatis.annotations.PoKo 4 | 5 | /** 6 | * Created by benny on 3/25/17. 7 | */ 8 | @PoKo 9 | data class User (var id: Int, var username: String = "", var age: Int, var passwd: String = "") 10 | 11 | fun main(args: Array) { 12 | //println(User::class.java.newInstance()) 13 | } -------------------------------------------------------------------------------- /legacy/code/MyBatisIssue/src/main/kotlin/net/println/kotlin/mybatis/UserMapper.java: -------------------------------------------------------------------------------- 1 | package net.println.kotlin.mybatis; 2 | 3 | /** 4 | * Created by benny on 3/25/17. 5 | */ 6 | //@Mapper 7 | public interface UserMapper { 8 | // @Select("SELECT * FROM userinfo WHERE id = #{id}") 9 | User selectUser(int id); 10 | } -------------------------------------------------------------------------------- /legacy/code/MyBatisIssue/src/main/kotlin/net/println/kotlin/mybatis/annotations/PoKo.kt: -------------------------------------------------------------------------------- 1 | package net.println.kotlin.mybatis.annotations 2 | 3 | /** 4 | * Created by benny on 3/25/17. 5 | */ 6 | annotation class PoKo -------------------------------------------------------------------------------- /legacy/code/MyBatisIssue/src/main/resources/net/println/kotlin/mybatis/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 9 | -------------------------------------------------------------------------------- /legacy/code/MyBatisIssue/src/main/resources/net/println/kotlin/mybatis/config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /legacy/code/MyBatisIssue/src/main/resources/net/println/kotlin/mybatis/db.properties: -------------------------------------------------------------------------------- 1 | driver=com.mysql.jdbc.Driver 2 | url=jdbc:mysql://localhost:3306/User 3 | username=root 4 | password= -------------------------------------------------------------------------------- /legacy/code/MyBatisIssue/src/main/resources/net/println/kotlin/mybatis/userinfo.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE userinfo 2 | ( 3 | id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT, 4 | username VARCHAR(45), 5 | age INT(11), 6 | passwd VARCHAR(45) 7 | ); -------------------------------------------------------------------------------- /legacy/code/RealmIssue/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /legacy/code/RealmIssue/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /legacy/code/RealmIssue/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | apply plugin: 'realm-android' 5 | apply plugin: "kotlin-allopen" 6 | apply plugin: "kotlin-noarg" 7 | 8 | android { 9 | compileSdkVersion 25 10 | buildToolsVersion "25.0.2" 11 | defaultConfig { 12 | applicationId "net.println.kotlin.realm" 13 | minSdkVersion 16 14 | targetSdkVersion 25 15 | versionCode 1 16 | versionName "1.0" 17 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 18 | } 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | } 26 | 27 | allOpen { 28 | annotation("net.println.kotlin.realm.PoKo") 29 | } 30 | 31 | noArg { 32 | annotation("net.println.kotlin.realm.PoKo") 33 | } 34 | 35 | 36 | dependencies { 37 | compile fileTree(dir: 'libs', include: ['*.jar']) 38 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 39 | exclude group: 'com.android.support', module: 'support-annotations' 40 | }) 41 | compile 'com.android.support:appcompat-v7:25.3.1' 42 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 43 | testCompile 'junit:junit:4.12' 44 | compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" 45 | } 46 | -------------------------------------------------------------------------------- /legacy/code/RealmIssue/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Users\kanade\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /legacy/code/RealmIssue/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /legacy/code/RealmIssue/app/src/main/java/net/println/kotlin/realm/App.kt: -------------------------------------------------------------------------------- 1 | package net.println.kotlin.realm 2 | 3 | import android.app.Application 4 | 5 | import io.realm.Realm 6 | import io.realm.RealmConfiguration 7 | 8 | class App : Application() { 9 | override fun onCreate() { 10 | super.onCreate() 11 | Realm.init(this) 12 | Realm.setDefaultConfiguration( 13 | RealmConfiguration.Builder() 14 | .deleteRealmIfMigrationNeeded() 15 | .schemaVersion(1) 16 | .build()) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /legacy/code/RealmIssue/app/src/main/java/net/println/kotlin/realm/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package net.println.kotlin.realm 2 | 3 | import android.os.Bundle 4 | import android.support.v7.app.AppCompatActivity 5 | import android.util.Log 6 | import io.realm.Realm 7 | import kotlinx.android.synthetic.main.activity_main.* 8 | 9 | class MainActivity : AppCompatActivity() { 10 | companion object{ 11 | const val TAG = "KotlinRealm" 12 | } 13 | 14 | override fun onCreate(savedInstanceState: Bundle?) { 15 | super.onCreate(savedInstanceState) 16 | setContentView(R.layout.activity_main) 17 | add.setOnClickListener { 18 | Realm.getDefaultInstance().use { 19 | it.beginTransaction() 20 | val d = it.createObject(User::class.java, it.where(User::class.java).count()) 21 | d.name = "User ${d.id}" 22 | it.commitTransaction() 23 | } 24 | 25 | } 26 | 27 | query.setOnClickListener { 28 | Realm.getDefaultInstance().use { 29 | it.where(User::class.java).findAll().map { 30 | Log.d(TAG, it.toString()) 31 | } 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /legacy/code/RealmIssue/app/src/main/java/net/println/kotlin/realm/PoKo.kt: -------------------------------------------------------------------------------- 1 | package net.println.kotlin.realm 2 | 3 | annotation class PoKo 4 | -------------------------------------------------------------------------------- /legacy/code/RealmIssue/app/src/main/java/net/println/kotlin/realm/User.kt: -------------------------------------------------------------------------------- 1 | package net.println.kotlin.realm 2 | 3 | import io.realm.RealmObject 4 | import io.realm.annotations.PrimaryKey 5 | 6 | @PoKo 7 | data class User(@PrimaryKey var id: Int, var name: String) : RealmObject() -------------------------------------------------------------------------------- /legacy/code/RealmIssue/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 |