Stream Data Calculation In Flux - project-reactor

The following code illustrates a logic I need in a Spring Reactive project:
Inputs:
var period = 3;
int [] inArr = {2, 4, 6, 7, 9, 11, 13, 16, 17, 18, 20, 22 };
Calculation:
var upbond = inArr[0] + period;
var count =0;
List<Integer> result = new ArrayList();
for(int a: inArr){
if(a <= upbond){
count++;
}else{
result.add(count);
count = 1;
upbond += period;
}
}
result.add(count);
System.out.println(Arrays.toString(result.toArray()));
The data source of the sorted integers is the Flux from DB where it shall continually fetch data once a new suitable data is written into the DB. And the result shall be a stream that is sending out to another node through RSocket (by the request-stream communication mode).
After some online searching on Reactor, including some tutorials, I still can't figure out how to write the logic in the Flux fashion. The difficulty I have is that those calculations on data defined outside of the loop.
How shall I approach it in the Reactor?

The scan() variant that lets you use a separately typed accumulator is your friend here.
I'd approach this with a separate State class:
public class State {
private int count;
private Optional<Integer> upbond;
private Optional<Integer> result;
public State() {
this.count = 0;
this.upbond = Optional.empty();
this.result = Optional.empty();
}
public State(int count, int upbond) {
this.count = count;
this.upbond = Optional.of(upbond);
this.result = Optional.empty();
}
public State(int count, int upbond, int result) {
this.count = count;
this.upbond = Optional.of(upbond);
this.result = Optional.of(result);
}
public int getCount() {
return count;
}
public Optional<Integer> getUpbond() {
return upbond;
}
public Optional<Integer> getResult() {
return result;
}
}
...and then use scan() to build up the state element by element:
sourceFlux
.concatWithValues(0)
.scan(new State(), (state, a) ->
a <= state.getUpbond().orElse(a + period) ?
new State(state.getCount() + 1, state.getUpbond().orElse(a + period)) :
new State(1, state.getUpbond().orElse(a + period) + period, state.getCount())
)
.windowUntil(s -> s.getResult().isPresent())
.flatMap(f -> f.reduce((s1, s2) -> s1.getResult().isPresent()?s1:s2).map(s -> s.getResult().orElse(s.getCount() - 1)))
Aside: The concatWithValues() / windowUntil() / flatMap() bits are there to handle the last element - there's probably a cleaner way of achieving that, if I think of it I'll edit the answer.

I think scan is definitely the right tool here, combined with a stateful class, although my approach would be slightly different than Michaels.
Accumulator:
class UpbondAccumulator{
final Integer period;
Integer upbond;
Integer count;
Boolean first;
Queue<Integer> results;
UpbondAccumulator(Integer period){
this.period = period;
this.count = 0;
this.upbond = 0;
this.results = new ConcurrentLinkedQueue<>();
this.first = true;
}
//Logic is inside accumulator, since accumulator is the only the only thing
//that needs it. Allows reuse of accumulator w/o code repetition
public UpbondAccumulator process(Integer in){
//If impossible value
//Add current count to queue and return
//You will have to determine what is impossible
//Since we concat this value on the end of flux
//It will signify the end of processing
//And emit the last count
if(in<0){
results.add(count);
return this;
}
//If first value
//Do stuff outside loop
if(this.first) {
upbond = in + period;
first=false;
}
//Same as your loop
if(in <= upbond)
count++;
else {
results.add(count);
count = 1;
upbond += period;
}
//Return accumulator
//This could be put elsewhere since it isn't
//Immediately obvious that `process` should return
//the object but is simpler for example
return this;
}
public Mono<Integer> getResult() {
//Return mono empty if queue is empty
//Otherwise return queued result
return Mono.justOrEmpty(results.poll());
}
}
Usage:
dbFlux
//Concat with impossible value
.concatWithValues(-1)
//Create accumulator, process value and return
.scan(new UpbondAccumulator(period), UpbondAccumulator::process)
//Get results, note if there are no results, this will be empty
//meaning it isn't passed on in chain
.flatMap(UpbondAccumulator::getResult)
Following comment from Michael here is an immutable approach
Accumulator:
public class UpbondAccumulator{
public static UpbondState process(int period,Integer in,UpbondState previous){
Integer upbond = previous.getUpbond().orElse(in + period);
int count = previous.getCount();
if(in<0) return new UpbondState(upbond, count, count);
if(in <= upbond) return new UpbondState(upbond,count + 1 , null);
return new UpbondState(upbond + period, 1, count);
}
}
State object:
public class UpbondState {
private final Integer upbond;
private final int count;
private final Integer result;
public UpbondState() {
this.count = 0;
this.upbond = null;
this.result = null;
}
public UpbondState(Integer upbond, int count,Integer result) {
this.upbond = upbond;
this.count = count;
this.result = result;
}
public int getCount() { return count; }
public Optional<Integer> getUpbond() { return Optional.ofNullable(upbond); }
public Integer getResult() { return result; }
public boolean hasResult() { return result!=null; }
}
Usage:
dbFlux
.concatWithValues(-1)
.scan(new UpbondState(),
(prev, in) -> UpbondAccumulator.process(period,in,prev))
//Could be switched for Optional, but would mean one more map
//+ I personally think makes logic less clear in this scenario
.filter(UpbondState::hasResult)
.map(UpbondState::getResult)

