From 2167439b7a1a59f963eb8c1c88174027bb0d2a3d Mon Sep 17 00:00:00 2001 From: Jieyi Date: Sat, 21 Jul 2018 16:47:16 +0900 Subject: [PATCH] Feature: added the casting feature. --- README.md | 23 +++++++------------ gradle.properties | 4 ++-- .../java/com/devrapid/kotlinshaver/Casting.kt | 13 +++++++++++ 3 files changed, 23 insertions(+), 17 deletions(-) create mode 100644 kotlinshaver/src/main/java/com/devrapid/kotlinshaver/Casting.kt diff --git a/README.md b/README.md index 6e102a7..d637f63 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ The library has util tools as below: 6. String 7. Thread 8. Time +9. Casting # NOTE @@ -87,9 +88,9 @@ allprojects { And add our dependency to your app `bundle.gradle`. ```gradle -implementation 'com.devrapid.jieyi:kotlinknifer:1.5.7' +implementation 'com.devrapid.jieyi:kotlinknifer:2.0.1' -implementation 'com.devrapid.jieyi:kotlinshaver:0.2.2' +implementation 'com.devrapid.jieyi:kotlinshaver:1.0.1' ``` Then you can use it!!! @@ -100,14 +101,14 @@ Then you can use it!!! com.devrapid.jieyi kotlinknifer - 1.5.7 + 2.0.1 pom com.devrapid.jieyi kotlinshaver - 0.2.2 + 1.0.1 pom ``` @@ -120,6 +121,7 @@ Then you can use it!!! 2. Glide 4.6.1 ## kotlinshaver +### For Androidx 1. RxJava 2.1.10 2. RxKotlin 2.2.0 @@ -127,16 +129,7 @@ Then you can use it!!! If you'd not like to use them to your project, you can add the exclude as like below ```gradle -implementation('com.devrapid.jieyi:kotlinknifer:1.5.7', { - exclude group: 'com.google.code.gson', module: 'gson' - exclude group: 'com.github.bumptech.glide', module: 'glide' -}) -``` - -### For Androidx - -```gradle -implementation('com.devrapid.jieyi:kotlinknifer:2.0.0', { +implementation('com.devrapid.jieyi:kotlinknifer:2.0.1', { exclude group: 'com.google.code.gson', module: 'gson' exclude group: 'com.github.bumptech.glide', module: 'glide' }) @@ -159,7 +152,7 @@ Please let me know, thank you! # License ``` -Copyright (C) 2017 Jieyi Wu +Copyright (C) 2018 Jieyi Wu Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/gradle.properties b/gradle.properties index 0c76ed0..2185fc2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -21,7 +21,7 @@ proj_name=kotlinknifer proj_libname=KotlinKnifer proj_group=com.devrapid.jieyi proj_artifactid=kotlinknifer -proj_version=2.0.0 +proj_version=2.0.1 proj_description=For developing an Android conveniently and rapidly. proj_websiteurl=https://github.com/pokk/KotlinKnifer proj_issuetrackerurl=https://github.com/pokk/KotlinKnifer/issues @@ -39,4 +39,4 @@ developer_email=pokkbaby@gmail.com proj_kotlin_name=kotlinshaver proj_kotlin_libname=KotlinShaver proj_kotlin_artifactid=kotlinshaver -proj_kotlin_version=1.0.0 +proj_kotlin_version=1.0.1 diff --git a/kotlinshaver/src/main/java/com/devrapid/kotlinshaver/Casting.kt b/kotlinshaver/src/main/java/com/devrapid/kotlinshaver/Casting.kt new file mode 100644 index 0000000..9df1da3 --- /dev/null +++ b/kotlinshaver/src/main/java/com/devrapid/kotlinshaver/Casting.kt @@ -0,0 +1,13 @@ +package com.devrapid.kotlinshaver + +inline fun castOrNull(from: Any?) = from as? T +inline fun cast(from: Any?): T { + if (null == from) throw NullPointerException("Null object can't cast.") + + try { + return T::class.java.cast(from) + } + catch (castExcept: ClassCastException) { + throw ClassCastException("Class casting failed.") + } +}