-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[java-matter-controller] Convert Java to Kotlin Phase III (#25578)
* [java-matter-controller] Convert Java to Kotlin Phase III * Address review comments
- Loading branch information
1 parent
138fef4
commit 3847784
Showing
19 changed files
with
895 additions
and
911 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 0 additions & 164 deletions
164
examples/java-matter-controller/java/src/com/matter/controller/commands/common/Argument.java
This file was deleted.
Oops, something went wrong.
157 changes: 157 additions & 0 deletions
157
examples/java-matter-controller/java/src/com/matter/controller/commands/common/Argument.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.