Skip to content

Commit

Permalink
[java-matter-controller] Convert Java to Kotlin Phase III (#25578)
Browse files Browse the repository at this point in the history
* [java-matter-controller] Convert Java to Kotlin Phase III

* Address review comments
  • Loading branch information
yufengwangca authored and pull[bot] committed Sep 7, 2023
1 parent 138fef4 commit 3847784
Show file tree
Hide file tree
Showing 19 changed files with 895 additions and 911 deletions.
14 changes: 7 additions & 7 deletions examples/java-matter-controller/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,7 @@ java_library("java") {
]

sources = [
"java/src/com/matter/controller/commands/common/Argument.java",
"java/src/com/matter/controller/commands/common/ArgumentType.java",
"java/src/com/matter/controller/commands/common/Command.java",
"java/src/com/matter/controller/commands/common/CommandManager.java",
"java/src/com/matter/controller/commands/common/CredentialsIssuer.java",
"java/src/com/matter/controller/commands/common/FutureResult.java",
"java/src/com/matter/controller/commands/common/IPAddress.java",
"java/src/com/matter/controller/commands/common/MatterCommand.java",
"java/src/com/matter/controller/commands/common/RealResult.java",
]

Expand All @@ -50,6 +43,13 @@ kotlin_binary("java-matter-controller") {

sources = [
"java/src/com/matter/controller/Main.kt",
"java/src/com/matter/controller/commands/common/Argument.kt",
"java/src/com/matter/controller/commands/common/ArgumentType.kt",
"java/src/com/matter/controller/commands/common/Command.kt",
"java/src/com/matter/controller/commands/common/CommandManager.kt",
"java/src/com/matter/controller/commands/common/CredentialsIssuer.kt",
"java/src/com/matter/controller/commands/common/IPAddress.kt",
"java/src/com/matter/controller/commands/common/MatterCommand.kt",
"java/src/com/matter/controller/commands/discover/DiscoverCommand.kt",
"java/src/com/matter/controller/commands/discover/DiscoverCommissionablesCommand.kt",
"java/src/com/matter/controller/commands/discover/DiscoverCommissionersCommand.kt",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.matter.controller.commands.common

import java.net.InetAddress
import java.net.UnknownHostException
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicLong

class Argument {
val name: String
val type: ArgumentType
private val minValue: Long
private val maxValue: Long
val value: Any
val desc: String?
val isOptional: Boolean

constructor(name: String, value: IPAddress, optional: Boolean) {
this.name = name
type = ArgumentType.ADDRESS
minValue = 0
maxValue = 0
this.value = value
desc = null
isOptional = optional
}

constructor(name: String, value: StringBuffer, desc: String?, optional: Boolean) {
this.name = name
type = ArgumentType.STRING
minValue = 0
maxValue = 0
this.value = value
this.desc = desc
isOptional = optional
}

constructor(name: String, value: AtomicBoolean, desc: String?, optional: Boolean) {
this.name = name
type = ArgumentType.BOOL
minValue = 0
maxValue = 0
this.value = value
this.desc = desc
isOptional = optional
}

constructor(
name: String,
min: Short,
max: Short,
value: AtomicInteger,
desc: String?,
optional: Boolean
) {
this.name = name
type = ArgumentType.NUMBER_INT16
minValue = min.toLong()
maxValue = max.toLong()
this.value = value
this.desc = desc
isOptional = optional
}

constructor(
name: String, min: Int, max: Int, value: AtomicInteger, desc: String?, optional: Boolean
) {
this.name = name
type = ArgumentType.NUMBER_INT32
minValue = min.toLong()
maxValue = max.toLong()
this.value = value
this.desc = desc
isOptional = optional
}

constructor(
name: String, min: Short, max: Short, value: AtomicLong, desc: String?, optional: Boolean
) {
this.name = name
type = ArgumentType.NUMBER_INT32
minValue = min.toLong()
maxValue = max.toLong()
this.value = value
this.desc = desc
isOptional = optional
}

constructor(
name: String, min: Long, max: Long, value: AtomicLong, desc: String?, optional: Boolean
) {
this.name = name
type = ArgumentType.NUMBER_INT64
minValue = min
maxValue = max
this.value = value
this.desc = desc
isOptional = optional
}

fun setValue(value: String) {
var isValidArgument = false
when (type) {
ArgumentType.ATTRIBUTE -> {
val str = this.value as String
isValidArgument = value == str
}

ArgumentType.NUMBER_INT16 -> {
val numShort = this.value as AtomicInteger
numShort.set(value.toInt())
isValidArgument = numShort.toInt() >= minValue && numShort.toInt() <= maxValue
}

ArgumentType.NUMBER_INT32 -> {
val num = this.value as AtomicInteger
num.set(value.toInt())
isValidArgument = num.toInt() >= minValue && num.toInt() <= maxValue
}

ArgumentType.NUMBER_INT64 -> {
val numLong = this.value as AtomicLong
numLong.set(value.toLong())
isValidArgument = numLong.toInt() >= minValue && numLong.toInt() <= maxValue
}

ArgumentType.ADDRESS -> isValidArgument = try {
val ipAddress = this.value as IPAddress
ipAddress.setAddress(InetAddress.getByName(value))
true
} catch (e: UnknownHostException) {
false
}

else -> {
}
}
require(isValidArgument) { "Invalid argument " + name + ": " + value }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Project CHIP Authors
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -15,10 +15,9 @@
* limitations under the License.
*
*/
package com.matter.controller.commands.common

package com.matter.controller.commands.common;

public enum ArgumentType {
enum class ArgumentType {
NUMBER_INT8,
NUMBER_INT16,
NUMBER_INT32,
Expand All @@ -36,5 +35,5 @@ public enum ArgumentType {
VECTOR_BOOL,
VECTOR16,
VECTOR32,
VECTOR_CUSTOM,
}
VECTOR_CUSTOM
}
Loading

0 comments on commit 3847784

Please sign in to comment.