Related

Why is javac telling me illegal start of expression?

Here's my code, i do not know what I'm doing wrong seriously. I tried many different things like taking the public modifier away from get. but I still get the same thing. This program is supposed to print out the Nth number line in the pascal triangle do to that I am using recursion a little bit.
import java.util.*;
public class Triangle{
private int lineNumber, count;
private int[] num;
public Triangle(){
lineNumber = 1;
}
public Triangle(int n){
set(n);
}
public void set(int n){
if(n < 1){
lineNumber = 1;
}
else{
lineNumber = n;
}
public int get()//Triangle.java:26: error: ';' expected //
{
return lineNumber;
}
private void pascal(int[] row){ //Triangle.java:30: error: illegal start of expression
if(count >= lineNumber){
return;
}
num = new int[row.length + 1];
num[0] = 1;
for(int i = 1; i < row.length; i++){
num[i] = row[i - 1] + row[i];
}
num[row.length] = 1;
count ++;
pascal(num);
return;
}
public int[] output(){
count = 1;
num = new int[count];
num[0] = 1;
pascal(num);
return num;
}
public static void main(String[] args){
int i,userNum;
Scanner scnr = new Scanner(System.in);
System.out.println("Enter a number to get the nth line of"+
" Pascal's Triangle." );
userNum = input.nextInt();
PascalTriangle triangle = new Triangle(userNum);
int[] result = triangle.output();
System.out.println("\n Line " + triangle.get() + " of "
+ "Pascal's Triangle is ");
for(i = 0; i < result.length; i++){
System.out.println(result[i] + " ");
}
}
}
}
You need one more closing bracket after the else statement in the set() method.Try to add the closing } before
else{
lineNumber = n;
}
}
public int get()//Triangle.java:26: error: ';' expected //

Why does this priority queue implementation only print one value repeatedly?

This program should print out the values in order ascending order. But it only displays 957.0 repeatedly. How do I display the numbers in order?
import java.io.*;
import java.util.*;
class PriorityQ {
public int maxSize;
public double[] queArray;
public int nItems;
//------
public PriorityQ(int s){
maxSize = s;
queArray = new double[maxSize];
nItems = 0;
}
//-----
public void insert(double item){
int j;
if(nItems == 0){
queArray[nItems++] = item;
}
else{
for(j = nItems-1; j >= 0; j--){
if(item > queArray[j]){
queArray[j + 1] = item;
}
else{
break;
}
}
queArray[j + 1] = item;
nItems++;
}
}
//-----
public double remove(){
return queArray[--nItems];
}
//-----
public double peekMin(){
return queArray[nItems - 1];
}
//-----
public boolean isEmpty(){
return(nItems == 0);
}
//-----
public boolean isFull(){
return(nItems == maxSize);
}
}
//-----
public class PriorityQApp{
public static void main(String[] args) throws IOException{
PriorityQ thePQ = new PriorityQ(5);
thePQ.insert(546);
thePQ.insert(687);
thePQ.insert(36);
thePQ.insert(98);
thePQ.insert(957);
while(!thePQ.isEmpty()){
double item = thePQ.remove();
System.out.print(item + " ");
}
System.out.println("");
}
}
You should save yourself the effort and use a priority queue with the generic type Double. If you wanted descending order you could even use a comparator that orders the highest value before the lowest, but you asked for ascending.
Your problem is that your array does contain many copies of 957.
This is because of this line in your code:
if(item > queArray[j]){
queArray[j + 1] = item;
}
Try:
import java.io.*;
import java.util.*;
public class PriorityQApp{
public static void main(String[] args) throws IOException{
PriorityQueue<Double> thePQ = new PriorityQueue<Double>(5);
thePQ.add(546);
thePQ.add(687);
thePQ.add(36);
thePQ.add(98);
thePQ.add(957);
while(thePQ.size() > 0){
double item = thePQ.poll();
System.out.print(item + " ");
}
System.out.println("");
}
}
Or I can fix your code to print out the queue in descending order leaving it to you to then make it print out in ascending order, the block I pointed to before should read like this instead:
if(item < queArray[j]){
queArray[j + 1] = queArray[j];
}

