7
7
import org .jetbrains .annotations .Contract ;
8
8
import org .jetbrains .annotations .Unmodifiable ;
9
9
import org .jspecify .annotations .NullMarked ;
10
+ import org .jspecify .annotations .Nullable ;
10
11
11
12
import java .util .Locale ;
12
13
import java .util .Map ;
13
14
import java .util .Optional ;
14
15
import java .util .OptionalInt ;
16
+ import java .util .function .Consumer ;
15
17
16
18
/**
17
19
* Represents a currency with support for localization, formatting, and symbolic representation.
@@ -34,39 +36,39 @@ public interface Currency {
34
36
* If the audience does not specify a locale, {@link Locale#US} is used.
35
37
*
36
38
* @param audience the audience whose locale is used to determine the singular display name
37
- * @return the singular display name as a {@code Component} for the audience's locale
39
+ * @return an {@code Optional} containing the singular display name as a {@code Component} for the audience's locale, or empty
38
40
*/
39
- default Component getDisplayNameSingular (Audience audience ) {
41
+ default Optional < Component > getDisplayNameSingular (Audience audience ) {
40
42
return getDisplayNameSingular (audience .getOrDefault (Identity .LOCALE , Locale .US ));
41
43
}
42
44
43
45
/**
44
46
* Retrieves the singular display name component of the currency for the specified locale.
45
47
*
46
48
* @param locale the locale for which the singular display name should be retrieved
47
- * @return the singular display name as a {@code Component} for the specified locale
49
+ * @return an {@code Optional} containing the singular display name as a {@code Component} for the specified locale, or empty
48
50
*/
49
- Component getDisplayNameSingular (Locale locale );
51
+ Optional < Component > getDisplayNameSingular (Locale locale );
50
52
51
53
/**
52
54
* Retrieves the plural display name component of the currency based on the audience's locale.
53
55
* <p>
54
56
* If the audience does not specify a locale, {@link Locale#US} is used.
55
57
*
56
58
* @param audience the audience whose locale is used to determine the plural display name
57
- * @return the plural display name as a {@code Component} for the audience's locale
59
+ * @return an {@code Optional} containing the plural display name as a {@code Component} for the audience's locale, or empty
58
60
*/
59
- default Component getDisplayNamePlural (Audience audience ) {
61
+ default Optional < Component > getDisplayNamePlural (Audience audience ) {
60
62
return getDisplayNamePlural (audience .getOrDefault (Identity .LOCALE , Locale .US ));
61
63
}
62
64
63
65
/**
64
66
* Retrieves the plural display name component of the currency for the specified locale.
65
67
*
66
68
* @param locale the locale for which the plural display name should be retrieved
67
- * @return the plural display name as a {@code Component} for the specified locale
69
+ * @return an {@code Optional} containing the plural display name as a {@code Component} for the specified locale, or empty
68
70
*/
69
- Component getDisplayNamePlural (Locale locale );
71
+ Optional < Component > getDisplayNamePlural (Locale locale );
70
72
71
73
/**
72
74
* Retrieves the currency symbol.
@@ -104,12 +106,44 @@ default Component format(Number amount, Audience audience) {
104
106
*/
105
107
int getFractionalDigits ();
106
108
109
+ /**
110
+ * Modifies the current configuration of the currency using the provided builder.
111
+ * The builder allows customization of various currency properties.
112
+ *
113
+ * @param consumer a {@code Consumer} that accepts a {@code Builder} instance to define customizations for the currency
114
+ */
115
+ void editCurrency (Consumer <Builder > consumer );
116
+
117
+ /**
118
+ * Converts the current {@code Currency} instance into a {@code Builder} for modification or reconstruction.
119
+ *
120
+ * @return a {@code Builder} instance initialized with the properties of the current {@code Currency}
121
+ */
122
+ @ ApiStatus .OverrideOnly
123
+ Builder toBuilder ();
124
+
107
125
/**
108
126
* A builder interface for constructing instances of {@link Currency}.
109
127
* The {@code Builder} allows for the configuration of currency properties such as
110
128
* singular and plural display names, currency symbol, and fractional digits.
111
129
*/
112
130
interface Builder {
131
+ /**
132
+ * Sets the name of the currency.
133
+ *
134
+ * @param name the name to be set
135
+ * @return the builder instance for method chaining
136
+ */
137
+ @ Contract (value = "_ -> this" )
138
+ Builder name (String name );
139
+
140
+ /**
141
+ * Retrieves the name currently set on the builder.
142
+ *
143
+ * @return the name as a string, or {@code null} if not set
144
+ */
145
+ String name ();
146
+
113
147
/**
114
148
* Retrieves a map containing the singular display names of the currency for various locales.
115
149
*
@@ -122,12 +156,12 @@ interface Builder {
122
156
/**
123
157
* Sets the singular display name of the currency for a specific locale.
124
158
*
159
+ * @param locale the locale for which the singular display name is being set, or {@code null} to remove
125
160
* @param name the singular display name component of the currency
126
- * @param locale the locale for which the singular display name is being set
127
161
* @return the builder instance for chaining
128
162
*/
129
- @ Contract (value = "_, _ -> this" , pure = true )
130
- Builder displayNameSingular (Component name , Locale locale );
163
+ @ Contract (value = "_, _ -> this" )
164
+ Builder displayNameSingular (Locale locale , @ Nullable Component name );
131
165
132
166
/**
133
167
* Retrieves the singular display name component of the currency for the specified locale.
@@ -150,12 +184,12 @@ interface Builder {
150
184
/**
151
185
* Sets the plural display name of the currency for a specific locale.
152
186
*
187
+ * @param locale the locale for which the plural display name is being set, or {@code null} to remove
153
188
* @param name the plural display name component of the currency
154
- * @param locale the locale for which the plural display name is being set
155
189
* @return the builder instance for chaining
156
190
*/
157
- @ Contract (value = "_, _ -> this" , pure = true )
158
- Builder displayNamePlural (Component name , Locale locale );
191
+ @ Contract (value = "_, _ -> this" )
192
+ Builder displayNamePlural (Locale locale , @ Nullable Component name );
159
193
160
194
/**
161
195
* Retrieves the plural display name component of the currency for the specified locale.
@@ -169,11 +203,11 @@ interface Builder {
169
203
/**
170
204
* Sets the currency symbol as a {@code Component}.
171
205
*
172
- * @param symbol the symbol component to represent the currency
206
+ * @param symbol the symbol component to represent the currency, or {@code null} to remove
173
207
* @return the builder instance for chaining
174
208
*/
175
- @ Contract (value = "_ -> this" , pure = true )
176
- Builder symbol (Component symbol );
209
+ @ Contract (value = "_ -> this" )
210
+ Builder symbol (@ Nullable Component symbol );
177
211
178
212
/**
179
213
* Retrieves the currency symbol set on the {@code Builder}.
@@ -189,12 +223,12 @@ interface Builder {
189
223
* Fractional digits are generally used to specify the precision of the currency values,
190
224
* for example, 2 fractional digits for most currencies such as USD (representing cents).
191
225
*
192
- * @param fractionalDigits the number of fractional digits to set (must be a non-negative integer)
226
+ * @param fractionalDigits the number of fractional digits to set (must be a non-negative integer), or {@code null} to remove
193
227
* @return the builder instance for chaining
194
228
* @throws IllegalArgumentException if {@code fractionalDigits} is negative
195
229
*/
196
230
@ Contract (value = "_ -> this" )
197
- Builder fractionalDigits (int fractionalDigits ) throws IllegalArgumentException ;
231
+ Builder fractionalDigits (@ Nullable Integer fractionalDigits ) throws IllegalArgumentException ;
198
232
199
233
/**
200
234
* Retrieves the number of fractional digits set for the currency.
0 commit comments