Skip to content

Instantly share code, notes, and snippets.

@DevPicon
Created May 20, 2017 16:21
Show Gist options
  • Save DevPicon/2de88f80563cea69f6666fe415f2ae75 to your computer and use it in GitHub Desktop.
Save DevPicon/2de88f80563cea69f6666fe415f2ae75 to your computer and use it in GitHub Desktop.
Algunos ejemplos de como usar Kotlin de una mejor manera
fun getDataSource(): MysqlDataSource {
val fileInputStream = FileInputStream("myfile.properties")
val properties = Properties()
properties.load(fileInputStream)
val mySqlDataSource = MysqlDataSource()?.apply {
setURL(properties.getProperty("MYSQL_DB_URL"))
user = properties.getProperty("MYSQL_DB_USERNAME")
setPassword(properties.getProperty("MYSQL_DB_PASSWORD"))
port = properties.getProperty("MYSQL_DB_PORT").toInt()
databaseName = properties.getProperty("MYSQL_DB_NAME")
maxQuerySizeToLog = properties.getProperty("MYSQL_DB_QUERYSIZE").toInt()
}
return mySqlDataSource
}
fun makeDir(path: String) = File(path).apply { mkdirs() }
fun findPhoneNumber(number: String, locale: String = "MX"): String {
TODO("Implementa tu búsqueda")
}
fun String.countAmountOfX(): Int {
return this.length - this.replace("x", "").length
}
db?.let {
try {
db.beginTransaction()
comicValues.forEach { db.insert(ComicContract.ComicEntry.TABLE_NAME, null, it) }
db.setTransactionSuccessful()
} catch (e: SQLException) {
Log.e(javaClass.simpleName, "Ha ocurrido un error durante la inserción.", e)
} finally {
db.endTransaction()
}
}
fun getDefaultLocale(deliveryArea: String) = when (deliveryArea.toLowerCase()) {
"mexico", "peru" -> MyLocale.SPANISH
"brazil" -> MyLocale.PORTUGUESE
"usa" -> MyLocale.ENGLISH
else -> MyLocale.SPANISH
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment