|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 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 | + |
| 19 | +package com.matter.controller.commands.common; |
| 20 | + |
| 21 | +import java.net.InetAddress; |
| 22 | +import java.net.UnknownHostException; |
| 23 | +import java.util.Optional; |
| 24 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 25 | +import java.util.concurrent.atomic.AtomicInteger; |
| 26 | +import java.util.concurrent.atomic.AtomicLong; |
| 27 | +import javax.annotation.Nullable; |
| 28 | + |
| 29 | +public final class Argument { |
| 30 | + private final String mName; |
| 31 | + private final ArgumentType mType; |
| 32 | + private final long mMin; |
| 33 | + private final long mMax; |
| 34 | + private final Object mValue; |
| 35 | + private final Optional<String> mDesc; |
| 36 | + |
| 37 | + public Argument(String name, IPAddress value) { |
| 38 | + this.mName = name; |
| 39 | + this.mType = ArgumentType.ADDRESS; |
| 40 | + this.mMin = 0; |
| 41 | + this.mMax = 0; |
| 42 | + this.mValue = value; |
| 43 | + this.mDesc = Optional.empty(); |
| 44 | + } |
| 45 | + |
| 46 | + public Argument(String name, StringBuffer value, @Nullable String desc) { |
| 47 | + this.mName = name; |
| 48 | + this.mType = ArgumentType.STRING; |
| 49 | + this.mMin = 0; |
| 50 | + this.mMax = 0; |
| 51 | + this.mValue = value; |
| 52 | + this.mDesc = Optional.ofNullable(desc); |
| 53 | + } |
| 54 | + |
| 55 | + public Argument(String name, AtomicBoolean value, @Nullable String desc) { |
| 56 | + this.mName = name; |
| 57 | + this.mType = ArgumentType.BOOL; |
| 58 | + this.mMin = 0; |
| 59 | + this.mMax = 0; |
| 60 | + this.mValue = value; |
| 61 | + this.mDesc = Optional.ofNullable(desc); |
| 62 | + } |
| 63 | + |
| 64 | + public Argument(String name, short min, short max, AtomicInteger value, @Nullable String desc) { |
| 65 | + this.mName = name; |
| 66 | + this.mType = ArgumentType.NUMBER_INT16; |
| 67 | + this.mMin = min; |
| 68 | + this.mMax = max; |
| 69 | + this.mValue = value; |
| 70 | + this.mDesc = Optional.ofNullable(desc); |
| 71 | + } |
| 72 | + |
| 73 | + public Argument(String name, int min, int max, AtomicInteger value, @Nullable String desc) { |
| 74 | + this.mName = name; |
| 75 | + this.mType = ArgumentType.NUMBER_INT32; |
| 76 | + this.mMin = min; |
| 77 | + this.mMax = max; |
| 78 | + this.mValue = value; |
| 79 | + this.mDesc = Optional.ofNullable(desc); |
| 80 | + } |
| 81 | + |
| 82 | + public Argument(String name, long min, long max, AtomicLong value, @Nullable String desc) { |
| 83 | + this.mName = name; |
| 84 | + this.mType = ArgumentType.NUMBER_INT64; |
| 85 | + this.mMin = min; |
| 86 | + this.mMax = max; |
| 87 | + this.mValue = value; |
| 88 | + this.mDesc = Optional.ofNullable(desc); |
| 89 | + } |
| 90 | + |
| 91 | + public String getName() { |
| 92 | + return mName; |
| 93 | + } |
| 94 | + |
| 95 | + public ArgumentType getType() { |
| 96 | + return mType; |
| 97 | + } |
| 98 | + |
| 99 | + public Object getValue() { |
| 100 | + return mValue; |
| 101 | + } |
| 102 | + |
| 103 | + public Optional<String> getDesc() { |
| 104 | + return mDesc; |
| 105 | + } |
| 106 | + |
| 107 | + public void setValue(String value) { |
| 108 | + boolean isValidArgument = false; |
| 109 | + |
| 110 | + switch (mType) { |
| 111 | + case ATTRIBUTE: |
| 112 | + String str = (String) mValue; |
| 113 | + isValidArgument = value.equals(str); |
| 114 | + break; |
| 115 | + case NUMBER_INT32: |
| 116 | + AtomicInteger num = (AtomicInteger) mValue; |
| 117 | + num.set(Integer.parseInt(value)); |
| 118 | + isValidArgument = (num.intValue() >= mMin && num.intValue() <= mMax); |
| 119 | + break; |
| 120 | + case ADDRESS: |
| 121 | + try { |
| 122 | + IPAddress ipAddress = (IPAddress) mValue; |
| 123 | + ipAddress.setAddress(InetAddress.getByName(value)); |
| 124 | + } catch (UnknownHostException e) { |
| 125 | + isValidArgument = true; |
| 126 | + } |
| 127 | + break; |
| 128 | + } |
| 129 | + |
| 130 | + if (!isValidArgument) { |
| 131 | + throw new IllegalArgumentException("Invalid argument " + mName + ": " + value); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments