|
1 | 1 | package com.bloxbean.cardano.client.transaction.spec;
|
2 | 2 |
|
| 3 | +import co.nstant.in.cbor.model.Map; |
3 | 4 | import co.nstant.in.cbor.model.Number;
|
4 | 5 | import co.nstant.in.cbor.model.*;
|
5 | 6 | import com.bloxbean.cardano.client.common.cbor.CborSerializationUtil;
|
6 | 7 | import com.bloxbean.cardano.client.common.cbor.custom.SortedMap;
|
7 |
| -import com.bloxbean.cardano.client.util.Tuple; |
8 | 8 | import lombok.AllArgsConstructor;
|
9 | 9 | import lombok.Builder;
|
10 | 10 | import lombok.Data;
|
11 | 11 | import lombok.NoArgsConstructor;
|
12 | 12 |
|
13 | 13 | import java.math.BigInteger;
|
14 |
| -import java.util.ArrayList; |
15 |
| -import java.util.Collections; |
16 |
| -import java.util.HashMap; |
17 |
| -import java.util.List; |
| 14 | +import java.util.*; |
18 | 15 | import java.util.stream.Collectors;
|
19 | 16 | import java.util.stream.Stream;
|
20 | 17 |
|
@@ -172,27 +169,42 @@ public Value add(String policyId, String assetName, BigInteger amount) {
|
172 | 169 | return this.add(from(policyId, assetName, amount));
|
173 | 170 | }
|
174 | 171 |
|
175 |
| - public Value add(String unit, BigInteger amount) { |
176 |
| - Tuple<String, String> policyAndAssetName = getPolicyAndAssetName(unit); |
177 |
| - return this.add(from(policyAndAssetName._1, policyAndAssetName._2, amount)); |
| 172 | + /** |
| 173 | + * Adds the specified coin(lovelace) amount to the current {@code Value} instance. |
| 174 | + * |
| 175 | + * @param amount The amount in lovelace to be added |
| 176 | + * @return A new {@code Value} instance with the added coin amount. |
| 177 | + */ |
| 178 | + public Value addCoin(BigInteger amount) { |
| 179 | + return this.add(fromCoin(amount)); |
178 | 180 | }
|
179 | 181 |
|
180 |
| - |
| 182 | + /** |
| 183 | + * Creates a new Value instance from provided policy ID, asset name, and amount. |
| 184 | + * |
| 185 | + * @param policyId The policy ID associated with the asset. |
| 186 | + * @param assetName The name of the asset. |
| 187 | + * @param amount The amount of the asset. |
| 188 | + * @return A new Value instance containing the provided asset information. |
| 189 | + */ |
181 | 190 | public static Value from(String policyId, String assetName, BigInteger amount) {
|
182 |
| - if ((policyId != null && policyId.equals("lovelace")) || (assetName != null && assetName.equals("lovelace"))) { |
183 |
| - return fromLovelace(amount); |
184 |
| - } else { |
185 |
| - return Value.builder() |
186 |
| - .multiAssets(List.of(MultiAsset.builder() |
187 |
| - .policyId(policyId) |
188 |
| - .assets(List.of(Asset.builder().name(assetName).value(amount).build())) |
189 |
| - .build())) |
190 |
| - .build(); |
191 |
| - } |
| 191 | + Objects.requireNonNull(policyId); |
| 192 | + return Value.builder() |
| 193 | + .multiAssets(List.of(MultiAsset.builder() |
| 194 | + .policyId(policyId) |
| 195 | + .assets(List.of(Asset.builder().name(assetName).value(amount).build())) |
| 196 | + .build())) |
| 197 | + .build(); |
192 | 198 | }
|
193 | 199 |
|
194 |
| - public static Value fromLovelace(BigInteger lovelaces) { |
195 |
| - return Value.builder().coin(lovelaces).build(); |
| 200 | + /** |
| 201 | + * Creates a {@link Value} instance from the given amount of lovelaces. |
| 202 | + * |
| 203 | + * @param coin The amount of lovelaces to be converted into a {@link Value} instance. |
| 204 | + * @return A new {@link Value} instance containing the specified amount of lovelaces. |
| 205 | + */ |
| 206 | + public static Value fromCoin(BigInteger coin) { |
| 207 | + return Value.builder().coin(coin).build(); |
196 | 208 | }
|
197 | 209 |
|
198 | 210 | /**
|
@@ -237,44 +249,53 @@ public Value minus(Value that) {
|
237 | 249 | }
|
238 | 250 |
|
239 | 251 |
|
240 |
| - public Value subtractLovelace(BigInteger amount) { |
241 |
| - return this.subtract(fromLovelace(amount)); |
| 252 | + /** |
| 253 | + * Subtracts the specified coin (lovelace) amount from the current {@code Value} instance. |
| 254 | + * |
| 255 | + * @param amount The amount in lovelace to be subtracted. |
| 256 | + * @return A new {@code Value} instance with the subtracted coin amount. |
| 257 | + */ |
| 258 | + public Value substractCoin(BigInteger amount) { |
| 259 | + return this.subtract(fromCoin(amount)); |
242 | 260 | }
|
243 | 261 |
|
| 262 | + /** |
| 263 | + * Subtracts a specified amount of an asset from the current {@code Value} instance. |
| 264 | + * |
| 265 | + * @param policyId The policy ID associated with the asset. |
| 266 | + * @param assetName The name of the asset. |
| 267 | + * @param amount The amount of the asset to be subtracted. |
| 268 | + * @return A new {@code Value} instance with the subtracted asset amount. |
| 269 | + */ |
244 | 270 | public Value subtract(String policyId, String assetName, BigInteger amount) {
|
245 | 271 | return this.subtract(from(policyId, assetName, amount));
|
246 | 272 | }
|
247 | 273 |
|
248 |
| - public Value subtract(String unit, BigInteger amount) { |
249 |
| - Tuple<String, String> policyAndAssetName = getPolicyAndAssetName(unit); |
250 |
| - return this.subtract(from(policyAndAssetName._1, policyAndAssetName._2, amount)); |
251 |
| - } |
252 |
| - |
253 |
| - |
| 274 | + /** |
| 275 | + * Returns the amount of a specific asset identified by the given policy ID and asset name. |
| 276 | + * If the asset is not found, the method returns BigInteger.ZERO. |
| 277 | + * |
| 278 | + * @param policyId The policy ID corresponding to the asset. |
| 279 | + * @param assetName The name of the asset. |
| 280 | + * @return The amount of the specified asset as a BigInteger, or BigInteger.ZERO if the asset is not found. |
| 281 | + */ |
254 | 282 | public BigInteger amountOf(String policyId, String assetName) {
|
255 | 283 | return getMultiAssets()
|
256 | 284 | .stream()
|
257 | 285 | .filter(multiAsset -> multiAsset.getPolicyId().equals(policyId))
|
258 | 286 | .findAny()
|
259 | 287 | .stream()
|
260 |
| - .flatMap(multiAsset -> multiAsset.getAssets().stream().filter(asset -> asset.getName().equals(assetName))) |
| 288 | + .flatMap(multiAsset -> multiAsset.getAssets().stream().filter(asset -> asset.hasName(assetName))) |
261 | 289 | .map(Asset::getValue)
|
262 | 290 | .findAny()
|
263 | 291 | .orElse(BigInteger.ZERO);
|
264 | 292 | }
|
265 | 293 |
|
266 |
| - public BigInteger amountOf(String unit) { |
267 |
| - Tuple<String, String> policyAndAssetName = getPolicyAndAssetName(unit); |
268 |
| - return amountOf(policyAndAssetName._1, policyAndAssetName._2); |
269 |
| - } |
270 |
| - |
271 |
| - private Tuple<String, String> getPolicyAndAssetName(String unit) { |
272 |
| - String sanitisedUnit = unit.replace(".", ""); |
273 |
| - String policyId = sanitisedUnit.substring(0, 56); |
274 |
| - String assetName = "0x" + sanitisedUnit.substring(56); |
275 |
| - return new Tuple<>(policyId, assetName); |
276 |
| - } |
277 |
| - |
| 294 | + /** |
| 295 | + * Determines if the value represented by this instance is zero. |
| 296 | + * |
| 297 | + * @return true if both `multiAssets` is null or empty and `coin` equals to zero, otherwise false. |
| 298 | + */ |
278 | 299 | public boolean isZero() {
|
279 | 300 | return (multiAssets == null || multiAssets.isEmpty()) && BigInteger.ZERO.equals(coin);
|
280 | 301 | }
|
|
0 commit comments