Settings for financial stock updates

Let's start with the financial stock quote retrieval block. First of all, we will need to retrieve the preferences by calling this:

settings.getMonitoredSymbols()

Next, we will use .switchMap() to pass those symbols to the createFinancialStockUpdateObservable():

.switchMap(symbols -> {
String query = createQuery(symbols);
String env = "store://datatables.org/alltableswithkeys";
return createFinancialStockUpdateObservable(yahooService, query,
env);
})

We could have used .flatMap() to get a very similar effect here, but that would not have ended well. The problem with .flatMap() is that whenever the getMonitoredSymbols() produces a new symbol list, it will create a new Observable for financial stock quote updates but will not destroy the old one! If the symbol's property is changed five times, it means that there will be five different Observables created that will continue producing values until the entire root Observable is destroyed.

The .switchMap() block avoids that by terminating the previously created Observable inside the .switchMap() clause before producing the new one.

The block contains a new method--createQuery(). It is used to create a query for YQL, and its body can be seen here:

private String createQuery(List<String> symbols) {
StringBuilder buffer = new StringBuilder("select * from yahoo.finance.quote where symbol in (");
boolean first = true;
for (String symbol : symbols) {
if (!first) {
buffer.append(",");
}
buffer.append("'").append(symbol).append("'");
first = false;
}
buffer.append(")");
return buffer.toString();
}

It consumes the received list by appending its values to the YQL select query.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.226.240