1
2
3
4
5
6 package dev.metaschema.core.metapath.item.atomic;
7
8 import dev.metaschema.core.datatype.adapter.MetaschemaDataTypeProvider;
9 import dev.metaschema.core.metapath.function.InvalidValueForCastFunctionException;
10 import dev.metaschema.core.metapath.item.atomic.impl.StringItemImpl;
11 import dev.metaschema.core.metapath.type.IAtomicOrUnionType;
12 import dev.metaschema.core.metapath.type.InvalidTypeMetapathException;
13 import edu.umd.cs.findbugs.annotations.NonNull;
14
15
16
17
18 public interface IStringItem extends IAnyAtomicItem {
19
20
21
22
23
24 @NonNull
25 static IAtomicOrUnionType<IStringItem> type() {
26 return MetaschemaDataTypeProvider.STRING.getItemType();
27 }
28
29 @Override
30 default IAtomicOrUnionType<? extends IStringItem> getType() {
31 return type();
32 }
33
34
35
36
37
38
39
40
41
42
43
44 @NonNull
45 static IStringItem valueOf(@NonNull String value) {
46 try {
47 return new StringItemImpl(MetaschemaDataTypeProvider.STRING.parse(value));
48 } catch (IllegalArgumentException ex) {
49 throw new InvalidTypeMetapathException(
50 null,
51 String.format("Invalid string value '%s'. %s",
52 value,
53 ex.getLocalizedMessage()),
54 ex);
55 }
56 }
57
58
59
60
61
62
63 default IBase64BinaryItem encode() {
64
65 return IBase64BinaryItem.encode(asString());
66 }
67
68
69
70
71
72
73
74
75
76
77
78 @NonNull
79 static IStringItem cast(@NonNull IAnyAtomicItem item) {
80 try {
81 return item instanceof IStringItem
82 ? (IStringItem) item
83 : valueOf(item.asString());
84 } catch (IllegalStateException ex) {
85
86 throw new InvalidValueForCastFunctionException(ex);
87 }
88 }
89
90 @Override
91 default IStringItem asStringItem() {
92 return this;
93 }
94
95 @Override
96 default IStringItem castAsType(IAnyAtomicItem item) {
97 return cast(item);
98 }
99
100
101
102
103
104
105
106
107
108
109 default int compareTo(@NonNull IStringItem other) {
110 return asString().compareTo(other.asString());
111 }
112
113
114
115
116
117
118
119 @NonNull
120 IStringItem normalizeSpace();
121
122
123
124
125
126
127 default int length() {
128 return asString().length();
129 }
130 }