Skip to content

Commit 3847784

Browse files
yufengwangcapull[bot]
authored andcommitted
[java-matter-controller] Convert Java to Kotlin Phase III (#25578)
* [java-matter-controller] Convert Java to Kotlin Phase III * Address review comments
1 parent 138fef4 commit 3847784

19 files changed

+895
-911
lines changed

examples/java-matter-controller/BUILD.gn

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,7 @@ java_library("java") {
2727
]
2828

2929
sources = [
30-
"java/src/com/matter/controller/commands/common/Argument.java",
31-
"java/src/com/matter/controller/commands/common/ArgumentType.java",
32-
"java/src/com/matter/controller/commands/common/Command.java",
33-
"java/src/com/matter/controller/commands/common/CommandManager.java",
34-
"java/src/com/matter/controller/commands/common/CredentialsIssuer.java",
3530
"java/src/com/matter/controller/commands/common/FutureResult.java",
36-
"java/src/com/matter/controller/commands/common/IPAddress.java",
37-
"java/src/com/matter/controller/commands/common/MatterCommand.java",
3831
"java/src/com/matter/controller/commands/common/RealResult.java",
3932
]
4033

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

5144
sources = [
5245
"java/src/com/matter/controller/Main.kt",
46+
"java/src/com/matter/controller/commands/common/Argument.kt",
47+
"java/src/com/matter/controller/commands/common/ArgumentType.kt",
48+
"java/src/com/matter/controller/commands/common/Command.kt",
49+
"java/src/com/matter/controller/commands/common/CommandManager.kt",
50+
"java/src/com/matter/controller/commands/common/CredentialsIssuer.kt",
51+
"java/src/com/matter/controller/commands/common/IPAddress.kt",
52+
"java/src/com/matter/controller/commands/common/MatterCommand.kt",
5353
"java/src/com/matter/controller/commands/discover/DiscoverCommand.kt",
5454
"java/src/com/matter/controller/commands/discover/DiscoverCommissionablesCommand.kt",
5555
"java/src/com/matter/controller/commands/discover/DiscoverCommissionersCommand.kt",

examples/java-matter-controller/java/src/com/matter/controller/commands/common/Argument.java

-164
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
* Copyright (c) 2023 Project CHIP Authors
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
package com.matter.controller.commands.common
19+
20+
import java.net.InetAddress
21+
import java.net.UnknownHostException
22+
import java.util.concurrent.atomic.AtomicBoolean
23+
import java.util.concurrent.atomic.AtomicInteger
24+
import java.util.concurrent.atomic.AtomicLong
25+
26+
class Argument {
27+
val name: String
28+
val type: ArgumentType
29+
private val minValue: Long
30+
private val maxValue: Long
31+
val value: Any
32+
val desc: String?
33+
val isOptional: Boolean
34+
35+
constructor(name: String, value: IPAddress, optional: Boolean) {
36+
this.name = name
37+
type = ArgumentType.ADDRESS
38+
minValue = 0
39+
maxValue = 0
40+
this.value = value
41+
desc = null
42+
isOptional = optional
43+
}
44+
45+
constructor(name: String, value: StringBuffer, desc: String?, optional: Boolean) {
46+
this.name = name
47+
type = ArgumentType.STRING
48+
minValue = 0
49+
maxValue = 0
50+
this.value = value
51+
this.desc = desc
52+
isOptional = optional
53+
}
54+
55+
constructor(name: String, value: AtomicBoolean, desc: String?, optional: Boolean) {
56+
this.name = name
57+
type = ArgumentType.BOOL
58+
minValue = 0
59+
maxValue = 0
60+
this.value = value
61+
this.desc = desc
62+
isOptional = optional
63+
}
64+
65+
constructor(
66+
name: String,
67+
min: Short,
68+
max: Short,
69+
value: AtomicInteger,
70+
desc: String?,
71+
optional: Boolean
72+
) {
73+
this.name = name
74+
type = ArgumentType.NUMBER_INT16
75+
minValue = min.toLong()
76+
maxValue = max.toLong()
77+
this.value = value
78+
this.desc = desc
79+
isOptional = optional
80+
}
81+
82+
constructor(
83+
name: String, min: Int, max: Int, value: AtomicInteger, desc: String?, optional: Boolean
84+
) {
85+
this.name = name
86+
type = ArgumentType.NUMBER_INT32
87+
minValue = min.toLong()
88+
maxValue = max.toLong()
89+
this.value = value
90+
this.desc = desc
91+
isOptional = optional
92+
}
93+
94+
constructor(
95+
name: String, min: Short, max: Short, value: AtomicLong, desc: String?, optional: Boolean
96+
) {
97+
this.name = name
98+
type = ArgumentType.NUMBER_INT32
99+
minValue = min.toLong()
100+
maxValue = max.toLong()
101+
this.value = value
102+
this.desc = desc
103+
isOptional = optional
104+
}
105+
106+
constructor(
107+
name: String, min: Long, max: Long, value: AtomicLong, desc: String?, optional: Boolean
108+
) {
109+
this.name = name
110+
type = ArgumentType.NUMBER_INT64
111+
minValue = min
112+
maxValue = max
113+
this.value = value
114+
this.desc = desc
115+
isOptional = optional
116+
}
117+
118+
fun setValue(value: String) {
119+
var isValidArgument = false
120+
when (type) {
121+
ArgumentType.ATTRIBUTE -> {
122+
val str = this.value as String
123+
isValidArgument = value == str
124+
}
125+
126+
ArgumentType.NUMBER_INT16 -> {
127+
val numShort = this.value as AtomicInteger
128+
numShort.set(value.toInt())
129+
isValidArgument = numShort.toInt() >= minValue && numShort.toInt() <= maxValue
130+
}
131+
132+
ArgumentType.NUMBER_INT32 -> {
133+
val num = this.value as AtomicInteger
134+
num.set(value.toInt())
135+
isValidArgument = num.toInt() >= minValue && num.toInt() <= maxValue
136+
}
137+
138+
ArgumentType.NUMBER_INT64 -> {
139+
val numLong = this.value as AtomicLong
140+
numLong.set(value.toLong())
141+
isValidArgument = numLong.toInt() >= minValue && numLong.toInt() <= maxValue
142+
}
143+
144+
ArgumentType.ADDRESS -> isValidArgument = try {
145+
val ipAddress = this.value as IPAddress
146+
ipAddress.setAddress(InetAddress.getByName(value))
147+
true
148+
} catch (e: UnknownHostException) {
149+
false
150+
}
151+
152+
else -> {
153+
}
154+
}
155+
require(isValidArgument) { "Invalid argument " + name + ": " + value }
156+
}
157+
}

examples/java-matter-controller/java/src/com/matter/controller/commands/common/ArgumentType.java examples/java-matter-controller/java/src/com/matter/controller/commands/common/ArgumentType.kt

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022 Project CHIP Authors
2+
* Copyright (c) 2023 Project CHIP Authors
33
* All rights reserved.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,10 +15,9 @@
1515
* limitations under the License.
1616
*
1717
*/
18+
package com.matter.controller.commands.common
1819

19-
package com.matter.controller.commands.common;
20-
21-
public enum ArgumentType {
20+
enum class ArgumentType {
2221
NUMBER_INT8,
2322
NUMBER_INT16,
2423
NUMBER_INT32,
@@ -36,5 +35,5 @@ public enum ArgumentType {
3635
VECTOR_BOOL,
3736
VECTOR16,
3837
VECTOR32,
39-
VECTOR_CUSTOM,
40-
}
38+
VECTOR_CUSTOM
39+
}

0 commit comments

Comments
 (0)