|
| 1 | +package net.thenextlvl.service.api.economy.currency; |
| 2 | + |
| 3 | +import net.kyori.adventure.audience.Audience; |
| 4 | +import net.kyori.adventure.identity.Identity; |
| 5 | +import net.kyori.adventure.text.Component; |
| 6 | +import org.jetbrains.annotations.ApiStatus; |
| 7 | +import org.jspecify.annotations.NullMarked; |
| 8 | + |
| 9 | +import java.util.Locale; |
| 10 | +import java.util.Optional; |
| 11 | +import java.util.OptionalInt; |
| 12 | + |
| 13 | +@NullMarked |
| 14 | +public interface Currency { |
| 15 | + /** |
| 16 | + * Retrieves the name of the currency. |
| 17 | + * |
| 18 | + * @return the name of the currency as a string |
| 19 | + */ |
| 20 | + String getName(); |
| 21 | + |
| 22 | + /** |
| 23 | + * Retrieves the singular display name component of the currency based on the audience's locale. |
| 24 | + * <p> |
| 25 | + * If the audience does not specify a locale, {@link Locale#US} is used. |
| 26 | + * |
| 27 | + * @param audience the audience whose locale is used to determine the singular display name |
| 28 | + * @return the singular display name as a {@code Component} for the audience's locale |
| 29 | + */ |
| 30 | + @ApiStatus.NonExtendable |
| 31 | + default Component getDisplayNameSingular(Audience audience) { |
| 32 | + return getDisplayNameSingular(audience.getOrDefault(Identity.LOCALE, Locale.US)); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Retrieves the singular display name component of the currency for the specified locale. |
| 37 | + * |
| 38 | + * @param locale the locale for which the singular display name should be retrieved |
| 39 | + * @return the singular display name as a {@code Component} for the specified locale |
| 40 | + */ |
| 41 | + Component getDisplayNameSingular(Locale locale); |
| 42 | + |
| 43 | + |
| 44 | + /** |
| 45 | + * Retrieves the plural display name component of the currency based on the audience's locale. |
| 46 | + * <p> |
| 47 | + * If the audience does not specify a locale, {@link Locale#US} is used. |
| 48 | + * |
| 49 | + * @param audience the audience whose locale is used to determine the plural display name |
| 50 | + * @return the plural display name as a {@code Component} for the audience's locale |
| 51 | + */ |
| 52 | + @ApiStatus.NonExtendable |
| 53 | + default Component getDisplayNamePlural(Audience audience) { |
| 54 | + return getDisplayNamePlural(audience.getOrDefault(Identity.LOCALE, Locale.US)); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Retrieves the plural display name component of the currency for the specified locale. |
| 59 | + * |
| 60 | + * @param locale the locale for which the plural display name should be retrieved |
| 61 | + * @return the plural display name as a {@code Component} for the specified locale |
| 62 | + */ |
| 63 | + Component getDisplayNamePlural(Locale locale); |
| 64 | + |
| 65 | + /** |
| 66 | + * Retrieves the currency symbol. |
| 67 | + * |
| 68 | + * @return the currency symbol as a component |
| 69 | + */ |
| 70 | + Component getSymbol(); |
| 71 | + |
| 72 | + /** |
| 73 | + * Formats the specified amount as a component. |
| 74 | + * |
| 75 | + * @param amount the amount to be formatted |
| 76 | + * @param audience the audience to format the amount for |
| 77 | + * @return the formatted amount as a component |
| 78 | + * @see #format(Number, Locale) |
| 79 | + */ |
| 80 | + @ApiStatus.NonExtendable |
| 81 | + default Component format(Number amount, Audience audience) { |
| 82 | + return format(amount, audience.getOrDefault(Identity.LOCALE, Locale.US)); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Formats the specified amount as a component. |
| 87 | + * |
| 88 | + * @param amount the amount to be formatted |
| 89 | + * @param locale the locale to format the amount in |
| 90 | + * @return the formatted amount as a component |
| 91 | + * @see #format(Number, Audience) |
| 92 | + */ |
| 93 | + Component format(Number amount, Locale locale); |
| 94 | + |
| 95 | + /** |
| 96 | + * Retrieves the number of fractional digits used for formatting currency amounts. |
| 97 | + * |
| 98 | + * @return the number of fractional digits used for formatting currency amounts |
| 99 | + */ |
| 100 | + int getFractionalDigits(); |
| 101 | + |
| 102 | + /** |
| 103 | + * A builder interface for constructing instances of {@link Currency}. |
| 104 | + * The {@code Builder} allows for the configuration of currency properties such as |
| 105 | + * singular and plural display names, currency symbol, and fractional digits. |
| 106 | + */ |
| 107 | + interface Builder { |
| 108 | + /** |
| 109 | + * Sets the singular display name of the currency for a specific locale. |
| 110 | + * |
| 111 | + * @param name the singular display name component of the currency |
| 112 | + * @param locale the locale for which the singular display name is being set |
| 113 | + * @return the builder instance for chaining |
| 114 | + */ |
| 115 | + Builder displayNameSingular(Component name, Locale locale); |
| 116 | + |
| 117 | + /** |
| 118 | + * Retrieves the singular display name component of the currency for the specified locale. |
| 119 | + * |
| 120 | + * @param locale the locale for which the singular display name should be retrieved |
| 121 | + * @return an {@code Optional} containing the singular display name as a {@code Component}, or empty |
| 122 | + */ |
| 123 | + Optional<Component> displayNameSingular(Locale locale); |
| 124 | + |
| 125 | + /** |
| 126 | + * Sets the plural display name of the currency for a specific locale. |
| 127 | + * |
| 128 | + * @param name the plural display name component of the currency |
| 129 | + * @param locale the locale for which the plural display name is being set |
| 130 | + * @return the builder instance for chaining |
| 131 | + */ |
| 132 | + Builder displayNamePlural(Component name, Locale locale); |
| 133 | + |
| 134 | + /** |
| 135 | + * Retrieves the plural display name component of the currency for the specified locale. |
| 136 | + * |
| 137 | + * @param locale the locale for which the plural display name should be retrieved |
| 138 | + * @return an {@code Optional} containing the plural display name as a {@code Component}, or empty |
| 139 | + */ |
| 140 | + Optional<Component> displayNamePlural(Locale locale); |
| 141 | + |
| 142 | + /** |
| 143 | + * Sets the currency symbol as a {@code Component}. |
| 144 | + * |
| 145 | + * @param symbol the symbol component to represent the currency |
| 146 | + * @return the builder instance for chaining |
| 147 | + */ |
| 148 | + Builder symbol(Component symbol); |
| 149 | + |
| 150 | + /** |
| 151 | + * Retrieves the currency symbol set on the {@code Builder}. |
| 152 | + * |
| 153 | + * @return an {@code Optional} containing the symbol as a {@code Component}, or empty |
| 154 | + */ |
| 155 | + Optional<Component> symbol(); |
| 156 | + |
| 157 | + /** |
| 158 | + * Sets the number of fractional digits to be used for the currency. |
| 159 | + * <p> |
| 160 | + * Fractional digits are generally used to specify the precision of the currency values, |
| 161 | + * for example, 2 fractional digits for most currencies such as USD (representing cents). |
| 162 | + * |
| 163 | + * @param fractionalDigits the number of fractional digits to set (must be a non-negative integer) |
| 164 | + * @return the builder instance for chaining |
| 165 | + * @throws IllegalArgumentException if {@code fractionalDigits} is negative |
| 166 | + */ |
| 167 | + Builder fractionalDigits(int fractionalDigits) throws IllegalArgumentException; |
| 168 | + |
| 169 | + /** |
| 170 | + * Retrieves the number of fractional digits set for the currency. |
| 171 | + * <p> |
| 172 | + * Fractional digits represent the precision of the currency, |
| 173 | + * such as 2 for most currencies like USD (representing cents). |
| 174 | + * |
| 175 | + * @return an {@code OptionalInt} containing the number of fractional digits, or empty |
| 176 | + */ |
| 177 | + OptionalInt fractionalDigits(); |
| 178 | + |
| 179 | + /** |
| 180 | + * Builds and returns a {@link Currency} instance based on the properties set on the {@code Builder}. |
| 181 | + * |
| 182 | + * @return the constructed {@link Currency} instance |
| 183 | + */ |
| 184 | + @ApiStatus.OverrideOnly |
| 185 | + Currency build(); |
| 186 | + } |
| 187 | +} |
0 commit comments