use of io.nem.symbol.sdk.model.transaction.MetadataTransactionFactory in project nem2-sdk-java by nemtech.
the class MetadataTransactionServiceImpl method processMetadata.
/**
* Generic way of processing a metadata entity and creating a new metadata transaction factory
* depending on the existing metadata value. This works for Account, Mosaic and Namespace
* metadata.
*
* @param criteria the criteria
* @param transactionFactory the function that creates a transaction factory
* @param newValue the new value you want to set.
* @param <T> the type of the transaction factory.
* @return an Observable of a transaction factory.
*/
private <T extends MetadataTransactionFactory> Observable<T> processMetadata(MetadataSearchCriteria criteria, TriFunction<byte[], NetworkType, Deadline, T> transactionFactory, String newValue) {
return Observable.combineLatest(networkTypeObservable, epochAdjustmentObservable, (networkType, epochAdjustment) -> metadataRepository.search(criteria).map(page -> {
Deadline deadline = Deadline.create(epochAdjustment);
if (page.getData().isEmpty()) {
return transactionFactory.apply(StringEncoder.getBytes(newValue), networkType, deadline);
} else {
byte[] originalValue = page.getData().get(0).getValue();
byte[] newValueBytes = StringEncoder.getBytes(newValue);
byte[] xoredBytes = ConvertUtils.xor(originalValue, newValueBytes);
T factory = transactionFactory.apply(xoredBytes, networkType, deadline);
factory.valueSizeDelta(newValueBytes.length - originalValue.length);
return factory;
}
})).flatMap(f -> f);
}
Aggregations