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
+ }
0 commit comments