将此功能迁移到RxJava

在我的项目中,我使用RxJava2,因为我发现它非常强大。不过,我一直在努力寻找如何将方法迁移到RxJava2语法的方法。

应用程序读取XML文件,并在XML的每个节点上发出可观察到的信息。然后,我对每个被忽略的项目执行一些任务,最后将所有操作分组在一个列表中。

问题在于某些操作被“重复”了。我们来看一个例子:

<CashTransaction dateTime="07/06/19;20:20:00" type="Withholding Tax" symbol="LNR" amount="-3.02" fxRateToBase="0.66496" />
<CashTransaction dateTime="07/06/19;20:20:00" type="Withholding Tax" symbol="LNR" amount="3.02" fxRateToBase="0.68529" />
<CashTransaction dateTime="07/06/19;20:20:00" type="Withholding Tax" symbol="LNR" amount="-3.02" fxRateToBase="0.68529" />

我在XML中有3种不同的操作,但我想做一个单独的操作,以显示与3种操作相同的内容:

<CashTransaction dateTime="07/06/19;20:20:00" type="Withholding Tax" symbol="LNR" amount="-3.02" fxRateToBase="0.66496" />

此刻,我正在最终列表上执行此方法,但我想知道是否可以在Observable Stream中执行此操作。

public static List<iIB> groupSplittedOperations(List<iIB> iIBNewTransactions)
{
    ArrayList<iIB> groupedOperations = new ArrayList<>();

    //Iterate all over the operations retrieved from the XML report
    for(int originalIndex = 0; originalIndex < iIBNewTransactions.size(); originalIndex++)
    {
        boolean exists = false;

        //This is current operation
        iIB currentOperation = iIBNewTransactions.get(originalIndex);

        //Check if current operation could be grouped into another operation iterating all 
        //over grouped operations
        for(int groupedIndex = 0; groupedIndex < groupedOperations.size(); groupedIndex++)
        {
            //This is current grouped operation
            iIB groupedOperation = groupedOperations.get(groupedIndex);

            //This is the criteria to determine if an operation could be grouped into another one
            if(currentOperation.getDateTime().equals(groupedOperation.getDateTime())
                && currentOperation.getOperationType().equals(groupedOperation.getOperationType())
                && currentOperation.getSymbol().equals(groupedOperation.getSymbol())
                && currentOperation.getDescription().equals(groupedOperation.getDescription()))
            {
                //Update amount of new operation and mark it as existent to avoid adding it to the grouped list
                groupedOperation.setamount(String.valueOf(groupedOperation.getamount() + currentOperation.getamount()));
                exists = true;
                break;
            }
        }

        //Only add operation to grouped list if it was not found on previous search
        if(!exists)
            groupedOperations.add(currentOperation);
    }

    return groupedOperations;
}
tkhxq 回答:将此功能迁移到RxJava

最后,我得到了这段按预期方式运行的代码。谢谢@Froyo的指导。

//For each XML node,convert it to the appropriate object (cashTransaction or Trade)
//I'm not interested on CashTransactions of type 'Other_fees' with description containing 'CONSOLIDATED SNAPSHOTS'
Observable.just(XML.getObjectFromXMLNode(node)))
.filter(iIBTransaction -> !iIBTransaction.getDescription().contains(CashTransaction.OtherFees_filter))
.filter(iIBTransaction -> iIBTransaction.getTransactionID() != null)
.filter(iIBTransaction -> !iIBTransaction.getDateTime().equals("17/04/19"))

//As there might be some splitted or duplicated operations,I'd like to group them in a single one
.groupBy(iIB::getSymbol)
.flatMap(
 grp -> grp.groupBy(iIB::getDateTime)
     .flatMap(grp2 -> grp2.groupBy(iIB::getDescription))
         .flatMap(gpr3 -> gpr3.groupBy(iIB::getOperationType))
            .flatMapSingle(Observable::toList)
)

//The previous groupBy block emits List<iIB> that have the same datetime,description,operationType and symbol
.flatMap((Function<List<iIB>,ObservableSource<iIB>>) listiIB ->

    //For each List<iIB> group them in a single iIB
    listiIB.get(0).getTradePrice().equals(AppConstants.defaultEmptyFloat) ?
        Observable.just(CashTransaction.groupCashTransactions(listiIB)) :
        Observable.just(Trade.groupTrades(listiIB)))


//Then we check if current iiB is in Firebase (check its transactionID).
// If it is in Firebase,a new CashTransaction is returned (tradePrice == null).
// Otherwise,the original transaction is returned
.flatMap((Function<iIB,ObservableSource<iIB>>) iIB -> FirebaseClass.getInstance().checkIfNewOperation(iIB))
.filter(iIB -> iIB.getTradePrice() != null)

//We group all the emissions of previous observable in a single list to return all operations together
.toList());
本文链接:https://www.f2er.com/2950433.html

大家都在问