001/**
002 * Copyright (c) 2025-2026, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.gnu.org/licenses/lgpl-3.0.txt
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package dev.tinyflow.core.chain;
017
018
019import java.io.Serializable;
020import java.util.ArrayList;
021import java.util.Arrays;
022import java.util.Collection;
023import java.util.List;
024
025public class Parameter implements Serializable, Cloneable {
026    protected String id;
027    protected String name;
028    protected String description;
029    protected DataType dataType = DataType.String;
030
031    /**
032     * 数据类型:文字内容、图片、音频、视频、文件
033     */
034    protected String contentType;
035    protected String ref;
036    protected RefType refType;
037    protected String value;
038    protected boolean required;
039    protected String defaultValue;
040    protected List<Parameter> children;
041
042    /**
043     * 枚举值列表
044     */
045    protected List<Object> enums;
046
047    /**
048     * 用户输入的表单类型,例如:"input"  "textarea"  "select"  "radio"  "checkbox"  等等
049     */
050    protected String formType;
051
052    /**
053     * 用户界面上显示的提示文字,用于引导用户进行选择
054     */
055    protected String formLabel;
056
057
058    /**
059     * 表单的提示文字,用于引导用户进行选择或填写
060     */
061    protected String formPlaceholder;
062
063    /**
064     * 用户界面上显示的描述文字,用于引导用户进行选择
065     */
066    protected String formDescription;
067
068    /**
069     * 表单的其他属性
070     */
071    protected String formAttrs;
072
073
074    public Parameter() {
075    }
076
077    public Parameter(String name) {
078        this.name = name;
079    }
080
081    public Parameter(String name, DataType dataType) {
082        this.name = name;
083        this.dataType = dataType;
084    }
085
086    public Parameter(String name, boolean required) {
087        this.name = name;
088        this.required = required;
089    }
090
091    public Parameter(String name, DataType dataType, boolean required) {
092        this.name = name;
093        this.dataType = dataType;
094        this.required = required;
095    }
096
097    public String getId() {
098        return id;
099    }
100
101    public void setId(String id) {
102        this.id = id;
103    }
104
105    public String getName() {
106        return name;
107    }
108
109    public void setName(String name) {
110        this.name = name;
111    }
112
113    public String getDescription() {
114        return description;
115    }
116
117    public void setDescription(String description) {
118        this.description = description;
119    }
120
121    public DataType getDataType() {
122        return dataType;
123    }
124
125    public void setDataType(DataType dataType) {
126        this.dataType = dataType;
127    }
128
129    public String getContentType() {
130        return contentType;
131    }
132
133    public void setContentType(String contentType) {
134        this.contentType = contentType;
135    }
136
137    public String getRef() {
138        return ref;
139    }
140
141    public void setRef(String ref) {
142        this.ref = ref;
143    }
144
145    public RefType getRefType() {
146        return refType;
147    }
148
149    public void setRefType(RefType refType) {
150        this.refType = refType;
151    }
152
153    public String getValue() {
154        return value;
155    }
156
157    public void setValue(String value) {
158        this.value = value;
159    }
160
161    public String getDefaultValue() {
162        return defaultValue;
163    }
164
165    public void setDefaultValue(String defaultValue) {
166        this.defaultValue = defaultValue;
167    }
168
169    public boolean isRequired() {
170        return required;
171    }
172
173    public void setRequired(boolean required) {
174        this.required = required;
175    }
176
177    public List<Parameter> getChildren() {
178        return children;
179    }
180
181    public void setChildren(List<Parameter> children) {
182        this.children = children;
183    }
184
185    public void addChild(Parameter parameter) {
186        if (children == null) {
187            children = new ArrayList<>();
188        }
189        children.add(parameter);
190    }
191
192    public void addChildren(Collection<Parameter> parameters) {
193        if (children == null) {
194            children = new ArrayList<>();
195        }
196        children.addAll(parameters);
197    }
198
199    public List<Object> getEnums() {
200        return enums;
201    }
202
203    public void setEnums(List<Object> enums) {
204        this.enums = enums;
205    }
206
207    public void setEnumsObject(Object enumsObject) {
208        if (enumsObject == null) {
209            this.enums = null;
210        } else if (enumsObject instanceof Collection) {
211            this.enums = new ArrayList<>();
212            this.enums.addAll((Collection<?>) enumsObject);
213        } else if (enumsObject.getClass().isArray()) {
214            this.enums = new ArrayList<>();
215            this.enums.addAll(Arrays.asList((Object[]) enumsObject));
216        } else {
217            this.enums = new ArrayList<>(1);
218            this.enums.add(enumsObject);
219        }
220    }
221
222    public String getFormType() {
223        return formType;
224    }
225
226    public void setFormType(String formType) {
227        this.formType = formType;
228    }
229
230    public String getFormLabel() {
231        return formLabel;
232    }
233
234    public void setFormLabel(String formLabel) {
235        this.formLabel = formLabel;
236    }
237
238    public String getFormPlaceholder() {
239        return formPlaceholder;
240    }
241
242    public void setFormPlaceholder(String formPlaceholder) {
243        this.formPlaceholder = formPlaceholder;
244    }
245
246    public String getFormDescription() {
247        return formDescription;
248    }
249
250    public void setFormDescription(String formDescription) {
251        this.formDescription = formDescription;
252    }
253
254    public String getFormAttrs() {
255        return formAttrs;
256    }
257
258    public void setFormAttrs(String formAttrs) {
259        this.formAttrs = formAttrs;
260    }
261
262    @Override
263    public String toString() {
264        return "Parameter{" +
265            "id='" + id + '\'' +
266            ", name='" + name + '\'' +
267            ", description='" + description + '\'' +
268            ", dataType=" + dataType +
269            ", contentType='" + contentType + '\'' +
270            ", ref='" + ref + '\'' +
271            ", refType=" + refType +
272            ", value='" + value + '\'' +
273            ", required=" + required +
274            ", defaultValue='" + defaultValue + '\'' +
275            ", children=" + children +
276            ", enums=" + enums +
277            ", formType='" + formType + '\'' +
278            ", formLabel='" + formLabel + '\'' +
279            ", formPlaceholder='" + formPlaceholder + '\'' +
280            ", formDescription='" + formDescription + '\'' +
281            ", formAttrs='" + formAttrs + '\'' +
282            '}';
283    }
284
285    @Override
286    public Parameter clone() {
287        try {
288            Parameter clone = (Parameter) super.clone();
289            if (this.children != null) {
290                clone.children = new ArrayList<>(this.children.size());
291                for (Parameter child : this.children) {
292                    clone.children.add(child.clone()); // 递归克隆
293                }
294            }
295            if (this.enums != null) {
296                clone.enums = new ArrayList<>(this.enums.size());
297                clone.enums.addAll(this.enums);
298            }
299            return clone;
300        } catch (CloneNotSupportedException e) {
301            throw new AssertionError();
302        }
303    }
304}