thymeleaf 0.1.0-beta.0

A framework-neutral Thymeleaf-compatible dynamic template engine for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
use std::collections::HashSet;
use std::fmt::{Debug, Formatter};
use std::hash::Hash;
use std::sync::Arc;

use thiserror::Error;

use crate::expression::ObjectArrayValue;

use super::{Validate, ValidateError};

type ArrayPredicate<T> = dyn Fn(&T) -> bool + Send + Sync;

/// Java 数组或 `Iterable` 中元素的运行时类信息。
///
/// 对应 Java: `java.lang.Object#getClass()` 以及
/// `org.thymeleaf.util.ArrayUtils#toArray(Class,Object)` 的组件类推断。
pub trait ArrayElementValue {
    /// 返回 Java `Class#getName()` 形式的精确运行时类名。
    ///
    /// # 返回
    /// 当前元素的精确 Java 运行时类名。
    fn class_name(&self) -> &str;

    /// 判断当前对象能否赋值给指定 Java 组件类。
    ///
    /// # 参数
    /// - `component_class_name`:目标数组的组件类名。
    ///
    /// # 返回
    /// Java `ArrayStoreException` 检查通过时返回 `true`。
    fn is_instance_of(&self, component_class_name: &str) -> bool {
        component_class_name == "java.lang.Object" || component_class_name == self.class_name()
    }
}

impl ArrayElementValue for String {
    fn class_name(&self) -> &str {
        "java.lang.String"
    }
}

impl ArrayElementValue for i32 {
    fn class_name(&self) -> &str {
        "java.lang.Integer"
    }
}

impl ArrayElementValue for i64 {
    fn class_name(&self) -> &str {
        "java.lang.Long"
    }
}

impl ArrayElementValue for f64 {
    fn class_name(&self) -> &str {
        "java.lang.Double"
    }
}

impl ArrayElementValue for f32 {
    fn class_name(&self) -> &str {
        "java.lang.Float"
    }
}

impl ArrayElementValue for bool {
    fn class_name(&self) -> &str {
        "java.lang.Boolean"
    }
}

/// Java `Class<? extends T[]>` 的 Rust 等价描述。
///
/// 对应 Java: `org.thymeleaf.util.ArrayUtils#copyOf(Object[],int,Class)` 的
/// `newType` 参数。
#[derive(Clone)]
pub struct ArrayTypeValue<T> {
    component_class_name: String,
    component_predicate: Arc<ArrayPredicate<T>>,
}

impl<T> ArrayTypeValue<T> {
    /// 创建具有运行时写入约束的引用数组类型。
    ///
    /// # 参数
    /// - `component_class_name`:Java 数组组件类名;
    /// - `component_predicate`:判断元素能否写入该类型。
    ///
    /// # 返回
    /// 可传给 [`ArrayUtils::copy_of_with_type`] 的类型描述。
    /// 对应 Java 语义:`ArrayUtils` 的 `typed` 行为(Rust 侧辅助/私有路径)。
    #[must_use]
    pub fn typed(
        component_class_name: impl Into<String>,
        component_predicate: impl Fn(&T) -> bool + Send + Sync + 'static,
    ) -> Self {
        Self {
            component_class_name: component_class_name.into(),
            component_predicate: Arc::new(component_predicate),
        }
    }

    /// 创建 Java `Object[]` 类型描述。
    ///
    /// # 返回
    /// 接受任意非 null 元素的数组类型。
    /// 对应 Java 语义:`ArrayUtils` 的 `object` 行为(Rust 侧辅助/私有路径)。
    #[must_use]
    pub fn object() -> Self {
        Self::typed("java.lang.Object", |_| true)
    }

    /// 返回 Java 组件类名。
    ///
    /// # 返回
    /// `Class#getComponentType().getName()` 等价文本。
    /// 对应 Java 语义:`ArrayUtils` 的 `component_class_name` 行为(Rust 侧辅助/私有路径)。
    #[must_use]
    pub fn component_class_name(&self) -> &str {
        &self.component_class_name
    }
}

