Skip to content

Commit 5888939

Browse files
committed
Refactored currency and account APIs for flexibility
Updated `Currency` interface methods to return `Optional` values, improving null safety. Added builders and editing functionalities. Enhanced `Account` methods for simpler balance operations, with `BigDecimal` returns for precision. Bumped version to 3.0.0-pre2.
1 parent 08d10a7 commit 5888939

File tree

6 files changed

+85
-34
lines changed

6 files changed

+85
-34
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group = "net.thenextlvl.services"
8-
version = "3.0.0-pre1"
8+
version = "3.0.0-pre2"
99

1010
java {
1111
toolchain.languageVersion = JavaLanguageVersion.of(21)

plugin/src/main/java/net/thenextlvl/service/wrapper/service/model/WrappedBank.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import net.milkbowl.vault.economy.Economy;
44
import net.thenextlvl.service.ServicePlugin;
5-
import net.thenextlvl.service.api.economy.currency.Currency;
65
import net.thenextlvl.service.api.economy.bank.Bank;
76
import net.thenextlvl.service.api.economy.bank.BankController;
7+
import net.thenextlvl.service.api.economy.currency.Currency;
88
import net.thenextlvl.service.api.economy.currency.CurrencyHolder;
99
import org.bukkit.OfflinePlayer;
1010
import org.bukkit.World;
@@ -70,10 +70,12 @@ public UUID getOwner() {
7070
}
7171

7272
@Override
73-
public void setBalance(Number balance, Currency currency) {
74-
var difference = balance.doubleValue() - getBalance(currency).doubleValue();
75-
if (difference > 0) deposit(difference, currency);
76-
else if (difference < 0) withdraw(-difference, currency);
73+
public BigDecimal setBalance(Number balance, Currency currency) {
74+
var current = getBalance(currency);
75+
var difference = balance.doubleValue() - current.doubleValue();
76+
if (difference > 0) return deposit(difference, currency);
77+
else if (difference < 0) return withdraw(-difference, currency);
78+
return current;
7779
}
7880

7981
@Override

src/main/java/net/thenextlvl/service/api/economy/Account.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ default BigDecimal deposit(Number amount) {
4646
* @return the new balance after the deposit
4747
* @since 3.0.0
4848
*/
49-
BigDecimal deposit(Number amount, Currency currency);
49+
default BigDecimal deposit(Number amount, Currency currency) {
50+
return setBalance(getBalance(currency).add(BigDecimal.valueOf(amount.doubleValue())), currency);
51+
}
5052

5153
/**
5254
* Retrieves the balance of the account.
@@ -88,7 +90,9 @@ default BigDecimal withdraw(Number amount) {
8890
* @return the new balance after the withdrawal
8991
* @since 3.0.0
9092
*/
91-
BigDecimal withdraw(Number amount, Currency currency);
93+
default BigDecimal withdraw(Number amount, Currency currency) {
94+
return setBalance(getBalance(currency).subtract(BigDecimal.valueOf(amount.doubleValue())), currency);
95+
}
9296

9397
/**
9498
* Returns an optional containing the world associated with this account.
@@ -147,7 +151,8 @@ default void setBalance(Number balance) {
147151
*
148152
* @param balance the new balance to be set
149153
* @param currency the currency of the balance
154+
* @return the new balance after the operation
150155
* @since 3.0.0
151156
*/
152-
void setBalance(Number balance, Currency currency);
157+
BigDecimal setBalance(Number balance, Currency currency);
153158
}

src/main/java/net/thenextlvl/service/api/economy/EconomyController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public interface EconomyController extends Controller, CurrencyHolder {
3434
*/
3535
@Deprecated(forRemoval = true, since = "3.0.0")
3636
default String getCurrencyNamePlural(Locale locale) {
37-
return PlainTextComponentSerializer.plainText().serialize(getDefaultCurrency().getDisplayNamePlural(locale));
37+
return getDefaultCurrency().getDisplayNamePlural(locale).map(PlainTextComponentSerializer.plainText()::serialize).orElse("");
3838
}
3939

4040
/**
@@ -46,7 +46,7 @@ default String getCurrencyNamePlural(Locale locale) {
4646
*/
4747
@Deprecated(forRemoval = true, since = "3.0.0")
4848
default String getCurrencyNameSingular(Locale locale) {
49-
return PlainTextComponentSerializer.plainText().serialize(getDefaultCurrency().getDisplayNameSingular(locale));
49+
return getDefaultCurrency().getDisplayNameSingular(locale).map(PlainTextComponentSerializer.plainText()::serialize).orElse("");
5050
}
5151

5252
/**

src/main/java/net/thenextlvl/service/api/economy/currency/Currency.java

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
import org.jetbrains.annotations.Contract;
88
import org.jetbrains.annotations.Unmodifiable;
99
import org.jspecify.annotations.NullMarked;
10+
import org.jspecify.annotations.Nullable;
1011

1112
import java.util.Locale;
1213
import java.util.Map;
1314
import java.util.Optional;
1415
import java.util.OptionalInt;
16+
import java.util.function.Consumer;
1517

1618
/**
1719
* Represents a currency with support for localization, formatting, and symbolic representation.
@@ -34,39 +36,39 @@ public interface Currency {
3436
* If the audience does not specify a locale, {@link Locale#US} is used.
3537
*
3638
* @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
3840
*/
39-
default Component getDisplayNameSingular(Audience audience) {
41+
default Optional<Component> getDisplayNameSingular(Audience audience) {
4042
return getDisplayNameSingular(audience.getOrDefault(Identity.LOCALE, Locale.US));
4143
}
4244

4345
/**
4446
* Retrieves the singular display name component of the currency for the specified locale.
4547
*
4648
* @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
4850
*/
49-
Component getDisplayNameSingular(Locale locale);
51+
Optional<Component> getDisplayNameSingular(Locale locale);
5052

5153
/**
5254
* Retrieves the plural display name component of the currency based on the audience's locale.
5355
* <p>
5456
* If the audience does not specify a locale, {@link Locale#US} is used.
5557
*
5658
* @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
5860
*/
59-
default Component getDisplayNamePlural(Audience audience) {
61+
default Optional<Component> getDisplayNamePlural(Audience audience) {
6062
return getDisplayNamePlural(audience.getOrDefault(Identity.LOCALE, Locale.US));
6163
}
6264

6365
/**
6466
* Retrieves the plural display name component of the currency for the specified locale.
6567
*
6668
* @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
6870
*/
69-
Component getDisplayNamePlural(Locale locale);
71+
Optional<Component> getDisplayNamePlural(Locale locale);
7072

7173
/**
7274
* Retrieves the currency symbol.
@@ -104,12 +106,44 @@ default Component format(Number amount, Audience audience) {
104106
*/
105107
int getFractionalDigits();
106108

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+
107125
/**
108126
* A builder interface for constructing instances of {@link Currency}.
109127
* The {@code Builder} allows for the configuration of currency properties such as
110128
* singular and plural display names, currency symbol, and fractional digits.
111129
*/
112130
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+
113147
/**
114148
* Retrieves a map containing the singular display names of the currency for various locales.
115149
*
@@ -122,12 +156,12 @@ interface Builder {
122156
/**
123157
* Sets the singular display name of the currency for a specific locale.
124158
*
159+
* @param locale the locale for which the singular display name is being set, or {@code null} to remove
125160
* @param name the singular display name component of the currency
126-
* @param locale the locale for which the singular display name is being set
127161
* @return the builder instance for chaining
128162
*/
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);
131165

132166
/**
133167
* Retrieves the singular display name component of the currency for the specified locale.
@@ -150,12 +184,12 @@ interface Builder {
150184
/**
151185
* Sets the plural display name of the currency for a specific locale.
152186
*
187+
* @param locale the locale for which the plural display name is being set, or {@code null} to remove
153188
* @param name the plural display name component of the currency
154-
* @param locale the locale for which the plural display name is being set
155189
* @return the builder instance for chaining
156190
*/
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);
159193

160194
/**
161195
* Retrieves the plural display name component of the currency for the specified locale.
@@ -169,11 +203,11 @@ interface Builder {
169203
/**
170204
* Sets the currency symbol as a {@code Component}.
171205
*
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
173207
* @return the builder instance for chaining
174208
*/
175-
@Contract(value = "_ -> this", pure = true)
176-
Builder symbol(Component symbol);
209+
@Contract(value = "_ -> this")
210+
Builder symbol(@Nullable Component symbol);
177211

178212
/**
179213
* Retrieves the currency symbol set on the {@code Builder}.
@@ -189,12 +223,12 @@ interface Builder {
189223
* Fractional digits are generally used to specify the precision of the currency values,
190224
* for example, 2 fractional digits for most currencies such as USD (representing cents).
191225
*
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
193227
* @return the builder instance for chaining
194228
* @throws IllegalArgumentException if {@code fractionalDigits} is negative
195229
*/
196230
@Contract(value = "_ -> this")
197-
Builder fractionalDigits(int fractionalDigits) throws IllegalArgumentException;
231+
Builder fractionalDigits(@Nullable Integer fractionalDigits) throws IllegalArgumentException;
198232

199233
/**
200234
* Retrieves the number of fractional digits set for the currency.

src/main/java/net/thenextlvl/service/api/economy/currency/CurrencyHolder.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,26 @@ default boolean hasCurrency(String name) {
7777

7878
/**
7979
* Creates a new currency by configuring a {@link Currency.Builder}.
80-
* <p>
81-
* If a currency with the same name already exists, this method returns empty.
8280
*
8381
* @param name the name of the new currency
8482
* @param builder a consumer to configure the {@link Currency.Builder} for currency creation
85-
* @return an optional containing the created {@link Currency}, or empty
83+
* @return the newly created {@link Currency}
8684
* @throws UnsupportedOperationException if {@link #hasMultiCurrencySupport()} is {@code false}
85+
* @throws IllegalArgumentException if a currency with the same name already exists
8786
*/
8887
@Contract("_, _ -> new")
89-
default Optional<Currency> createCurrency(String name, Consumer<Currency.Builder> builder) {
88+
default Currency createCurrency(String name, Consumer<Currency.Builder> builder) throws IllegalArgumentException {
89+
throw new UnsupportedOperationException("Not implemented yet");
90+
}
91+
92+
/**
93+
* Adds a new currency to the currency holder.
94+
*
95+
* @param currency the {@code Currency} object to add
96+
* @return {@code true} if the currency was successfully added, otherwise {@code false}
97+
* @throws UnsupportedOperationException if {@link #hasMultiCurrencySupport()} is {@code false}
98+
*/
99+
default boolean addCurrency(Currency currency) {
90100
throw new UnsupportedOperationException("Not implemented yet");
91101
}
92102

0 commit comments

Comments
 (0)