Skip to content

Commit

Permalink
Misc kotlin stylistic fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacSweers committed Sep 4, 2017
1 parent 869b1bd commit af4262e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 40 deletions.
2 changes: 1 addition & 1 deletion psync/src/main/kotlin/io/sweers/psync/PSyncPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PSyncPlugin : Plugin<Project> {
}
}

fun processVariants(project: Project, psyncModel: PSyncModel,
private fun processVariants(project: Project, psyncModel: PSyncModel,
variants: Iterable<BaseVariant>) {
val includesPattern = psyncModel.includesPattern
variants.forEach { variant ->
Expand Down
39 changes: 20 additions & 19 deletions psync/src/main/kotlin/io/sweers/psync/PSyncTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ open class PSyncTask : SourceTask() {
@Input
var generateRx: Boolean = false

@Suppress("unused")
@TaskAction
fun generate(inputs: IncrementalTaskInputs) {

Expand All @@ -80,7 +81,7 @@ open class PSyncTask : SourceTask() {
*
* @return Observable of all the distinct keys in this directory.
*/
fun getPrefEntriesFromFiles(sources: Iterable<File>): Single<List<PrefEntry<*>>> {
private fun getPrefEntriesFromFiles(sources: Iterable<File>): Single<List<PrefEntry<*>>> {
return Observable.fromIterable(sources) // Fetch the keys from each file
.map { file -> XmlParser().parse(file) } // Parse the file
.flatMap { rootNode ->
Expand All @@ -102,10 +103,10 @@ open class PSyncTask : SourceTask() {
* @param attributes attributes on the node to parse
* @return a generated PrefEntry, or {@link PrefEntry#BLANK} if we can't do anything with it
*/
fun generatePrefEntry(attributes: Map<QName, String>): PrefEntry<*> {
var entry: PrefEntry<*> by Delegates.notNull<PrefEntry<*>>()
private fun generatePrefEntry(attributes: Map<QName, String>): PrefEntry<*> {
var entry: PrefEntry<*> by Delegates.notNull()
var key: String? = null
var defaultValue: String = ""
var defaultValue = ""

// These are present for list-type preferences
var entries: String? = null
Expand Down Expand Up @@ -160,7 +161,7 @@ open class PSyncTask : SourceTask() {
* @param defaultValue String representation of the default value (e.g. "@string/hello")
* @return PrefEntry object representing this, or {@link PrefEntry#BLANK} if we couldn't resolve its resource ID
*/
fun generateResourcePrefEntry(key: String, defaultValue: String): PrefEntry<*> {
private fun generateResourcePrefEntry(key: String, defaultValue: String): PrefEntry<*> {
val split = defaultValue.split('/')

if (split.size < 2) {
Expand Down Expand Up @@ -191,7 +192,7 @@ open class PSyncTask : SourceTask() {
* @throws IOException because Java
*/
@Throws(IOException::class)
fun generate(inputKeys: List<PrefEntry<*>>,
private fun generate(inputKeys: List<PrefEntry<*>>,
packageName: String,
outputDir: File,
className: String,
Expand Down Expand Up @@ -393,7 +394,7 @@ open class PSyncTask : SourceTask() {
return entryClass.build()
}

internal fun camelCaseKey(input: String): String {
private fun camelCaseKey(input: String): String {

// Default to lower_underscore, as this is the platform convention
var format = CaseFormat.LOWER_UNDERSCORE
Expand Down Expand Up @@ -431,12 +432,12 @@ open class PSyncTask : SourceTask() {
if (!clazz.isPrimitive) {
return clazz
}
when (clazz.simpleName) {
"Boolean", "boolean" -> return java.lang.Boolean::class.java
"Integer", "int" -> return Integer::class.java
return when (clazz.simpleName) {
"Boolean", "boolean" -> java.lang.Boolean::class.java
"Integer", "int" -> Integer::class.java
else ->
// Currently unsupported
return null
null
}
}

Expand All @@ -445,18 +446,18 @@ open class PSyncTask : SourceTask() {
val simpleName = entry.valueType!!.simpleName.capitalize()
if (entry.defaultType == null) {
// No defaultValue() method will be available
when (simpleName) {
"Boolean" -> defaultValue = "false"
"Int" -> defaultValue = "-1"
"String" -> defaultValue = "null"
else -> defaultValue = "null"
defaultValue = when (simpleName) {
"Boolean" -> "false"
"Int" -> "-1"
"String" -> "null"
else -> "null"
}
}

if (isGetter) {
return "get$simpleName(KEY, $defaultValue)"
return if (isGetter) {
"get$simpleName(KEY, $defaultValue)"
} else {
return "put$simpleName(KEY, val)"
"put$simpleName(KEY, val)"
}
}
}
Expand Down
44 changes: 24 additions & 20 deletions psync/src/main/kotlin/io/sweers/psync/PrefEntry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package io.sweers.psync
/**
* This represents a preference entry
*
*
* T represents the type of value this preference is backed by, such as a boolean
* [T] represents the type of value this preference is backed by, such as a boolean
*/
class PrefEntry<T : Any>(var key: String,
var defaultValue: T? = null,
Expand All @@ -28,23 +27,28 @@ class PrefEntry<T : Any>(var key: String,
this.isResource = true
}

if (defaultValue == null) {
this.defaultType = null
this.valueType = null
} else if (isResource) {
this.defaultType = String::class.java
resolveResourceInfo()
} else if (defaultValue is Boolean) {
this.defaultType = Boolean::class.javaPrimitiveType
this.valueType = this.defaultType
} else if (defaultValue is Int) {
this.defaultType = Int::class.javaPrimitiveType
this.valueType = this.defaultType
} else if (defaultValue is String) {
this.defaultType = String::class.java
this.valueType = this.defaultType
} else {
throw UnsupportedOperationException(
when {
defaultValue == null -> {
this.defaultType = null
this.valueType = null
}
isResource -> {
this.defaultType = String::class.java
resolveResourceInfo()
}
defaultValue is Boolean -> {
this.defaultType = Boolean::class.javaPrimitiveType
this.valueType = this.defaultType
}
defaultValue is Int -> {
this.defaultType = Int::class.javaPrimitiveType
this.valueType = this.defaultType
}
defaultValue is String -> {
this.defaultType = String::class.java
this.valueType = this.defaultType
}
else -> throw UnsupportedOperationException(
"Unsupported type: " + defaultValue!!.javaClass.simpleName)
}
}
Expand Down Expand Up @@ -85,7 +89,7 @@ class PrefEntry<T : Any>(var key: String,


val isBlank: Boolean
get() = key.isNullOrBlank()
get() = key.isBlank()

// TODO
// Float
Expand Down

0 comments on commit af4262e

Please sign in to comment.