Using scanner to read phrases

Hey StackOverflow Community,
So, I have this line of information from a txt file that I need to parse.
Here is an example lines:
-> date & time AC Power Insolation Temperature Wind Speed
-> mm/dd/yyyy hh:mm.ss kw W/m^2 deg F mph
Using a scanner.nextLine() gives me a String with a whole line in it, and then I pass this off into StringTokenizer, which then separates them into individual Strings using whitespace as a separator.
so for the first line it would break up into:
date
&
time
AC
Power
Insolation
etc...
I need things like "date & time" together, and "AC Power" together. Is there anyway I can specify this using a method already defined in StringTokenizer or Scanner? Or would I have to develop my own algorithm to do this?
Would you guys suggest I use some other form of parsing lines instead of Scanner? Or, is Scanner sufficient enough for my needs?
ejay
oh, this one was tricky, maybe you could build up some Trie structure with your tokens, i was bored and wrote a little class which solves your problem. Warning: it's a bit hacky, but was fun to implement.
The Trie class:
class Trie extends HashMap<String, Trie> {
private static final long serialVersionUID = 1L;
boolean end = false;
public void addToken(String strings) {
addToken(strings.split("\\s+"), 0);
}
private void addToken(String[] strings, int begin) {
if (begin == strings.length) {
end = true;
return;
}
String key = strings[begin];
Trie t = get(key);
if (t == null) {
t = new Trie();
put(key, t);
}
t.addToken(strings, begin + 1);
}
public List<String> tokenize(String data) {
String[] split = data.split("\\s+");
List<String> tokens = new ArrayList<String>();
int pos = 0;
while (pos < split.length) {
int tokenLength = getToken(split, pos, 0);
tokens.add(glue(split, pos, tokenLength));
pos += tokenLength;
}
return tokens;
}
public String glue(String[] parts, int pos, int length) {
StringBuilder sb = new StringBuilder();
sb.append(parts[pos]);
for (int i = pos + 1; i < pos + length; i++) {
sb.append(" ");
sb.append(parts[i]);
}
return sb.toString();
}
private int getToken(String[] tokens, int begin, int length) {
if (end) {
return length;
}
if (begin == tokens.length) {
return 1;
}
String key = tokens[begin];
Trie t = get(key);
if (t != null) {
return t.getToken(tokens, begin + 1, length + 1);
}
return 1;
}
}
and how to use it:
Trie t = new Trie();
t.addToken("AC Power");
t.addToken("date & time");
t.addToken("date & foo");
t.addToken("Speed & fun");
String data = "date & time AC Power Insolation Temperature Wind Speed";
List<String> tokens = t.tokenize(data);
for (String s : tokens) {
System.out.println(s);
}

foreach statement overload in d programing language