impl<T> Debug for ArrayTypeValue<T> {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("ArrayTypeValue")
            .field("component_class_name", &self.component_class_name)
            .finish_non_exhaustive()
    }
}

/// `ArrayUtils#toArray` 可接受的 Java 运行时目标。
///
/// 对应 Java: `Object target`;枚举显式区分引用数组、primitive 数组、
/// `Iterable` 与其他对象,保留 JVM 分派及异常类别。
pub enum ArrayTarget<'a, T> {
    /// Java 引用数组。
    Reference(&'a ObjectArrayValue<T>),
    /// Java primitive 数组,值为 `Class#getName()`,例如 `[I`。
    PrimitiveArray {
        /// primitive 数组运行时类名。
        class_name: &'a str,
        /// 诊断消息使用的组件类名,例如 `int`。
        component_class_name: &'a str,
    },
    /// Java `Iterable<?>`,按迭代顺序保存元素。
    Iterable(&'a [Option<T>]),
    /// 既不是数组也不是 `Iterable` 的对象。
    Other {
        /// Java `Class#getName()` 形式的运行时类名。
        class_name: &'a str,
    },
}

/// `ArrayUtils#toArray` 的借用或新建数组结果。
///
/// Java 在兼容引用数组输入时原样返回同一实例;`Iterable` 输入则反射创建新数组。
/// 对应 Java 语义:`ArrayUtils` 的 Rust 侧类型 `ArrayValue`。
#[derive(Debug)]
pub enum ArrayValue<'a, T> {
    /// 原数组的同一引用。
    Borrowed(&'a ObjectArrayValue<T>),
    /// 从 `Iterable` 新建的数组。
    Owned(ObjectArrayValue<T>),
}

impl<'a, T> ArrayValue<'a, T> {
    /// 返回结果数组。
    ///
    /// # 返回
    /// 借用输入或持有新数组的统一只读引用。
    /// 对应 Java 语义:`ArrayUtils` 的 `as_array` 行为(Rust 侧辅助/私有路径)。
    #[must_use]
    pub fn as_array(&self) -> &ObjectArrayValue<T> {
        match self {
            Self::Borrowed(array) => array,
            Self::Owned(array) => array,
        }
    }

    /// 判断结果是否与指定输入数组为同一实例。
    ///
    /// # 参数
    /// - `target`:用于比较引用身份的输入数组。
    ///
    /// # 返回
    /// 结果借用该数组同一实例时返回 `true`。
    /// 对应 Java 语义:`ArrayUtils` 的 `is_same_reference` 行为(Rust 侧辅助/私有路径)。
    #[must_use]
    pub fn is_same_reference(&self, target: &ObjectArrayValue<T>) -> bool {
        matches!(self, Self::Borrowed(array) if std::ptr::eq(*array, target))
    }

    /// 消费结果并返回 owned 数组;借用结果会按 Java 数组组件约束克隆。
    ///
    /// # 返回
    /// 与当前结果等值且独立的引用数组。
    /// 对应 Java 语义:`ArrayUtils` 的 `into_owned` 行为(Rust 侧辅助/私有路径)。
    #[must_use]
    pub fn into_owned(self) -> ObjectArrayValue<T>
    where
        T: Clone,
    {
        match self {
            Self::Borrowed(array) => array.clone(),
            Self::Owned(array) => array,
        }
    }
}

/// `ArrayUtils` 的类型化异常。
///
/// 对应 Java: `org.thymeleaf.util.ArrayUtils` 显式参数异常及 JVM 数组运行时异常。
#[derive(Debug, Error, Clone, Eq, PartialEq)]
pub enum ArrayUtilsError {
    /// `Validate.notNull` 的 `IllegalArgumentException`。
    #[error(transparent)]
    Validation(#[from] ValidateError),
    /// 目标无法转换为所需数组。
    #[error("{message}")]
    CannotConvert {
        /// 上游生成的精确 detail message。
        message: String,
    },
    /// primitive array 强制转换到 `Object[]` 失败。
    #[error("class {class_name} cannot be cast to class [Ljava.lang.Object;")]
    ClassCast {
        /// primitive 数组运行时类名。
        class_name: String,
    },
    /// 元素无法写入目标运行时组件类。
    #[error("element cannot be stored in array with component class \"{component_class_name}\"")]
    ArrayStore {
        /// Java 组件类名。
        component_class_name: String,
    },
    /// Java `NegativeArraySizeException`。
    #[error("{length}")]
    NegativeArraySize {
        /// 请求的负长度。
        length: i32,
    },
    /// Java 隐式 `NullPointerException`。
    #[error("")]
    NullPointer,
    /// Java `ArrayIndexOutOfBoundsException`。
    #[error("{message}")]
    ArrayIndexOutOfBounds {
        /// 与 JVM 边界原因等价的诊断消息。
        message: String,
    },
    /// `copyOfRange` 的反向范围参数异常。
    #[error("Cannot copy array range with indexes {from} and {to}")]
    InvalidRange {
        /// 起始索引。
        from: i32,
        /// 结束索引。
        to: i32,
    },
}

impl ArrayUtilsError {
    /// 返回对应 Java 异常类名。
    ///
    /// # 返回
    /// 当前错误对应的 JVM 异常全限定名。
    #[must_use]
    pub const fn class_name(&self) -> &'static str {
        match self {
            Self::Validation(error) => error.class_name(),
            Self::CannotConvert { .. } | Self::InvalidRange { .. } => {
                "java.lang.IllegalArgumentException"
            }
            Self::ClassCast { .. } => "java.lang.ClassCastException",
            Self::ArrayStore { .. } => "java.lang.ArrayStoreException",
            Self::NegativeArraySize { .. } => "java.lang.NegativeArraySizeException",
            Self::NullPointer => "java.lang.NullPointerException",
            Self::ArrayIndexOutOfBounds { .. } => "java.lang.ArrayIndexOutOfBoundsException",
        }
    }
}

/// Java 数组转换、查询和复制工具。
///
/// 对应 Java: `org.thymeleaf.util.ArrayUtils`。
pub struct ArrayUtils;

impl ArrayUtils {
    /// 将引用数组原样返回,或按元素运行时类从 Iterable 创建新数组。
    ///
    /// 对应 Java: `ArrayUtils#toArray(Object)`。
    ///
    /// # 参数
    /// - `target`:可空 Java 运行时目标。
    ///
    /// # 返回
    /// 原引用数组或新建数组。
    pub fn to_array<'a, T>(
        target: Option<ArrayTarget<'a, T>>,
    ) -> Result<ArrayValue<'a, T>, ArrayUtilsError>
    where
        T: Clone + ArrayElementValue + 'static,
    {
        Self::convert(None, target)
    }

    /// 转换为 `String[]`。对应 Java: `ArrayUtils#toStringArray(Object)`。
    ///
    /// # 参数
    /// - `target`:可空 Java 运行时目标。
    ///
    /// # 返回
    /// 组件类为 `java.lang.String` 的数组。
    pub fn to_string_array<'a, T>(
        target: Option<ArrayTarget<'a, T>>,
    ) -> Result<ArrayValue<'a, T>, ArrayUtilsError>
    where
        T: Clone + ArrayElementValue + 'static,
    {
        Self::convert(Some("java.lang.String"), target)
    }

    /// 转换为 `Integer[]`。对应 Java: `ArrayUtils#toIntegerArray(Object)`。
    ///
    /// # 参数
    /// - `target`:可空 Java 运行时目标。
    ///
    /// # 返回
    /// 组件类为 `java.lang.Integer` 的数组。
    pub fn to_integer_array<'a, T>(
        target: Option<ArrayTarget<'a, T>>,
    ) -> Result<ArrayValue<'a, T>, ArrayUtilsError>
    where
        T: Clone + ArrayElementValue + 'static,
    {
        Self::convert(Some("java.lang.Integer"), target)
    }

    /// 转换为 `Long[]`。对应 Java: `ArrayUtils#toLongArray(Object)`。
    ///
    /// # 参数
    /// - `target`:可空 Java 运行时目标。
    ///
    /// # 返回
    /// 组件类为 `java.lang.Long` 的数组。
    pub fn to_long_array<'a, T>(
        target: Option<ArrayTarget<'a, T>>,
    ) -> Result<ArrayValue<'a, T>, ArrayUtilsError>
    where
        T: Clone + ArrayElementValue + 'static,
    {
        Self::convert(Some("java.lang.Long"), target)
    }

    /// 转换为 `Double[]`。对应 Java: `ArrayUtils#toDoubleArray(Object)`。
    ///
    /// # 参数
    /// - `target`:可空 Java 运行时目标。
    ///
    /// # 返回
    /// 组件类为 `java.lang.Double` 的数组。
    pub fn to_double_array<'a, T>(
        target: Option<ArrayTarget<'a, T>>,
    ) -> Result<ArrayValue<'a, T>, ArrayUtilsError>
    where
        T: Clone + ArrayElementValue + 'static,
    {
        Self::convert(Some("java.lang.Double"), target)
    }

    /// 转换为 `Float[]`。对应 Java: `ArrayUtils#toFloatArray(Object)`。
    ///
    /// # 参数
    /// - `target`:可空 Java 运行时目标。
    ///
    /// # 返回
    /// 组件类为 `java.lang.Float` 的数组。
    pub fn to_float_array<'a, T>(
        target: Option<ArrayTarget<'a, T>>,
    ) -> Result<ArrayValue<'a, T>, ArrayUtilsError>
    where
        T: Clone + ArrayElementValue + 'static,
    {
        Self::convert(Some("java.lang.Float"), target)
    }

    /// 转换为 `Boolean[]`。对应 Java: `ArrayUtils#toBooleanArray(Object)`。
    ///
    /// # 参数
    /// - `target`:可空 Java 运行时目标。
    ///
    /// # 返回
    /// 组件类为 `java.lang.Boolean` 的数组。
    pub fn to_boolean_array<'a, T>(
        target: Option<ArrayTarget<'a, T>>,
    ) -> Result<ArrayValue<'a, T>, ArrayUtilsError>
    where
        T: Clone + ArrayElementValue + 'static,
    {
        Self::convert(Some("java.lang.Boolean"), target)
    }

    /// 返回数组长度。对应 Java: `ArrayUtils#length(Object[])`。
    ///
    /// # 参数
    /// - `target`:可空目标数组。
    ///
    /// # 返回
    /// Java `int` 范围的数组长度。
    pub fn length<T>(target: Option<&[Option<T>]>) -> Result<i32, ArrayUtilsError> {
        Validate::not_null(target, Some("Cannot get array length of null"))?;
        Ok(i32::try_from(target.expect("validated target").len()).unwrap_or(i32::MAX))
    }

    /// 判断数组为 null 或空。对应 Java: `ArrayUtils#isEmpty(Object[])`。
    ///
    /// # 参数
    /// - `target`:可空目标数组。
    ///
    /// # 返回
    /// null 或零长度时返回 `true`。
    #[must_use]
    pub fn is_empty<T>(target: Option<&[Option<T>]>) -> bool {
        target.is_none_or(<[Option<T>]>::is_empty)
    }

    /// 判断数组是否包含元素。对应 Java: `ArrayUtils#contains(Object[],Object)`。
    ///
    /// # 参数
    /// - `target`:可空目标数组;
    /// - `element`:可空请求元素。
    ///
    /// # 返回
    /// 找到相等元素时返回 `true`。
    pub fn contains<T>(
        target: Option<&[Option<T>]>,
        element: &Option<T>,
    ) -> Result<bool, ArrayUtilsError>
    where
        T: PartialEq,
    {
        Validate::not_null(
            target,
            Some("Cannot execute array contains: target is null"),
        )?;
        Ok(target
            .expect("validated target")
            .iter()
            .any(|target_element| element == target_element))
    }

    /// 判断数组是否包含另一个数组的全部不同元素。
    ///
    /// 对应 Java: `ArrayUtils#containsAll(Object[],Object[])`。
    ///
    /// # 参数
    /// - `target`:可空目标数组;
    /// - `elements`:可空请求数组。
    ///
    /// # 返回
    /// 全部不同请求元素都存在时返回 `true`。
    pub fn contains_all_array<T>(
        target: Option<&[Option<T>]>,
        elements: Option<&[Option<T>]>,
    ) -> Result<bool, ArrayUtilsError>
    where
        T: Clone + Eq + Hash,
    {
        Validate::not_null(
            target,
            Some("Cannot execute array containsAll: target is null"),
        )?;
        Validate::not_null(
            elements,
            Some("Cannot execute array containsAll: elements is null"),
        )?;
        Self::contains_all_collection(target, elements)
    }

    /// 判断数组是否包含 Collection 的全部不同元素。
    ///
    /// 对应 Java: `ArrayUtils#containsAll(Object[],Collection)`。
    ///
    /// # 参数
    /// - `target`:可空目标数组;
    /// - `elements`:可空 Collection 等价序列。
    ///
    /// # 返回
    /// 全部不同请求元素都存在时返回 `true`。
    pub fn contains_all_collection<T>(
        target: Option<&[Option<T>]>,
        elements: Option<&[Option<T>]>,
    ) -> Result<bool, ArrayUtilsError>
    where
        T: Clone + Eq + Hash,
    {
        Validate::not_null(
            target,
            Some("Cannot execute array contains: target is null"),
        )?;
        Validate::not_null(
            elements,
            Some("Cannot execute array containsAll: elements is null"),
        )?;
        let mut remaining: HashSet<Option<T>> = elements
            .expect("validated elements")
            .iter()
            .cloned()
            .collect();
        for target_element in target.expect("validated target") {
            remaining.remove(target_element);
        }
        Ok(remaining.is_empty())
    }

    /// 使用显式运行时数组类型复制引用数组。
    ///
    /// 对应 Java: `ArrayUtils#copyOf(T[],int,Class<? extends X[]>)`。
    ///
    /// # 参数
    /// - `original`:可空源引用数组;
    /// - `new_length`:请求的新长度;
    /// - `new_type`:可空目标运行时数组类型。
    ///
    /// # 返回
    /// 独立的目标类型数组。
    pub fn copy_of_with_type<T>(
        original: Option<&ObjectArrayValue<T>>,
        new_length: i32,
        new_type: Option<&ArrayTypeValue<T>>,
    ) -> Result<ObjectArrayValue<T>, ArrayUtilsError>
    where
        T: Clone,
    {
        let new_type = new_type.ok_or(ArrayUtilsError::NullPointer)?;
        if new_length < 0 {
            return Err(ArrayUtilsError::NegativeArraySize { length: new_length });
        }
        let mut elements = vec![None; new_length as usize];
        let original = original.ok_or(ArrayUtilsError::NullPointer)?;
        let copy_length = original.len().min(elements.len());
        for (index, element) in original.as_slice()[..copy_length].iter().enumerate() {
            if element
                .as_ref()
                .is_some_and(|value| !(new_type.component_predicate)(value))
            {
                return Err(ArrayUtilsError::ArrayStore {
                    component_class_name: new_type.component_class_name.clone(),
                });
            }
            elements[index] = element.clone();
        }
        Ok(ObjectArrayValue::from_parts(
            new_type.component_class_name.clone(),
            elements,
            Arc::clone(&new_type.component_predicate),
        ))
    }

    /// 使用原数组运行时类型复制引用数组。
    ///
    /// 对应 Java: `ArrayUtils#copyOf(T[],int)`。
    ///
    /// # 参数
    /// - `original`:可空源引用数组;
    /// - `new_length`:请求的新长度。
    ///
    /// # 返回
    /// 独立且保留源运行时组件类的数组。
    pub fn copy_of<T>(
        original: Option<&ObjectArrayValue<T>>,
        new_length: i32,
    ) -> Result<ObjectArrayValue<T>, ArrayUtilsError>
    where
        T: Clone,
    {
        let original = original.ok_or(ArrayUtilsError::NullPointer)?;
        if new_length < 0 {
            return Err(ArrayUtilsError::NegativeArraySize { length: new_length });
        }
        let mut elements = vec![None; new_length as usize];
        let copy_length = original.len().min(elements.len());
        elements[..copy_length].clone_from_slice(&original.as_slice()[..copy_length]);
        Ok(ObjectArrayValue::from_parts(
            original.component_class_name().to_owned(),
            elements,
            original.component_predicate(),
        ))
    }

    /// 复制 Java `char[]`,扩展位置以 `\0` 填充。
    ///
    /// 对应 Java: `ArrayUtils#copyOf(char[],int)`;Rust 用 `u16` 保留 Java
    /// UTF-16 code unit。
    ///
    /// # 参数
    /// - `original`:可空 Java char 序列;
    /// - `new_length`:请求的新长度。
    ///
    /// # 返回
    /// 截断或以零扩展的独立 UTF-16 code unit 向量。
    pub fn copy_of_chars(
        original: Option<&[u16]>,
        new_length: i32,
    ) -> Result<Vec<u16>, ArrayUtilsError> {
        if new_length < 0 {
            return Err(ArrayUtilsError::NegativeArraySize { length: new_length });
        }
        let mut copy = vec![0; new_length as usize];
        let original = original.ok_or(ArrayUtilsError::NullPointer)?;
        let copy_length = original.len().min(copy.len());
        copy[..copy_length].copy_from_slice(&original[..copy_length]);
        Ok(copy)
    }

    /// 复制 Java `char[]` 的半开区间,并在 `to` 超过源长度时以零扩展。
    ///
    /// 对应 Java: `ArrayUtils#copyOfRange(char[],int,int)`。
    ///
    /// # 参数
    /// - `original`:可空 Java char 序列;
    /// - `from`:包含的起始索引;
    /// - `to`:不包含的结束索引。
    ///
    /// # 返回
    /// 指定半开区间的独立 UTF-16 code unit 向量。
    pub fn copy_of_range(
        original: Option<&[u16]>,
        from: i32,
        to: i32,
    ) -> Result<Vec<u16>, ArrayUtilsError> {
        let new_length = to.wrapping_sub(from);
        if new_length < 0 {
            return Err(ArrayUtilsError::InvalidRange { from, to });
        }
        let mut copy = vec![0; new_length as usize];
        let original = original.ok_or(ArrayUtilsError::NullPointer)?;
        let available = i64::try_from(original.len()).unwrap_or(i64::MAX) - i64::from(from);
        let copy_length = available.min(i64::from(new_length));
        if from < 0 {
            return Err(ArrayUtilsError::ArrayIndexOutOfBounds {
                message: format!(
                    "arraycopy: source index {from} out of bounds for char[{}]",
                    original.len()
                ),
            });
        }
        if copy_length < 0 {
            return Err(ArrayUtilsError::ArrayIndexOutOfBounds {
                message: format!("arraycopy: length {copy_length} is negative"),
            });
        }
        let from = from as usize;
        let copy_length = copy_length as usize;
        if copy_length > 0 {
            copy[..copy_length].copy_from_slice(&original[from..from + copy_length]);
        }
        Ok(copy)
    }

    fn convert<'a, T>(
        component_class_name: Option<&'static str>,
        target: Option<ArrayTarget<'a, T>>,
    ) -> Result<ArrayValue<'a, T>, ArrayUtilsError>
    where
        T: Clone + ArrayElementValue + 'static,
    {
        let target = target.ok_or_else(|| ArrayUtilsError::CannotConvert {
            message: "Cannot convert null to array".to_owned(),
        })?;
        match target {
            ArrayTarget::Reference(array) => {
                if component_class_name.is_none()
                    || component_class_name == Some(array.component_class_name())
                {
                    return Ok(ArrayValue::Borrowed(array));
                }
                Err(Self::incompatible_array(
                    array.component_class_name(),
                    component_class_name,
                ))
            }
            ArrayTarget::PrimitiveArray {
                class_name,
                component_class_name: primitive_component,
            } => {
                if component_class_name.is_none() {
                    Err(ArrayUtilsError::ClassCast {
                        class_name: class_name.to_owned(),
                    })
                } else {
                    Err(Self::incompatible_array(
                        primitive_component,
                        component_class_name,
                    ))
                }
            }
            ArrayTarget::Iterable(elements) => {
                let computed = component_class_name.map_or_else(
                    || {
                        let mut computed: Option<&str> = None;
                        for element in elements.iter().flatten() {
                            computed = match computed {
                                None => Some(element.class_name()),
                                Some("java.lang.Object") => Some("java.lang.Object"),
                                Some(current) if current == element.class_name() => Some(current),
                                Some(_) => Some("java.lang.Object"),
                            };
                        }
                        computed.unwrap_or("java.lang.Object").to_owned()
                    },
                    str::to_owned,
                );
                let predicate_component = computed.clone();
                let predicate: Arc<ArrayPredicate<T>> =
                    Arc::new(move |value| value.is_instance_of(&predicate_component));
                if let Some(value) = elements.iter().flatten().find(|value| !predicate(value)) {
                    let _ = value;
                    return Err(ArrayUtilsError::ArrayStore {
                        component_class_name: computed,
                    });
                }
                Ok(ArrayValue::Owned(ObjectArrayValue::from_parts(
                    computed,
                    elements.to_vec(),
                    predicate,
                )))
            }
            ArrayTarget::Other { class_name } => Err(ArrayUtilsError::CannotConvert {
                message: format!(
                    "Cannot convert object of class \"{class_name}\" to an array{}",
                    component_class_name.map_or("", |_| " of Class")
                ),
            }),
        }
    }

    fn incompatible_array(
        component_class_name: &str,
        requested_component_class_name: Option<&str>,
    ) -> ArrayUtilsError {
        ArrayUtilsError::CannotConvert {
            message: format!(
                "Cannot convert object of class \"{component_class_name}[]\" to an array{}",
                requested_component_class_name.map_or("", |_| " of Class")
            ),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{ArrayElementValue, ArrayTarget, ArrayTypeValue, ArrayUtils, ArrayUtilsError};
    use crate::expression::ObjectArrayValue;

    #[derive(Clone, Debug, Eq, Hash, PartialEq)]
    enum Value {
        Text(String),
        Number(i32),
    }

    impl ArrayElementValue for Value {
        fn class_name(&self) -> &str {
            match self {
                Self::Text(_) => "java.lang.String",
                Self::Number(_) => "java.lang.Integer",
            }
        }
    }

    #[test]
    fn conversion_preserves_identity_and_infers_exact_component_class() {
        let source = ObjectArrayValue::typed(
            "java.lang.String",
            vec![Some(Value::Text("one".to_owned()))],
            |value| matches!(value, Value::Text(_)),
        )
        .expect("source");
        let result = ArrayUtils::to_array(Some(ArrayTarget::Reference(&source))).expect("array");
        assert!(result.is_same_reference(&source));

        let values = [
            Some(Value::Text("one".to_owned())),
            None,
            Some(Value::Text("two".to_owned())),
        ];
        let result = ArrayUtils::to_array(Some(ArrayTarget::Iterable(&values))).expect("iterable");
        assert_eq!(result.as_array().component_class_name(), "java.lang.String");
        assert_eq!(result.as_array().as_slice(), &values);
    }

    #[test]
    fn typed_conversion_and_primitive_errors_keep_java_categories() {
        let mixed = [
            Some(Value::Text("one".to_owned())),
            Some(Value::Number(2)),
            Some(Value::Text("two".to_owned())),
        ];
        let inferred =
            ArrayUtils::to_array(Some(ArrayTarget::Iterable(&mixed))).expect("object array");
        assert_eq!(
            inferred.as_array().component_class_name(),
            "java.lang.Object"
        );
        let error = ArrayUtils::to_string_array(Some(ArrayTarget::Iterable(&mixed)))
            .expect_err("array store");
        assert_eq!(error.class_name(), "java.lang.ArrayStoreException");

        let error = ArrayUtils::to_array::<Value>(Some(ArrayTarget::PrimitiveArray {
            class_name: "[I",
            component_class_name: "int",
        }))
        .expect_err("class cast");
        assert_eq!(error.class_name(), "java.lang.ClassCastException");
    }

    #[test]
    fn query_and_copy_contracts_are_preserved() {
        let values = [Some("one".to_owned()), None, Some("two".to_owned())];
        assert_eq!(ArrayUtils::length(Some(&values)).expect("length"), 3);
        assert!(ArrayUtils::contains(Some(&values), &None).expect("contains"));
        assert!(
            ArrayUtils::contains_all_array(Some(&values), Some(&[Some("one".to_owned()), None]))
                .expect("all")
        );

        let source =
            ObjectArrayValue::typed("java.lang.String", values.to_vec(), |_| true).expect("source");
        let copied = ArrayUtils::copy_of(Some(&source), 5).expect("copy");
        assert_eq!(copied.len(), 5);
        assert_eq!(copied.component_class_name(), "java.lang.String");
        let integer_type = ArrayTypeValue::typed("java.lang.Integer", |_: &String| false);
        assert_eq!(
            ArrayUtils::copy_of_with_type(Some(&source), 1, Some(&integer_type))
                .expect_err("store")
                .class_name(),
            "java.lang.ArrayStoreException"
        );
    }

    #[test]
    fn char_copy_order_and_range_failures_match_java() {
        assert_eq!(
            ArrayUtils::copy_of_chars(Some(&[97, 0, 122]), 5).expect("copy"),
            vec![97, 0, 122, 0, 0]
        );
        assert_eq!(
            ArrayUtils::copy_of_range(Some(&[97, 98, 99, 100]), 2, 6).expect("range"),
            vec![99, 100, 0, 0]
        );
        assert_eq!(
            ArrayUtils::copy_of_range(Some(&[1]), 3, 1).expect_err("range"),
            ArrayUtilsError::InvalidRange { from: 3, to: 1 }
        );
        assert_eq!(
            ArrayUtils::copy_of_chars(None, -1).expect_err("negative"),
            ArrayUtilsError::NegativeArraySize { length: -1 }
        );
    }

    #[test]
    fn runtime_adapters_cover_builtin_classes_types_and_owned_results() {
        assert_eq!(String::new().class_name(), "java.lang.String");
        assert_eq!(0_i32.class_name(), "java.lang.Integer");
        assert_eq!(0_i64.class_name(), "java.lang.Long");
        assert_eq!(0_f64.class_name(), "java.lang.Double");
        assert_eq!(0_f32.class_name(), "java.lang.Float");
        assert_eq!(false.class_name(), "java.lang.Boolean");
        assert!("text".to_owned().is_instance_of("java.lang.Object"));
        assert!(!0_i32.is_instance_of("java.lang.String"));

        let array_type = ArrayTypeValue::<String>::object();
        assert_eq!(array_type.component_class_name(), "java.lang.Object");
        assert_eq!(
            format!("{array_type:?}"),
            "ArrayTypeValue { component_class_name: \"java.lang.Object\", .. }"
        );

        let source = ObjectArrayValue::object(vec![Some("one".to_owned())]);
        let borrowed = ArrayUtils::to_array(Some(ArrayTarget::Reference(&source)))
            .expect("borrowed")
            .into_owned();
        assert_eq!(borrowed.as_slice(), source.as_slice());
        let values = [Some("one".to_owned())];
        let owned = ArrayUtils::to_array(Some(ArrayTarget::Iterable(&values)))
            .expect("owned")
            .into_owned();
        assert_eq!(owned.as_slice(), &values);

        assert_eq!(
            ArrayUtils::copy_of_with_type(Some(&source), -1, Some(&array_type))
                .expect_err("negative"),
            ArrayUtilsError::NegativeArraySize { length: -1 }
        );
        assert_eq!(
            ArrayUtils::copy_of_with_type(None, 1, Some(&array_type)).expect_err("null original"),
            ArrayUtilsError::NullPointer
        );
    }
}