Skip to content

Commit b36da65

Browse files
committed
Added @Contract annotations for API methods
Enhanced method contracts with `@Contract` annotations to improve nullability and immutability handling while clarifying method behaviors.
1 parent 65b6d1b commit b36da65

File tree

6 files changed

+35
-4
lines changed

6 files changed

+35
-4
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.thenextlvl.service.api.economy.currency.Currency;
44
import net.thenextlvl.service.api.economy.currency.CurrencyHolder;
55
import org.bukkit.World;
6+
import org.jetbrains.annotations.Contract;
67
import org.jspecify.annotations.NullMarked;
78

89
import java.math.BigDecimal;
@@ -20,6 +21,7 @@ public interface Account extends Comparable<Account> {
2021
* @return the {@code CurrencyHolder} capable of managing currencies for the account
2122
*/
2223
CurrencyHolder getController();
24+
@Contract(pure = true)
2325

2426
/**
2527
* Deposits the specified amount into the account balance.

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import net.thenextlvl.service.api.economy.currency.CurrencyHolder;
77
import org.bukkit.OfflinePlayer;
88
import org.bukkit.World;
9+
import org.jetbrains.annotations.Contract;
910
import org.jetbrains.annotations.Unmodifiable;
1011
import org.jspecify.annotations.NullMarked;
1112

@@ -167,6 +168,7 @@ default CompletableFuture<Optional<Account>> tryGetAccount(UUID uuid, World worl
167168
* @return a CompletableFuture that will complete with the created account
168169
* @throws IllegalStateException if a similar account already exists
169170
*/
171+
@Contract("_ -> new")
170172
default CompletableFuture<Account> createAccount(OfflinePlayer player) {
171173
return createAccount(player.getUniqueId());
172174
}
@@ -178,7 +180,8 @@ default CompletableFuture<Account> createAccount(OfflinePlayer player) {
178180
* @param world the world in which the player's account will be created
179181
* @return a CompletableFuture that will complete with the created account
180182
*/
181-
default CompletableFuture<Account> createAccount(OfflinePlayer player, World world) {
183+
@Contract("_, _ -> new")
184+
default CompletableFuture<Account> createAccount(OfflinePlayer player, @Nullable World world) {
182185
return createAccount(player.getUniqueId(), world);
183186
}
184187

@@ -188,7 +191,10 @@ default CompletableFuture<Account> createAccount(OfflinePlayer player, World wor
188191
* @param uuid the uuid of the account to be created
189192
* @return a CompletableFuture that will complete with the created account
190193
*/
191-
CompletableFuture<Account> createAccount(UUID uuid);
194+
@Contract("_ -> new")
195+
default CompletableFuture<Account> createAccount(UUID uuid) {
196+
return createAccount(uuid, null);
197+
}
192198

193199
/**
194200
* Creates an account with the given uuid and world.
@@ -197,7 +203,8 @@ default CompletableFuture<Account> createAccount(OfflinePlayer player, World wor
197203
* @param world the world in which the account will be created
198204
* @return a CompletableFuture that will complete with the created account
199205
*/
200-
CompletableFuture<Account> createAccount(UUID uuid, World world);
206+
@Contract("_, _ -> new")
207+
CompletableFuture<Account> createAccount(UUID uuid, @Nullable World world);
201208

202209
/**
203210
* Loads the account for the specified player asynchronously.

src/main/java/net/thenextlvl/service/api/economy/bank/Bank.java

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

33
import net.thenextlvl.service.api.economy.Account;
44
import org.bukkit.OfflinePlayer;
5+
import org.jetbrains.annotations.Contract;
56
import org.jetbrains.annotations.Unmodifiable;
67
import org.jspecify.annotations.NullMarked;
78

@@ -28,6 +29,7 @@ public interface Bank extends Account {
2829
*
2930
* @return the name of the bank.
3031
*/
32+
@Contract(pure = true)
3133
String getName();
3234

3335
/**

src/main/java/net/thenextlvl/service/api/economy/bank/BankController.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.thenextlvl.service.api.economy.currency.CurrencyHolder;
55
import org.bukkit.OfflinePlayer;
66
import org.bukkit.World;
7+
import org.jetbrains.annotations.Contract;
78
import org.jetbrains.annotations.Unmodifiable;
89
import org.jspecify.annotations.NullMarked;
910

@@ -23,6 +24,7 @@ public interface BankController extends Controller, CurrencyHolder {
2324
* @param name the name of the bank (must be unique)
2425
* @return a CompletableFuture that completes with the created bank
2526
*/
27+
@Contract("_, _ -> new")
2628
default CompletableFuture<Bank> createBank(OfflinePlayer player, String name) {
2729
return createBank(player.getUniqueId(), name);
2830
}
@@ -37,7 +39,8 @@ default CompletableFuture<Bank> createBank(OfflinePlayer player, String name) {
3739
* @param world the world in which the bank is located
3840
* @return a CompletableFuture that completes with the created bank
3941
*/
40-
default CompletableFuture<Bank> createBank(OfflinePlayer player, String name, World world) {
42+
@Contract("_, _, _ -> new")
43+
default CompletableFuture<Bank> createBank(OfflinePlayer player, String name, @Nullable World world) {
4144
return createBank(player.getUniqueId(), name, world);
4245
}
4346

@@ -51,6 +54,7 @@ default CompletableFuture<Bank> createBank(OfflinePlayer player, String name, Wo
5154
* @return a CompletableFuture that completes with the created bank
5255
*/
5356
CompletableFuture<Bank> createBank(UUID uuid, String name);
57+
@Contract("_, _ -> new")
5458

5559
/**
5660
* Creates a new bank with the provided UUID, name, and world.
@@ -63,6 +67,7 @@ default CompletableFuture<Bank> createBank(OfflinePlayer player, String name, Wo
6367
* @return a CompletableFuture that completes with the created bank
6468
*/
6569
CompletableFuture<Bank> createBank(UUID uuid, String name, World world);
70+
@Contract("_, _, _ -> new")
6671

6772
/**
6873
* Loads a bank asynchronously with the specified player.

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.kyori.adventure.identity.Identity;
55
import net.kyori.adventure.text.Component;
66
import org.jetbrains.annotations.ApiStatus;
7+
import org.jetbrains.annotations.Contract;
78
import org.jspecify.annotations.NullMarked;
89

910
import java.util.Locale;
@@ -17,6 +18,7 @@ public interface Currency {
1718
*
1819
* @return the name of the currency as a string
1920
*/
21+
@Contract(pure = true)
2022
String getName();
2123

2224
/**
@@ -112,6 +114,7 @@ interface Builder {
112114
* @param locale the locale for which the singular display name is being set
113115
* @return the builder instance for chaining
114116
*/
117+
@Contract(value = "_, _ -> this", pure = true)
115118
Builder displayNameSingular(Component name, Locale locale);
116119

117120
/**
@@ -120,6 +123,7 @@ interface Builder {
120123
* @param locale the locale for which the singular display name should be retrieved
121124
* @return an {@code Optional} containing the singular display name as a {@code Component}, or empty
122125
*/
126+
@Contract(value = "_ -> new")
123127
Optional<Component> displayNameSingular(Locale locale);
124128

125129
/**
@@ -129,6 +133,7 @@ interface Builder {
129133
* @param locale the locale for which the plural display name is being set
130134
* @return the builder instance for chaining
131135
*/
136+
@Contract(value = "_, _ -> this", pure = true)
132137
Builder displayNamePlural(Component name, Locale locale);
133138

134139
/**
@@ -137,6 +142,7 @@ interface Builder {
137142
* @param locale the locale for which the plural display name should be retrieved
138143
* @return an {@code Optional} containing the plural display name as a {@code Component}, or empty
139144
*/
145+
@Contract(value = "_ -> new")
140146
Optional<Component> displayNamePlural(Locale locale);
141147

142148
/**
@@ -145,13 +151,15 @@ interface Builder {
145151
* @param symbol the symbol component to represent the currency
146152
* @return the builder instance for chaining
147153
*/
154+
@Contract(value = "_ -> this", pure = true)
148155
Builder symbol(Component symbol);
149156

150157
/**
151158
* Retrieves the currency symbol set on the {@code Builder}.
152159
*
153160
* @return an {@code Optional} containing the symbol as a {@code Component}, or empty
154161
*/
162+
@Contract(value = "-> new")
155163
Optional<Component> symbol();
156164

157165
/**
@@ -164,6 +172,7 @@ interface Builder {
164172
* @return the builder instance for chaining
165173
* @throws IllegalArgumentException if {@code fractionalDigits} is negative
166174
*/
175+
@Contract(value = "_ -> this")
167176
Builder fractionalDigits(int fractionalDigits) throws IllegalArgumentException;
168177

169178
/**
@@ -174,13 +183,15 @@ interface Builder {
174183
*
175184
* @return an {@code OptionalInt} containing the number of fractional digits, or empty
176185
*/
186+
@Contract(value = "-> new")
177187
OptionalInt fractionalDigits();
178188

179189
/**
180190
* Builds and returns a {@link Currency} instance based on the properties set on the {@code Builder}.
181191
*
182192
* @return the constructed {@link Currency} instance
183193
*/
194+
@Contract("-> new")
184195
@ApiStatus.OverrideOnly
185196
Currency build();
186197
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.thenextlvl.service.api.economy.currency;
22

33
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
4+
import org.jetbrains.annotations.Contract;
45
import org.jetbrains.annotations.Unmodifiable;
56

67
import java.util.Locale;
@@ -83,6 +84,7 @@ default boolean hasCurrency(String name) {
8384
* @throws UnsupportedOperationException if {@link #hasMultiCurrencySupport()} is {@code false}
8485
* @since 3.0.0
8586
*/
87+
@Contract("_, _ -> new")
8688
default Optional<Currency> createCurrency(String name, Consumer<Currency.Builder> builder) {
8789
throw new UnsupportedOperationException("Not implemented yet");
8890
}
@@ -105,6 +107,7 @@ default boolean deleteCurrency(String name) {
105107
* @return the default currency
106108
* @since 3.0.0
107109
*/
110+
@Contract(pure = true)
108111
Currency getDefaultCurrency();
109112

110113
/**
@@ -119,6 +122,7 @@ default boolean deleteCurrency(String name) {
119122
* @see #hasCurrency(String)
120123
* @since 3.0.0
121124
*/
125+
@Contract(pure = true)
122126
default boolean hasMultiCurrencySupport() {
123127
return false;
124128
}

0 commit comments

Comments
 (0)