-
Notifications
You must be signed in to change notification settings - Fork 134
Convert exchange rate API to Java #12166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package org.hiero.mirror.restjava.dto; | ||
|
|
||
| import com.google.protobuf.GeneratedMessage; | ||
| import org.hiero.mirror.common.domain.file.FileData; | ||
|
|
||
| public record SystemFile<T extends GeneratedMessage>(FileData fileData, T protobuf) {} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package org.hiero.mirror.restjava.mapper; | ||
|
|
||
| import static org.hiero.mirror.restjava.mapper.CommonMapper.QUALIFIER_TIMESTAMP; | ||
|
|
||
| import com.hederahashgraph.api.proto.java.ExchangeRateSet; | ||
| import org.hiero.mirror.rest.model.ExchangeRate; | ||
| import org.hiero.mirror.rest.model.NetworkExchangeRateSetResponse; | ||
| import org.hiero.mirror.restjava.dto.SystemFile; | ||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.Mapping; | ||
|
|
||
| @Mapper(config = MapperConfiguration.class, uses = CommonMapper.class) | ||
| public interface ExchangeRateMapper { | ||
|
|
||
| @Mapping(source = "protobuf.currentRate", target = "currentRate") | ||
|
Check notice on line 17 in rest-java/src/main/java/org/hiero/mirror/restjava/mapper/ExchangeRateMapper.java
|
||
| @Mapping(source = "protobuf.nextRate", target = "nextRate") | ||
| @Mapping(source = "fileData.consensusTimestamp", target = "timestamp", qualifiedByName = QUALIFIER_TIMESTAMP) | ||
| NetworkExchangeRateSetResponse map(SystemFile<ExchangeRateSet> source); | ||
|
|
||
| @Mapping(source = "centEquiv", target = "centEquivalent") | ||
|
Check notice on line 22 in rest-java/src/main/java/org/hiero/mirror/restjava/mapper/ExchangeRateMapper.java
|
||
| @Mapping(source = "hbarEquiv", target = "hbarEquivalent") | ||
| ExchangeRate map(com.hederahashgraph.api.proto.java.ExchangeRate source); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package org.hiero.mirror.restjava.parameter; | ||
|
|
||
| import java.util.regex.Pattern; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.hiero.mirror.common.util.DomainUtils; | ||
| import org.hiero.mirror.restjava.common.RangeOperator; | ||
| import org.hiero.mirror.restjava.common.RangeParameter; | ||
|
|
||
| public record TimestampParameter(RangeOperator operator, Long value) implements RangeParameter<Long> { | ||
|
|
||
| public static final TimestampParameter EMPTY = new TimestampParameter(null, null); | ||
|
|
||
| private static final String ERROR = "Invalid timestamp parameter"; | ||
| private static final Pattern PATTERN = | ||
| Pattern.compile("^((eq|gt|gte|lt|lte|ne):)?(\\d{1,17})(\\.(\\d{1,9}))?$", Pattern.CASE_INSENSITIVE); | ||
|
|
||
| public static TimestampParameter valueOf(String param) { | ||
| if (StringUtils.isBlank(param)) { | ||
| return EMPTY; | ||
| } | ||
|
|
||
| var matcher = PATTERN.matcher(param); | ||
| if (!matcher.matches()) { | ||
| throw new IllegalArgumentException(ERROR); | ||
| } | ||
|
|
||
| final var operator = parseOperator(matcher.group(2)); | ||
| final var timestamp = parseTimestamp(matcher.group(3), matcher.group(5)); | ||
| return new TimestampParameter(operator, timestamp); | ||
| } | ||
|
|
||
| private static RangeOperator parseOperator(String name) { | ||
| if (StringUtils.isEmpty(name)) { | ||
| return RangeOperator.EQ; | ||
| } | ||
|
|
||
| final var operator = RangeOperator.of(name); | ||
|
|
||
| if (operator == RangeOperator.NE) { | ||
| throw new IllegalArgumentException(ERROR); | ||
| } | ||
|
|
||
| return operator; | ||
| } | ||
|
|
||
| private static long parseTimestamp(String secondsStr, String nanosStr) { | ||
| try { | ||
| final long seconds = Long.parseLong(secondsStr); | ||
| final long nanos = StringUtils.isNotEmpty(nanosStr) ? Long.parseLong(nanosStr) : 0L; | ||
| return DomainUtils.convertToNanos(seconds, nanos); | ||
| } catch (RuntimeException e) { | ||
|
Check warning on line 53 in rest-java/src/main/java/org/hiero/mirror/restjava/parameter/TimestampParameter.java
|
||
| throw new IllegalArgumentException(ERROR); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.