Skip to content

Commit 1f4eda8

Browse files
committed
small refactorings
1 parent 0ec883f commit 1f4eda8

File tree

9 files changed

+168
-66
lines changed

9 files changed

+168
-66
lines changed

hedera-base/src/main/java/com/openelements/hedera/base/ContractParam.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,30 @@ public record ContractParam<T>(@NonNull T value, @NonNull String nativeType, @No
3232
"value '" + value + "' is not valid for native type '" + nativeType + "'");
3333
}
3434
}
35-
35+
3636
private static <T> ContractParam<T> of(T value, ParamSupplier<T> paramSupplier) {
3737
Objects.requireNonNull(paramSupplier, "paramSupplier must not be null");
3838
return new ContractParam<>(value, paramSupplier.getNativeType(), paramSupplier);
3939
}
4040

41+
/**
42+
* Creates a new contract parameter with the given value and native type {@code string}.
43+
*
44+
* @param value the value of the parameter
45+
* @return the new contract parameter
46+
*/
4147
@NonNull
4248
public static ContractParam<String> string(String value) {
4349
return of(value, StringBasedDatatype.STRING);
4450
}
4551

52+
/**
53+
* Creates a new contract parameter with the given value and native type {@code address}. The input must be address
54+
* in solidty format.
55+
*
56+
* @param value the value of the parameter
57+
* @return the new contract parameter
58+
*/
4659
@NonNull
4760
public static ContractParam<String> addressBySolidty(String value) {
4861
return of(value, StringBasedDatatype.ADDRESS);
@@ -70,6 +83,12 @@ public static ContractParam<Boolean> bool(boolean value) {
7083
return of(value, BooleanDatatype.BOOL);
7184
}
7285

86+
/**
87+
* Creates a new contract parameter with the given value and native type {@code int8}.
88+
*
89+
* @param value the value of the parameter
90+
* @return the new contract parameter
91+
*/
7392
@NonNull
7493
public static ContractParam<Long> int8(byte value) {
7594
return of((long) value, LongBasedNumericDatatypes.INT8);

hedera-base/src/main/java/com/openelements/hedera/base/implementation/ProtocolLayerClientImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,8 @@ private <T extends Transaction<T>> Transaction<T> sign(Transaction<T> transactio
466466
private ContractFunctionParameters createParameters(@NonNull final List<ContractParam<?>> params) {
467467
Objects.requireNonNull(params, "params must not be null");
468468
final ContractFunctionParameters constructorParams = new ContractFunctionParameters();
469-
final Consumer<ContractParam> consumer = param -> param.supplier().addParam(param.value(), constructorParams);
469+
final Consumer<ContractParam> consumer = param -> param.supplier()
470+
.addParamToFunctionParameters(param.value(), constructorParams);
470471
params.forEach(consumer);
471472
return constructorParams;
472473
}

hedera-base/src/main/java/com/openelements/hedera/base/implementation/data/BigIntegerBasedNumericDatatypes.java

Lines changed: 102 additions & 53 deletions
Large diffs are not rendered by default.

hedera-base/src/main/java/com/openelements/hedera/base/implementation/data/BooleanDatatype.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public enum BooleanDatatype implements ParamSupplier<Boolean> {
1515
this.addParam = (v, params) -> params.addBool(v);
1616
}
1717

18-
public void addParam(@NonNull final Boolean value, @NonNull final ContractFunctionParameters params) {
18+
public void addParamToFunctionParameters(@NonNull final Boolean value,
19+
@NonNull final ContractFunctionParameters params) {
1920
Objects.requireNonNull(value, "value must not be null");
2021
Objects.requireNonNull(params, "params must not be null");
2122
addParam.accept(value, params);

hedera-base/src/main/java/com/openelements/hedera/base/implementation/data/LongBasedNumericDatatypes.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,23 @@ public enum LongBasedNumericDatatypes implements ParamSupplier<Long> {
2929

3030
private final String nativeType;
3131

32-
LongBasedNumericDatatypes(final String nativeType, final BiConsumer<Long, ContractFunctionParameters> addParam, long minValue, long maxValue) {
32+
LongBasedNumericDatatypes(final String nativeType, final BiConsumer<Long, ContractFunctionParameters> addParam,
33+
long minValue, long maxValue) {
3334
this.nativeType = nativeType;
3435
this.addParam = addParam;
3536
this.minValue = minValue;
3637
this.maxValue = maxValue;
3738
}
3839

3940
@Override
40-
public void addParam(@NonNull final Long value, final ContractFunctionParameters params) {
41+
public void addParamToFunctionParameters(@NonNull final Long value, final ContractFunctionParameters params) {
4142
Objects.requireNonNull(value, "value must not be null");
4243
addParam.accept(value.longValue(), params);
4344
}
4445

4546
@Override
4647
public boolean isValidParam(final Long value) {
47-
if(value == null) {
48+
if (value == null) {
4849
return false;
4950
}
5051
return value >= minValue && value <= maxValue;
@@ -57,7 +58,7 @@ public String getNativeType() {
5758

5859
public void addParam(final long value, @NonNull final ContractFunctionParameters params) {
5960
Objects.requireNonNull(params, "params must not be null");
60-
if(value < minValue || value > maxValue) {
61+
if (value < minValue || value > maxValue) {
6162
throw new IllegalArgumentException("value out of range for type '" + this + "': " + value);
6263
}
6364
addParam.accept(value, params);

hedera-base/src/main/java/com/openelements/hedera/base/implementation/data/ParamSupplier.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,38 @@
22

33
import com.hedera.hashgraph.sdk.ContractFunctionParameters;
44

5+
/**
6+
* Interface for supplying parameters to a smart contract function parameter list. An implementation can be used to add
7+
* data in Java type {@code T} to a {@link ContractFunctionParameters} object. Here the data will normally be converted
8+
* to a native type that can be used in a smart contract function call.
9+
*
10+
* @param <T>
11+
*/
512
public interface ParamSupplier<T> {
613

7-
void addParam(T value, ContractFunctionParameters params);
14+
/**
15+
* Add the given value of Java type {@code T} as a parameter of the native type (see {@link #getNativeType()}) to a
16+
* {@link ContractFunctionParameters} object.
17+
*
18+
* @param value The value to add
19+
* @param params The parameters object to add the value to
20+
*/
21+
void addParamToFunctionParameters(T value, ContractFunctionParameters params);
822

23+
/**
24+
* Check if the given value of Java type {@code T} is a valid parameter type for this supplier. A valid type can be
25+
* converted to the native type (see {@link #getNativeType()}) and added to a {@link ContractFunctionParameters}
26+
* object.
27+
*
28+
* @param value The value to check
29+
* @return {@code true} if the value is a valid parameter type, {@code false} otherwise
30+
*/
931
boolean isValidParam(T value);
1032

33+
/**
34+
* Get the native type of the parameter that this supplier can add to a {@link ContractFunctionParameters} object.
35+
*
36+
* @return The native type
37+
*/
1138
String getNativeType();
1239
}

hedera-base/src/main/java/com/openelements/hedera/base/implementation/data/StringBasedDatatype.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ public enum StringBasedDatatype implements ParamSupplier<String> {
2020
this.addParam = addParam;
2121
}
2222

23-
public void addParam(final String value, @NonNull final ContractFunctionParameters params) {
23+
public void addParamToFunctionParameters(final String value, @NonNull final ContractFunctionParameters params) {
2424
Objects.requireNonNull(params, "params must not be null");
2525
addParam.accept(value, params);
2626
}
2727

2828
@Override
2929
public boolean isValidParam(@NonNull final String value) {
3030
Objects.requireNonNull(value, "value must not be null");
31-
if(this.equals(ADDRESS)) {
31+
if (this.equals(ADDRESS)) {
3232
try {
3333
AccountId.fromString(value);
3434
} catch (final Exception e) {

hedera-base/src/main/java/com/openelements/hedera/base/mirrornode/MirrorNodeClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import java.util.Optional;
99
import org.jspecify.annotations.NonNull;
1010

11+
/**
12+
* A client for querying the Hedera Mirror Node REST API.
13+
*/
1114
public interface MirrorNodeClient {
1215

1316
@NonNull

hedera-base/src/test/java/com/openelements/hedera/base/test/LongBasedNumericDatatypesTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ void checkMethodsForNonNullResult(final LongBasedNumericDatatypes type) {
1717

1818
//then
1919
Assertions.assertThrows(NullPointerException.class, () -> type.addParam(1L, null));
20-
Assertions.assertThrows(NullPointerException.class, () -> type.addParam(null, parameters));
21-
Assertions.assertThrows(NullPointerException.class, () -> type.addParam(Long.valueOf(1L), null));
22-
Assertions.assertThrows(NullPointerException.class, () -> type.addParam(null, null));
20+
Assertions.assertThrows(NullPointerException.class, () -> type.addParamToFunctionParameters(null, parameters));
21+
Assertions.assertThrows(NullPointerException.class,
22+
() -> type.addParamToFunctionParameters(Long.valueOf(1L), null));
23+
Assertions.assertThrows(NullPointerException.class, () -> type.addParamToFunctionParameters(null, null));
2324
}
2425

2526
@Test

0 commit comments

Comments
 (0)