Hello I would like to define my own class collection, and make it iterable in a foreach statement, just like this :
public class Collection(Type)
{
...
private T head;
private Collection!(T) queue;
}
Collection!(int) temp;
foreach (int t; temp) { ... }
What methods should I define, and how ?
you can specify the front, popfront() and empty functions: (but this will consume your collection unless you use save())
public class Collection(T) { ... private T head; private Collection!(T) queue;
#property T front(){
return head;
}
#property bool empty(){
return queue is null;
}
void popfront(){
head = queue.head;
queue = queue.queue;
}
Collection!T save(){
return new Collection!T(head,queue);
}
}
or use a dedicated struct for iteration (as is done in the std.container module
public class Collection(T) { ... private T head; private Collection!(T) queue;
Range opSlice(){
return Range(head,queue);
}
struct Range{
T h;
Collection!(T) q;
this(T he, Collection!(T) qu){
h=he;
q=qu;
}
#property T front(){
return h;
}
#property bool empty(){
return q is null;
}
void popfront(){
h = q.head;
q= q.queue;
}
Collection!T save(){
return this;
}
}
}
so iteration is done like so
Collection!(int) temp; foreach (int t;temp[]) { ... }
you also can add an opApply for the normal foreach:
public int opApply(int delegate(ref T) dg){
int res=0;
foreach(ref T;this[]){
res = dg(t);
if(res)return res;
}
return res;
}
Take a look at this documentation on ForeachStatements and scroll down a bit.
If I'm reading your example correctly, you could define an opApply for Collection as follows:
public int opApply(int delegate(ref T) dg){
Collection!T p = this;
int res = 0;
while(!res && p !is null){
res = dg(p.head);
p = p.queue;
}
return res;
}
Your Collection class should implement opApply. Your foreach body becomes a delegate to an internal for loop, and you iterate over your internal collection (in your case a queue) using a for loop.
Consider the example given in the docs
class Foo {
uint array[2];
int opApply(int delegate(ref uint) dg)
{ int result = 0;
for (int i = 0; i < array.length; i++)
{
result = dg(array[i]);
if (result)
break;
}
return result;
}
}

Return the correct value from a JavaME thread

I wrote a function which returns a string. And there is a Thread implementation the function, like follows, and I am calling [metaDataTrimmed = getMetaData(url);] this function and store the return value to a string value. My problem is the the function immediately returns the null string, which is its initial value. And I checked my function works properly.
So I try for a Thread.sleep() method using a dirtybit and also tried for Thread.join(). Is there any standard method in BlackBerry to solve the problem, if not what is a good approach to solve the problem?
private String getMetaData(final String mediaUrl){
String metaDataT = "";
Thread metaThread = new Thread(new Runnable(){
public void run(){
try {
StreamConnection streamConnection=null;
HttpConnection httpConnection = null;
InputStream inputStream =null;
streamConnection=(StreamConnection)Connector.open(mediaUrl);
httpConnection=(HttpConnection)streamConnection;
httpConnection.setRequestProperty("Icy-metadata", "1");
int httpStatus=httpConnection.getResponseCode();
if(httpStatus==HttpConnection.HTTP_OK){
String mint = httpConnection.getHeaderField("icy-metaint");
inputStream = streamConnection.openInputStream();
int length= Integer.parseInt(mint);
int b = 0;
int count =0;
while(count++ < length){
b = inputStream.read();
}
int metalength = ((int)b)*16;
// if(metalength <= 0){waitBitMetaData = 1;return;}
byte buf[] = new byte[metalength];
inputStream.read(buf,0,buf.length);
String metaData = new String(buf);
int streamTilleIndex = metaData.indexOf("StreamTitle");
// if(streamTilleIndex <= 0){waitBitMetaData = 1;return;}
String streamTille = metaData.substring(streamTilleIndex);
int eqindex = streamTille.indexOf('=');
// if(eqindex <= 0){waitBitMetaData = 1;return;}
int colindex = streamTille.indexOf(';');
// if(colindex <= 0){waitBitMetaData = 1;return;}
String metaDatam = streamTille.substring(eqindex, colindex);
int lengthOfMaetaDataM = metaDatam.length();
if(lengthOfMaetaDataM <= 0){waitBitMetaData = 1;return;}
metaDataParsed =metaDatam.substring(2, lengthOfMaetaDataM-2);
System.out.println(metaDataParsed);
waitBitMetaData = 1;
}
}
catch (Exception e){
System.out.println(e);
waitBitMetaData = 1;
}
}
});
metaThread.start();
metaThread.start();
//while( metaDataParsed.equals("") || waitBitMetaData == 0){
//try {
// Thread.sleep(50);
//} catch (InterruptedException e) {
//System.out.println(e);
//}
//}
return metaDataParsed;
}
I think the reason you are getting null with a Thread is you don't wait for the end of the thread execution. This is not related to BlackBerry, but is purely related to Java. To solve this just do the whole task (not just networking part of it) on a separate (from UI) thread. When upon task completion you'll need to update the UI, then just use UiApplication.getUiApplication().invokeLater(Runnable action) pattern.

Resources